File tree Expand file tree Collapse file tree 1 file changed +65
-0
lines changed
group08/1425809544/02-26/com/xuyangyang/util Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments