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,6 +1,6 @@
package basic.dataStructure.binaryTree;

import java.util.List;
import java.util.*;

public class BinarySearchTree<T extends Comparable> {

Expand Down Expand Up @@ -88,5 +88,116 @@ private void remove(BinaryTreeNode<T> node, int value) {
}
}

/**
* 逐层遍历
*/
public List<T> levelVisit(){
Queue<BinaryTreeNode> buffer = new LinkedList<BinaryTreeNode>();
List<T> result = new ArrayList<T>();

result = level(root, result, buffer);

return result;
}

private List<T> level(BinaryTreeNode<T> node, List<T> result, Queue<BinaryTreeNode> buffer){
result.add(node.getData());
if(node.getLeft() != null){
buffer.offer(node.left);
}

if(node.getRight() != null){
buffer.offer(node.right);
}

while(!buffer.isEmpty()){
result = level(buffer.poll(), result, buffer);
}

return result;
}

/**
* 判断一个二叉树是不是二叉查找树
*/
public boolean isValid(){
if(root.left == null || root.right == null){
return false;
}

return root.left.getData().compareTo(root.getData()) == -1 &&
root.right.getData().compareTo(root.getData()) == 1;
}

/**
* 获取两个节点的最小公共祖先
*/
public T getLowestCommonAncestor(T n1, T n2){
if(!isValid()){
throw new RuntimeException("this tree is not binary search tree");
}

return getLowestCommonAncestor(root, n1, n2).data;
}

private BinaryTreeNode<T> getLowestCommonAncestor(BinaryTreeNode<T> node, T n1, T n2){
// if(node == null || node.data.compareTo(n1) == 0 || node.data.compareTo(n2) == 0){
// return node;
// }
//
// BinaryTreeNode<T> left = getLowestCommonAncestor(node.left, n1, n2);
// BinaryTreeNode<T> right = getLowestCommonAncestor(node.right, n1, n2);
//
// if (left != null && right != null) {
// return node;
// }
// if (left != null) {
// return left;
// }
// if (right != null) {
// return right;
// }
// return null;
if(node == null){
return null;
}

int cmp1 = n1.compareTo(node.data);
int cmp2 = n2.compareTo(node.data);

if(cmp1 > 0 && cmp2 > 0){
return getLowestCommonAncestor(node.right, n1, n2);
}

if(cmp1 < 0 && cmp2 < 0){
return getLowestCommonAncestor(node.left, n1, n2);
}

return node;
}


/**
* 给定两个值, 获得处于这两个值中间的节点
*/
public List<T> getNodesBetween(T n1, T n2){
if(!isValid()) throw new RuntimeException("this is not binary search tree");
List<T> list = new ArrayList<T>();
getNodesBetween(root, n1, n2, list);
return list;
}

private void getNodesBetween(BinaryTreeNode<T> node, T n1, T n2, List<T> list){
int cmp1 = n1.compareTo(node.data);
int cmp2 = n2.compareTo(node.data);

if(cmp1 == -1 && node.left != null){
if(cmp2 != 0) list.add(node.data);
getNodesBetween(node.left, n1, n2, list);
}else if(cmp2 == 1 && node.right != null){
if(cmp1 != 0) list.add(node.data);
getNodesBetween(node.right, n1, n2, list);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.Before;
import org.junit.Test;

import java.util.List;


public class BinarySearchTreeTest {
Expand Down Expand Up @@ -52,11 +53,37 @@ public void testRemoveLeaf() {
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());
}

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

@Test
public void testGetLowestCommonAncestor(){
int num = tree.getLowestCommonAncestor(3, 8);
Assert.assertEquals(6, num);
}

@Test
public void testGetNodesBetween(){
{
List<Integer> list = tree.getNodesBetween(3, 8);
Assert.assertEquals("[6, 2, 4]", list.toString());
}
{
List<Integer> list = tree.getNodesBetween(3, 6);
Assert.assertEquals("[2, 4]", list.toString());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static void testOutOfMemory(){
}
}

//死循环的递归会引起stackOverFlow
public static void testStackOverFlowError(){
testStackOverFlowError();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public int size() {
public void remove(T e){

}
public List<T> levelVisit(){
public List<T> c(){

return null;
}
Expand Down