Data Structures-Basic Operations of BST

Basic Operations of BST

The following are the basic operations of Binary Search Tree:

  • Insert – It inserts an element in a tree / Create a tree.
  • Search – It searches an element in a tree.
  • Preorder traversal – Traverses a tree in a pre-order manner.
  • Postorder traversal – Traverses a tree in a post-order manner.
  • Inorder traversal – Traverses a tree in an in-order manner.

Let see how we insert a element in a tree, search an element and traversing a tree in detail below.

Insert Operation

While inserting an element to the tree, we start from the root node. If the given element is less than the root node, then go to the left node of the tree and if the given element is greater than the root node, then go to the right node. Traverse till you find the empty node and insert the given element to the tree in the empty location. The very first insertion creates the tree.

Example:

Let us consider, we insert an element 4 to the given tree:

                            

The following is the C code to insert an element to the tree. If the tree is empty, the inserted node becomes the root node.

//To insert an element to the tree / create a tree.
void insert(int data) {
   struct node *tempNode = (struct node*) malloc(sizeof(struct node));
   struct node *current;
   struct node *parent;

   tempNode->data = data;
   tempNode->leftChild = NULL;
   tempNode->rightChild = NULL;

   //Check if tree is empty, create root node
   if(root == NULL) {
      root = tempNode;
   } else {
      current = root;
      parent  = NULL;

      while(1) {
         parent = current;

         //Go to left of the tree
         if(data < parent->data) {
            current = current->leftChild;

            //Insert an element to the left
            if(current == NULL) {
               parent->leftChild = tempNode;
               return;
            }
         }

         //Go to right of the tree
         else {
            current = current->rightChild;

            //Insert an element to the right
            if(current == NULL) {
               parent->rightChild = tempNode;
               return;
            }
         }
      }
   }
}

Search Operation

Similar to insert operation, the search operation is also performed. Whenever an element to be searched, start traversing from the root node, if the element to be searched is less than the root element, then search for the element in the left subtree, else search for the element in the right subtree. Sometimes, the given element may not present in the binary tree, in such cases simply return NULL.

Example

Search an element 6 in the below tree:

                                             

From the above example, the tree node 6 can be searched using following traversal steps:

  • Check if the root node element 5 is less than the number to be searched (yes, 5<6), then traverse to right subtree. If the root node is equal to the given number then return.
  • First check whether the both the elements are equal. If not, check the right subtree element 7 is less than the number to be searched(no, 7>6), then traverse to left subtree.
  • Check if the left subtree element 6 is equal to the given number(yes, 6==6), then simply return true.

Below is the C code implementation for search operation of BST:

//To search a given element in the binary tree
struct node* search(int data) {
   struct node *current = root;
   printf("Visiting elements: ");

   while(current->data != data) {
      if(current != NULL)
      printf("%d ",current->data); 
      
      //if the given number is less than the current element,
      //then go to left subtree

      if(current->data > data) {
         current = current->leftChild;
      }
      //else go to right subtree
      else {                
         current = current->rightChild;
      }

      //The given number is not found on the binary tree.
      if(current == NULL) {
         return NULL;
      }

      return current;
   }  
}