Advertisement
Guest User

dasdsadsa

a guest
Nov 13th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class BST {
  5.  
  6. struct node {
  7. int data;
  8. node* left;
  9. node* right;
  10. };
  11. node* root;
  12. node* insert(int x, node* t)
  13. {
  14. if(t == NULL)
  15. {
  16. t = new node;
  17. t->data = x;
  18. t->left = t->right = NULL;
  19. }
  20. else if(x < t->data)
  21. t->left = insert(x, t->left);
  22. else if(x > t->data)
  23. t->right = insert(x, t->right);
  24. return t;
  25. }
  26. void inorder(node* t) {
  27. if(t == NULL)
  28. return;
  29. inorder(t->left);
  30. cout << t->data << " ";
  31. inorder(t->right);
  32. }
  33. void Postorder(node* t) {
  34. if(t == NULL)
  35. return;
  36. inorder(t->left);
  37. inorder(t->right);
  38. cout << t->data << " ";
  39. }
  40. void Preorder(node* t) {
  41. if(t == NULL)
  42. return;
  43. cout << t->data << " ";
  44. inorder(t->left);
  45. inorder(t->right);
  46. }
  47.  
  48. public:
  49. BST() {
  50. root = NULL;
  51. }
  52. void insert(int x) {
  53. root = insert(x, root);
  54. }
  55. void display(int i) {
  56. if(i==1)
  57. inorder(root);
  58. if(i==2)
  59. Preorder(root);
  60. if(i==3)
  61. Postorder(root);
  62. cout << endl;
  63. }
  64.  
  65. };
  66.  
  67. int main() {
  68. BST t;
  69. int a;
  70. char s[13]="Danci Daniel";
  71. //for(int i=1;i<13;i++){
  72. //t.insert((int)s[i]);
  73. //t.insert(i);
  74. //}
  75. t.insert(3);
  76. t.insert(2);
  77. t.insert(7);
  78. t.insert(1);
  79. t.insert(8);
  80. t.insert(5);
  81. t.insert(4);
  82. t.insert(9);
  83. t.insert(6);
  84.  
  85.  
  86. t.display(1);
  87. t.display(2);
  88. t.display(3);
  89. return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement