You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Basic Data Structures/Implementation of Binary Search Tree in Javascript/Trees.md
+92Lines changed: 92 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,8 @@ Binary trees are a special type of tree in which each node can only have a maxim
17
17
18
18
## Binary Search Tree
19
19
20
+
> A Binary Search tree is a binary tree in which nodes that have lesser value are stored on the left while the nodes with a higher value are stored at the right.
21
+
20
22
```js
21
23
// Node class
22
24
classNode
@@ -119,3 +121,93 @@ insertNode(node, newNode)
119
121
}
120
122
}
121
123
```
124
+
125
+
In the above code we have two methods insert(data) and insertNode(node, newNode). Let’s understand them one by one:-
126
+
127
+
***insert(data)** – It creates a new node with a value data, if the tree is empty it add this node to a tree and make it a root, otherwise it calls insert(node, data).
128
+
***insert(node, data)** – It compares the given data with the data of the current node and moves left or right accordingly and recur until it finds a correct node with a null value where new node can be added.
129
+
130
+
**2.remove(data)** – This function removes a node with a given data.
131
+
132
+
```js
133
+
// helper method that calls the
134
+
// removeNode with a given data
135
+
remove(data)
136
+
{
137
+
// root is re-initialized with
138
+
// root of a modified tree.
139
+
this.root=this.removeNode(this.root, data);
140
+
}
141
+
142
+
// Method to remove node with a
143
+
// given data
144
+
// it recur over the tree to find the
145
+
// data and removes it
146
+
removeNode(node, key)
147
+
{
148
+
149
+
// if the root is null then tree is
150
+
// empty
151
+
if(node ===null)
152
+
returnnull;
153
+
154
+
// if data to be delete is less than
155
+
// roots data then move to left subtree
156
+
elseif(key <node.data)
157
+
{
158
+
node.left=this.removeNode(node.left, key);
159
+
return node;
160
+
}
161
+
162
+
// if data to be delete is greater than
163
+
// roots data then move to right subtree
164
+
elseif(key >node.data)
165
+
{
166
+
node.right=this.removeNode(node.right, key);
167
+
return node;
168
+
}
169
+
170
+
// if data is similar to the root's data
171
+
// then delete this node
172
+
else
173
+
{
174
+
// deleting node with no children
175
+
if(node.left===null&&node.right===null)
176
+
{
177
+
node =null;
178
+
return node;
179
+
}
180
+
181
+
// deleting node with one children
182
+
if(node.left===null)
183
+
{
184
+
node =node.right;
185
+
return node;
186
+
}
187
+
188
+
elseif(node.right===null)
189
+
{
190
+
node =node.left;
191
+
return node;
192
+
}
193
+
194
+
// Deleting node with two children
195
+
// minimum node of the right subtree
196
+
// is stored in aux
197
+
var aux =this.findMinNode(node.right);
198
+
node.data=aux.data;
199
+
200
+
node.right=this.removeNode(node.right, aux.data);
201
+
return node;
202
+
}
203
+
204
+
}
205
+
```
206
+
207
+
In the above code we have two methods remove(data) and removeNode(node, data), let understand them one by one:
208
+
209
+
***remove(data)** – It is helper methods which call removeNode by passing root node and given data and updates the root of the tree with the value returned by the function
210
+
***removeNode(node, data)** – It searches for a node with a given data and then perform certain steps to delete it.
211
+
***Deleting the leaf node** – As leaf node does not have any children, hence they can be easily removed and null is returned to the parent node
212
+
***Deleting a node with one child** – If a node has a left child, then we update the pointer of the parent node to the left child of the node to be deleted and similarly, if a node have a right child then we update the pointer of the parent node to the right child of the node to be deleted
213
+
***Deleting a node with two children** – In order to delete a node with two children we find the node with minimum value in its right subtree and replace this node with the minimum valued node and remove the minimum valued node from the tree
0 commit comments