Skip to content

Commit 6d3967a

Browse files
BinaryTree
1 parent 8d011fa commit 6d3967a

File tree

1 file changed

+42
-2
lines changed
  • group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic

1 file changed

+42
-2
lines changed

group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
package com.github.congcongcong250.coding2017.basic;
22

3-
public class BinaryTreeNode {
3+
public class BinaryTreeNode <Object extends Comparable<Object>>{
44

55
private Object data;
66
private BinaryTreeNode left;
77
private BinaryTreeNode right;
88

9+
public BinaryTreeNode(){
10+
data = null;
11+
left = null;
12+
right = null;
13+
}
14+
15+
public BinaryTreeNode(Object obj){
16+
data = obj;
17+
left = null;
18+
right = null;
19+
}
20+
921
public Object getData() {
1022
return data;
1123
}
@@ -26,7 +38,35 @@ public void setRight(BinaryTreeNode right) {
2638
}
2739

2840
public BinaryTreeNode insert(Object o){
29-
return null;
41+
//If is empty root
42+
if(data == null){
43+
data = o;
44+
return this;
45+
}
46+
47+
//If it is a normal root
48+
BinaryTreeNode in;
49+
50+
if(o.compareTo(data) <= 0){
51+
if(left == null){
52+
in = new BinaryTreeNode(o);
53+
left = in;
54+
}else{
55+
in = left.insert(o);
56+
}
57+
}else{
58+
if(right == null){
59+
in = new BinaryTreeNode(o);
60+
right = in;
61+
}else{
62+
in = right.insert(o);
63+
}
64+
}
65+
66+
assert (in == null):"Insert error";
67+
return in;
3068
}
3169

70+
71+
3272
}

0 commit comments

Comments
 (0)