From f1e9d6ea697b8796e37e537c4ade9d64bdd6e3ae Mon Sep 17 00:00:00 2001 From: Patrick Date: Fri, 26 May 2017 23:28:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91?= =?UTF-8?q?=E7=BB=83=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../basic/linkedlist/LinkedList.java | 2 +- .../basic/tree/BinarySearchTree.java | 136 +++++++++++++++++- .../basic/tree/BinarySearchTreeTest.java | 35 +++++ 3 files changed, 171 insertions(+), 2 deletions(-) diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/linkedlist/LinkedList.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/linkedlist/LinkedList.java index 9bd545ef4f..a62faa3898 100644 --- a/group24/121111914/src/com/github/ipk2015/coding2017/basic/linkedlist/LinkedList.java +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/linkedlist/LinkedList.java @@ -117,7 +117,7 @@ public Object removeFirst(){ } Node node=head; head=node.next; - return node; + return node.data; } public Object removeLast(){ if(null==head){ diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinarySearchTree.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinarySearchTree.java index 175711100b..6126080259 100644 --- a/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinarySearchTree.java +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinarySearchTree.java @@ -1,8 +1,12 @@ -package com.github.ipk2015.coding2017.basic.tree; + package com.github.ipk2015.coding2017.basic.tree; import java.util.ArrayList; import java.util.List; +import com.github.ipk2015.coding2017.basic.queue.Queue; + + + public class BinarySearchTree { BinaryTreeNode root; @@ -201,5 +205,135 @@ private boolean findNodeAParentByData(List> list,BinaryTreeNod return false; } + public List levelVisit(){ + List list = new ArrayList(); + if(null == root){ + return list; + } + Queue queue = new Queue(); + queue.enQueue(root); + + while(!queue.isEmpty()){ + BinaryTreeNode node = (BinaryTreeNode)queue.deQueue(); + list.add(node.getData()); + BinaryTreeNode left = node.getLeft(); + if(null != left){ + queue.enQueue(left); + } + + BinaryTreeNode right = node.getRight(); + if(null != right){ + queue.enQueue(right); + } + } + return list; + } + /* + * 每一个节点的值都大于左节点的所有值,小于右节点的所有值 + * */ + public boolean isValid(){ + return isNodeValid(root); + } + private boolean isNodeValid(BinaryTreeNode node){ + if(null == node){ + return true; + } + BinaryTreeNode left = node.getLeft(); + if(null != left){ + boolean isLeftNodeValid = isNodeValid(left); + if(!isLeftNodeValid){ + return false; + } + if(node.getData().compareTo(findMaxInOneNode(left,left.getData())) <= 0){ + return false; + } + } + BinaryTreeNode right = node.getRight(); + if(null != right){ + boolean isRightNodeValid = isNodeValid(right); + if(!isRightNodeValid){ + return false; + } + if(node.getData().compareTo(findMaxInOneNode(right,right.getData())) >= 0){ + return false; + } + } + return true; + } + + public T getLowestCommonAncestor(T n1, T n2){ + if(null == root){ + return null; + } + List> list1 = getNodeAllAncestor(n1); + List> list2 = getNodeAllAncestor(n2); + int size1 = list1.size(); + int size2 = list2.size(); + int minSize = size1 < size2? size1 :size2; + T result = null; + for(int i = 0;i < minSize; i++){ + T data1 = list1.get(size1-1-i).getData(); + if(data1.compareTo(list2.get(size2-1-i).getData()) != 0){ + break; + } + result = data1; + } + return result; + } + private List> getNodeAllAncestor(T data){ + List> list = new ArrayList(); + isAncestor(list,root,data); + return list; + } + private boolean isAncestor(List> list,BinaryTreeNode node,T data){ + if(null == node){ + return false; + } + if(data.compareTo(node.getData()) == 0){ + list.add(node); + return true; + } + BinaryTreeNode left = node.getLeft(); + if(null != left){ + boolean isLeftAncetor = isAncestor(list,left,data); + if(isLeftAncetor){ + list.add(node); + return true; + } + } + BinaryTreeNode right = node.getRight(); + if(null != right){ + boolean isRightAncetor = isAncestor(list,right,data); + if(isRightAncetor){ + list.add(node); + return true; + } + } + return false; + } + + public List getNodesBetween(T n1, T n2){ + List list = new ArrayList(); + getNodesBetween(root,list,n1,n2); + return list; + } + private void getNodesBetween(BinaryTreeNode node,List list,T n1,T n2){ + if(null == node){ + return; + } + T data = node.getData(); + if(data.compareTo(n2) >= 0){ + getNodesBetween(node.getLeft(),list,n1,n2); + return; + } + if(data.compareTo(n1) <= 0){ + getNodesBetween(node.getRight(),list,n1,n2); + return; + } + list.add(data); + getNodesBetween(node.getLeft(),list,n1,n2); + getNodesBetween(node.getRight(),list,n1,n2); + } + } diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinarySearchTreeTest.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinarySearchTreeTest.java index 42d3b6c721..44c49f824d 100644 --- a/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinarySearchTreeTest.java +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinarySearchTreeTest.java @@ -4,6 +4,8 @@ import static org.junit.Assert.fail; +import java.util.List; + import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -74,4 +76,37 @@ public void testRemoveRootNode() { Assert.assertEquals(2, root.left.data.intValue()); } + + @Test + public void testLevelVisit() { + List list = tree.levelVisit(); + System.out.println(list.toString()); + Assert.assertEquals("[6, 2, 8, 1, 4, 3]", list.toString()); + } + + @Test + public void testIsValid() { + Assert.assertEquals(true, tree.isValid()); + BinarySearchTree tree2; + BinaryTreeNode root2 = new BinaryTreeNode(6); + root2.left = new BinaryTreeNode(2); + root2.right = new BinaryTreeNode(8); + root2.left.left = new BinaryTreeNode(1); + root2.left.right = new BinaryTreeNode(7); + root2.left.right.left = new BinaryTreeNode(3); + tree2 = new BinarySearchTree(root2); + Assert.assertEquals(false, tree2.isValid()); + } + @Test + public void testGetLowestCommonAncestor() { + Assert.assertEquals(2, tree.getLowestCommonAncestor(1, 3).intValue()); + Assert.assertEquals(6, tree.getLowestCommonAncestor(3, 8).intValue()); + } + @Test + public void testGetNodesBetween() { + List list = tree.getNodesBetween(0, 9); + Assert.assertEquals("[6, 2, 1, 4, 3, 8]", list.toString()); + list = tree.getNodesBetween(1, 7); + Assert.assertEquals("[6, 2, 4, 3]", list.toString()); + } }