Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct node
  5. {
  6. int storage;
  7. node* left;
  8. node* right;
  9. };
  10.  
  11. node* root = NULL;
  12.  
  13. node* GetNewNode(int info)
  14. {
  15. node* newNode = new node();
  16. newNode->storage = info;
  17. newNode->left = NULL;
  18. newNode->right = NULL;
  19. return newNode;
  20. }
  21.  
  22. node* insert(node* root, int info)
  23. {
  24. if (root == NULL)
  25. {
  26. root = GetNewNode(info);
  27. return root;
  28. }
  29. else if (info <= root->storage)
  30. {
  31. root->left = insert(root->left, info);
  32. }
  33. else
  34. {
  35. root->right = insert(root->right, info);
  36. }
  37. return root;
  38. }
  39.  
  40. void displayPreOrder(node* root)
  41. {
  42. if (root != NULL)
  43. {
  44. cout << root->storage << " ";
  45. displayPreOrder(root->left);
  46. displayPreOrder(root->right);
  47. }
  48. }
  49.  
  50. void displayPostOrder(node* root)
  51. {
  52. if (root != NULL)
  53. {
  54. displayPostOrder(root->left);
  55. displayPostOrder(root->right);
  56. cout << root->storage << " ";
  57. }
  58. }
  59.  
  60. void displayInOrder(node* root)
  61. {
  62. if (root != NULL)
  63. {
  64. displayInOrder(root->left);
  65. cout << root->storage << " ";
  66. displayInOrder(root->right);
  67. }
  68. }
  69.  
  70. bool search(node* root, int info)
  71. {
  72. if (root == NULL)
  73. return false;
  74. else if (root->storage == info)
  75. return true;
  76. else if (info <= root->storage)
  77. return search(root->left, info);
  78. else
  79. return search(root->right, info);
  80. }
  81.  
  82. void find(node* root)
  83. {
  84. int number;
  85. cout << "Scrie numarul pe care vrei sa-l cauti: ";
  86. cin >> number;
  87. if (search(root, number) == true)
  88. cout << "Gasit" << "\n";
  89. else
  90. cout << "Nu a fost gasit" << "\n";
  91. }
  92.  
  93. int main()
  94. {
  95. int lungime, valoare;
  96. cout << "Lungimea sirului: ";
  97. cin >> lungime;
  98. for (int i = 1; i <= lungime; i++)
  99. {
  100. cout << "Scrie elementul " << i << ": ";
  101. cin >> valoare;
  102. root = insert(root, valoare);
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement