Skip to main content

Operation On Binary Search Tree(BST)

 Operation On Binary Search Tree(BST)


  1. Create:- Create tree with 'n' nodes.
  2. Traverse:- Visit each & every node of tree.
  3. Search:- Searches for a specific element in the tree.
  4.  Insert:- The new element will be inserted according to its value.
  5. Delete:- Delete a specific node from the tree.
  6. Counting total nodes
  7. Counting leaf nodes
  8. Copying tree to another
  9. Mirror of tree

  • Create:-
        Some rules satisfy for create a nodes. 
  1. All keys in the left subtree < root.
  2. All keys in the right subtree >= root.
  3. The right & left subtree are also Binary Search Tree(BST).
    


    Algorithm:-
    Step 1:- Start
    Step 2:- root=NULL
    Step 3:- Read the value and create a newnode to store this value.
    Step 4:- If root==NULL
                  root=newnode
                  goto Step 7
    Step 5:- temp=root
    Step 6:- If key<temp->info
                     If temp doesn't have left child
                           attach newnode to temp->left
                            go to Step 7
                     Else
                           temp=temp->left;
                            go to Step 7
                Else
                        IF temp doesn't have right child,
                            attach newnode to temp->right
                        Else
                             temp=temp->right
                              go to Step 6
    Step 7:- Repeat from 3 till all values have been inserted in tree.
    Step 8:- Stop




  • Traverse
  1. Traversing tree means visiting all it's nodes to get the complete tree tree information.
  2. Data structure like array and linked list are linear. Hence it is easy to traverse all element.
  3. A tree is a non linear data structure hence tree traversal is complicated.

Three methods to traverse tree:-
  • Perorder(Data-Left-Right):-
  1. Visit the root.
  2. Traverse left subtree in Preorder.
  3. Treverse right subtree in Preorder.
  • Inorder(Left-Data-Right)
  1. Traverse left subtree in Inorder.
  2. Visit the root.
  3. Treverse right subtree in Inorder.
  • Postorder(Left-Right-Data)
  1. Traverse left subtree in Postorder.
  2. Treverse right subtree in Postorder. 
  3. Visit the root.    



    Code:-
                  👇
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. typedef struct node
  4. {
  5. int info;
  6. struct node *left, *right;
  7. }NODE;

  8. //TREE FUNCTION
  9. NODE * createbst(NODE *root)
  10. {
  11. NODE *newnode, *temp, *parent;
  12. int i, n, num;
  13. printf("\nHow many nodes you want to enter?\t");
  14. scanf("%d", &n);
  15. for(i=1; i<=n; i++)
  16. {
  17. newnode = (NODE*)malloc(sizeof (NODE));
  18. printf("Enter the %d elements:\t", n);
  19. scanf("%d", &num);
  20. newnode->info=num;
  21. newnode->left = newnode->right = NULL;
  22. if(root == NULL)
  23. {
  24. root = newnode;
  25. continue;
  26. }
  27. temp = root;
  28. while(temp != NULL)
  29. {
  30. parent = temp;
  31. if(num < temp->info)
  32. temp = temp->left;
  33. else
  34. temp = temp->right;
  35. }
  36. if(num < parent->info)
  37. parent->left = newnode;
  38. else
  39. parent->right = newnode;
  40. }
  41. return(root);
  42. }
  43.                    // Traversal Function
  44. void preorder(NODE * root)
  45. {
  46. NODE *temp = root;
  47. if(temp != NULL)
  48. {
  49. printf("%d\t", temp->info);
  50. preorder(temp->left);
  51. preorder(temp->right);
  52. }
  53. }

  54. void inorder(NODE * root)
  55. {
  56. NODE *temp = root;
  57. if(temp != NULL)
  58. {
  59. inorder(temp->left);
  60. printf("%d\t", temp->info);
  61. inorder(temp->right);
  62. }
  63. }

  64. void postorder(NODE * root)
  65. {
  66. NODE *temp = root;
  67. if(temp != NULL)
  68. {
  69. postorder(temp->left);
  70. postorder(temp->right);
  71. printf("%d\t", temp->info);
  72. }
  73. }



  74. void main()
  75. {
  76. NODE *root=NULL;
  77. printf("\t>>CREATE & TRAVERSAL<<\n");
  78. root = createbst(root);
  79. //Recursive Traversal
  80. printf("\nPerorder traversal is:\t"); 
  81. preorder(root);
  82. printf("\nInorder traversal is:\t");
  83. inorder(root);
  84. printf("\nPostorder traversal is:\t");
  85. postorder(root);

  86. }
                                                                                                                                                                                                              👉Execute👈

                                                                                                                                                                                                              //OUTPUT
                                                                                                                                                                                                              /*
                                                                                                                                                                                                              >>CREATE & TRAVERSAL<<

                                                                                                                                                                                                              How many nodes you want to enter? 5
                                                                                                                                                                                                              Enter the 5 elements: 10
                                                                                                                                                                                                              Enter the 5 elements: 5
                                                                                                                                                                                                              Enter the 5 elements: 15
                                                                                                                                                                                                              Enter the 5 elements: 6
                                                                                                                                                                                                              Enter the 5 elements: 12

                                                                                                                                                                                                              Perorder traversal is: 10 5 6 15 12
                                                                                                                                                                                                              Inorder traversal is: 5 6 10 12 15
                                                                                                                                                                                                              Postorder traversal is: 6 5 12 15 10

                                                                                                                                                                                                              */

                                                                                                                                                                                                              Comments