Advertisement
vkichukova

Untitled

Jan 31st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6. struct Participant
  7. {
  8. int coefficient;
  9. string name;
  10.  
  11. };
  12.  
  13. template <class T>
  14. struct Node
  15. {
  16. T value;
  17. Node* left;
  18. Node* right;
  19.  
  20. Node(T _value) {
  21. value = _value;
  22. left = NULL;
  23. right = NULL;
  24. }
  25.  
  26. Node(T _value, Node<T>* _left, Node<T>* _right) {
  27. value = _value;
  28. left = _left;
  29. right = _right;
  30. }
  31. ~Node() {
  32. delete left;
  33. delete right;
  34. }
  35. };
  36.  
  37. template <typename T>
  38. class BinTree
  39. {
  40. public:
  41. Node<T>* root;
  42.  
  43. BinTree() {
  44. root = NULL;
  45. }
  46.  
  47. ~BinTree() {
  48. delete root;
  49. }
  50.  
  51. bool insert(T x, string trace) {
  52. return insert_helper(x, trace, root);
  53. }
  54.  
  55. unsigned height() {
  56. return height(root);
  57. }
  58.  
  59. void get_leaves() {
  60. get_leaves(root);
  61. }
  62.  
  63. int count_leaves() {
  64. return count_leaves_help(root, 0);
  65. }
  66.  
  67. T max_leaf() {
  68. T maxLeaf = root->value;
  69. return max_leaf_help(root, maxLeaf);
  70. }
  71.  
  72. bool find(T value) {
  73. return find_helper(value, root);
  74. }
  75.  
  76. void findPath(T value) {
  77. find_path(value, root);
  78. }
  79.  
  80. int count() {
  81. return count_help(root, 0);
  82. }
  83.  
  84. int countEvens() {
  85. return count_evens(root, 0);
  86. }
  87. Node<T> * getRoot() const
  88. {
  89. return root;
  90. }
  91. public:
  92.  
  93. bool insert_helper(T x, string trace, Node<T>*& subRoot) {
  94. if (trace == "" && subRoot == NULL) {
  95. subRoot = new Node<T>(x, NULL, NULL);
  96. return true;
  97. }
  98. if (trace != "" && subRoot == NULL)
  99. return false;
  100.  
  101. if (trace[0] == 'L') {
  102. trace.erase(trace.begin());
  103. insert_helper(x, trace, subRoot->left);
  104. }
  105. if (trace[0] == 'R') {
  106. trace.erase(trace.begin());
  107. insert_helper(x, trace, subRoot->right);
  108. }
  109. }
  110.  
  111. T height(Node<T>* sub_root) {
  112. if (sub_root == NULL)
  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 == NULL)
  122. return;
  123.  
  124. if (sub_root->left == NULL &&
  125. sub_root->right == NULL) {
  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 == NULL)
  136. return 0;
  137. if (currentRoot->left == NULL && currentRoot->right == NULL)
  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 == NULL && currentRoot->right == NULL ? 1 : 0) + countL + countR;
  143. }
  144.  
  145. bool isLeaf(Node<T>* currentRoot) {
  146. if (currentRoot == NULL)
  147. return false;
  148. if (currentRoot->left == NULL && currentRoot->right == NULL)
  149. return true;
  150. }
  151.  
  152. // Изкарва най-голямото листо
  153. T max_leaf_help(Node<T>* currentRoot, T maxLeaf) {
  154. if (currentRoot == NULL) {
  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 == NULL)
  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 == NULL)
  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) {
  193. if (currentRoot == NULL)
  194. return false;
  195. if (currentRoot->value == value)
  196. return true;
  197. else
  198. return find_helper(value, currentRoot->left) ||
  199. find_helper(value, currentRoot->right);
  200.  
  201. return false;
  202. }
  203.  
  204. void find_path(T value, Node<T>* currentRoot) {
  205. if (find_helper(value, currentRoot) == false) {
  206. cout << value << " is not found" << endl;
  207. return;
  208. }
  209. if (currentRoot->value == value) {
  210. cout << "Found " << value << endl;
  211. }
  212. else {
  213.  
  214. if (find_helper(value, currentRoot->left) == true) {
  215. cout << "L->";
  216. find_path(value, currentRoot->left);
  217. }
  218. else if (find_helper(value, currentRoot->right) == true) {
  219. cout << "R->";
  220. find_path(value, currentRoot->right);
  221. }
  222. }
  223. }
  224.  
  225.  
  226. };
  227. template<class T>
  228. bool find_helper_string(string value, Node<T>* currentRoot) {
  229. if (currentRoot == NULL)
  230. return false;
  231. if (value.compare(currentRoot->value) == 0)
  232. return true;
  233. else
  234. return find_helper(value, currentRoot->left) ||
  235. find_helper(value, 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! "; return;
  248. }
  249.  
  250. if (root == NULL)
  251. {
  252. cout << "Error! "; return;
  253. }
  254. //if (root->left == NULL && root->right == NULL) {
  255. // cout << root->value.name << endl;
  256. if(root->left->value.name.compare(_name) == 0 || root->right->value.name.compare(_name) == 0)
  257. {
  258. cout << root->left->value.name << " VS " << root->right->value.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.  
  271. int main()
  272. {
  273. // Participant p {"lili", 8};
  274. BinTree<int> tr;
  275. tr.insert(5, " ");
  276. tr.insert(6, "L");
  277. tr.insert(8, "R");
  278. tr.insert(6, "RL");
  279. tr.insert(8, "RR");
  280. tr.insert(6, "LL");
  281. tr.insert(6, "LR");
  282. tr.insert(50, "LLR");
  283. tr.insert(25, "RLR");
  284. tr.insert(2, "RLL");
  285. tr.insert(2, "LRR");
  286. tr.insert(7, "LRL");
  287. tr.insert(19, "LLL");
  288. // Node<Participant> *t = tr.getRoot();
  289. // findpath("RR", t1);
  290. return 0;
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement