Skip to content

Commit b520d9b

Browse files
authored
Merge pull request onlyliuxin#29 from yangyangxu2016/master
二叉树
2 parents fa28c2d + 9816c2e commit b520d9b

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Created by 呢喃 on 2017/2/26.
3+
*/
4+
public class BinaryTree {
5+
6+
7+
private Node root;
8+
9+
private static class Node {
10+
Node left;
11+
Node right;
12+
int data;
13+
14+
Node(int newData){
15+
left = null;
16+
right = null;
17+
data = newData;
18+
}
19+
}
20+
21+
public BinaryTree(){
22+
root = null;
23+
}
24+
25+
26+
public void insert(int data){
27+
root = insert( root,data);
28+
}
29+
30+
private Node insert(Node node,int data){
31+
32+
if (node == null){
33+
node = new Node(data);
34+
}
35+
else {
36+
if (data<= node.data){
37+
node.left = insert(node.left,data);
38+
}else {
39+
node.right = insert(node.right,data);
40+
}
41+
}
42+
return node;
43+
}
44+
45+
public void bulidTree(int[] data){
46+
for (int i= 0;i<data.length;i++){
47+
insert(data[i]);
48+
}
49+
}
50+
51+
public void printTree(){
52+
printTree(root);
53+
System.out.println();
54+
}
55+
private void printTree(Node node){
56+
if (node == null)
57+
return;
58+
printTree(node.left);
59+
System.out.println(node.data+"");
60+
printTree(node.right);
61+
}
62+
63+
64+
65+
}

0 commit comments

Comments
 (0)