Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. # include <iostream>
  2. # include <cstdlib>
  3. using namespace std;
  4. /*
  5. * Node Declaration
  6. */
  7. struct node
  8. {
  9. int info;
  10. struct node *left;
  11. struct node *right;
  12. }*root;
  13. /*
  14. * Class Declaration
  15. */
  16. class BST
  17. {
  18. public:
  19. void insert(node *, node *);
  20. void doubleOrder(node *);
  21. void display(node *, int);
  22. BST()
  23. {
  24. root = NULL;
  25. }
  26. };
  27. /*
  28. * Main Contains Menu
  29. */
  30. int main()
  31. {
  32. int choice, num;
  33. BST bst;
  34. node *temp;
  35. while (1)
  36. {
  37. cout << "-----------------" << endl;
  38. cout << "Operations on BST" << endl;
  39. cout << "-----------------" << endl;
  40. cout << "1.Insert Element " << endl;
  41. cout << "2.Double-Order Traversal" << endl;
  42. cout << "3.Display" << endl;
  43. cout << "4.Quit" << endl;
  44. cout << "Enter your choice : ";
  45. cin >> choice;
  46. switch (choice)
  47. {
  48. case 1:
  49. temp = new node;
  50. cout << "Enter the number to be inserted : ";
  51. cin >> temp->info;
  52. bst.insert(root, temp);
  53. break;
  54. case 2:
  55. cout << "Double-Order Traversal of BST:" << endl;
  56. bst.doubleOrder(root);
  57. cout << endl;
  58. break;
  59. case 3:
  60. cout << "Display BST:" << endl;
  61. bst.display(root, 1);
  62. cout << endl;
  63. break;
  64. case 4:
  65. exit(1);
  66. default:
  67. cout << "Wrong choice" << endl;
  68. }
  69. }
  70. }
  71. /*
  72. * Inserting Element into the Tree
  73. */
  74. void BST::insert(node *tree, node *newnode)
  75. {
  76. if (root == NULL)
  77. {
  78. root = new node;
  79. root->info = newnode->info;
  80. root->left = NULL;
  81. root->right = NULL;
  82. cout << "Root Node is Added" << endl;
  83. return;
  84. }
  85. if (tree->info == newnode->info)
  86. {
  87. cout << "Element already in the tree" << endl;
  88. return;
  89. }
  90. if (tree->info > newnode->info)
  91. {
  92. if (tree->left != NULL)
  93. {
  94. insert(tree->left, newnode);
  95. }
  96. else
  97. {
  98. tree->left = newnode;
  99. (tree->left)->left = NULL;
  100. (tree->left)->right = NULL;
  101. cout << "Node Added To Left" << endl;
  102. return;
  103. }
  104. }
  105. else
  106. {
  107. if (tree->right != NULL)
  108. {
  109. insert(tree->right, newnode);
  110. }
  111. else
  112. {
  113. tree->right = newnode;
  114. (tree->right)->left = NULL;
  115. (tree->right)->right = NULL;
  116. cout << "Node Added To Right" << endl;
  117. return;
  118. }
  119. }
  120. }
  121. /*
  122. * In Order Traversal
  123. */
  124. void BST::doubleOrder(node *ptr)
  125. {
  126. if (root == NULL)
  127. {
  128. cout << "Tree is empty" << endl;
  129. return;
  130. }
  131. if (ptr != NULL)
  132. {
  133. cout << ptr->info << " ";
  134. doubleOrder(ptr->left);
  135. cout << ptr->info << " ";
  136. doubleOrder(ptr->right);
  137. }
  138. }
  139. /*
  140. * Display Tree Structure
  141. */
  142. void BST::display(node *ptr, int level)
  143. {
  144. int i;
  145. if (ptr != NULL)
  146. {
  147. display(ptr->right, level + 1);
  148. cout << endl;
  149. if (ptr == root)
  150. cout << "Root->: ";
  151. else
  152. {
  153. for (i = 0; i < level; i++)
  154. cout << " ";
  155. }
  156. cout << ptr->info;
  157. display(ptr->left, level + 1);
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement