-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStandardBinaryTree.java
201 lines (184 loc) · 5.96 KB
/
StandardBinaryTree.java
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
public class StandardBinaryTree<T extends Comparable<T>> extends BinaryTree<T> {
@Override
public void insert(T data){
super.insert(data, true);
}
@Override
public int height(){
//TODO: Implement this function
Leaf<T> tempRoot = this.root;
return HeightHelper(tempRoot) - 1;
}
@Override
public boolean contains(T data){
//TODO: Implement this function
Leaf<T> tempRoot = this.root;
return ContainsHelper(tempRoot, data);
}
@Override
public Leaf<T> find(T data){
//TODO: Implement this function
Leaf<T> tempRoot = this.root;
return FindHelper(tempRoot, data);
}
@Override
public void depthFirstTraversal(){
//TODO: Implement this function
Leaf<T> tempRoot = this.root;
DFST_Inoder(tempRoot);
}
@Override
public int numLeavesInTree(){
//TODO: Implement this function
Leaf<T> tempRoot = this.root;
return numLeavesInTreeHelper(tempRoot);
}
@Override
public boolean perfectlyBalanced(){
//TODO: Implement this function
Leaf<T> tempRoot = this.root;
return (BalancedHelper(tempRoot) % 2 == 0);
}
@Override
public Leaf<T> findParent(T data){
//TODO: Implement this function
Leaf<T> tempRoot = this.root;
if(tempRoot != null && tempRoot.data.compareTo(data) == 0){
System.out.println(tempRoot.toString());
return null;
}
return findParentHelper(tempRoot, data);
}
@Override
public BinaryTree<T> convertTree(){
//TODO: Implement this function
Leaf<T> tempRoot = this.root;
Leaf<T> cloneTreeNode = clone(tempRoot);
BinaryTree<T> returnTree = new MirroredBinaryTree<T>();
returnTree.root = convertTreeHelper(cloneTreeNode);
return returnTree;
}
// HELPER FUNCTIONS
public Leaf<T> clone(Leaf<T> tempRoot){
if(isEmpty() == true || tempRoot == null){
return null;
}
Leaf<T> cloneHead = new Leaf<T>(tempRoot.data);
cloneHead.right = clone(tempRoot.right);
cloneHead.left = clone(tempRoot.left);
return cloneHead;
}
public Leaf<T> convertTreeHelper(Leaf<T> tempRoot){
if(isEmpty() == true || tempRoot == null){
return null;
}
Leaf<T> leftSubtre = convertTreeHelper(tempRoot.left);
Leaf<T> rightSubtre = convertTreeHelper(tempRoot.right);
tempRoot.left = rightSubtre;
tempRoot.right = leftSubtre;
return tempRoot;
}
public boolean isEmpty(){
return (this.root == null);
}
// * Correct
public boolean ContainsHelper(Leaf<T> tempRoot, T elem){
if(isEmpty() == true || tempRoot == null){
return false;
}
System.out.println(tempRoot.toString());
if(elem.compareTo(tempRoot.data) == 0){
return true;
}
if(elem.compareTo(tempRoot.data) < 0){
return ContainsHelper(tempRoot.left, elem);
}
else{
return ContainsHelper(tempRoot.right, elem);
}
}
// * Correct
public Leaf<T> FindHelper(Leaf<T> tempRoot, T elem){
if(isEmpty() == true || tempRoot == null){
return null;
}
System.out.println(tempRoot.toString());
if(elem.compareTo(tempRoot.data) == 0){
return tempRoot;
}
if(elem.compareTo(tempRoot.data) < 0){
return FindHelper(tempRoot.left, elem);
}
else{
return FindHelper(tempRoot.right, elem);
}
}
public int HeightHelper(Leaf<T> tempRoot){
if(isEmpty() == true || tempRoot == null){
return 0;
}
else{
int tempLeft = HeightHelper(tempRoot.left);
int tempRight = HeightHelper(tempRoot.right);
return (Math.max(tempLeft, tempRight)) + 1;
}
}
// * Correct
public int BalancedHelper(Leaf<T> tempRoot){
if(isEmpty() == true || tempRoot == null){
return 0;
}
else{
int tempLeft = BalancedHelper(tempRoot.left);
int tempRight = BalancedHelper(tempRoot.right);
if(tempLeft == tempRight){
return 2;
}
return 3;
}
}
// * Correct
public void DFST_Inoder(Leaf<T> tempRoot){
if(isEmpty() == true || tempRoot != null){
DFST_Inoder(tempRoot.left);
System.out.println(tempRoot.toString());
DFST_Inoder(tempRoot.right);
}
}
// * Correct
public int numLeavesInTreeHelper(Leaf<T> tempRoot){
if(isEmpty() == true || tempRoot == null){
return 0;
}
return 1 + numLeavesInTreeHelper(tempRoot.left) + numLeavesInTreeHelper(tempRoot.right);
}
// * Correct
public Leaf<T> findParentHelper(Leaf<T> tempRoot, T data){
//TODO: Implement this function
if(isEmpty() == true || tempRoot == null || tempRoot.data.compareTo(data) == 0){
return null;
}
System.out.println(tempRoot.toString());
if(data.compareTo(tempRoot.data) < 0){
if(tempRoot.left != null && tempRoot.left.data.compareTo(data) == 0){
if(tempRoot.left.data.compareTo(data) == 0){
return tempRoot;
}
}
else{
return findParentHelper(tempRoot.left, data);
}
}
else{
if(tempRoot.right != null && tempRoot.right.data.compareTo(data) == 0){
if(tempRoot.right.data.compareTo(data) == 0){
return tempRoot;
}
}
else{
return findParentHelper(tempRoot.right, data);
}
}
return null;
}
}