Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4.  
  5. using namespace std;
  6. struct node
  7. {
  8. int value;
  9. node *left;
  10. node *right;
  11. node()
  12. {
  13. value = 0;
  14. left = right = nullptr;
  15. }
  16. node(int data)
  17. {
  18. value = data;
  19. left = right = nullptr;
  20. }
  21. };
  22.  
  23. struct Tree
  24. {
  25. node *root;
  26. Tree()
  27. {
  28. root = nullptr;
  29. }
  30. };
  31.  
  32. void Free(node *root)
  33. {
  34. if (root == nullptr)
  35. return;
  36. //Free(root);
  37.  
  38. Free(root->left);
  39. Free(root->right);
  40. delete root;
  41.  
  42. }
  43.  
  44. //void Free1(Tree T)
  45. //{
  46. // if (T.root == nullptr)
  47. // return;
  48. // Free(T.root->left);
  49. // Free(T.root->right);
  50. // Free(T.root);
  51. //}
  52.  
  53. //int LeftOf(const int value, const Tree &T)
  54. //{
  55. // return value < T.root->value;
  56. //}
  57. //
  58. //int RightOf(const int value, const Tree &T)
  59. //{
  60. // return value > T.root->value;
  61. //}
  62. //
  63. //node* Insert(Tree &T, const int data)
  64. //{
  65. // if (!T.root)
  66. // {
  67. // node* temp = new node(data);
  68. // return temp;
  69. // }
  70. // if (LeftOf(data, T))
  71. // T.root->left = Insert(T.root->left, data);
  72. // else if (RightOf(data, T))
  73. // T.root->right=Insert()
  74. //}
  75.  
  76. //int Insert(node* &p, int data)
  77. //{
  78. // if (p)
  79. // {
  80. // if (p->value == data)
  81. // return 0;
  82. // if (p->value > data)
  83. // return Insert(p->left, data);
  84. // else
  85. // return Insert(p->right, data);
  86. // }
  87. // p = new node();
  88. // if (p == nullptr)
  89. // return -1;
  90. // p->value = data;
  91. // p->left = p->right = nullptr;
  92. // return 1;
  93. //}
  94.  
  95. void Insert(node *&root, const int x)
  96. {
  97. if (root == NULL)
  98. {
  99. root = new node();
  100. root->value = x;
  101. root->right = NULL;
  102. root->left = NULL;
  103. }
  104. else
  105. {
  106. if (root->value <= x)
  107. Insert(root->left, x);
  108. else
  109. Insert(root->right, x);
  110. }
  111. }
  112.  
  113. node* Search(node*p, int value)
  114. {
  115. if (p)
  116. {
  117. if (p->value == value)
  118. return p;
  119. if (p->value > value)
  120. return Search(p->left, value);
  121. else
  122. return Search(p->right, value);
  123. }
  124. return nullptr;
  125. }
  126.  
  127. void Print_LNR(node *p)
  128. {
  129. if (p)
  130. {
  131. Print_LNR(p->left);
  132. cout << p->value << " ";
  133. Print_LNR(p->right);
  134. }
  135. }
  136.  
  137. void CountHD(node *p, int hd, map<int, vector<int>>& mp)
  138. {
  139. if (p == nullptr)
  140. return;
  141. mp[hd].push_back(p->value);
  142. CountHD(p->left, hd - 1, mp);
  143. CountHD(p->right, hd + 1, mp);
  144. }
  145.  
  146. void Print_Cau4(node* p)
  147. {
  148. if (p)
  149. {
  150. char s = 'A';
  151. map<int, vector<int>> mp;
  152. CountHD(p, 0, mp);
  153. map<int, vector<int>>::iterator it;
  154. for (it = mp.begin(); it != mp.end(); it++)
  155. {
  156. cout << s++ << ". ";
  157. for (int i = 0; i < it->second.size(); i++)
  158. cout << it->second[i] << " ";
  159. cout << endl;
  160. }
  161.  
  162. }
  163. }
  164.  
  165. int main()
  166. {
  167. Tree T;
  168. /*Insert(T.root, 44);
  169. Insert(T.root, 18);
  170. Insert(T.root, 88);
  171. Insert(T.root, 13);
  172. Insert(T.root, 37);
  173. Insert(T.root, 59);
  174. Insert(T.root, 108);
  175. Insert(T.root, 15);
  176. Insert(T.root, 23);
  177. Insert(T.root, 40);
  178. Insert(T.root, 55);
  179. Insert(T.root, 71);*/
  180.  
  181. Insert(T.root, 1);
  182. Insert(T.root->left, 2);
  183. Insert(T.root->right, 3);
  184. Insert(T.root->left->left, 4);
  185. Insert(T.root->left->right, 5);
  186. Insert(T.root->right->left, 6);
  187. Insert(T.root->right->right, 7);
  188. Insert(T.root->right->left->right, 8);
  189. Insert(T.root->right->right->right, 9);
  190.  
  191. //Insert(T.root, 30);
  192. //Print_LNR(T.root);
  193. cout << endl;
  194. /*node *temp = Search(T.root, 69);
  195. cout << temp->value;*/
  196. Print_Cau4(T.root);
  197. Free(T.root);
  198. system("pause");
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement