Binary Search Tree ADT

Binary Search Tree ADT

A binary search tree is a binary tree in which the nodes are arranged in a specific order. That means the values at the left subtree are less than the root node value. Similarly, the values at the right subtree are greater than the root node value. The binary search tree is based on the binary search algorithm.

Binary search tree

In a binary search tree structure, the top root node holds a benchmark value. All values present in its left subtree strictly fall below the value of the root node, whereas all values in its right subtree strictly exceed the value of the root node. In the representation above, the root node value is 1010. The left subtree consists of node 77, which has a left child 55 and a right child 99. The right subtree consists of node 1515, which has a left child 1212 and a right child 1818.

Operations on Binary Search Tree

Insertion of a Node in a Binary Search Tree

The step-by-step algorithm to insert a node into a binary search tree is defined as follows:

  1. Read the value for the node which is to be created and store it in a node called New.

  2. Initially, if (root != NULL), then root = New.

  3. Again read the next value of node created in New.

  4. If (New->value < root->value), then attach New node as a left child of root; otherwise, attach New node as a right child of root.

  5. Repeat steps 3 and 4 for constructing the required binary search tree completely.

While inserting any node in a binary search tree, first look for its appropriate position in the binary search tree. Start comparing this new node with each node of the tree. If the value of the node which is to be inserted is greater than the value of the current node, move onto the right subbranch; otherwise, move onto the left subbranch. As soon as the appropriate position is found, attach this new node as a left or right child appropriately.

Before insertion

Consider inserting the value 2323 into the tree shown in the diagram above:

