Skip to content

Commit

Permalink
Merge pull request #61 from onlyliuxin/master
Browse files Browse the repository at this point in the history
拉取代码
  • Loading branch information
BlindingDark authored May 28, 2017
2 parents 9c14ed6 + e1fbba2 commit 966de72
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.coding.basic.tree;

import java.util.ArrayList;
import java.util.List;

import com.coding.basic.queue.Queue;

public class BinarySearchTree<T extends Comparable> {

BinaryTreeNode<T> root;
Expand All @@ -24,6 +29,27 @@ public int size() {
public void remove(T e){

}
public List<T> levelVisit(){

return null;
}
public boolean isValid(){
return false;
}
public T getLowestCommonAncestor(T n1, T n2){
return null;

}
/**
* 返回所有满足下列条件的节点的值: n1 <= n <= n2 , n 为
* 该二叉查找树中的某一节点
* @param n1
* @param n2
* @return
*/
public List<T> getNodesBetween(T n1, T n2){
return null;
}

}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.coding.basic.tree;

import static org.junit.Assert.fail;
import java.util.List;

import org.junit.After;
import org.junit.Assert;
Expand All @@ -21,6 +21,7 @@ public void setUp() throws Exception {
root.left.left = new BinaryTreeNode<Integer>(1);
root.left.right = new BinaryTreeNode<Integer>(4);
root.left.right.left = new BinaryTreeNode<Integer>(3);
root.left.right.right = new BinaryTreeNode<Integer>(5);
tree = new BinarySearchTree<Integer>(root);
}

Expand All @@ -47,21 +48,62 @@ public void testHeight() {

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

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

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

@Test
public void testLevelVisit() {
List<Integer> values = tree.levelVisit();
Assert.assertEquals("[6, 2, 8, 1, 4, 3, 5]", values.toString());

}
@Test
public void testLCA(){
Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 5).intValue());
Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 4).intValue());
Assert.assertEquals(6,tree.getLowestCommonAncestor(3, 8).intValue());
}
@Test
public void testIsValid() {

Assert.assertTrue(tree.isValid());

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>(4);
root.left.right = new BinaryTreeNode<Integer>(1);
root.left.right.left = new BinaryTreeNode<Integer>(3);
tree = new BinarySearchTree<Integer>(root);

Assert.assertFalse(tree.isValid());
}
@Test
public void testGetNodesBetween(){
List<Integer> numbers = this.tree.getNodesBetween(3, 8);
System.out.println(numbers.toString());

}
}

0 comments on commit 966de72

Please sign in to comment.