2. BINARY SEARCH TREE:
• Binary search tree is a data structure that quickly allows us to maintain a
sorted list of numbers. It is called a binary tree because each tree node has
a maximum of two children. It is called a search tree because it can be used
to search for the presence of a number in O(log(n)) time.
3. STRUCTURE:
• Each node in a BST contains a key/value pair and has
at most two children: a left child and a right child.
• The left child contains a key/value pair smaller than
its parent, while the right child contains a key/value
pair greater than its parent.
• This property holds true for every node in the tree,
making it easy to search for elements.
4. BASIC OPERATIONS
In-order Traversal − Traverses a tree in an in-order manner.
Pre-order Traversal − Traverses a tree in a pre-order manner.
Post-order Traversal − Traverses a tree in a post-order manner.
Search − Searches an element in a tree.
Insert − Inserts an element in a Tree.
Deletion- Delete an element in tree.
5. TEVERSAL IN BINARY SEARCH TREE:
• 1. In order Teversal:
Visit the left subtree, then the current node, and
finally the right subtree. It prints the nodes in ascending order.(L,N,R)
• Example: 3,4,5,7,9,14,15,16,17,18,20
• 2. Preorder Traversal:
Visit the current node, then the left subtree, and
finally the right subtree. (N,L,R)
• Example: 14,4,3,9,7,5,15,18,16,17,20
• 3. Post order Traversal:
Visit the left subtree, then the right subtree, and
finally the current node. (L,R,N)
• Example: 3,5,7,9,4,17,16,20,18,15,14
6. SEARCH OPERATION:
• The key property of a BST enables efficient searching. Starting from the root, we
compare the target value with the current node.
• If the target is smaller, we move to the left child; if it's larger, we move to the right
child.
• We repeat this process until we find a match or reach a null node, indicating that
the value is not present in the tree.
• The search operation has an average and worst-case time complexity of O(log n) in
balanced BSTs, where n is the number of elements.
7. EXAMPLE:
• Consider the graph shown below and the key = 6.
• Step 1: Initially compare the key with the root i.e., 8. As 6
is less than 8, search in the left subtree of 8.
• Step 2: Now compare the key with 3. As key is greater
than 3, search next in the right subtree of 3.
• Step 3:Now compare the key with 6. The value of the key
is 6. So we have found the key.
8. INSERTION OPERATION:
The insertion logic into BST is similar to its searching operation. A new value is always inserted
at the leaf node of the BST.
Compare the value with the root of the BST.
If the value to be inserted is less than the root, move to the left subtree.
Otherwise, if the value is greater than the root, move to the right subtree.
Continue this process, until we hit a leaf node.
If the value is less than the leaf, create a left child of the leaf and insert the value.
Otherwise, if the value is greater than the leaf, create a right child of the leaf and insert the value
in the right child.
9. EXAMPLE:
• Consider the following BST and the value
= 40 to be added.
• Initially, 40 is less than 100. So move to
the left subtree.
• Now, 40 is greater than 20. So move to
the right subtree.
• Now we reach the leaf node 30. As 40 is
greater than 30, create right child of 30
and insert the value 40.
After insertion
Before insertion
10. DELETION OPERATION
Deleting a node from a BST involves three possible cases:
Case 1: The node has no children: We can simply remove the node.
Case 2: The node has one child: We replace the node with its child.
Case 3: The node has two children: We find the minimum value in the right subtree (or the maximum value
in the left subtree) and replace the node with it, then delete the duplicate value from the subtree.
The time complexity of the deletion operation is also O(log n) in balanced BSTs.