Skip to content

Commit 676f50a

Browse files
authored
Merge pull request onlyliuxin#8 from dutekt/master
二叉树
2 parents 3a9761a + 4a04135 commit 676f50a

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

group01/280646174/basic/src/main/java/com/coding2017/basic/BinaryTreeNode.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,35 @@
22

33
public class BinaryTreeNode {
44

5-
private Object data;
5+
private Integer data;
66
private BinaryTreeNode left;
77
private BinaryTreeNode right;
88

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+
}
1123
}
1224

13-
public Object getData() {
25+
public BinaryTreeNode(Integer data) {
26+
this.data = data;
27+
}
28+
29+
public Integer getData() {
1430
return data;
1531
}
1632

17-
public void setData(Object data) {
33+
public void setData(Integer data) {
1834
this.data = data;
1935
}
2036

@@ -34,4 +50,8 @@ public void setRight(BinaryTreeNode right) {
3450
this.right = right;
3551
}
3652

53+
@Override
54+
public String toString() {
55+
return data + " " + left + " " + right;
56+
}
3757
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

0 commit comments

Comments
 (0)