• API
• FAQ
• Tools
• Archive
SHARE
TWEET Untitled a guest Sep 19th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include<stdio.h>
2. typedef struct BinarySearchTree
3. {
4.     int key;
5.     struct BinarySearchTree *left;
6.     struct BinarySearchTree *right;
7. }BST;
8. int *arr = NULL;
9.
10. BST *head = NULL;
11. void create(int k)
12. {
13.     BST *x = (BST*)malloc(sizeof(BST));
14.     BST *r1 = head, *r2;
15.     x->key = k;
16.     x->left = x->right = NULL;
18.     {
19.         head = x;
20.         return;
21.     }
22.     while (r1)
23.     {
24.         r2 = r1; //stores the address of the last node visited by r1
25.         if( k < r1->key)
26.         {
27.             r1 = r1->left;
28.         }
29.         else
30.         {
31.             r1 = r1->right;
32.         }
33.     }
34.     if (k < r2->key)
35.     {
36.         r2->left = x;
37.     }
38.     else
39.     {
40.         r2->right = x;
41.     }
42. }
43. void inorder_iterative()
44. {
45.     printf("\n Inorder traversal of the Binary Search Tree is ------ \n");
46.     BST *r = head;
47.     do
48.     {
49.         while (r)
50.         {
51.             push(r);
52.             r = r->left;
53.         }
54.         r = pop();
55.         printf("\t %d", r->key);
56.         r = r->right;
57.     } while (r || top == -1);
58. }
59. void preorder_iterative()
60. {
61.     printf("\n Preorder traversal of the Binary Search Tree is ------ \n");
62.     BST *r = head;
63.     do
64.     {
65.         while (r)
66.         {
67.             printf("\t %d", r->key);
68.             push(r);
69.             r = r->left;
70.         }
71.         r = pop();
72.         r = r->right;
73.     } while (r || top == -1);
74. }
75. int main()
76. {
77.     int choice;
78.     do
79.     {
80.         printf("\n ----------Menu---------- ");
81.         printf("\n Press 1 to enter nodes in the Binary Search Tree.");
82.         printf("\n Press 2 to inorderly traverse the Binary Search Tree (iterative).");
83.         printf("\n Press 3 to inorderly traverse the Binary Search Tree (recursive).");
84.         printf("\n Press 4 to preorderly traverse the Binary Search Tree (iterative).");
85.         printf("\n Press 5 to inorderly traverse the Binary Search Tree (recursive).");
86.         printf("\n Press -1 to exit. ");
87.         printf("\n Enter choice : ");
88.         scanf("%d", &choice);
89.         switch (choice)
90.         {
91.         case 1 :
92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!

Top