Skip to content

Commit d553117

Browse files
committed
二叉树
1 parent b3b6f88 commit d553117

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.Objects;
2+
3+
public class BinaryTreeNode {
4+
5+
private Integer data;
6+
private BinaryTreeNode left;
7+
private BinaryTreeNode right;
8+
9+
public Integer getData() {
10+
return data;
11+
}
12+
public void setData(Integer data) {
13+
this.data = data;
14+
}
15+
public BinaryTreeNode getLeft() {
16+
return left;
17+
}
18+
public void setLeft(BinaryTreeNode left) {
19+
this.left = left;
20+
}
21+
public BinaryTreeNode getRight() {
22+
return right;
23+
}
24+
public void setRight(BinaryTreeNode right) {
25+
this.right = right;
26+
}
27+
//TODO
28+
public BinaryTreeNode insert(Integer o){
29+
if(data==null){
30+
data=o;
31+
return this;
32+
}
33+
34+
BinaryTreeNode b=new BinaryTreeNode();
35+
b.setData(o);
36+
37+
if(Objects.equals(data, o)){
38+
return this;
39+
}
40+
if(data<o){
41+
if(left==null){
42+
left=b;
43+
return b;
44+
}
45+
left.insert(o);
46+
}
47+
if(data>o){
48+
if(right==null){
49+
right=b;
50+
return b;
51+
}
52+
right.insert(o);
53+
}
54+
return b;
55+
}
56+
57+
public void showAll(){
58+
if(right!=null){
59+
right.showAll();
60+
}
61+
System.out.print(data+" ");
62+
if(left!=null){
63+
left.showAll();
64+
}
65+
66+
}
67+
68+
69+
70+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import org.junit.Test;
2+
3+
/**
4+
* Created by Administrator on 2017/2/25.
5+
*/
6+
public class BinaryTreeNodeTest {
7+
8+
9+
@Test
10+
public void showAll() throws Exception {
11+
BinaryTreeNode b=new BinaryTreeNode();
12+
b.insert(4);
13+
b.insert(5);
14+
b.insert(-1);
15+
b.insert(44);
16+
b.insert(34);
17+
b.insert(49);
18+
b.showAll();
19+
}
20+
21+
}

0 commit comments

Comments
 (0)