File tree Expand file tree Collapse file tree 1 file changed +21
-3
lines changed
group08/108621969/com/coding/basic Expand file tree Collapse file tree 1 file changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -8,8 +8,14 @@ public class BinaryTreeNode {
8
8
private BinaryTreeNode left ;
9
9
private BinaryTreeNode right ;
10
10
11
- public Object getData () {
12
- return data ;
11
+ public BinaryTreeNode (Object o ) {
12
+ this .data = o ;
13
+ this .left = null ;
14
+ this .right = null ;
15
+ }
16
+
17
+ public int getData () {
18
+ return (int ) data ;
13
19
}
14
20
15
21
public void setData (Object data ) {
@@ -33,6 +39,18 @@ public void setRight(BinaryTreeNode right) {
33
39
}
34
40
35
41
public BinaryTreeNode insert (Object o ) {
36
- return null ;
42
+ BinaryTreeNode newNode = new BinaryTreeNode (o );
43
+ insertInto (this , newNode );
44
+ return this ;
45
+ }
46
+
47
+ private void insertInto (BinaryTreeNode tree , BinaryTreeNode o ) {
48
+ if (o .getData () <= tree .getData ()) {
49
+ if (tree .getLeft () != null ) insertInto (tree .getLeft (), o );
50
+ else tree .setLeft (o );
51
+ } else {
52
+ if (tree .getRight () != null ) insertInto (tree .getRight (), o );
53
+ else tree .setRight (o );
54
+ }
37
55
}
38
56
}
You can’t perform that action at this time.
0 commit comments