Skip to content

Commit d728768

Browse files
author
张加涛
committed
BinaryTreeNode Completed
1 parent c6b11b7 commit d728768

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

group08/108621969/com/coding/basic/BinaryTreeNode.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ public class BinaryTreeNode {
88
private BinaryTreeNode left;
99
private BinaryTreeNode right;
1010

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;
1319
}
1420

1521
public void setData(Object data) {
@@ -33,6 +39,18 @@ public void setRight(BinaryTreeNode right) {
3339
}
3440

3541
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+
}
3755
}
3856
}

0 commit comments

Comments
 (0)