×

Binary Search Tree

Binary Search Tree: A binary search tree is a type of tree in which every node is organized in the sorted order. It is also called an ordered binary tree.

Properties of BST

  1. The left sub-tree value is less than the root node.
  2. Similarly, the right sub-tree value is higher than the root node.
  3. This rule is reapplied to all left and right sub-trees of the root.
Binary Search Tree

Operations of the BST

There are three types of operations in the BST.

  1. Search operation
  2. Insertion operation
  3. Delete operation

Search operation:

The search operation is used to search a particular node in a BST. Whenever any node in BST is searched, that node is first compared with the root node. If the node is less than the root node, it is searched in the left subtree. If the node is greater than the root node, it is searched in the correct subtree.

Algorithm of the Search operation

struct node* search(int data)
{    
struct node *current = root;   
 printf("Visiting elements: ");                
while(current->data != data)
{           
 if(current != NULL) 
 { 
 printf("%d ",current->data);  
 }   
     //go to left tree                  
 if(current->data > data) 
          { 
             current = current->leftChild;
           } 
       //else go to right tree   
   Else  
  {      
  current = current->rightChild;   
 }          
 if(current == NULL){  
  return NULL;     
 }   
 }    
    }    
   return current;
 }

Insertion Operation

It is used to add a new node to a particular location in a specific situation. Whenever a new node is inserted in the BST, the location of that node is first searched. The new node is first compared with the root node. If the new node is less than the root node, search the null location in the left subtree and insert that node. If the node is greater than the root node, search the null location in the right subtree and insert that node.

Algorithm of the Insertion operation

TreeNode insert (int data, TreeNode T) 
{         
if T is NULL     
      {   
                T = (TreeNode *)malloc(sizeof (Struct TreeNode));         
          (Allocate Memory of new node and load the data into it)               
    T ? data = data;                   T ? left   = NULL;                   T ? right = NULL;           }                  
  else if T is less than T ? left     
      {              
    T ? left = insert(data, T ? left);    
               (Then node needs to be inserted in the left sub-tree. So, 
                  recursively traverse left sub-tree to find the place         
          where the new node needs to be inserted)     
      }           
     else if T is greater than T ? right    
       {                
    T ? right = insert(data, T ? right);     
               (Then node needs to be inserted in right sub-tree     
               So, recursively traverse right sub-tree to find the               
     place where the new node needs to be inserted.)    
      }    
      return T; 
}

Delete operation

This operation is used to delete a node in a specific situation. A node can be deleted from the following locations.

  1. Leaf node
  2. One child node
  3. Two child nodes

Leaf node: In this case, it simply removes the leaf node in the tree. This case is much simpler than other cases.

For example, remove the 6 in this BST.

Binary Search Tree

One child node: In this case, first, the original node is replaced with the child node, and then the node is removed. For example, remove the 9 in this BST.

Binary Search Tree

Related Topics

Function to Insert a Node in a Binary Search Tree

Implementation // writing C++ code that will help us in implementing the insertion operation in a binary search tree. #include <bits/stdc++.h> using namespace std; // creating a new binary search tree node struct __nod { int...

8 minutes read.

Bitwise Operators and their Important Tricks

In most of the programs you write today, you deal with data types comprising bytes, such as integer, float, double, etc. Dealing with bytes? It is a quite normal task,...

5 minutes read.

Data Structure Infix to Postfix Conversion

Infix to Postfix Conversion The infix expression is easy to read and write by humans. In present time, we use the infix expression in our daily life but the computers are...

4 minutes read.

Dijkstra’s vs Bellman-Ford Algorithm

The Dijkstra Algorithm One of the SSSP (Single Source Shortest Path) algorithms is Dijkstra's. As a result, it finds the shortest path between a source node and all other nodes in...

7 minutes read.

What are Forest Trees in Data Structure

Data structure A data model manages and optimizes computer resources, and a database stores and manages data. It's one of many uses for data structures to hold data. Data structures come...

5 minutes read.

Implementation of stack

Implementation of stack: The stack can be implemented in two ways: using array and using a linked list. The pop and push operations in the array are simpler than the...

3 minutes read.

What is a full Binary Tree?

A full binary tree is considered to be a special kind of binary tree in which every single node or leaf node present either contains two children or no children...

4 minutes read.

What is the Use of Segment Trees in Data Structure?

Segment trees Segment trees are also called statistical trees in computer science. They are a type of tree data structure. Segment trees are used to store information regarding segments and intervals....

6 minutes read.

Quick Sort vs Merge Sort

In this article, we will take an overview of Quick Sort and Merge Sort and then discuss the differences between them. What is Quick Sort? Quick Sort – The idea behind the...

7 minutes read.

Deletion Operation from A B Tree

This article will show the deletion operation through the b tree in C++ programming language. Implementation #include <iostream> using namespace std; class B_TreeNod {   int *kys;   int m;   BTreeNod **C;   int j;   bool leaf;  ...

5 minutes read.

Doubly Linked List

Doubly Linked List Doubly linked list is another kind of Linked list. Doubly linked list contains two pointers for navigation. In this, we can traverse the list in both directions, either...

4 minutes read.

Binary Tree Inorder Traversal

The binary tree is a type of tree in which each and every node has atleast two children except the leaf nodes. We have various operations in the binary tree,...

4 minutes read.

Linear vs Binary Search: Data Structure

Difference Between Linear and Binary Search What is Linear Search? A linear search also referred as a sequential search. It is a way to find an element within a list and it...

3 minutes read.

Cocktail Sort

C Program executes cocktail sort. Combo sort is a somewhat straightforward arranging calculation initially planned by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered by Stephen Lacey and Richard Box...

5 minutes read.

Left View of Binary Tree

Implementation // creating a C++ program to print the Left view of the binary tree. #include <bits/stdc++.h> using namespace std; struct Nod { int record; struct Nod *Lft, *Rt; }; // creating a utility function that will eventually help...

4 minutes read.

Polish Notation in Data Structures

Arithmetic Expression: An arithmetic expression is defined as several operands or data items combined using several operators. For example; a+b*(c-d) is an expression. Operands: Operands represent the data in an expression...

2 minutes read.

Find all possible words from board

We have been given a dictionary of words and a board of characters from which we can form strings. Now, we have to check if the string is present in...

5 minutes read.

Difference between complete and full binary tree

As we all know that the  binary tree is a tree it contains one or two children at each other node. It contains two children's nodes in the Binary tree. The...

6 minutes read.

Given a Perfect Binary Tree, Reverse Alternate Levels

Implementation //writing a program in C++ language to see how to approach it. #include <bits/stdc++.h> using namespace std; // creating a tree node. struct Nod { char ky; struct Nod *Lft, *Rt; }; // creating a new utility function...

9 minutes read.

Count pairs from two linked lists whose sum is equal to a given value

Count pairs from two linked lists whose sum is equal to a given value In this problem, we have given two linked lists of size n1 and n2 with distinct elements...

4 minutes read.