Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5485b41

Browse files
authoredMar 12, 2022
Update Trees.md
1 parent d934600 commit 5485b41

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
 

‎Basic Data Structures/Implementation of Binary Search Tree in Javascript/Trees.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Binary trees are a special type of tree in which each node can only have a maxim
1717

1818
## Binary Search Tree
1919

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+
2022
```js
2123
// Node class
2224
class Node
@@ -119,3 +121,93 @@ insertNode(node, newNode)
119121
}
120122
}
121123
```
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+
return null;
153+
154+
// if data to be delete is less than
155+
// roots data then move to left subtree
156+
else if(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+
else if(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+
else if(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

Comments
 (0)
Please sign in to comment.