diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/tree/BinarySearchTree.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/tree/BinarySearchTree.java index 2a3f0e26d5..1172c2fa7b 100644 --- a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/tree/BinarySearchTree.java +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/tree/BinarySearchTree.java @@ -1,7 +1,6 @@ package com.github.miniyk2012.coding2017.basic.tree; -import java.util.List; -import java.util.NoSuchElementException; +import java.util.*; public class BinarySearchTree { @@ -113,19 +112,106 @@ private BinaryTreeNode deleteMin(BinaryTreeNode root) { return root; } + /** + * 树的层序遍历 + * @return + */ public List levelVisit(){ + return levelVisit(root); + } - return null; + private List levelVisit(BinaryTreeNode root) { + ArrayDeque> queue = new ArrayDeque<>(); + List list = new LinkedList<>(); + if (root == null) return list; + queue.add(root); + BinaryTreeNode head; + while (!queue.isEmpty()) { + head = queue.remove(); + if (head.left != null) { + queue.add(head.left); + } + if (head.right != null) { + queue.add(head.right); + } + list.add(head.getData()); + } + return list; } + + /** + * 判断该二叉树是否是查找二叉树 + * @return + */ public boolean isValid(){ - return false; + if (root == null) return true; + BinarySearchTree leftTree = new BinarySearchTree<>(root.left); + BinarySearchTree rightTree = new BinarySearchTree<>(root.right); + boolean isValid = true; + if (root.left != null) { + if (!leftTree.isValid() || root.data.compareTo(leftTree.findMax())<0) { + isValid = false; + } + } + if (isValid && root.right != null) { + if (!rightTree.isValid() || root.data.compareTo(rightTree.findMin())>0) { + isValid = false; + } + } + return isValid; } + + /** + * 返回n1, n2的最低公共父节点(n1, n2分别为搜索二叉树中某节点的值,只考虑各节点值不同的情况, 且n1, n2是不同的节点值) + * @param n1 + * @param n2 + * @return + */ public T getLowestCommonAncestor(T n1, T n2){ - return null; + return getLowestCommonAncestor(root, n1, n2); + } + private T getLowestCommonAncestor(BinaryTreeNode root, T n1, T n2) { + if (root == null) { + return null; + } + if (n1.compareTo(root.data)>0 && n2.compareTo(root.data)>0) { + return getLowestCommonAncestor(root.right, n1, n2); + } + if (n1.compareTo(root.data)<0 && n2.compareTo(root.data)<0) { + return getLowestCommonAncestor(root.left, n1, n2); + } + return root.data; } + + /** + * 返回一个数组,每个元素e满足n1 getNodesBetween(T n1, T n2){ - return null; + List elements = new ArrayList<>(); + getNodesBetween(elements, root, n1, n2); + return elements; + } + + private void getNodesBetween(List elements, BinaryTreeNode root, T n1, T n2) { + if (root == null) { + return; + } + // 若根节点在范围内,把根节点加入element + if (n1.compareTo(root.data)<0 && n2.compareTo(root.data)>0) { + elements.add(root.data); + } + // 找左子树的满足范围的节点 + if (n1.compareTo(root.data)<0) { + getNodesBetween(elements, root.left, n1, n2); + } + // 找右子树的满足范围的节点 + if (n2.compareTo(root.data)>0) { + getNodesBetween(elements, root.right, n1, n2); + } } } diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/tree/BinarySearchTreeTest.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/tree/BinarySearchTreeTest.java index 127064a721..c9bccd488d 100644 --- a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/tree/BinarySearchTreeTest.java +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/tree/BinarySearchTreeTest.java @@ -1,20 +1,34 @@ package com.github.miniyk2012.coding2017.basic.tree; -import static org.junit.Assert.fail; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import java.util.Arrays; +import java.util.List; +import static org.junit.Assert.assertTrue; public class BinarySearchTreeTest { BinarySearchTree tree = null; - + BinarySearchTree tree2 = null; + BinarySearchTree tree3 = null; + @Before public void setUp() throws Exception { + /** + 6 + / \ + 2 8 + / \ + 1 4 + / + 3 + + */ BinaryTreeNode root = new BinaryTreeNode(6); root.left = new BinaryTreeNode(2); root.right = new BinaryTreeNode(8); @@ -22,7 +36,15 @@ public void setUp() throws Exception { root.left.right = new BinaryTreeNode(4); root.left.right.left = new BinaryTreeNode(3); tree = new BinarySearchTree(root); - } + + BinaryTreeNode root2 = new BinaryTreeNode<>(5); + tree2 = new BinarySearchTree<>(root2); + + BinaryTreeNode root3 = new BinaryTreeNode<>(5); + root3.left = new BinaryTreeNode(6); + tree3 = new BinarySearchTree<>(root3); + + } @After public void tearDown() throws Exception { @@ -76,4 +98,78 @@ public void testRemoveRoot() { } + @Test + public void testIsValid() { + assertTrue(tree.isValid()); + assertTrue(tree2.isValid()); + Assert.assertFalse(tree3.isValid()); + + BinarySearchTree tree4 = new BinarySearchTree<>(tree.root.left); + assertTrue(tree4.isValid()); + + tree.root.right.right = new BinaryTreeNode<>(7); + Assert.assertFalse(tree.isValid()); + assertTrue(tree4.isValid()); + + } + + @Test + public void testLevelVisit() { + List list = tree.levelVisit(); + Assert.assertEquals("[6, 2, 8, 1, 4, 3]", Arrays.toString(list.toArray())); + List list2 = tree2.levelVisit(); + Assert.assertEquals("[5]", Arrays.toString(list2.toArray())); + BinarySearchTree nullTree = new BinarySearchTree(null); + List nullList = nullTree.levelVisit(); + Assert.assertEquals("[]", Arrays.toString(nullList.toArray())); + } + + @Test + public void testGetLowestCommonAncestor() { + int ancestor = tree.getLowestCommonAncestor(1, 3); + Assert.assertEquals(2, ancestor); + ancestor = tree.getLowestCommonAncestor(2, 3); + Assert.assertEquals(2, ancestor); + ancestor = tree.getLowestCommonAncestor(2, 6); + Assert.assertEquals(6, ancestor); + ancestor = tree.getLowestCommonAncestor(1, 8); + Assert.assertEquals(6, ancestor); + ancestor = tree.getLowestCommonAncestor(3, 4); + Assert.assertEquals(4, ancestor); + } + + @Test + public void testGetNodesBetween() { + List list = tree.getNodesBetween(1, 4); + List expectedList = Arrays.asList(2, 3); + assertTrue(list.containsAll(expectedList) && expectedList.containsAll(list)); + + list = tree.getNodesBetween(6, 8); + expectedList = Arrays.asList(); + assertTrue(list.containsAll(expectedList) && expectedList.containsAll(list)); + + list = tree.getNodesBetween(4, 6); + expectedList = Arrays.asList(); + assertTrue(list.containsAll(expectedList) && expectedList.containsAll(list)); + + list = tree.getNodesBetween(1, 6); + expectedList = Arrays.asList(2, 4, 3); + assertTrue(list.containsAll(expectedList) && expectedList.containsAll(list)); + + list = tree.getNodesBetween(1, 8); + expectedList = Arrays.asList(2, 6, 4, 3); + assertTrue(list.containsAll(expectedList) && expectedList.containsAll(list)); + + + // 当n1, n2不一定为某节点值时 + list = tree.getNodesBetween(5, 8); + expectedList = Arrays.asList(6); + assertTrue(list.containsAll(expectedList) && expectedList.containsAll(list)); + + list = tree.getNodesBetween(-1, 9); + expectedList = Arrays.asList(1, 2, 6, 4, 3, 8); + assertTrue(list.containsAll(expectedList) && expectedList.containsAll(list)); + + } + }