Binary search tree - IT magazine

IT magazine

Knowledge that matters

Binary search tree

Share This


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 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.

The properties that separates a binary search tree from a regular binary tree is


  • All nodes of left sub tree are less than root node
  • All nodes of right sub tree are more than root node
  • Both sub trees of each node are also BSTs i.e. they have the above two properties


The binary tree on the right isn't a binary search tree because the right sub tree of the node "3" contains a value smaller that it.

There are two basic operations that you can perform on a binary search tree:

1. Check if number is present in binary search tree


The algorithm depends on the property of BST that if each left sub tree has values below root and each right sub tree has values above root.

If the value is below root, we can say for sure that the value is not in the right sub tree; we need to only search in the left sub tree and if the value is above root, we can say for sure that the value is not in the left sub tree; we need to only search in the right sub tree.

Algorithm:


If root == NULL 
    return NULL;
If number == root->data 
    return root->data;
If number < root->data 
    return search(root->left)
If number > root->data 
    return search(root->right)
Let us try to visualize this with a diagram.


If the value is found, we return the value so that it gets propagated in each recursion step as shown in the image below.

If you might have noticed, we have called return search(struct node*) four times. When we return either the new node or NULL, the value gets returned again and again until search(root) returns the final result.


If the value is not found, we eventually reach the left or right child of a leaf node which is NULL and it gets propagated and returned.

2. Insert value in Binary Search Tree(BST)

Inserting a value in the correct position is similar to searching because we try to maintain the rule that left sub tree is lesser than root and right sub tree is larger than root.

We keep going to either right sub tree or left sub tree depending on the value and when we reach a point left or right sub tree is null, we put the new node there.

Algorithm:

If node == NULL 
    return createNode(data)
if (data < node->data)
    node->left  = insert(node->left, data);
else if (data > node->data)
    node->right = insert(node->right, data);  
return node;
The algorithm isn't as simple as it looks. Let's try to visualize how we add a number to an existing BST.


We have attached the node but we still have to exit from the function without doing any damage to the rest of the tree. This is where the return node; at the end comes in handy. In the case of NULL, the newly created node is returned and attached to the parent node, otherwise the same node is returned without any change as we go up until we return to the root.

This makes sure that as we move back up the tree, the other node connections aren't changed.

Advantages of using binary search tree


  • Searching become very efficient in a binary search tree since, we get a hint at each step, about which sub-tree contains the desired element.
  • The binary search tree is considered as efficient data structure in compare to arrays and linked lists. In searching process, it removes half sub-tree at every step. Searching for an element in a binary search tree takes o(log2n) time. In worst case, the time it takes to search an element is 0(n).
  • It also speed up the insertion and deletion operations as compare to that in array and linked list.

Example
Create the binary search tree using the following data elements.
43, 10, 79, 90, 12, 54, 11, 9, 50

  • Insert 43 into the tree as the root of the tree.
  • Read the next element, if it is lesser than the root node element, insert it as the root of the left sub-tree.
  • Otherwise, insert it as the root of the right of the right sub-tree.

The process of creating BST by using the given elements, is shown in the image below.





No comments:

Post a Comment