bogdan_obukhovskii

binary tree

Apr 24th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. struct Node
  7. {
  8. string tel; //ключ
  9. string name;
  10.  
  11. int step;
  12.  
  13. Node* left;
  14. Node* right;
  15. };
  16.  
  17. void print(Node* root)
  18. {
  19. if (root != NULL)
  20. {
  21. cout << root->step << ": " << root->tel << endl;
  22. //cout << "Телефон: " << root->tel << "; Имя: " << root->name << endl;
  23. //cout << "влево ";
  24. print(root->left);
  25. //cout << "вправо ";
  26. print(root->right);
  27. }
  28. else {
  29. //cout << " конец ветки " << endl;
  30. }
  31. }
  32.  
  33. Node* add(string tel, string name, Node* root, int step = 0)
  34. {
  35. if (root == NULL)
  36. {
  37. root = new Node;
  38. root->tel = tel;
  39. root->name = name;
  40.  
  41. root->left = NULL;
  42. root->right = NULL;
  43.  
  44. root->step = step;
  45. }
  46. else if (tel < root->tel)
  47. {
  48. //cout << "<- ";
  49. step += 1;
  50. root->left = add(tel, name, root->left, step);
  51. }
  52. else
  53. {
  54. step += 1;
  55. root->right = add(tel, name, root->right, step);
  56. }
  57. return root;
  58. }
  59.  
  60. int main()
  61. {
  62. setlocale(0, "rus");
  63.  
  64. Node* tree = NULL;
  65. tree = add("10000000001", "Вася", tree);
  66. tree = add("11000000000", "Вася", tree);
  67. tree = add("10000000010", "Вася", tree);
  68. tree = add("10000000100", "Вася", tree);
  69. tree = add("10000000000", "Вася", tree);
  70. tree = add("11100010000", "Вася", tree);
  71. tree = add("10000010000", "Вася", tree);
  72. tree = add("1000000000", "Вася", tree);
  73.  
  74. print(tree);
  75.  
  76. system("pause");
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment