File tree Expand file tree Collapse file tree 2 files changed +49
-5
lines changed
group01/280646174/basic/src
main/java/com/coding2017/basic
test/java/com/coding2017/basic Expand file tree Collapse file tree 2 files changed +49
-5
lines changed Original file line number Diff line number Diff line change 2
2
3
3
public class BinaryTreeNode {
4
4
5
- private Object data ;
5
+ private Integer data ;
6
6
private BinaryTreeNode left ;
7
7
private BinaryTreeNode right ;
8
8
9
- public BinaryTreeNode insert (Object o ) {
10
- return null ;
9
+ public BinaryTreeNode insert (Integer o ) {
10
+ if (o <= data ) {
11
+ if (left == null ) {
12
+ left = new BinaryTreeNode (o );
13
+ return left ;
14
+ }
15
+ return left .insert (o );
16
+ } else {
17
+ if (right == null ) {
18
+ right = new BinaryTreeNode (o );
19
+ return right ;
20
+ }
21
+ return right .insert (o );
22
+ }
11
23
}
12
24
13
- public Object getData () {
25
+ public BinaryTreeNode (Integer data ) {
26
+ this .data = data ;
27
+ }
28
+
29
+ public Integer getData () {
14
30
return data ;
15
31
}
16
32
17
- public void setData (Object data ) {
33
+ public void setData (Integer data ) {
18
34
this .data = data ;
19
35
}
20
36
@@ -34,4 +50,8 @@ public void setRight(BinaryTreeNode right) {
34
50
this .right = right ;
35
51
}
36
52
53
+ @ Override
54
+ public String toString () {
55
+ return data + " " + left + " " + right ;
56
+ }
37
57
}
Original file line number Diff line number Diff line change
1
+ package com .coding2017 .basic ;
2
+
3
+ import org .junit .Test ;
4
+
5
+ import static org .junit .Assert .*;
6
+
7
+ /**
8
+ * Created by kaitao.li on 17/2/24.
9
+ */
10
+ public class BinaryTreeNodeTest {
11
+
12
+ @ Test
13
+ public void insert () throws Exception {
14
+ BinaryTreeNode binaryTreeNode = new BinaryTreeNode (5 );
15
+ binaryTreeNode .insert (4 );
16
+ binaryTreeNode .insert (6 );
17
+ binaryTreeNode .insert (5 );
18
+ assertTrue (binaryTreeNode .getLeft ().getData () == 4 );
19
+ assertTrue (binaryTreeNode .getRight ().getData () == 6 );
20
+ assertTrue (binaryTreeNode .getLeft ().getRight ().getData () == 5 );
21
+ System .out .println (binaryTreeNode );
22
+ }
23
+
24
+ }
You can’t perform that action at this time.
0 commit comments