diff --git a/group24/494800949/src/main/java/com/coding/week10/BinaryTreeNode.java b/group24/494800949/src/main/java/com/coding/week10/BinaryTreeNode.java index c7fdd2b665..39fcd355d7 100644 --- a/group24/494800949/src/main/java/com/coding/week10/BinaryTreeNode.java +++ b/group24/494800949/src/main/java/com/coding/week10/BinaryTreeNode.java @@ -49,6 +49,15 @@ public BinaryTreeNode insert(Object o){ return newNode; } + @Override + public String toString() { + return "{" + + "data:" + data + + ", left:" + left + + ", right:" + right + + '}'; + } + public static void main(String[] args) { BinaryTreeNode b = new BinaryTreeNode<>(2); b.insert(3).insert(5).insert(1); diff --git a/group24/494800949/src/main/java/com/coding/week11/BinarySearchTree.java b/group24/494800949/src/main/java/com/coding/week11/BinarySearchTree.java index c49d57586a..45558ff69f 100644 --- a/group24/494800949/src/main/java/com/coding/week11/BinarySearchTree.java +++ b/group24/494800949/src/main/java/com/coding/week11/BinarySearchTree.java @@ -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> { BinaryTreeNode root; + public BinarySearchTree() { + } + public BinarySearchTree(BinaryTreeNode root) { this.root = root; } @@ -92,6 +100,143 @@ private BinaryTreeNode remove(BinaryTreeNode node, T e) { return node; } + //按层次遍历: levelVisit + @SuppressWarnings("unchecked") + public List levelVisit(){ + List list = new ArrayList<>(); + if (root == null) { + return list; + } + Queue q = new Queue(); + q.enQueue(root); + while (!q.isEmpty()) { + BinaryTreeNode n = (BinaryTreeNode)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 > List levelVisit(BinaryTreeNode root) { + List result = new ArrayList<>(); + BinaryTreeNode left = root.getLeft(); + BinaryTreeNode right = root.getRight(); + + if (right != null) { + List rightList = levelVisit(right); + result.addAll(rightList); + } + if (left != null) { + List leftList = levelVisit(left); + result.addAll(leftList); + } + result.add(root.getData()); + return result; + } + + //判断一个二叉树是不是二叉查找树 + public boolean isValid(){ + + return isValid(root); + } + + private boolean isValid(BinaryTreeNode node) { + boolean valid = true; + if (node != null) { + T d = node.getData(); + + BinaryTreeNode l = node.getLeft(); + BinaryTreeNode 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 betweenNode(T n1, T n2, BinaryTreeNode 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 l) { + for (T data : l) { + if (data == null) { + continue; + } + if (root == null) { + root = new BinaryTreeNode<>(data); + } else { + root.insert(data); + } + } + } + + //给定两个值, 获得处于这两个值中间的节点 + public List getNodesBetween(T n1, T n2){ + List result = new ArrayList(); + Stack> stack = new Stack<>(); + stack.push(root); + while (!stack.isEmpty()){ + BinaryTreeNode 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)); } diff --git a/group24/494800949/src/test/java/com/coding/week11/BinarySearchTreeTest.java b/group24/494800949/src/test/java/com/coding/week11/BinarySearchTreeTest.java index 433d3dba55..1ae98c349b 100644 --- a/group24/494800949/src/test/java/com/coding/week11/BinarySearchTreeTest.java +++ b/group24/494800949/src/test/java/com/coding/week11/BinarySearchTreeTest.java @@ -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 { @@ -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 li = tree.levelVisit(); + Assert.assertEquals("[6, 2, 8, 1, 4, 3]", li.toString()); + } + + @Test + public void testIsValid() throws Exception { + BinaryTreeNode root = new BinaryTreeNode(6); + root.setLeft(new BinaryTreeNode(3)); + root.setRight( new BinaryTreeNode(8)); + root.getLeft().setLeft(new BinaryTreeNode(1)); + root.getLeft().setRight(new BinaryTreeNode(4)); + root.getLeft().getRight().setLeft(new BinaryTreeNode(3)); + BinarySearchTree t = new BinarySearchTree(root); + boolean valid = t.isValid(); + Assert.assertTrue(valid); + } + + @Test + public void testInsert() { + BinarySearchTree 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 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)); + } + + }