×

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

Bubble Sort vs Quick Sort

In this article, we are going to compare two sorting techniques, Bubble sort and Quick Sort. In starting, we will first discuss the idea of sorting an array using bubble...

7 minutes read.

Binary Tree Uses

A binary tree is a tree data structure containing hubs with at most two children for instance a right and left child. The node at the top is insinuated as the...

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

Compare Balanced Binary Tree and Complete Binary Tree

Complete and balanced binary trees are important and general topics in the concept – Tree data structure. Before discussing the complete and balanced binary tree, we need to have an...

8 minutes read.

What is the difference between DFS and BFS?

What is BFS? BFS is generally known as the low level traversal. As we already know that it stands for breadth first search and is mainly used in the queue data...

4 minutes read.

Function to Create a Copy of Binary Search Tree

Implementation // creating a new hashmap in the language C++ that will help us clone a binary tree with arbitrary pointers.  #include<iostream> #include<unordered_map> using namespace std; /* A given binary tree has a record, a...

9 minutes read.

Convert Binary Tree into a Threaded Binary Tree

Implementation /*Writing a C++ program that will help us change the binary tree into a threaded binary tree and help us transform. */ #include <bits/stdc++.h> using namespace std; /*Creating the structure of a node...

11 minutes read.

Binary Tree to Doubly Linked List

Binary Tree to Doubly Linked List This article will explain how to convert the given binary tree into a Doubly Linked List. The left and right pointers in tree nodes are...

2 minutes read.

Graph Data Structure

A graph is a non-primitive and non-linear data structure. It is a group of (V, E) where V is a set of vertexes, and E is a set of edge....

3 minutes read.

Linear Queue VS Circular Queue

What is Queue? A queue is one of the important linear data structures extensively used in various computer applications. It is based on the FIFO (First In First Out) principle. It...

9 minutes read.

Given a Binary Tree Swap Nodes at K Height

Implementation // Writing a C++ program that will help us exchange the nodes.  #include<bits/stdc++.h> using namespace std; // Creating a binary tree node. struct __nod { int record; struct __nod *Lft, *Rt; }; // creating a function that will help...

8 minutes read.

Digital Search Tree in Data Structures

What is a digital search Tree in Data Structures? The Digital search tree is known for its application and diversity in the way it has impacted our world in the field...

3 minutes read.

Circular Queue

Circular Queue Circular Queue is special type queue, which follows First in First Out (FIFO) rule and as well as instead of ending queue at the last position, it starts again...

4 minutes read.

B+ Tree in Data Structure

A B-Tree extension called B+ Tree, which enables effective search, insertion, and deletion operations. Both Records and keys can be stored in internal and leaf nodes in a B tree. Contrarily,...

4 minutes read.

Bubble Sort vs Merge Sort

In this article, we are going to compare two sorting techniques, Bubble sort and Merge Sort. In starting, we will first discuss the idea of sorting an array using bubble...

7 minutes read.

Strictly binary tree in Data Structures?

What is a strictly Binary Tree in Data Structures? There are various kinds of binary trees that we know exist in data structures, and they all have their purposes. In this...

4 minutes read.

Cycle sort

Cycle sort is an examination arranging calculation which powers exhibit to be figured into the quantity of cycles where every one of them can be pivoted to create an arranged...

5 minutes read.

2-3 Trees and Basic Operations on them

2-3 Trees, like any other AVL trees or B-trees, are just a type of Height Balanced Tree. 2-3 Trees are the B-trees of order 3. Like every other B-tree, the...

4 minutes read.

Find the fractional (n/kth) node in the linked list

Find the fractional (n/kth) node in the linked list In this problem, we have given a singly linked list and a number k. Here we need to find the (n/k)th element...

2 minutes read.

Given a Binary Tree Print the Shortest Path

Implementation // Writing a program in C++ to find the shortest between the nodes i and j.  #include <bits/stdc++.h> using namespace std; // the given function will print the path between nodes i and...

7 minutes read.