Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
package com.github.orajavac.coding2017.basic;

public class BinaryTreeNode {
public class BinaryTreeNode<T> {

private Object data;
private BinaryTreeNode left;
private BinaryTreeNode right;
public T data;
public BinaryTreeNode<T> left;
public BinaryTreeNode<T> right;

public Object getData() {
public BinaryTreeNode(T data){
this.data=data;
}
public T getData() {
return data;
}
public void setData(Object data) {
public void setData(T data) {
this.data = data;
}
public BinaryTreeNode getLeft() {
public BinaryTreeNode<T> getLeft() {
return left;
}
public void setLeft(BinaryTreeNode left) {
public void setLeft(BinaryTreeNode<T> left) {
this.left = left;
}
public BinaryTreeNode getRight() {
public BinaryTreeNode<T> getRight() {
return right;
}
public void setRight(BinaryTreeNode right) {
public void setRight(BinaryTreeNode<T> right) {
this.right = right;
}

public BinaryTreeNode insert(Object o){
public BinaryTreeNode<T> insert(Object o){
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package com.github.orajavac.coding2017.basic.tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import com.github.orajavac.coding2017.basic.BinaryTreeNode;
import com.github.orajavac.coding2017.basic.stack.Stack;

@SuppressWarnings("rawtypes")
public class BinarySearchTree<T extends Comparable> {

private static Stack s = new Stack();
BinaryTreeNode<T> root;
public BinarySearchTree(BinaryTreeNode<T> root){
this.root = root;
}
public BinaryTreeNode<T> getRoot(){
return root;
}
public T findMin(){
T result = findMinOrMax(Integer.parseInt(getRoot().getData().toString()),getRoot(),false);
root.setLeft(root.getRight());
result = findMinOrMax((Integer) result,getRoot(),false);
return result;
}
public T findMax(){
T result = findMinOrMax(Integer.parseInt(getRoot().getData().toString()),getRoot(),true);
root.setLeft(root.getRight());
result = findMinOrMax((Integer) result,getRoot(),true);
return result;
}

public int height(){
return height(root);
}

public int height(BinaryTreeNode<T> root) {
if(root==null){
return 0;
}else{
int l = height(root.left);
int r = height(root.right);
if (l>r){
return l+1;
}else{
return r+1;
}
}
}

public int size(){
return size(root);
}

public int size(BinaryTreeNode<T> root) {
if(root==null){
return 0;
}
return size(root.left)+1+size(root.right);
}


public void remove(T e){

}

@SuppressWarnings("unchecked")
public T findMinOrMax(Integer num,BinaryTreeNode<T> node,boolean bol){
while(true){
if (node.getLeft()!=null){
if (bol){
if (Integer.parseInt(node.getLeft().getData().toString()) > num){ //max
num = Integer.parseInt(node.getLeft().getData().toString());
}
}else{
if (Integer.parseInt(node.getLeft().getData().toString()) < num){ //min
num = Integer.parseInt(node.getLeft().getData().toString());
}
}
s.push(node.getLeft());
node = node.getLeft();
}else{
node = (BinaryTreeNode<T>)s.pop();
node.setLeft(node.getRight());
}
if (node.getLeft()==null&&s.length()==0)
break;
}
return (T)num;
}

@SuppressWarnings("unchecked")
public List<T> levelVisit(){
Queue q = new LinkedList();
List<T> result = new ArrayList<T>();
result.add(root.data);
q.add(root.getLeft());
q.add(root.getRight());
while(q.size()!=0){
BinaryTreeNode<T> node = (BinaryTreeNode<T>)q.poll();
result.add(node.data);
if (node!=null&&node.left!=null){
q.add(node.getLeft());
}
if (node!=null&&node.right!=null){
q.add(node.getRight());
}
}
return result;
}

public boolean isValid(){
return isValid(root,Integer.parseInt(root.data.toString()));
}

public boolean isValid(BinaryTreeNode<T> node,int d){
boolean f = false;
if (node == null){
return false;
}
if (Integer.parseInt(node.data.toString()) <= d) {
f = true;
d = Integer.parseInt(node.data.toString());
isValid(node.left,d);
}else{
f = false;
}

if (Integer.parseInt(node.data.toString()) >= d) {
f = true;
d = Integer.parseInt(node.data.toString());
isValid(node.right,d);
}else{
f = false;
}

return f;
}

public T getLowestCommonAncestor(T n1, T n2){
return getLowestCommonAncestor(root,n1,n2);

}

public T getLowestCommonAncestor(BinaryTreeNode<T> n,T n1, T n2){
if (root == null)
return null;
if ((Integer.parseInt(root.data.toString()) >= (Integer) n1 && Integer
.parseInt(root.data.toString()) <= (Integer) n2)
|| (Integer.parseInt(root.data.toString()) >= (Integer) n1 && Integer
.parseInt(root.data.toString()) <= (Integer) n2))
return (T) root.data.toString();
else if (Integer.parseInt(root.data.toString()) > (Integer) n1
&& Integer.parseInt(root.data.toString()) > (Integer) n2)
return getLowestCommonAncestor(root.left, n1, n2);
else if (Integer.parseInt(root.data.toString()) < (Integer) n1
&& Integer.parseInt(root.data.toString()) < (Integer) n2)
return getLowestCommonAncestor(root.right, n1, n2);
return null;
}

public List<T> getNodesBetween(T n1, T n2){
List<T> result = new ArrayList<T>();
List<T> seq = new ArrayList<T>();
result.add((T) root.getData());
BinaryTreeNode<T> node = root;
getNodesBetweenProcess(node,result,seq,n1,n2);
node.setLeft(root.getRight());
getNodesBetweenProcess(node,result,seq,n1,n2);
return seq;
}

public List<T> getNodesBetweenProcess(BinaryTreeNode<T> node,List<T> result,List<T> seq,
T n1, T n2){
while(true){
if (node.getLeft()!=null){
if (Integer.parseInt(node.getLeft().getData().toString()) >= Integer
.parseInt(n1.toString())
&& Integer
.parseInt(node.getLeft().getData().toString()) <= Integer
.parseInt(n2.toString())) {
seq.add((T) node.getLeft().getData());
}
s.push(node.getLeft());
node = node.getLeft();
}else{
node = (BinaryTreeNode<T>)s.pop();
node.setLeft(node.getRight());
}
if (node.getLeft()==null&&s.length()==0)
break;
}
return seq;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.github.orajavac.coding2017.basic.tree;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.github.orajavac.coding2017.basic.BinaryTreeNode;

public class BinarySearchTreeTest {
BinarySearchTree<Integer> tree = null;

@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(6);
root.left = new BinaryTreeNode<Integer>(2);
root.right = new BinaryTreeNode<Integer>(8);
root.left.left = new BinaryTreeNode<Integer>(1);
root.left.right = new BinaryTreeNode<Integer>(4);
root.left.right.left = new BinaryTreeNode<Integer>(3);
tree = new BinarySearchTree<Integer>(root);
}

@After
public void tearDown() throws Exception {
tree = null;
}

@Test
public void testFindMin() {
Assert.assertEquals(1, tree.findMin().intValue());

}

@Test
public void testFindMax() {
Assert.assertEquals(8, tree.findMax().intValue());
}

@Test
public void testHeight() {
Assert.assertEquals(4, tree.height());
}

@Test
public void testSize() {
Assert.assertEquals(6, tree.size());
}

@Test
public void testRemoveLeaf() {
tree.remove(4);
BinaryTreeNode<Integer> root= tree.getRoot();
Assert.assertEquals(3, root.left.right.data.intValue());

}
@Test
public void testRemoveMiddleNode() {
tree.remove(2);
BinaryTreeNode<Integer> root= tree.getRoot();
Assert.assertEquals(3, root.left.data.intValue());
Assert.assertEquals(4, root.left.right.data.intValue());
}
}
Loading