-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTree.js
More file actions
230 lines (192 loc) · 6.25 KB
/
binaryTree.js
File metadata and controls
230 lines (192 loc) · 6.25 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
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
/**
* This is my impelemntation of binary search tree
* by Vesko Vujovic
*/
// create node for binary tree that contains data and left and right links to child nodes
var nodes = [];
var nodes2 = [];
var nodes3 = [];
var nodes4 = [];
var min = '';
var max = '';
// Initialize node
function Node(data, left, right){
this.data = data;
this.left = left;
this.right = right;
this.show = show;
}
// displays data stored in node
function show() {
return this.data;
}
function BST(){
this.root = null;
this.insert = insert;
this.inOrderTraversal = inOrderTraversal;
this.preOrderTraversal = preOrderTraversal;
this.postOrderTraversal = postOrderTraversal;
this.getMinimum = getMinimum;
this.getMaximum = getMaximum;
this.findNode = findNode;
this.remove = remove;
}
/**
* ----- Logic for the binary search tree -----
* 1. Set the root node to be the current node.
* 2. If the data value in the inserted node is less than the data value in current node, set
* the new current node to be the left child of the current node. If the data value in the
* inserted bide us greater than the data value in the current node, skip to step 4
* 3. If the value of the left child of the current node is null insert the new node here, and
* exit the loop . Otherwise, skip to the next iteration of the loop
* 4.Set the current node to be the right child of the current node
* 5.If the right child of the current node is null, insert new node here, and exit the loop/
* Otherwise, skip to next iteration of the loop
*/
function insert(data){
var n = new Node(data, null, null);
if(this.root == null){
this.root = n;
}
else {
var current = this.root;
var parent;
while(true){
parent = current;
if(data < current.data){
current = current.left;
if(current == null){
parent.left = n;
break;
}
}
else{
current = current.right;
if(current == null){
parent.right = n;
break;
}
}
}
}
}
// Traverse the binary tree with inOrder traversal method
function inOrderTraversal(node) {
if (!(node == null)) {
inOrderTraversal(node.left);
nodes.push(node.show());
inOrderTraversal(node.right);
}
//console.log(nodes);
}
// preOrder traversal
function preOrderTraversal(node) {
if (!(node == null)) {
nodes2.push(node.show());
preOrderTraversal(node.left);
preOrderTraversal(node.right);
}
}
// postOrder traversal
function postOrderTraversal(node){
if (!(node == null)) {
postOrderTraversal(node.left);
postOrderTraversal(node.right);
nodes3.push(node.show());
}
}
/**
* Here we traverse whole left side of the tree, when current.left is null is reached we have our minimum
*/
// getMin node in the tree
function getMinimum() {
var current = this.root;
while(!(current.left == null)){
current = current.left;
}
return current.data;
}
// getMax node in the tree
function getMaximum() {
var current = this.root;
while(!(current.right == null)){
current = current.right;
}
return current.data;
}
// find node in tree
function findNode(data){
var current = this.root;
while(data !== current.data){
if(data < current.left){
current = current.left;
}
else {
current = current.right;
}
if(current == null){
return null;
}
}
return current.data;
}
/**
* Removing leaf and nodes from tree
*
* The first step to take when removing a node in TREE is to check to see if the current node holds the data that
* that we are trying to remove. If so, remove that node. If not, then we compare the data in the current node with
* the data we are trying to remove . If the data we want to remove is less than the data in current node, move to
* the left child of the current node and compare data. If the data we want to remove is greater than the data in
* current node, move to the right child of the current node and compare data.
*
* The first case to consider is when the node to be removed is LEAF( a node with no children ). Then all we have
* to do is set the link that is pointing to the node of parent node to null.
*
* The node removal is made with a help of two functions. The remove() function simply receives the value to be
* removed and calls the second function removeNode(), which does all the heavy lifting. :)
*/
// find smallest node on the right side of the tree
function getSmallestNodeToTheRight(node){
var current = node;
while(!(current.left == null )){
current = current.left;
}
return current.data;
}
// remove node
function remove(data){
var root = removeNode(this.root, data);
}
// function that actually removes nodes
function removeNode(node, data){
if(node == null){
return null;
}
if(data == node.data){
// node has no children
if(node.left == null && node.right == null){
return null;
}
// node has no left child
if(node.left == null){
return node.right;
}
// node has no right child
if(node.right == null){
return node.left;
}
// node has two children
var tempNode = getSmallestNodeToTheRight(node.right);
node.data = tempNode.data;
node.right = removeNode(node.right, node.data);
return node;
}
else if(data < node.data){
node.left = removeNode(node.left, data);
return node;
}
else {
node.right = removeNode(node.right, data);
return node;
}
}