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
+88Lines changed: 88 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -6,3 +6,91 @@ section: "Binary Search Tree"
6
6
description: "learn about Binary Search Tree from scratch"
7
7
icon: "search"
8
8
---
9
+
### Binary Search Tree
10
+
A Binary Search Tree is a binary tree that additionally satisfies the binary search property.
11
+
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.
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
+
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
+
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
+
4. This process is repeated until the key is found or the remaining subtree is null.
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
+
26
+
### Insertion
27
+
To insert for a key in the tree , we follow the binary search property and insert accordingly.
28
+
1. Compare the key to be searched with the root key.
29
+
2. If the key is lesser than the roots value, we return the left subtree of the node.
30
+
3. If the key is greater than the roots value, we return the right subtree of node.
31
+
4. This process continued until we hit a leaf node. The new node is inserted to this location as a new node.
32
+
33
+
### Deletion
34
+
When removing a node from a binary search tree it is mandatory to maintain the in-order sequence of the nodes.
35
+
There are three possible cases to consider:
36
+
1. Deleting a node with no childeren :simply remove the node from the tree
37
+
2. Deleting a node with one child: remove the node and replace it with its child.
38
+
3. Deleting a node with two children: First we find the inorder successor of the node. <br>
39
+
Then the contents of this in-order successor are copied to the node to be deleted.
40
+
Finally, the in-order successor is deleted
41
+
<br>
42
+
43
+
Let's see the implementation of Binary Search Tree using an example:
44
+
45
+
```java
46
+
// Java implementation of recursive Binary Search
47
+
classBinarySearch {
48
+
// Returns index of x if it is present in arr[l..r],
49
+
// else return -1
50
+
intbinarySearch(intarr[], intl, intr, intx)
51
+
{
52
+
if (r >= l) {
53
+
int mid = l + (r - l) /2;
54
+
55
+
// If the element is present at the
56
+
// middle itself
57
+
if (arr[mid] == x)
58
+
return mid;
59
+
60
+
// If element is smaller than mid, then
61
+
// it can only be present in left subarray
62
+
if (arr[mid] > x)
63
+
return binarySearch(arr, l, mid -1, x);
64
+
65
+
// Else the element can only be present
66
+
// in right subarray
67
+
return binarySearch(arr, mid +1, r, x);
68
+
}
69
+
70
+
// We reach here when element is not present
71
+
// in array
72
+
return-1;
73
+
}
74
+
75
+
// Driver method to test above
76
+
publicstaticvoidmain(Stringargs[])
77
+
{
78
+
BinarySearch ob =newBinarySearch();
79
+
int arr[] = { 2, 3, 4, 10, 40 };
80
+
int n = arr.length;
81
+
int x =10;
82
+
int result = ob.binarySearch(arr, 0, n -1, x);
83
+
if (result ==-1)
84
+
System.out.println("Element not present");
85
+
else
86
+
System.out.println("Element found at index "+ result);
0 commit comments