fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class tnode {
  4. public:
  5. int data;
  6. tnode *left,*right;
  7. };
  8. class bst {
  9. public:
  10. tnode* root=NULL;
  11. tnode* insert(tnode *temp, int item) {
  12. if (temp==NULL) {
  13. temp=new tnode;
  14. temp->data = item;
  15. temp->left=NULL;
  16. temp->right=NULL;
  17. }
  18. else {
  19. if (item<=temp->data) {
  20. temp->left=insert(temp->left,item);
  21. }
  22. else {
  23. temp->right = insert(temp->right,item);
  24. }
  25. }
  26. return temp;
  27. }
  28. bool search(tnode *s,int key) {
  29. if (s==NULL) {
  30. return false;
  31. }
  32. else if (key==s->data) {
  33. return true;
  34. }
  35. else if (key<s->data) {
  36. return search(s->left,key);
  37. }
  38. else {
  39. return search (s->right,key);
  40. }
  41. }
  42. void preorder(tnode*node) {
  43. if (node==NULL) {
  44. return;
  45. }
  46. cout<<node->data<<" ";
  47. preorder(node->left);
  48. preorder(node->right);
  49. }
  50. void postorder(tnode*node) {
  51. if (node==NULL) {
  52. return;
  53. }
  54. postorder(node->left);
  55. postorder(node->right);
  56. cout<<node->data<<" ";
  57. }
  58. void inorder(tnode*node) {
  59. if (node==NULL) {
  60. return;
  61. }
  62. inorder(node->left);
  63. cout<<node->data<<" ";
  64. inorder(node->right);
  65. }
  66. tnode* min(tnode* temp) {
  67. if (temp==NULL) {
  68. return NULL;
  69. }
  70. else if (temp->left==NULL){
  71. return temp;
  72. }
  73. else {
  74. return min(temp->left);
  75. }
  76. }
  77. tnode* max(tnode* temp) {
  78. if (temp==NULL) {
  79. return NULL;
  80. }
  81. else if (temp->right==NULL){
  82. return temp;
  83. }
  84. else {
  85. return max(temp->right);
  86. }
  87. }
  88.  
  89. };
  90.  
  91. int main() {
  92. bst b;
  93. b.root=b.insert(b.root,45);
  94. b.root=b.insert(b.root,15);
  95. b.root=b.insert(b.root,79);
  96. b.root=b.insert(b.root,90);
  97. b.root=b.insert(b.root,10);
  98. b.root=b.insert(b.root,55);
  99. b.root=b.insert(b.root,12);
  100. b.root=b.insert(b.root,20);
  101. b.root=b.insert(b.root,50);
  102.  
  103. cout << "Display the Tree Content\n";
  104.  
  105. b.preorder(b.root);
  106.  
  107. cout << "\n";
  108.  
  109. b.inorder(b.root);
  110.  
  111. cout <<"\n";
  112.  
  113. b.postorder(b.root);
  114. cout <<"\n";
  115. int key=5;
  116. cout << "Enter item to research for 5 \n";
  117. if (b.search(b.root,key))
  118. cout << "item found \n";
  119. else {
  120. cout << "sorry, item not found \n";
  121. }
  122. int k=15;
  123. cout << "Enter item to research for 15 \n";
  124. if (b.search(b.root,k))
  125. cout << "item found \n";
  126. else {
  127. cout << "sorry, item not found \n";
  128. }
  129. cout << "Find Minimum \n";
  130. tnode* minimum = b.min (b.root);
  131. if (minimum == NULL)
  132. cout << "No Item Exist \n";
  133. else
  134. cout << "Minimum is " << minimum ->data << "\n";
  135. cout << "Find Maximum \n";
  136. tnode* maximum = b.max (b.root);
  137. if (maximum == NULL)
  138. cout << "No Item Exist \n";
  139. else
  140. cout << "Maximum is " << maximum ->data << "\n";
  141. }
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
Display the Tree Content
45 15 10 12 20 79 55 50 90 
10 12 15 20 45 50 55 79 90 
12 10 20 15 50 55 90 79 45 
Enter item to research for 5 
sorry, item not found 
Enter item to research for 15 
item found 
Find Minimum 
Minimum is 10
Find Maximum 
Maximum is 90