Skip to content

Commit

Permalink
二叉树
Browse files Browse the repository at this point in the history
  • Loading branch information
hiei17 committed Feb 25, 2017
1 parent b3b6f88 commit d553117
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
70 changes: 70 additions & 0 deletions group15/1503_1311822904/myCollection/src/BinaryTreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.util.Objects;

public class BinaryTreeNode {

private Integer data;
private BinaryTreeNode left;
private BinaryTreeNode right;

public Integer getData() {
return data;
}
public void setData(Integer data) {
this.data = data;
}
public BinaryTreeNode getLeft() {
return left;
}
public void setLeft(BinaryTreeNode left) {
this.left = left;
}
public BinaryTreeNode getRight() {
return right;
}
public void setRight(BinaryTreeNode right) {
this.right = right;
}
//TODO
public BinaryTreeNode insert(Integer o){
if(data==null){
data=o;
return this;
}

BinaryTreeNode b=new BinaryTreeNode();
b.setData(o);

if(Objects.equals(data, o)){
return this;
}
if(data<o){
if(left==null){
left=b;
return b;
}
left.insert(o);
}
if(data>o){
if(right==null){
right=b;
return b;
}
right.insert(o);
}
return b;
}

public void showAll(){
if(right!=null){
right.showAll();
}
System.out.print(data+" ");
if(left!=null){
left.showAll();
}

}



}
21 changes: 21 additions & 0 deletions group15/1503_1311822904/myCollection/test/BinaryTreeNodeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import org.junit.Test;

/**
* Created by Administrator on 2017/2/25.
*/
public class BinaryTreeNodeTest {


@Test
public void showAll() throws Exception {
BinaryTreeNode b=new BinaryTreeNode();
b.insert(4);
b.insert(5);
b.insert(-1);
b.insert(44);
b.insert(34);
b.insert(49);
b.showAll();
}

}

0 comments on commit d553117

Please sign in to comment.