-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from WANGCANCER/master
Signed-off-by: 789作业补交[email protected]
- Loading branch information
Showing
8 changed files
with
634 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
258 changes: 258 additions & 0 deletions
258
group11/996108220/src/com/coding/basic/tree/BinarySearchTree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
package com.coding.basic.tree; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Queue; | ||
|
||
import com.coding.basic.tree.BinaryTreeNode; | ||
import com.sun.corba.se.impl.orbutil.graph.Node; | ||
|
||
|
||
public class BinarySearchTree<T extends Comparable> { | ||
|
||
BinaryTreeNode<T> root; | ||
int size; | ||
public BinarySearchTree(BinaryTreeNode<T> root){ | ||
this.root = root; | ||
} | ||
public BinaryTreeNode<T> getRoot(){ | ||
return root; | ||
} | ||
public T findMin(){ | ||
return findMin(root); | ||
} | ||
public T findMin(BinaryTreeNode<T> node){ | ||
while (node.left!=null) { | ||
node=node.left; | ||
} | ||
return node.data; | ||
} | ||
public T findMax(){ | ||
BinaryTreeNode<T> node=root; | ||
while (node.right!=null) { | ||
node=node.right; | ||
} | ||
return node.data; | ||
} | ||
public int height() { | ||
|
||
return height(root); | ||
|
||
} | ||
private int height(BinaryTreeNode<T> node) { | ||
if (node==null) { | ||
return 0; | ||
} | ||
else { | ||
int lheight=height(node.left); | ||
int rheight=height(node.right); | ||
int height=lheight>rheight?lheight:rheight; | ||
return height+1; | ||
} | ||
} | ||
public int size() { | ||
return size(root); | ||
} | ||
private int size(BinaryTreeNode<T> node) { | ||
if (node==null) { | ||
return 0; | ||
} | ||
else { | ||
return size(node.left)+size(node.right)+1; | ||
} | ||
} | ||
public void remove(T e){ | ||
BinaryTreeNode<T> node=searchNode(e, root); | ||
if (node==null) { | ||
return; | ||
} | ||
if (node.left==null&&node.right!=null) { | ||
node.data=node.right.data; | ||
node.right=null; | ||
} | ||
else if (node.right==null&&node.left!=null) { | ||
node.data=node.left.data; | ||
node.left=null; | ||
} | ||
else if (node.right==null&&node.left==null) { | ||
node=null; | ||
} | ||
else { | ||
T data=findMin(node.right); | ||
remove(data); | ||
node.data=data; | ||
|
||
} | ||
|
||
} | ||
public BinaryTreeNode searchNode(T key,BinaryTreeNode<T> node) { | ||
if (node!=null) { | ||
if (node.data.compareTo(key)==0) { | ||
return node; | ||
} | ||
else if (node.data.compareTo(key)>0) { | ||
return searchNode(key,node.left); | ||
} | ||
else { | ||
return searchNode(key,node.right); | ||
} | ||
} | ||
else{ | ||
return null; | ||
} | ||
} | ||
public void insert(T o){ | ||
if (size==0) { | ||
root=new BinaryTreeNode<T>(o); | ||
} | ||
else{ | ||
insert(o,root); | ||
} | ||
size++; | ||
} | ||
|
||
private void insert(T o, BinaryTreeNode<T> ptr) { | ||
if ((ptr.right==null&&(ptr.data.compareTo(o)<=0))){ | ||
ptr.right=new BinaryTreeNode<T>(o); | ||
} | ||
else if (ptr.left==null&&(ptr.data.compareTo(o)>0)) { | ||
ptr.left=new BinaryTreeNode<T>(o); | ||
|
||
} | ||
else if (ptr.left!=null&&(( ptr.data).compareTo(o)>0)) { | ||
insert(o, ptr.left); | ||
} | ||
else { | ||
insert(o, ptr.right); | ||
} | ||
} | ||
public void preOrder(BinaryTreeNode Node) | ||
{ | ||
if (Node != null) | ||
{ | ||
System.out.print(Node.data + " "); | ||
preOrder(Node.left); | ||
preOrder(Node.right); | ||
} | ||
} | ||
|
||
/** | ||
* 中序遍历 | ||
*/ | ||
public void midOrder(BinaryTreeNode Node) | ||
{ | ||
if (Node != null) | ||
{ | ||
midOrder(Node.left); | ||
System.out.print(Node.data + " "); | ||
midOrder(Node.right); | ||
} | ||
} | ||
public void midOrder(BinaryTreeNode<T> node,List<T> list) { | ||
if (node != null) | ||
{ | ||
midOrder(node.left,list); | ||
list.add(node.data); | ||
midOrder(node.right,list); | ||
} | ||
} | ||
/** | ||
* 后序遍历 | ||
*/ | ||
public void posOrder(BinaryTreeNode Node) | ||
{ | ||
if (Node != null) | ||
{ | ||
posOrder(Node.left); | ||
posOrder(Node.right); | ||
System.out.print(Node.data + " "); | ||
} | ||
} | ||
public List<T> levelVisit(){ | ||
List<BinaryTreeNode<T>> queue=new LinkedList<BinaryTreeNode<T>>(); | ||
queue.add(root); | ||
List<T> list=new ArrayList<T>(); | ||
while (queue.size()!=0) { | ||
BinaryTreeNode<T> node=queue.remove(0); | ||
list.add(node.data); | ||
if (node.left!=null) { | ||
queue.add(node.left); | ||
} | ||
if (node.right!=null) { | ||
queue.add(node.right); | ||
} | ||
} | ||
return list; | ||
} | ||
public boolean isValid(){ | ||
ArrayList<T> list=new ArrayList<T>(); | ||
midOrder(root, list); | ||
for (int i = 0; i < list.size()-1; i++) { | ||
if (list.get(i).compareTo(list.get(i+1))>=0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
public T getLowestCommonAncestor(T n1, T n2){ | ||
return getLowestCommonAncestor(root,n1,n2); | ||
} | ||
|
||
private T getLowestCommonAncestor(BinaryTreeNode<T> node, T n1, T n2) { | ||
if (node.data.compareTo(n1)<0&&node.data.compareTo(n2)<0) { | ||
return getLowestCommonAncestor(node.right, n1, n2); | ||
} | ||
else if (node.data.compareTo(n1)>0&&node.data.compareTo(n2)>0) { | ||
return getLowestCommonAncestor(node.left, n1, n2); | ||
} | ||
else { | ||
return node.data; | ||
} | ||
} | ||
/** | ||
* 返回所有满足下列条件的节点的值: n1 <= n <= n2 , n 为 | ||
* 该二叉查找树中的某一节点 | ||
* @param n1 | ||
* @param n2 | ||
* @return | ||
*/ | ||
public List<T> getNodesBetween(T n1, T n2){ | ||
T key=getLowestCommonAncestor(n1, n2); | ||
List<T> list=new ArrayList<>(); | ||
BinaryTreeNode<T> node=findNode(key, root); | ||
getNodesBetween(n1, n2,list,node); | ||
|
||
return list; | ||
} | ||
private void getNodesBetween(T n1, T n2, List<T> list, | ||
BinaryTreeNode<T> node) { | ||
if (node.data.compareTo(n1)<0&&node.data.compareTo(n2)<0) { | ||
getNodesBetween(n1, n2, list, node); | ||
} | ||
else if (node.data.compareTo(n1)>0&&node.data.compareTo(n2)>0) { | ||
getNodesBetween(n1, n2, list, node); | ||
} | ||
else { | ||
list.add(node.data); | ||
} | ||
} | ||
|
||
public BinaryTreeNode findNode(T key,BinaryTreeNode<T> node) { | ||
if (node!=null) { | ||
if (node.data.compareTo(key)==0) { | ||
return node; | ||
} | ||
else if (node.data.compareTo(key)>0) { | ||
return findNode(key,node.left); | ||
} | ||
else { | ||
return findNode(key,node.right); | ||
} | ||
} | ||
else{ | ||
return null; | ||
} | ||
} | ||
} | ||
|
108 changes: 108 additions & 0 deletions
108
group11/996108220/src/com/coding/basic/tree/BinarySearchTreeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.coding.basic.tree; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
|
||
|
||
public class BinarySearchTreeTest { | ||
|
||
BinarySearchTree<Integer> tree = null; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(6); | ||
root.left = new BinaryTreeNode<Integer>(2); | ||
root.right = new BinaryTreeNode<Integer>(8); | ||
root.left.left = new BinaryTreeNode<Integer>(1); | ||
root.left.right = new BinaryTreeNode<Integer>(4); | ||
root.left.right.left = new BinaryTreeNode<Integer>(3); | ||
root.left.right.right = new BinaryTreeNode<Integer>(5); | ||
tree = new BinarySearchTree<Integer>(root); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
tree = null; | ||
} | ||
|
||
@Test | ||
public void testFindMin() { | ||
Assert.assertEquals(1, tree.findMin().intValue()); | ||
|
||
} | ||
|
||
@Test | ||
public void testFindMax() { | ||
Assert.assertEquals(8, tree.findMax().intValue()); | ||
} | ||
|
||
@Test | ||
public void testHeight() { | ||
Assert.assertEquals(4, tree.height()); | ||
} | ||
|
||
@Test | ||
public void testSize() { | ||
Assert.assertEquals(7, tree.size()); | ||
} | ||
|
||
@Test | ||
public void testRemoveLeaf() { | ||
tree.remove(3); | ||
BinaryTreeNode<Integer> root= tree.getRoot(); | ||
Assert.assertEquals(4, root.left.right.data.intValue()); | ||
|
||
} | ||
@Test | ||
public void testRemoveMiddleNode1() { | ||
tree.remove(4); | ||
BinaryTreeNode<Integer> root= tree.getRoot(); | ||
Assert.assertEquals(5, root.left.right.data.intValue()); | ||
Assert.assertEquals(3, root.left.right.left.data.intValue()); | ||
} | ||
@Test | ||
public void testRemoveMiddleNode2() { | ||
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> values = tree.levelVisit(); | ||
Assert.assertEquals("[6, 2, 8, 1, 4, 3, 5]", values.toString()); | ||
|
||
} | ||
@Test | ||
public void testLCA(){ | ||
Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 5).intValue()); | ||
Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 4).intValue()); | ||
Assert.assertEquals(6,tree.getLowestCommonAncestor(3, 8).intValue()); | ||
} | ||
@Test | ||
public void testIsValid() { | ||
Assert.assertTrue(tree.isValid()); | ||
|
||
BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(6); | ||
root.left = new BinaryTreeNode<Integer>(2); | ||
root.right = new BinaryTreeNode<Integer>(8); | ||
root.left.left = new BinaryTreeNode<Integer>(4); | ||
root.left.right = new BinaryTreeNode<Integer>(1); | ||
root.left.right.left = new BinaryTreeNode<Integer>(3); | ||
tree = new BinarySearchTree<Integer>(root); | ||
|
||
Assert.assertFalse(tree.isValid()); | ||
} | ||
@Test | ||
public void testGetNodesBetween(){ | ||
List<Integer> numbers = this.tree.getNodesBetween(3, 8); | ||
System.out.println(numbers.toString()); | ||
|
||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...8220/src/com/coding/basic/BinaryTree.java → ...src/com/coding/basic/tree/BinaryTree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.coding.basic; | ||
package com.coding.basic.tree; | ||
|
||
import com.sun.corba.se.impl.orbutil.graph.Node; | ||
|
||
|
Oops, something went wrong.