-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary tree.java
242 lines (203 loc) · 5.5 KB
/
Binary tree.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import java.utils.*;
/**
* Created by BenjiFischman on 1-12-2016.
*/
public class Node {
private Node leftNode;
private Node rightNode;
private Node parentNode;
private int data;
public Node getLeftNode() {
return leftNode;
}
public void setLeftNode(Node leftNode) {
this.leftNode = leftNode;
}
public Node getRightNode() {
return rightNode;
}
public Node getParentNode() {
return parentNode;
}
public int getData() {
return data;
}
public void setRightNode(Node rightNode) {
this.rightNode = rightNode;
}
public void setParentNode(Node parentNode) {
this.parentNode = parentNode;
}
}
* For a node at index i
*
* Left Child = 2i+1
* Right Child = 2i+2
* Insert
*
* Find a position to insert O(logn)
* Start at root
private void addNode(Node pNode, Node newNode) {
if (newNode.getData() < pNode.getData()) {
if (pNode.getLeftNode() == null) {
pNode.setLeftNode(newNode);
newNode.setParentNode(pNode);
} else {
addNode(pNode.getLeftNode(), newNode);
}
} else {
if (pNode.getRightNode() == null) {
pNode.setRightNode(newNode);
newNode.setParentNode(pNode);
} else {
addNode(pNode.getRightNode(), newNode);
}
}
}
* Deletion
*
* No Children
*
* Unlink the leaf, and the garbage collector will delete it.
*
public void deleteNode(Node node) {
// Node is a leaf node //
if( node.getLeftNode() == null && node.getRightNode() == null){
if(isRightNode(node.getParentNode(), node)){
node.getParentNode().setRightNode(null);
}else{
node.getParentNode().setLeftNode(null);
}
* One Child
*
* Ensure node is not the root of a subtree
* Left Child
* // Only left child is there//
}else if( node.getLeftNode() != null && node.getRightNode() == null){
if(isRightNode(node.getParentNode(), node)){
node.getParentNode().setRightNode(node.getLeftNode());
}else{
node.getParentNode().setLeftNode(node.getLeftNode());
}
* Right Child
* // Only Right child is there //
}else if( node.getLeftNode() == null && node.getRightNode() != null){
if( isRightNode(node.getParentNode(), node)){
node.getParentNode().setRightNode(node.getRightNode());
}else{
node.getParentNode().setLeftNode(node.getRightNode());
}
* Height of a tree O(n)
*
* Counting edges
* When recursion returns a -1 method has reached an empty tree or points to null.
* Convention states the height of an empty tree is -1
* returning -1 accounts for the method counting the edge when no nodes exists.
public static int height (Node node){
if (node == null) {
return -1;}
else{
return 1 + Math.max(height(node.getLeftNode()),height((node.getRightNode())));
Breadth- first
* Visit all of a nodes children before visiting its grandchildren
* Level Order Traversal O(n)
* Implement with a Queue
private void levelOrderTraversal ( Node node){
if( node == null){
return;
}
Queue<Node> myQueue = new LinkedList<>();
myQueue.offer(node);
while (!myQueue.isEmpty()){
Node thisNode = myQueue.peek();
Node leftSubTree = thisNode.getLeftNode();
Node rightSubTree = thisNode.getRightNode();
myQueue.offer(leftSubTree);
myQueue.offer(rightSubTree);
myQueue.poll();
}
}
* Depth- first
* Left sub tree is always visited first
* The only variance in the methods is when the root is processed
*
* Pre-Order DLR
*
* Visit the root
* Left sub tree recursively
* Right sub tree recursively
* In-Order LDR
*
* Left sub tree recursively
* Visit root
* Right sub tree recursively
* Post- order LRD
*
* Left
* Right
* Root
* Check if a given tree is a BST
*
* //How can we implement with getMax and getMin?
public boolean IsBinarySearchTree (Node root){
if(root == null){
return true;
}
else if(isSubTreeGreater(root.getRightNode(),root.getData())
&& isSubTreeLesser(root.getLeftNode(),root.getData())
&& isBinarySearchTree(root.getLeftNode())
&& isBinarySearchTree(root.getLeftNode()){
return true;
}
return false;
}
public boolean isSubTreeGreater(Node root, int val){
if (root == null){
return true;
}
else if( root.getData()<= val
&& isSubTreeGreater(root.getLeftNode(),val)
&& isSubTreeGreater(root.getRightNode(), val)){
return true;
}
return false;
}
public boolean isSubTreeLesser(Node root, int val){
if (root == null){
return true;
}
else if( root.getData()<= val
&& isSubTreeLesser(root.getLeftNode(),val)
&& isSubTreeLesser(root.getRightNode(), val)){
return true;
}
return false;
}
public Node predessor(Node node) {
Node pNode = null;
if (node.getLeftNode() != null) {
Node newNode = node.getLeftNode();
while (newNode != null){
pNode=newNode;
newNode=newNode.getRightNode();
}
}
else{
pNode=findLeftParent(node);
}
return pNode;
}
public Node successor (Node node){
Node sNode = null;
if(node.getRightNode() != null){
Node newNode = node.getRightNode();
while (newNode != null){
sNode=newNode;
newNode=newNode.getLeftNode();
}
}
else {
sNode = findRightParent(node);
}
return sNode;
}