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: lessons/intro-bst.md
+7-8Lines changed: 7 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -10,40 +10,39 @@ icon: "search"
10
10
A Binary Search Tree is a binary tree that additionally satisfies the binary search property.
11
11
12
12
### Binary Tree Property
13
-
This property states that the key in each node must be greater than or equal to any key stored in the left sub-tree and less than or equal to any key stored in the right sub-tree.
13
+
This property states that the key in each node must be greater than or equal to any key stored in the left sub-tree and less than or equal to any key stored in the right sub-tree.
1. We begin by examining the root node. If the tree is null , the key we are searching for does not exist in the tree.
21
21
2. If the key equals that of the root If the key is equal that of route the search is successful and we return the node .
22
22
3. If the key is less than that of the root we search the left subtree. Similarly, if the key is greater than that of the root, we search the right subtree.
23
23
4. This process is repeated until the key is found or the remaining subtree is null.
24
24
5. If the search key is not found after a null subtree is reached, then the key is not present in the tree.
25
25
26
-
## Insertion
26
+
###Insertion
27
27
To insert for a key in the tree , we follow the binary search property and insert accordingly.
28
28
1. Compare the key to be searched with the root key.
29
29
2. If the key is lesser than the roots value, we return the left subtree of the node.
30
30
3. If the key is greater than the roots value, we return the right subtree of node.
31
31
4. This process continued until we hit a leaf node. The new node is inserted to this location as a new node.
32
32
33
-
## Deletion
33
+
###Deletion
34
34
When removing a node from a binary search tree it is mandatory to maintain the in-order sequence of the nodes.
35
35
There are three possible cases to consider:
36
36
1. Deleting a node with no childeren :simply remove the node from the tree
37
-
38
37
2. Deleting a node with one child: remove the node and replace it with its child.
39
38
3. Deleting a node with two children: First we find the inorder successor of the node. <br>
40
39
Then the contents of this in-order successor are copied to the node to be deleted.
41
40
Finally, the in-order successor is deleted
42
41
<br>
43
42
44
-
## Example Code
43
+
## Let's see the implementation of Binary Search Tree using an example:
45
44
46
-
```
45
+
```java
47
46
// Java implementation of recursive Binary Search
48
47
classBinarySearch {
49
48
// Returns index of x if it is present in arr[l..r],
0 commit comments