Skip to content

Commit 250c46e

Browse files
committed
完成二叉树
1 parent 478e4c4 commit 250c46e

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

group02/812350401/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/bin/
22
/lib/
3+
/src/java_training
Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,55 @@
11
package com.github.miniyk2012.coding2017.basic;
22

3-
public class BinaryTreeNode {
3+
public class BinaryTreeNode <E extends Comparable<E>> {
4+
private E data;
5+
private BinaryTreeNode<E> left;
6+
private BinaryTreeNode<E> right;
47

5-
private Object data;
6-
private BinaryTreeNode left;
7-
private BinaryTreeNode right;
8-
9-
public Object getData() {
8+
public BinaryTreeNode(E x) {
9+
data = x;
10+
}
11+
public E getData() {
1012
return data;
1113
}
12-
public void setData(Object data) {
14+
public void setData(E data) {
1315
this.data = data;
1416
}
15-
public BinaryTreeNode getLeft() {
17+
public BinaryTreeNode<E> getLeft() {
1618
return left;
1719
}
18-
public void setLeft(BinaryTreeNode left) {
20+
public void setLeft(BinaryTreeNode<E> left) {
1921
this.left = left;
2022
}
21-
public BinaryTreeNode getRight() {
23+
public BinaryTreeNode<E> getRight() {
2224
return right;
2325
}
24-
public void setRight(BinaryTreeNode right) {
26+
public void setRight(BinaryTreeNode<E> right) {
2527
this.right = right;
2628
}
2729

28-
public BinaryTreeNode insert(Object o){
29-
return null;
30+
public BinaryTreeNode<E> insert(E o){
31+
BinaryTreeNode<E> node = new BinaryTreeNode<E>(o);
32+
boolean left = true;
33+
BinaryTreeNode<E> currentNode = this;
34+
BinaryTreeNode<E> previousNode = null;
35+
36+
while (currentNode != null) {
37+
previousNode = currentNode;
38+
int compareTo = node.getData().compareTo(currentNode.getData());
39+
if (compareTo <= 0) { // 小于,往左插入
40+
currentNode = currentNode.left;
41+
left = true;
42+
} else {
43+
currentNode = currentNode.right;
44+
left = false;
45+
}
46+
}
47+
if (left) {
48+
previousNode.left = node;
49+
} else {
50+
previousNode.right = node;
51+
}
52+
return node;
3053
}
3154

3255
}

group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,24 @@
88

99
public class BinaryTreeNodeTest {
1010

11-
private BinaryTreeNode binaryTreeNode;
11+
private BinaryTreeNode<Integer> binaryTreeNode;
1212

1313
@Before
1414
public void setUpBinaryTreeNode() {
15-
binaryTreeNode = new BinaryTreeNode();
15+
binaryTreeNode = new BinaryTreeNode<Integer>(4);
1616
}
1717

1818
@Test
1919
public void testBinaryTreeNodeFunctional() {
20-
binaryTreeNode.insert(4);
2120
binaryTreeNode.insert(1);
2221
binaryTreeNode.insert(3);
2322
binaryTreeNode.insert(5);
2423
binaryTreeNode.insert(2);
25-
assertEquals(4, binaryTreeNode.getData());
26-
assertEquals(1, binaryTreeNode.getLeft().getData());
27-
assertEquals(5, binaryTreeNode.getRight().getData());
28-
assertEquals(3, binaryTreeNode.getLeft().getRight().getData());
29-
assertEquals(2, binaryTreeNode.getLeft().getRight().getLeft().getData());
30-
// binaryTreeNode.print();
24+
assertEquals(new Integer(4), binaryTreeNode.getData());
25+
assertEquals(new Integer(1), binaryTreeNode.getLeft().getData());
26+
assertEquals(new Integer(5), binaryTreeNode.getRight().getData());
27+
assertEquals(new Integer(3), binaryTreeNode.getLeft().getRight().getData());
28+
assertEquals(new Integer(2), binaryTreeNode.getLeft().getRight().getLeft().getData());
3129
}
3230

3331
}

0 commit comments

Comments
 (0)