-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBinaryTree.java
More file actions
138 lines (103 loc) · 3.34 KB
/
BinaryTree.java
File metadata and controls
138 lines (103 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package data.structures;
public class BinaryTree {
Object root;
BinaryTree left, right;
BinaryTree(){
this.root = null;
}
BinaryTree(Object root){
this.root = root;
}
BinaryTree(Object root, BinaryTree left, BinaryTree right){
this.root = root;
this.left = left;
this.right = right;
}
public Object getRoot() {
return root;
}
public void setRoot(Object root) {
this.root = root;
}
public BinaryTree getLeft() {
return left;
}
public void setLeft(BinaryTree left) {
this.left = left;
}
public BinaryTree getRight() {
return right;
}
public void setRight(BinaryTree right) {
this.right = right;
}
public String inOrder(){
StringBuffer buffString = new StringBuffer();
if(left != null)
buffString.append(left.inOrder()).append(" ");
buffString.append(root).append(" ");
if( right != null)
buffString.append(right.inOrder()).append(" ");
return buffString + "";
}
public String preOrder(){
StringBuffer buf = new StringBuffer();
buf.append(root).append(" ");
if( left != null)
buf.append(left.preOrder()).append(" ");
if( right != null)
buf.append(right.preOrder()).append(" ");
return buf + "";
}
public String postOrder(){
StringBuffer buf = new StringBuffer();
if(left != null)
buf.append(left.postOrder()).append(" ");
if(right != null)
buf.append(right.postOrder()).append(" ");
buf.append(root).append(" ");
return buf + "";
}
public boolean isLeaf(){
return (left == null && right == null);
}
public int size(){
if(root == null) return 0;
if(left == null && right == null)
return 1;
if(left == null)
return 1+ right.size();
if(right == null)
return 1+ left.size();
return 1 + left.size() + right.size();
}
public int height(){
if(root == null) return -1;
int leftNode = 0;
int rightNode = 0;
if(left != null)
leftNode = 1 + left.height();
if(right != null)
rightNode = 1 + right.height();
return leftNode>rightNode?leftNode:rightNode;
}
public boolean contain(Object target){
if(root == target) return true;
if(left != null)
if(left.contain(target))
return true;
if(right != null)
if(right.contain(target))
return true;
return false;
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree("Zohaib");
BinaryTree left = new BinaryTree("leftHand");
tree.setLeft(left);
BinaryTree right = new BinaryTree("rightHand");
tree.setRight(right);
System.out.println(tree.size());
System.out.println(tree.inOrder());
}
}