{"id":83520,"date":"2021-08-18T09:00:53","date_gmt":"2021-08-18T03:30:53","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=83520"},"modified":"2021-08-18T09:00:53","modified_gmt":"2021-08-18T03:30:53","slug":"binary-search-tree","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/","title":{"rendered":"Binary Search Tree in Data Structure"},"content":{"rendered":"<p>A binary search tree(BST) is a special type of binary tree and is also known as a sorted or ordered binary tree. In a binary search tree:<\/p>\n<ul>\n<li>The value of the left node is less than the value of the parent node.<\/li>\n<li>The value of the right node is greater than the value of the parent node.<\/li>\n<\/ul>\n<p>Thus, in a binary search tree, all the values in the left subtree are smaller than the value at the root node and all the values in the right subtree are larger than the value at the root node. A binary search tree usually does not have any duplicate nodes. The following tree represents a binary search tree:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/TV-Binary-search-tree-in-DS-normal-image01.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84106\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/TV-Binary-search-tree-in-DS-normal-image01.jpg\" alt=\"Binary search tree in DS\" width=\"494\" height=\"355\" \/><\/a><\/p>\n<p>The benefit of using a binary search tree over a binary tree is that search operation can be performed in O(log n) time because of ordering in the BST. A binary search tree performs better in search, insert and delete operations as compared to a simple binary tree.<\/p>\n<h3>Representation of Binary Search Tree:<\/h3>\n<p>Like a binary tree, a binary search tree is also a collection of nodes where each parent node can have a maximum of two children. A node in a binary search tree has the following structure:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">struct Node {\n   int key;   \n   struct Node *left_child;\n   struct Node *right_child;\n};\n<\/pre>\n<h3>Operations on Binary Search Tree:<\/h3>\n<p>1. Searching<br \/>\n2. Insertion<br \/>\n3. Deletion<br \/>\n4. Traversal<\/p>\n<p>The traversal operation is of three different types:<\/p>\n<p><strong>1. Pre-order traversal:<\/strong> It will traverse the root first, then the left child and at last the right child.<\/p>\n<p><strong>2. Post-order traversal:<\/strong> It will traverse the left subtree first, then root and then the right subtree.<\/p>\n<p><strong>3. Inorder traversal:<\/strong> It will traverse the left subtree, then root and then the right subtree.<\/p>\n<h3>Search Operation in a BST:<\/h3>\n<p>This operation is used to search a key value in a binary search tree. Searching in a BST is very efficient because the values are ordered. While searching for a value, we first compare it with the root node. If the value is less than the root node, then it must surely be present in the left subtree. If the value is greater than the root node, then it will be present in the right subtree. Thus, we will traverse the subtrees accordingly until we reach the desired location. The search operation takes O(log n) time.<\/p>\n<p>Let us understand with the help of the following example:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/TV-Binary-search-tree-in-DS-normal-image02.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84107\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/TV-Binary-search-tree-in-DS-normal-image02.jpg\" alt=\"Binary search tree in DS\" width=\"488\" height=\"371\" \/><\/a><\/p>\n<p>Suppose we wish to search 10. We will start from the root node i.e.40. The value of 40 is more than the value of 10, therefore 10 must be present in the left subtree. Thus, we will discard the right subtree and start traversing the left subtree in the same manner. Now we reach the node having a value of 20. Again 20&gt;10, so will discard the right subtree and move further in the left subtree. Moving this direction, we finally get our desired value i.e. 10. This is how search operation works in a binary search tree.<\/p>\n<h4>Algorithm for Searching in a BST:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">if (root == NULL) \n    return NULL;\nif (num == root-&gt;key) \n    return root-&gt;key;\nif (num &lt; root-&gt;key) \n    return Binary_search(root-&gt;left)\nif (num &gt; root-&gt;key) \n    return Binary_search(root-&gt;right)\n<\/pre>\n<h4>Pseudo-code for Searching in a BST:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">struct Node* Binary_search(int key){\n   struct Node *current = Root;\n   while(current-&gt;key != key){\n      if(current != NULL) {\n         display(current-&gt;key);\n         if(current-&gt;key &gt; key){\n            current = current-&gt;left_child;\n         else \n            current = current-&gt;right_child;\n         if(current == NULL){\n            return NULL;\n         }\n      }\t\t\t\n   }\n   return current;\n}\n<\/pre>\n<h3>Insertion in a Binary Search Tree:<\/h3>\n<p>Insertion operation is used to insert values inside the tree. While inserting a value, we need to take care that the order of the binary search tree is not disturbed. Thus, every value must be inserted at its right place according to its order.<\/p>\n<p>While performing the insert operation, we first compare the value to be inserted with the root node value. If the value is less than the root node, we place it in the left subtree. If the value is more than the root node, we will place it in the right subtree. For inserting a value at the right place, we need to search the correct location first.<\/p>\n<h4>Algorithm for Insertion in a BST:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">if (Node == NULL) \n    return create_node(key)\nif (key &lt; Node-&gt;key)\n    Node-&gt;left_child  = insert(Node-&gt;left_child, key);\nelse if (key &gt; Node-&gt;key)\n    Node-&gt;right_child = insert(Node-&gt;right_child, key);  \nreturn Node;\n<\/pre>\n<h4>Pseudo-code for Insertion in a BST:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">procedure insertion(int key):\n   struct Node *temp = (struct Node*) malloc(sizeof(struct Node));\n   struct Node *current;\n   struct Node *parent;\n\n   temp-&gt;key = key;\n   temp-&gt;left_child = NULL;\n   temp-&gt;right_child = NULL;\n   if(Root == NULL) \n      Root = temp;\n   else:\n      current = Root;\n      parent = NULL;\n    END else\n    while(1) {                \n         parent = current;\n         if(key &lt; parent-&gt;key):\n            current = current-&gt;left_child;                \n            if(current == NULL):\n               parent-&gt;left_child = temp;\n               return;\n            END if\n         END if\n         else:\n            current = current-&gt;right_child;\n            if(current == NULL):\n               parent-&gt;right_child = temp;\n               return;\n            END if\n         END else\n     END while          \nEND procedure\n<\/pre>\n<h3>Deletion in a BST<\/h3>\n<p>The deletion operation includes removing a node from the BST. For performing the deletion operation, first, we have to search the location of the element. The node could be a leaf node, an internal node with one child or an internal node with two children. Let us discuss all the different cases.<\/p>\n<p><strong>Case 1: When the node is a leaf node:<\/strong><\/p>\n<p>If we wish to delete a leaf node, we will simply remove it without affecting the other nodes of the tree and set it to NULL. For deleting a node, we need to know its parent node.<\/p>\n<p>Consider the following example:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/TV-Binary-search-tree-in-DS-normal-image03.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84108\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/TV-Binary-search-tree-in-DS-normal-image03.jpg\" alt=\"Deletion in BST\" width=\"613\" height=\"371\" \/><\/a><\/p>\n<p>Suppose we wish to delete 70 from this tree. We will first go to the root node and compare the value of 70 to it. As 70&lt;100, therefore, we will start looking for 70 in the left subtree. In the left subtree, we first encounter 90 which is again greater than 70. So, we will again traverse the left part of the subtree. Again 70&lt;80, therefore we traverse the left subtree. Finally, after this step, we reach 70.<\/p>\n<p>We will delete 70 from the tree and set the left pointer of 80 i.e. the parent of 70 to NULL. Thus, two operations are happening here: removing 70 from the tree and setting the left pointer of 80 to NULL. In case of deletion from the leaf node, if the height of the tree is h, then the number of comparisons will be <strong>h+1.<\/strong><\/p>\n<p><strong>Case 2: Deletion of an internal node having one child:<\/strong><\/p>\n<p>Let the binary search tree be:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/TV-Binary-search-tree-in-DS-normal-image04.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84109\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/TV-Binary-search-tree-in-DS-normal-image04.jpg\" alt=\"Deletion in BST\" width=\"552\" height=\"490\" \/><\/a><\/p>\n<p>Here, 80 has only 1 child. Suppose we wish to delete 80 from the tree. To delete 80, we need to know the inorder traversal of the tree. In this case, the inorder traversal is: 62, 75, 78, <strong>80,<\/strong> 90, 95, 100, 105, 110, 120.<br \/>\nOnce we remove 80, the inorder traversal will be: 62, 75, 78, 90, 95, 100, 105, 110, 120. Thus, after removing 80, the tree will be:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/TV-Binary-search-tree-in-DS-normal-image05.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84111\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/TV-Binary-search-tree-in-DS-normal-image05.jpg\" alt=\"Deletion in Binary search tree in DS\" width=\"524\" height=\"392\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Case 3: Deletion of an internal node having two child nodes:<\/strong><\/p>\n<p>To delete an internal node having 2 child nodes, first find the in order successor of the node to be deleted and then swap it with the node. Now we have our node at the leaf position, therefore, we will simply remove it.<\/p>\n<p>For example, consider the following tree:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/TV-Binary-search-tree-in-DS-normal-image06.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84110\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/TV-Binary-search-tree-in-DS-normal-image06.jpg\" alt=\"Binary search tree in DS\" width=\"472\" height=\"372\" \/><\/a><\/p>\n<p>Suppose we wish to remove 100 from the tree. We will find the inorder successor of 100 which comes out to be 110. We will swap 110 and 100 and then delete 100. Thus, the final tree will be:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/TV-Binary-search-tree-in-DS-normal-image07.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84112\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/TV-Binary-search-tree-in-DS-normal-image07.jpg\" alt=\"Binary search tree in DS\" width=\"472\" height=\"372\" \/><\/a><\/p>\n<p><strong>Implementation of Binary Search Tree in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nstruct Node {\n    int key;\n    struct node *left_child, *right_child;\n};\n\nstruct Node *new_node(int value) {\n    struct Node *temp = (struct Node *)malloc(sizeof(struct Node));\n    temp-&gt;key = value;\n    temp-&gt;left_child = temp-&gt;right_child = NULL;\n    return temp;\n}\n\nvoid inorder_traversal(struct Node *Root) {\n    if (Root != NULL) {\n        inorder_traversal(Root-&gt;left_child);\n        printf(\"%d -&gt; \", Root-&gt;key);\n        inorder_traversal(Root-&gt;right_child);\n    }\n}\n\nstruct Node *insertion(struct Node *Node, int key) {\n    if (Node == NULL) \n        return new_node(key);\n    if (key &lt; Node-&gt;key)\n        Node-&gt;left_child = insertion(Node-&gt;left_child, key);\n    else\n        Node-&gt;right_child = insertion(Node-&gt;right_child, key);\n    return node;\n}\n\nstruct Node *min_value(struct Node *Node) {\n    struct Node *current = Node;\n    while (current &amp;&amp; current-&gt;left_child != NULL)\n        current = current-&gt;left_child;\n    return current;\n}\n\nstruct Node *deletion(struct Node *Root, int key) {\n    if (Root == NULL) \n        return Root;\n    if (key &lt; Root-&gt;key)\n        Root-&gt;left_child = deletion(Root-&gt;left_child, key);\n    else if (key &gt; Root-&gt;key)\n        Root-&gt;right_child = deletion(Root-&gt;right_child, key);\n    else {\n        if (Root-&gt;left_child == NULL) {\n            struct Node *temp = Root-&gt;right_child;\n            free(Root);\n            return temp;\n        } \n        else if (Root-&gt;right_child == NULL) {\n            struct Node *temp = Root-&gt;left_child;\n            free(Root);\n            return temp;\n        }\n        struct Node *temp = min_value(Root-&gt;right_child);\n        Root-&gt;key = temp-&gt;key;\n        Root-&gt;right_child = deletion(Root-&gt;right_child, temp-&gt;key);\n    }\n    return Root;\n}\n\nint main() {\n    struct Node *root = NULL;\n    root = insert(root, 200);\n    root = insert(root, 100);\n    root = insert(root, 40);\n    root = insert(root, 120);\n    root = insert(root, 130);\n    root = insert(root, 300);\n    root = insert(root, 250);\n    root = insert(root, 360);\n    root = insert(root, 110);\n    printf(\"Inorder traversal: \");\n    inorder_traversal(root);\n    printf(\"\\nAfter deleting 10\\n\");\n    root = deletion(root, 100);\n    printf(\"Inorder traversal: \");\n    inorder_traversal(root);\n}\n<\/pre>\n<h3>Binary Search Tree Implementation in C++:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nstruct Node {\n    int key;\n    struct node *left_child, *right_child;\n};\n\nstruct Node *new_node(int value) {\n    struct Node *temp = (struct Node *)malloc(sizeof(struct Node));\n    temp-&gt;key = value;\n    temp-&gt;left_child = temp-&gt;right_child = NULL;\n    return temp;\n}\n\nvoid inorder_traversal(struct Node *Root) {\n    if (Root != NULL) {\n        inorder_traversal(Root-&gt;left_child);\n        printf(\"%d -&gt; \", Root-&gt;key);\n        inorder_traversal(Root-&gt;right_child);\n    }\n}\n\nstruct Node *insertion(struct Node *Node, int key) {\n    if (Node == NULL) \n        return new_node(key);\n    if (key &lt; Node-&gt;key)\n        Node-&gt;left_child = insertion(Node-&gt;left_child, key);\n    else\n        Node-&gt;right_child = insertion(Node-&gt;right_child, key);\n    return node;\n}\n\nstruct Node *min_value(struct Node *Node) {\n    struct Node *current = Node;\n    while (current &amp;&amp; current-&gt;left_child != NULL)\n        current = current-&gt;left_child;\n    return current;\n}\n\nstruct Node *deletion(struct Node *Root, int key) {\n    if (Root == NULL) \n        return Root;\n    if (key &lt; Root-&gt;key)\n        Root-&gt;left_child = deletion(Root-&gt;left_child, key);\n    else if (key &gt; Root-&gt;key)\n        Root-&gt;right_child = deletion(Root-&gt;right_child, key);\n    else {\n        if (Root-&gt;left_child == NULL) {\n            struct Node *temp = Root-&gt;right_child;\n            free(Root);\n            return temp;\n        } \n        else if (Root-&gt;right_child == NULL) {\n            struct Node *temp = Root-&gt;left_child;\n            free(Root);\n            return temp;\n        }\n        struct Node *temp = min_value(Root-&gt;right_child);\n        Root-&gt;key = temp-&gt;key;\n        Root-&gt;right_child = deletion(Root-&gt;right_child, temp-&gt;key);\n    }\n    return Root;\n}\n\nint main() {\n    struct Node *root = NULL;\n    root = insertion(root, 200);\n    root = insertion(root, 100);\n    root = insertion(root, 40);\n    root = insertion(root, 120);\n    root = insertion(root, 130);\n    root = insertion(root, 300);\n    root = insertion(root, 250);\n    root = insertion(root, 360);\n    root = insertion(root, 110);\n    printf(\"Inorder traversal: \");\n    inorder_traversal(root);\n    printf(\"\\nAfter deleting 10\\n\");\n    root = deletion(root, 100);\n    printf(\"Inorder traversal: \");\n    inorder_traversal(root);\n}\n<\/pre>\n<h3>Implementation of Binary Search Tree in Java:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class BinarySearchTree {\n  class Node {\n    int key;\n    Node left_child, right_child;\n\n    public Node(int value) {\n      key = value;\n      left_child = right_child = null;\n    }\n  }\n\n  Node root;\n\n  BinarySearchTree() {\n    root = null;\n  }\n\n  void insertion(int key) {\n    root = insert_key(root, key);\n  }\n\n  Node insert_key(Node root, int key) {\n    if (root == null) {\n      root = new Node(key);\n      return root;\n    }\n    if (key &lt; root.key)\n      root.left_child = insert_key(root.left_child, key);\n    else if (key &gt; root.key)\n      root.right_child = insert_key(root.right_child, key);\n    return root;\n  }\n\n  void inorder_traversal() {\n    inorder_rec(root);\n  }\n\n  void inorder_rec(Node root) {\n    if (root != null) {\n      inorder_rec(root.left_child);\n      System.out.print(root.key + \" -&gt; \");\n      inorder_rec(root.right_child);\n    }\n  }\n\n  void deletion(int key) {\n    root = deletion(root, key);\n  }\n\n  Node delete_rec(Node root, int key) {\n    if (root == null)\n      return root;\n    if (key &lt; root.key)\n      root.left_child = delete_rec(root.left_child, key);\n    else if (key &gt; root.key)\n      root.right_child = deletion(root.right_child, key);\n    else {\n      if (root.left_child == null)\n        return root.right_child;\n      else if (root.right_child == null)\n        return root.left_child;\n      root.key = min_value(root.right_child);\n      root.right_child = delete_rec(root.right_child, root.key);\n    }\n    return root;\n  }\n\n  int min_value(Node root) {\n    int minv = root.key;\n    while (root.left_child != null) {\n      minv = root.left_child.key;\n      root = root.left_child;\n    }\n    return minv;\n  }\n\n  public static void main(String[] args) {\n    BinarySearchTree tree = new BinarySearchTree();\n\n    tree.insertion(200);\n    tree.insertion(100);\n    tree.insertion(40);\n    tree.insertion(120);\n    tree.insertion(130);\n    tree.insertion(300);\n    tree.insertion(250);\n    tree.insertion(360);\n    tree.insertion(110);\n\n    System.out.print(\"Inorder traversal: \");\n    tree.inorder_traversal();\n\n    System.out.println(\"\\n\\nAfter deleting 10\");\n    tree.deletion(100);\n    System.out.print(\"Inorder traversal: \");\n    tree.inorder_traversal();\n  }\n}\n<\/pre>\n<h3>Implementation of BST in Python:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class Node:\n    def __init__(self, key):\n        self.key = key\n        self.left_child = None\n        self.right_child = None\n\ndef inorder_traversal(root):\n    if root is not None:\n        inorder_traversal(root.left_child)\n        print(str(root.key) + \"-&gt;\", end=' ')\n        inorder_traversal(root.right_child)\n\ndef insertion(Node, key):\n    if Node is None:\n        return Node(key)\n    if key &lt; Node.key:\n        Node.left_child = insertion(node.left_child, key)\n    else:\n        node.right_child = insertion(node.right_child, key)\n    return Node\n\ndef min_value(node):\n    current = node\n    while(current.left_child is not None):\n        current = current.left_child\n    return current\n\ndef deletion(root, key):\n    if root is None:\n        return root\n    if key &lt; root.key:\n        root.left_child = deletion(root.left_child, key)\n    elif(key &gt; root.key):\n        root.right_child = deletion(root.right_child, key)\n    else:\n        if root.left_child is None:\n            temp = root.right_child\n            root = None\n            return temp\n        elif root.right_child is None:\n            temp = root.left_child\n            root = None\n            return temp\n        temp = min_value(root.right_child)\n        root.key = temp.key\n        root.right_child = deletion(root.right_child, temp.key)\n    return root\n\n\nroot = None\nroot = insertion(root, 200)\nroot = insertion(root, 100)\nroot = insertion(root, 40)\nroot = insertion(root, 120)\nroot = insertion(root, 130)\nroot = insertion(root, 300)\nroot = insertion(root, 250)\nroot = insertion(root, 360)\nroot = insertion(root, 110)\nprint(\"Inorder traversal: \", end=' ')\ninorder_traversal(root)\nprint(\"\\nDelete 100\")\nroot = deletion(root, 10)\nprint(\"Inorder traversal: \", end=' ')\ninorder_traversal(root)\n<\/pre>\n<h3>Complexity Analysis of Binary Search Tree:<\/h3>\n<p>Consider a tree having n nodes. Then, the time complexity of Binary Search Tree will be:<\/p>\n<table style=\"height: 229px\" width=\"455\">\n<tbody>\n<tr>\n<td><strong>Scenario\/Operation\u00a0<\/strong><\/td>\n<td><strong>Search\u00a0<\/strong><\/td>\n<td><strong>Insert\u00a0<\/strong><\/td>\n<td><strong>Delete\u00a0<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Worst case<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n)<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n)<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Average case<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(log n)<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(log n)<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(log n)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Best case<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(log n)<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(log n)<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(log n)<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The space complexity in all operations is O(n).<\/p>\n<h3>Advantages of Binary Search Tree:<\/h3>\n<ul>\n<li>Searching is more efficient as compared to a simple binary tree.<\/li>\n<li>The insertion and deletion operations are faster in a binary search tree in comparison to arrays and linked lists.<\/li>\n<li>At each step, the binary search tree removes half the subtree and this makes it work faster.<\/li>\n<\/ul>\n<h3>Applications of Binary Search Tree:<\/h3>\n<ul>\n<li>It is used in multi-level indexing in the database management system.<\/li>\n<li>Used in UNIX kernel to manage virtual memory areas<\/li>\n<li>Used in dynamic sorting of elements.<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>In this article, we have seen the working of various operations such as insertion, deletion and searching in a binary search tree. We have also gone through the complexity analysis and various applications of a BST.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A binary search tree(BST) is a special type of binary tree and is also known as a sorted or ordered binary tree. In a binary search tree: The value of the left node is&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":84104,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3555],"tags":[1985,4068,4069,4070],"class_list":["post-83520","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structure","tag-binary-search-tree","tag-binary-search-tree-in-data-structure","tag-deletion-in-binary-search-tree","tag-insertion-in-bst"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Binary Search Tree in Data Structure - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn about Binary Search tree in Data Structure. See various operations like insertion, deletion &amp; searching in binary search tree.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Binary Search Tree in Data Structure - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn about Binary Search tree in Data Structure. See various operations like insertion, deletion &amp; searching in binary search tree.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-18T03:30:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/TV-Binary-search-tree-in-DS.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Binary Search Tree in Data Structure - TechVidvan","description":"Learn about Binary Search tree in Data Structure. See various operations like insertion, deletion & searching in binary search tree.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/","og_locale":"en_US","og_type":"article","og_title":"Binary Search Tree in Data Structure - TechVidvan","og_description":"Learn about Binary Search tree in Data Structure. See various operations like insertion, deletion & searching in binary search tree.","og_url":"https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-08-18T03:30:53+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/TV-Binary-search-tree-in-DS.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Binary Search Tree in Data Structure","datePublished":"2021-08-18T03:30:53+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/"},"wordCount":1227,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/TV-Binary-search-tree-in-DS.jpg","keywords":["Binary Search Tree","Binary Search Tree in Data Structure","Deletion in Binary search tree","Insertion in BST"],"articleSection":["Data Structure Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/","url":"https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/","name":"Binary Search Tree in Data Structure - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/TV-Binary-search-tree-in-DS.jpg","datePublished":"2021-08-18T03:30:53+00:00","description":"Learn about Binary Search tree in Data Structure. See various operations like insertion, deletion & searching in binary search tree.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/TV-Binary-search-tree-in-DS.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/TV-Binary-search-tree-in-DS.jpg","width":1200,"height":628,"caption":"Binary search tree in DS"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/binary-search-tree\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Binary Search Tree in Data Structure"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/83520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=83520"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/83520\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/84104"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=83520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=83520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=83520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}