Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include "time.h"
  4.  
  5. class Tree
  6. {
  7. public:
  8. class Node
  9. {
  10. public:
  11. int key;
  12. Node* L = nullptr;
  13. Node *R = nullptr;
  14.  
  15. Node(int key);
  16. };
  17. void insert(int key);
  18. void printLeft();
  19. void printRight();
  20. void printSort(bool = true);
  21. Node* GetRoot() {return this->root;};
  22. Tree::Node* printLeft(Node*);
  23. Tree::Node* printRight(Node*);
  24. Node* root = nullptr;
  25. private:
  26.  
  27. void insert(int key, Node*& root);
  28. void printSort(bool, Node*);
  29.  
  30. };
  31.  
  32. Tree::Node::Node(int key)
  33. {
  34. this->key = key;
  35. }
  36.  
  37. void Tree::insert(int key)
  38. {
  39. this->insert(key, this->root);
  40. }
  41.  
  42. void Tree::insert(int key, Node*& root)
  43. {
  44. if (root == nullptr)
  45. {
  46. root = new Node(key);
  47. return;
  48. }
  49.  
  50. if (root->key == key)
  51. {
  52. insert(key, root->L);
  53. }
  54.  
  55. if (root->key > key)
  56. {
  57. insert(key, root->L);
  58. }
  59.  
  60. if (root->key < key)
  61. {
  62. insert(key, root->R);
  63. }
  64. }
  65.  
  66. void Tree::printSort(bool asc)
  67. {
  68. this->printSort(asc, this->root);
  69. }
  70.  
  71. void Tree::printSort(bool asc, Node* root)
  72. {
  73. if (root == nullptr)
  74. {
  75. return;
  76. }
  77.  
  78. //pokud bool true, zavola se levy potomek
  79. printSort(asc, asc ? root->L : root->R);
  80. std::cout << root->key << " ";
  81. //pokud bool true, zavola se pravy potomek
  82. printSort(asc, asc ? root->R : root->L);
  83. }
  84.  
  85. void Tree::printLeft()
  86. {
  87. this->printLeft(this->root);
  88. }
  89.  
  90. Tree::Node* Tree::printLeft(Node* root)
  91. {
  92. if(root == nullptr)
  93. return 0;
  94. return root->L;
  95. //printLeft(root->L); //pro opakování na využití rekurze
  96.  
  97. }
  98.  
  99. void Tree::printRight()
  100. {
  101. this->printRight(this->root);
  102. }
  103.  
  104. Tree::Node* Tree::printRight(Node* root) //poze jednou
  105. {
  106. if(root == nullptr)
  107. return 0;
  108. return root->R;
  109.  
  110. }
  111. ///////////////////////////////////
  112. class Zasobnik
  113. {
  114. private:
  115. Tree::Node **zas;
  116. int index;
  117. int max;
  118. public:
  119. Zasobnik(int);
  120. ~Zasobnik();
  121. void AddTop(Tree::Node*);
  122. Tree::Node* PickTop();
  123. bool IsEmpty();
  124. void DeleteTop();
  125. };
  126.  
  127. Zasobnik::Zasobnik(int poc){
  128. max = poc;
  129. zas = new Tree::Node*[max];
  130. index = -1;
  131. }
  132.  
  133. Zasobnik::~Zasobnik(void)
  134. {
  135. for(int i = 0; i < this->index; i++)
  136. {
  137. delete zas[i];
  138. this->index--;
  139. }
  140. delete [] zas;
  141. }
  142.  
  143. void Zasobnik::AddTop(Tree::Node* h){
  144. if (index < max-1){
  145. index++;
  146. zas[this->index] = h;
  147. } else
  148. std::cout << "Zasobnik je plny!" << std::endl;
  149. }
  150.  
  151. void Zasobnik::DeleteTop()
  152. {
  153. if(index >= 0)
  154. {
  155. delete this->zas[this->index];
  156. index--;
  157. }
  158. }
  159.  
  160. Tree::Node* Zasobnik::PickTop()
  161. {
  162. if (index>=0){
  163. //std::cout << this->index << " " << this->zas[this->index]->key << std::endl;
  164. return this->zas[this->index];
  165. }
  166. else{
  167. std::cout << "Zasobnik je prazdny!" << std::endl;
  168. }
  169. }
  170.  
  171. bool Zasobnik::IsEmpty(){
  172. if (index < 0)
  173. return true;
  174. else
  175. return false;
  176. }
  177.  
  178. /////////////////////////////////////////////////////////////////////////////////////
  179. class Iterator
  180. {
  181. private:
  182. Tree::Node* root;
  183. Zasobnik* z;
  184. public:
  185. Iterator(Zasobnik*& z, Tree::Node* n);
  186.  
  187. void Reset(Tree t);
  188. void Next(Tree t);
  189. bool IsEnd();
  190. int CurrentKey(Zasobnik* z);
  191.  
  192. };
  193.  
  194. Iterator::Iterator(Zasobnik*& z, Tree::Node* n)
  195. {
  196. this->root = n;
  197. this->z = z;
  198. }
  199.  
  200. void Iterator::Reset(Tree t)
  201. {
  202. this->root = t.GetRoot();
  203. //pokud je uzel nullptr jsme na konci = nasli jsme minumum
  204. while (this->root != nullptr)
  205. {
  206. //uzel do zasobniku
  207. this->z->AddTop(this->root);
  208. //posun na levy uzel
  209. this->root = this->root->L;
  210. if(this->root!=nullptr)
  211. std::cout<< "->" << this->root->key;
  212. }
  213. }
  214.  
  215. void Iterator::Next(Tree t)
  216. {
  217. if(this->root->R == nullptr){
  218. std::cout << "Neexistuje pravý podstrom" << std::endl;
  219. return;
  220. }
  221. //posun na pravý uzel od kořene
  222. this->root = this->root->R;
  223. std::cout << "->" << this->root->key;
  224. //posun na nejlevější uzel v pravém podstromu
  225. while (this->root->L != nullptr)
  226. {
  227. //uzel do zasobniku
  228. this->z->AddTop(this->root);
  229. //posun na levy uzel
  230. this->root = this->root->L;
  231. if(this->root!=nullptr)
  232. std::cout<< "->" << this->root->key;
  233. }
  234. }
  235.  
  236. bool Iterator::IsEnd()
  237. {
  238. if(this->z->IsEmpty())
  239. return true;
  240. return false;
  241. }
  242.  
  243. int Iterator::CurrentKey(Zasobnik* z)
  244. {
  245. return z->PickTop()->key;
  246. }
  247. using namespace std;
  248.  
  249. int main()
  250. {
  251. int random, root;
  252. srand(time(NULL));
  253.  
  254. Tree tree;
  255.  
  256. cout << "Tree generator: ";
  257. for(int i = 0; i<12;i++){
  258. random = rand()%100;
  259. tree.insert(random);
  260. cout << random << " ";
  261. if(i == 0)
  262. root = random;
  263. }
  264.  
  265. cout << endl << "Sorted Tree: ";
  266. tree.printSort();
  267. cout << endl << endl;
  268. //cout << endl;
  269.  
  270. //tree.printLeft();
  271.  
  272. //cout << endl;
  273.  
  274. //tree.printRight();
  275. Zasobnik* zasobnik = new Zasobnik(12);
  276. Iterator* i = new Iterator(zasobnik, tree.root);
  277.  
  278. /*if(i->IsEnd())
  279. cout << "true" << endl;
  280. else
  281. cout << "false" <<endl;
  282. */
  283. cout << "Root: " << root << endl;
  284. cout << "Next: " << root;
  285. i->Next(tree);
  286. cout << endl;
  287. cout << "Reset: " << root;
  288. i->Reset(tree);
  289. cout << endl;
  290. cout << "CurrentKey: " << i->CurrentKey(zasobnik) << endl;
  291.  
  292. system("pause");
  293. return 0;
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement