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
Expand Up @@ -49,6 +49,15 @@ public BinaryTreeNode<T> insert(Object o){
return newNode;
}

@Override
public String toString() {
return "{" +
"data:" + data +
", left:" + left +
", right:" + right +
'}';
}

public static void main(String[] args) {
BinaryTreeNode<Integer> b = new BinaryTreeNode<>(2);
b.insert(3).insert(5).insert(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package com.coding.week11;

import com.coding.weak1.Queue;
import com.coding.week10.BinaryTreeNode;

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

public class BinarySearchTree<T extends Comparable<T>> {

BinaryTreeNode<T> root;

public BinarySearchTree() {
}

public BinarySearchTree(BinaryTreeNode<T> root) {
this.root = root;
}
Expand Down Expand Up @@ -92,6 +100,143 @@ private BinaryTreeNode<T> remove(BinaryTreeNode<T> node, T e) {
return node;
}

//按层次遍历: levelVisit
@SuppressWarnings("unchecked")
public List<T> levelVisit(){
List<T> list = new ArrayList<>();
if (root == null) {
return list;
}
Queue q = new Queue();
q.enQueue(root);
while (!q.isEmpty()) {
BinaryTreeNode<T> n = (BinaryTreeNode<T>)q.deQueue();
list.add(n.getData());
if (n.getLeft() != null) {
q.enQueue(n.getLeft());
}
if (n.getRight() != null) {
q.enQueue(n.getRight());
}
}
return list;
}

public static <T extends Comparable<T>> List<T> levelVisit(BinaryTreeNode<T> root) {
List<T> result = new ArrayList<>();
BinaryTreeNode<T> left = root.getLeft();
BinaryTreeNode<T> right = root.getRight();

if (right != null) {
List<T> rightList = levelVisit(right);
result.addAll(rightList);
}
if (left != null) {
List<T> leftList = levelVisit(left);
result.addAll(leftList);
}
result.add(root.getData());
return result;
}

//判断一个二叉树是不是二叉查找树
public boolean isValid(){

return isValid(root);
}

private boolean isValid(BinaryTreeNode<T> node) {
boolean valid = true;
if (node != null) {
T d = node.getData();

BinaryTreeNode<T> l = node.getLeft();
BinaryTreeNode<T> r = node.getRight();
if (l != null) {
if (d.compareTo(l.getData()) < 0) {
return false;
}
}
if (r != null) {
if (d.compareTo(r.getData()) >= 0) {
return false;
}
}
valid = isValid(l) && isValid(r);
}
return valid;
}


//获取两个节点的最小公共祖先
public T getLowestCommonAncestor(T n1, T n2){
return betweenNode(n1, n2, root).getData();
}

private BinaryTreeNode<T> betweenNode(T n1, T n2, BinaryTreeNode<T> node) {
if (node == null) {
return null;
} else {
T max = n1.compareTo(n2) >= 0 ? n1 : n2;
T min = max.compareTo(n1) == 0 ? n2 : n1;
if (max.compareTo(node.getData()) < 0) {
node = betweenNode(n1, n2, node.getLeft());
} else if (min.compareTo(node.getData()) > 0) {
node = betweenNode(n1, n2, node.getRight());
}
return node;
}

}



public void insert(List<T> l) {
for (T data : l) {
if (data == null) {
continue;
}
if (root == null) {
root = new BinaryTreeNode<>(data);
} else {
root.insert(data);
}
}
}

//给定两个值, 获得处于这两个值中间的节点
public List<T> getNodesBetween(T n1, T n2){
List<T> result = new ArrayList<T>();
Stack<BinaryTreeNode<T>> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()){
BinaryTreeNode<T> node = stack.pop();
if (isBetween(n1, n2, node.getData())) {
result.add(node.getData());
}
if (node.getRight() != null) {
stack.push(node.getRight());
}
if (node.getLeft() != null) {
stack.push(node.getLeft());
}

}

return result;
}

private boolean isBetween(T n1, T n2, T data) {
T max = n1.compareTo(n2) >= 0 ? n1 : n2;
T min = max.compareTo(n1) == 0 ? n2 : n1;
if (max.compareTo(data) < 0 || min.compareTo(data) > 0) {
return false;
} else {
return true;
}
}


public static void main(String[] args) {
System.out.println(Math.max(0,0));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.coding.week11;

import com.coding.week10.BinaryTreeNode;
import com.coding.week10.BinaryTreeUtil;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;


public class BinarySearchTreeTest {
Expand Down Expand Up @@ -63,4 +66,52 @@ public void testRemoveMiddleNode() {
Assert.assertEquals(3, root.getLeft().getData().intValue());
Assert.assertEquals(4, root.getLeft().getRight().getData().intValue());
}

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

@Test
public void testIsValid() throws Exception {
BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(6);
root.setLeft(new BinaryTreeNode<Integer>(3));
root.setRight( new BinaryTreeNode<Integer>(8));
root.getLeft().setLeft(new BinaryTreeNode<Integer>(1));
root.getLeft().setRight(new BinaryTreeNode<Integer>(4));
root.getLeft().getRight().setLeft(new BinaryTreeNode<Integer>(3));
BinarySearchTree<Integer> t = new BinarySearchTree<Integer>(root);
boolean valid = t.isValid();
Assert.assertTrue(valid);
}

@Test
public void testInsert() {
BinarySearchTree<Integer> tree1 = new BinarySearchTree<>();
tree1.insert(Arrays.asList(6, 8, 8, 1, 4, 9, 2));
List t = BinaryTreeUtil.inOrderVisit(tree1.root);
Assert.assertEquals("[1, 2, 4, 6, 8, 8, 9]", t.toString());
}

@Test
public void getLowestCommonAncestor() {

Assert.assertTrue(tree.getLowestCommonAncestor(1,5)==2);
Assert.assertTrue(tree.getLowestCommonAncestor(1,9)==6);
Assert.assertTrue(tree.getLowestCommonAncestor(4,5)==4);
Assert.assertTrue(tree.getLowestCommonAncestor(4,9)==6);
}


@Test
public void getNodesBetween(){
List<Integer> ret = tree.getNodesBetween(3, 10);
Assert.assertTrue(ret.contains(4));
Assert.assertTrue(ret.contains(6));
Assert.assertTrue(ret.contains(8));
Assert.assertTrue(ret.contains(3));
}


}