Binary Search Trees Searching AVL Tree B Tree
Binary Search Trees Searching AVL Tree B Tree
Binary Search Trees Searching AVL Tree B Tree
2 2
Binary Search Tree
• What is a Binary Search tree?
• A binary search tree follows some order to arrange
the elements. In a Binary search tree, the value of
left node must be smaller than the parent node,
and the value of right node must be greater than
the parent node. This rule is applied recursively to
the left and right subtrees of the root.
Advantages Of Binary
Search Tree
• Searching an element in the Binary search tree is
easy as we always have a hint that which subtree
has the desired element.
• As compared to array and linked lists, insertion and
deletion operations are faster in BST.
Example of creating a
binary search tree
• Now, let's see the creation of binary search tree using an
example.
• Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50
• First, we have to insert 45 into the tree as the root of the tree.
• Then, read the next element; if it is smaller than the root node,
insert it as the root of the left subtree, and move to the next
element.
• Otherwise, if the element is larger than the root node, then
insert it as the root of the right subtree.
• Now, let's see the process of creating the Binary search tree using
the given data element. The process of creating the BST is shown
below -
Continue..
• Step 1 - Insert 45.
This case of deleting a node in BST is a bit complex among other two cases.
In such a case, the steps to be followed are listed as follows -
• First, find the inorder successor of the node to be deleted.
• After that, replace that node with the inorder successor until the target
node is placed at the leaf of tree.
• And at last, replace the node with NULL and free up the allocated space.
The inorder successor is required when the right child of the node is not
empty. We can obtain the inorder successor by finding the minimum
element in the right child of the node.
We can see the process of deleting a node with two children from BST in
the below image. In the below image, suppose we have to delete node 45
that is the root node, as the node to be deleted has two children, so it will
be replaced with its inorder successor. Now, node 45 will be at the leaf of
the tree so that it can be deleted easily.
When the node to be deleted has
two children
Insertion in Binary Search tree
A new key in BST is always inserted at the leaf. To
insert an element in BST, we have to start searching
from the root node; if the node to be inserted is less
than the root node, then search for an empty location
in the left subtree. Else, search for the empty location
in the right subtree and insert the data. Insert in BST is
similar to searching, as we always have to maintain
the rule that the left subtree is smaller than the root,
and right subtree is larger than the root.
Now, let's see the process of inserting a node into BST
using an example.
Insertion in Binary Search tree
AVL Tree
70