Start by comparing 2323 with the value of the root node, which is 1010. As 2323 is greater than 1010, move to the right subtree root, which is 2020. Next, compare 2323 with 2020 and move right to node 2222. Compare 2323 with 2222 and move right to node 2424. Now compare 2323 with 2424. Since 2323 is less than 2424, move to the left branch of 2424. As there is a NULL node as the left child of 2424, attach 2323 as the left child of 24$.\n\n![After insertion](https://assets.knowt.com/pdf-flow-prod/2e4c58fe-0e48-45e3-8190-7f0fe3b25c04-figures/2.jpg)\n\n## Deletion of an Element from the Binary Search Tree\n\nFor the deletion of any node from a binary search tree, three distinct cases are possible:\n\ni. Deletion of a leaf node.\n\nii. Deletion of a node having one child.\n\niii. Deletion of a node having two children.\n\nCase i: Deletion of a leaf node\n\nThis is the simplest deletion scenario, in which the left or right pointer of the parent node is set to `NULL`. For instance, to delete the node having value 6fromatreewherenodefrom a tree where node8isitsparent,settheleftpointerofnodeis its parent, set the left pointer of node8 to `NULL`.\n\nCase ii: Deletion of a node having one child\n\nConsider a tree where node 15hasasinglechildnodehas a single child node18(whichinturnhasaleftchild(which in turn has a left child16).Todeletenode). To delete node15,simplycopynode, simply copy node18attheplaceofat the place of15 and then set the node free. The inorder successor is always copied at the position of a node being deleted.\n\n![Deletion of node with children](https://assets.knowt.com/pdf-flow-prod/2e4c58fe-0e48-45e3-8190-7f0fe3b25c04-figures/3.jpg)\n\nCase iii: Deletion of a node having two children\n\nConsider deleting node 7fromatreewherenodefrom a tree where node7hasaleftchildhas a left child6andarightchildand a right child9(whichhasaleftchild(which has a left child8):\n\nFind the inorder successor of node 7,whichisnode, which is node8.Copy. Copy8atthepositionwherethevalueofthenodeisat the position where the value of the node is7.Settheleftpointerofnode. Set the left pointer of node9 as `NULL`. This completes the deletion procedure.\n\n![Deletion of node with two children](https://assets.knowt.com/pdf-flow-prod/2e4c58fe-0e48-45e3-8190-7f0fe3b25c04-figures/4.jpg)\n\n## Searching a Node from Binary Search Tree\n\nIn searching, the node which is to be searched is called a key node. The key node is compared with each node starting from the root node. If the value of the key node is greater than the current node, search for it on the right subbranch; otherwise, search on the left subbranch. If a leaf node is reached and the value of the key node is still not found, declare "node is not present in the tree".\n\n![Binary search tree for searching](https://assets.knowt.com/pdf-flow-prod/2e4c58fe-0e48-45e3-8190-7f0fe3b25c04-figures/6.jpg)\n\nTo search for value 9 in the binary search tree above:\n\nCompare 9withtherootnodewith the root node10.As. As9islessthanis less than10,searchontheleftsubbranch(node, search on the left subbranch (node5).Next,compare). Next, compare9withwith5.As. As9isgreaterthanis greater than5,movetotherightsubbranch(node, move to the right subbranch (node8).Compare). Compare9withwith8.As. As9isgreaterthanis greater than8,movetotherightsubbranch.Readthenodevalueas, move to the right subbranch. Read the node value as9. Thus, the desired node is successfully located.\n\n# Program Implementation of Binary Search Tree\n\nThe complete C program implementation for performing insertion, deletion, searching, and displaying a Binary Search Tree in inorder fashion is given below:\n\n```c\n#include \n#include \n#include \n\ntypedef struct bst\n{\n int data;\n struct bst *left, *right;\n} node;\n\nvoid insert(node *, node *);\nvoid inorder(node *);\nnode *search(node *, int, node **);\nvoid del(node *, int);\nnode *get_node();\n\nvoid main()\n{\n int choice;\n char ans = 'N';\n int key;\n node *New, *root, *tmp, *parent;\n root = NULL;\n clrscr();\n\n printf("\n\t Program For Binary Search Tree ");\n do\n {\n printf("\n1.Create\n2.Search\n3.Delete\n4.Display");\n printf("\n\n Enter your choice :");\n scanf("%d", &choice);\n switch(choice)\n {\n case 1:\n do\n {\n New = get_node();\n printf("\n Enter The Element ");\n scanf("%d", &New->data);\n if(root == NULL) /* Tree is not Created */\n root = New;\n else\n insert(root, New);\n printf("\n Do u Want To enter More Elements?(y/n)");\n ans = getch();\n } while(ans == 'y');\n break;\n\n case 2:\n printf("\n Enter The Element Which You Want To Search");\n scanf("%d", &key);\n tmp = search(root, key, &parent);\n printf("\n Parent of node %d is %d", tmp->data, parent->data);\n break;\n\n case 3:\n printf("\n Enter The Element U wish to Delete");\n scanf("%d", &key);\n del(root, key);\n break;\n\n case 4:\n if(root == NULL)\n printf("Tree Is Not Created");\n else\n {\n printf("\n The Tree is: ");\n inorder(root);\n }\n break;\n }\n } while(choice != 5);\n}\n\nnode *get_node()\n{\n node *temp;\n temp = (node *)malloc(sizeof(node));\n temp->left = NULL;\n temp->right = NULL;\n return temp;\n}\n\n/* This function is for creating a binary search tree */\nvoid insert(node *root, node *New)\n{\n if(New->data < root->data)\n {\n if(root->left == NULL)\n root->left = New;\n else\n insert(root->left, New);\n }\n if(New->data > root->data)\n {\n if(root->right == NULL)\n root->right = New;\n else\n insert(root->right, New);\n }\n}\n\n/* This function is for searching the node from binary Search Tree */\nnode *search(node *root, int key, node **parent)\n{\n node *temp;\n temp = root;\n while(temp != NULL)\n {\n if(temp->data == key)\n {\n printf("\n The %d Element is Present", temp->data);\n return temp;\n }\n *parent = temp;\n if(temp->data > key)\n temp = temp->left;\n else\n temp = temp->right;\n }\n return NULL;\n}\n\n/* This function is for deleting a node from binary search tree. */\nvoid del(node *root, int key)\n{\n node *temp, *parent, *temp_succ;\n temp = search(root, key, &parent);\n\n /* deleting a node with two children */\n if(temp->left != NULL && temp->right != NULL)\n {\n parent = temp;\n temp_succ = temp->right;\n while(temp_succ->left != NULL)\n {\n parent = temp_succ;\n temp_succ = temp_succ->left;\n }\n temp->data = temp_succ->data;\n if(temp_succ == parent->left)\n parent->left = NULL;\n else\n parent->right = NULL;\n printf(" Now Deleted it!");\n return;\n }\n\n /* deleting a node having only one child */\n /* The node to be deleted has left child */\n if(temp->left != NULL && temp->right == NULL)\n {\n if(parent->left == temp)\n parent->left = temp->left;\n else\n parent->right = temp->left;\n temp = NULL;\n free(temp);\n printf(" Now Deleted it!");\n return;\n }\n\n /* The node to be deleted has right child */\n if(temp->left == NULL && temp->right != NULL)\n {\n if(parent->left == temp)\n parent->left = temp->right;\n else\n parent->right = temp->right;\n temp = NULL;\n free(temp);\n printf(" Now Deleted it!");\n return;\n }\n\n /* deleting a node which is having no child */\n if(temp->left == NULL && temp->right == NULL)\n {\n if(parent->left == temp)\n parent->left = NULL;\n else\n parent->right = NULL;\n printf(" Now Deleted it!");\n return;\n }\n}\n\n/* This function displays the tree in inorder fashion */\nvoid inorder(node *temp)\n{\n if(temp != NULL)\n {\n inorder(temp->left);\n printf("%d ", temp->data);\n inorder(temp->right);\n }\n}\n```\n\n# Execution Output and Examples\n\n## Program Execution Trace\n\nThe following trace details the creation, display, and searching steps during execution:\n\nChoice 1 (Create):\n- Enter The Element 10\n- Enter The Element 8\n- Enter The Element 9\n- Enter The Element 7\n- Enter The Element 15\n- Enter The Element 13\n- Enter The Element 14\n- Enter The Element 12\n- Enter The Element 16\n\nChoice 4 (Display):\n- Output: `The Tree is: 7 8 9 10 12 13 14 15 16`\n\nChoice 2 (Search):\n- Enter The Element Which You Want To Search: 16\n- Output: `The 16 Element is Present`, `Parent of node 16 is 15`\n\n## Exercise 4.9.1\n\nProblem Definition: Define binary search tree. Draw the binary search tree for the following sequence:\n14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5\n\nSolution Outline:\n\nA binary search tree is a binary tree in which all nodes are arranged such that left child values are strictly smaller than parent values, and right child values are strictly larger than parent values.\n\nConstruction sequence:\n- Step 1: Insert 14 (Root)\n- Step 2: Insert 15(Rightchildof(Right child of14)\n- Step 3: Insert 4(Leftchildof(Left child of14)\n- Step 4: Insert 9(Rightchildof(Right child of4)\n- Step 5: Insert 7(Leftchildof(Left child of9)\n- Step 6: Insert 18(Rightchildof(Right child of15)\n- Step 7: Insert 3(Leftchildof(Left child of4)\n- Step 8: Insert 5(Leftchildof(Left child of7)\n- Step 9: Insert 16(Leftchildof(Left child of18)\n- Step 10: Insert 4 (Duplicate value)\n- Step 11: Insert 20(Rightchildof(Right child of18)\n- Step 12: Insert 17(Leftchildof(Left child of20)\n- Step 13: Insert 9 (Duplicate value)\n- Step 14: Insert 14 (Duplicate value)\n- Step 15: Insert 5$$ (Duplicate value)