Skip to content

Commit 5918d32

Browse files
committed
Finished binary tree
1 parent ad1d702 commit 5918d32

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

group03/619224754/src/com/coding/basic/BinaryTreeNode.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.coding.basic;
22

3+
import java.util.Comparator;
4+
35
public class BinaryTreeNode {
46

57
private Object data;
@@ -9,24 +11,70 @@ public class BinaryTreeNode {
911
public Object getData() {
1012
return data;
1113
}
14+
1215
public void setData(Object data) {
1316
this.data = data;
1417
}
18+
1519
public BinaryTreeNode getLeft() {
1620
return left;
1721
}
22+
1823
public void setLeft(BinaryTreeNode left) {
1924
this.left = left;
2025
}
26+
2127
public BinaryTreeNode getRight() {
2228
return right;
2329
}
30+
2431
public void setRight(BinaryTreeNode right) {
2532
this.right = right;
2633
}
2734

2835
public BinaryTreeNode insert(Object o){
29-
return null;
36+
BinaryTreeNode treeNode = new BinaryTreeNode();
37+
treeNode.data = o;
38+
int intO = Integer.parseInt(o.toString());
39+
int intData = Integer.parseInt(this.data.toString());
40+
if(intO > intData){
41+
if(this.right == null){
42+
this.right = treeNode;
43+
}
44+
else {
45+
this.right.insert(o);
46+
}
47+
}
48+
else {
49+
if(this.left == null) {
50+
this.left = treeNode;
51+
}
52+
else {
53+
this.left.insert(o);
54+
}
55+
}
56+
return treeNode;
57+
}
58+
59+
private class MyComparator implements Comparator<BinaryTreeNode> {
60+
61+
@Override
62+
public int compare(BinaryTreeNode arg0, BinaryTreeNode arg1) {
63+
// TODO Auto-generated method stub
64+
int int0 = Integer.parseInt(arg0.data.toString());
65+
int int1 = Integer.parseInt(arg1.data.toString());
66+
if(int0 > int1) {
67+
return 1;
68+
}
69+
else if(int0 < int1){
70+
return -1;
71+
}
72+
73+
return 0;
74+
75+
}
76+
77+
3078
}
3179

3280
}

0 commit comments

Comments
 (0)