Advertisement
vkichukova

Untitled

Jan 31st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T>
  6. struct Node
  7. {
  8. T value;
  9. string name;
  10. Node* left;
  11. Node* right;
  12.  
  13. Node(T _value, string _name) {
  14. value = _value;
  15. name = _name;
  16. left = nullptr;
  17. right = nullptr;
  18. }
  19.  
  20. Node(T _value, string _name, Node<T>* _left, Node<T>* _right) {
  21. value = _value;
  22. name = _name;
  23. left = _left;
  24. right = _right;
  25. }
  26. ~Node() {
  27. delete left;
  28. delete right;
  29. }
  30. };
  31.  
  32. template <typename T>
  33. class BinTree
  34. {
  35. public:
  36. Node<T>* root;
  37. public:
  38. BinTree() {
  39. root = nullptr;
  40. }
  41.  
  42. ~BinTree() {
  43. delete root;
  44. }
  45. Node<T>* getRoot() const
  46. {
  47. return root;
  48. }
  49. bool insert(T x, string trace, string name) {
  50. return insert_helper(x, trace, root, name);
  51. }
  52.  
  53. unsigned height() {
  54. return height(root);
  55. }
  56.  
  57. void get_leaves() {
  58. get_leaves(root);
  59. }
  60.  
  61. int count_leaves() {
  62. return count_leaves_help(root, 0);
  63. }
  64.  
  65. T max_leaf() {
  66. T maxLeaf = root->value;
  67. return max_leaf_help(root, maxLeaf);
  68. }
  69.  
  70. bool find(T value) {
  71. return find_helper(value, root);
  72. }
  73.  
  74. bool find(string name) {
  75. return find_helper(name, root);
  76. }
  77.  
  78. void findPath(T value, string name) {
  79. find_path(value, root, name);
  80. }
  81.  
  82. int count() {
  83. return count_help(root, 0);
  84. }
  85.  
  86. int countEvens() {
  87. return count_evens(root, 0);
  88. }
  89.  
  90. public:
  91.  
  92.  
  93. bool insert_helper(T x, string trace, Node<T>*& subRoot, string name) {
  94. if (trace == "" && subRoot == nullptr) {
  95. subRoot = new Node<T>(x, name, nullptr, nullptr);
  96. return true;
  97. }
  98. if (trace != "" && subRoot == nullptr)
  99. return false;
  100.  
  101. if (trace[0] == 'L') {
  102. trace.erase(trace.begin());
  103. insert_helper(x, trace, subRoot->left, name);
  104. }
  105. if (trace[0] == 'R') {
  106. trace.erase(trace.begin());
  107. insert_helper(x, trace, subRoot->right, name);
  108. }
  109. }
  110.  
  111. T height(Node<T>* sub_root) {
  112. if (sub_root == nullptr)
  113. return 0;
  114. unsigned left = height(sub_root->left);
  115. unsigned right = height(sub_root->right);
  116.  
  117. return 1 + (left > right ? left : right);
  118. }
  119.  
  120. void get_leaves(Node<T>* sub_root) {
  121. if (sub_root == nullptr)
  122. return;
  123.  
  124. if (sub_root->left == nullptr &&
  125. sub_root->right == nullptr) {
  126.  
  127. cout << sub_root->value;
  128. }
  129. get_leaves(sub_root->left);
  130. get_leaves(sub_root->right);
  131. cout << " ";
  132. }
  133.  
  134. int count_leaves_help(Node<T>* currentRoot, int counter) {
  135. if (currentRoot == nullptr)
  136. return 0;
  137. if (currentRoot->left == nullptr && currentRoot->right == nullptr)
  138. counter++;
  139. int countL = count_leaves_help(currentRoot->left, counter + 1);
  140. int countR = count_leaves_help(currentRoot->right, counter + 1);
  141.  
  142. return (currentRoot->left == nullptr && currentRoot->right == nullptr ? 1 : 0) + countL + countR;
  143. }
  144.  
  145. bool isLeaf(Node<T>* currentRoot) {
  146. if (currentRoot == nullptr)
  147. return false;
  148. if (currentRoot->left == nullptr && currentRoot->right == nullptr)
  149. return true;
  150. }
  151.  
  152. // Èçêàðâà íàé-ãîëÿìîòî ëèñòî
  153. T max_leaf_help(Node<T>* currentRoot, T maxLeaf) {
  154. if (currentRoot == nullptr) {
  155. return maxLeaf;
  156. }
  157. if (isLeaf(currentRoot) == true) {
  158. if (maxLeaf <= currentRoot->value)
  159. maxLeaf = currentRoot->value;
  160. }
  161. else {
  162. unsigned maxL = max_leaf_help(currentRoot->left, maxLeaf);
  163. unsigned maxR = max_leaf_help(currentRoot->right, maxLeaf);
  164.  
  165. maxLeaf = (maxL > maxR ? maxL : maxR);
  166. }
  167. return maxLeaf;
  168. }
  169.  
  170. int count_help(Node<T>* currentRoot, int counter) {
  171. if (currentRoot == nullptr)
  172. return 0;
  173.  
  174. int counterL = count_help(currentRoot->left, counter);
  175. int counterR = count_help(currentRoot->right, counter);
  176.  
  177. return 1 + counterL + counterR;
  178. }
  179.  
  180.  
  181. int count_evens(Node<T>* currentRoot, int counter) {
  182. if (currentRoot == nullptr)
  183. return 0;
  184.  
  185. int counterL = count_evens(currentRoot->left, counter);
  186. int counterR = count_evens(currentRoot->right, counter);
  187.  
  188. return (currentRoot->value % 2 == 0 ? 1 : 0) + counterL + counterR;
  189. }
  190.  
  191. //Well this one is obvious
  192. bool find_helper(T value, Node<T>* currentRoot, string _name) {
  193. if (currentRoot == nullptr)
  194. return false;
  195. if (currentRoot->name.compare(_name) == 0)
  196. return true;
  197. else
  198. return find_helper(value, currentRoot->left, _name) ||
  199. find_helper(value, currentRoot->right, _name);
  200.  
  201. return false;
  202. }
  203.  
  204. //Òúðñè ïúòÿ äî äàäåí node
  205. void find_path(T value, Node<T>* currentRoot) {
  206. if (find_helper(value, currentRoot) == false) {
  207. cout << value << " is not found" << endl;
  208. return;
  209. }
  210. if (currentRoot->value == value) {
  211. cout << "Found " << value << endl;
  212. }
  213. else {
  214.  
  215. if (find_helper(value, currentRoot->left) == true) {
  216. cout << "L->";
  217. find_path(value, currentRoot->left);
  218. }
  219. else if (find_helper(value, currentRoot->right) == true) {
  220. cout << "R->";
  221. find_path(value, currentRoot->right);
  222. }
  223. }
  224. }
  225. };
  226.  
  227. template<class T>
  228. bool find_helper_string(string name, Node<T>* currentRoot) {
  229. if (currentRoot == NULL)
  230. return false;
  231. if (name.compare(currentRoot->name) == 0)
  232. return true;
  233. else
  234. return find_helper_string(name, currentRoot->left) ||
  235. find_helper_string(name, currentRoot->right);
  236.  
  237. return false;
  238. }
  239.  
  240.  
  241. template <class T>
  242. void findpath(string& _name, Node<T>*& root)
  243. {
  244.  
  245. if(_name.empty())
  246. {
  247. cout << "Error name! "; return;
  248. }
  249.  
  250. if (root == NULL)
  251. {
  252. cout << "Error root! "; return;
  253. }
  254. //if (root->left == NULL && root->right == NULL)
  255. // cout << root->value.name << endl;
  256. if(root->left->name.compare(_name) == 0 || root->right->name.compare(_name) == 0)
  257. {
  258. cout << root->left->name << " VS " << root->right->name << endl;
  259. }
  260. if(find_helper_string(_name, root->left))
  261. {
  262. findpath(_name, root->left);
  263. }
  264. if(find_helper_string(_name, root->right))
  265. {
  266. findpath(_name, root->right);
  267. }
  268. }
  269.  
  270. int main()
  271. {
  272. BinTree<int> tr;
  273. string name = "ivan";
  274. string name2 = "roni";
  275. string name3 = "stoil";
  276.  
  277.  
  278. string e = " ";
  279. tr.insert(5, e , name);
  280. tr.insert(15, "L" , name2);
  281. tr.insert(25, "R" , name3);
  282.  
  283. Node<int> * root = tr.getRoot();
  284. findpath(name3, root);
  285.  
  286. // system("pause");
  287. return 0;
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement