Advertisement
adwas33

Untitled

Nov 20th, 2021
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct drzewoBST
  4. {
  5. drzewoBST * up;
  6. drzewoBST * left;
  7. drzewoBST * right;
  8. int val;
  9. };
  10. void addBstNode(drzewoBST *&root, int x,drzewoBST * wezel2)
  11. {
  12. if(root== nullptr)
  13. {
  14. auto *w=new drzewoBST();
  15. w->left= nullptr;
  16. w->right= nullptr;
  17. w->up=wezel2;
  18. w->val=x;
  19.  
  20. root=w;
  21.  
  22. } else
  23. {
  24. if(x>=root->val)
  25. {
  26. addBstNode(root->right,x,root);
  27. }else
  28. addBstNode(root->left,x,root);
  29. }
  30. }
  31. void addBstNode(drzewoBST *&root, int x)
  32. {
  33. if(root== nullptr)
  34. {
  35. auto *w=new drzewoBST();
  36. w->left= nullptr;
  37. w->right= nullptr;
  38. w->val=x;
  39.  
  40. root=w;
  41.  
  42. } else
  43. {
  44. if(x>=root->val)
  45. {
  46. addBstNode(root->right,x,root);
  47. }else
  48. addBstNode(root->left,x,root);
  49. }
  50. }
  51.  
  52.  
  53. void addBstNodeWithRememberUp(drzewoBST *&root, int x)
  54. {
  55. if(root== nullptr)
  56. {
  57. auto *w=new drzewoBST();
  58. w->left= nullptr;
  59. w->right= nullptr;
  60. w->val=x;
  61.  
  62. root=w;
  63.  
  64. } else
  65. if(x>=root->val&&root->right== nullptr)
  66. {
  67. auto *w=new drzewoBST();
  68. w->left= nullptr;
  69. w->right= nullptr;
  70. w->up=root;
  71. w->val=x;
  72.  
  73. root->right=w;
  74.  
  75. }
  76. else
  77. if(x<root->val&&root->left== nullptr)
  78. {
  79. auto *w=new drzewoBST();
  80. w->left= nullptr;
  81. w->right= nullptr;
  82. w->up=root;
  83. w->val=x;
  84.  
  85. root->left=w;
  86.  
  87. }
  88. else{
  89. if(x>=root->val)
  90. {
  91. addBstNodeWithRememberUp(root->right,x);
  92. }else
  93. addBstNodeWithRememberUp(root->left,x);
  94. }
  95. }
  96. drzewoBST* znajdzMin(drzewoBST *root)
  97. {
  98. if(root!= nullptr)
  99. {
  100. if(root->left!= nullptr)
  101. znajdzMin(root->left);
  102. else return root;
  103.  
  104. }
  105.  
  106. }
  107. drzewoBST* znajdzMax(drzewoBST *root)
  108. {
  109. if(root!= nullptr)
  110. {
  111. if(root->right!= nullptr)
  112. znajdzMax(root->right);
  113. else return root;
  114.  
  115. }
  116.  
  117. }
  118. //drzewoBST* zdnajdzMaxWPodGalezi(drzewoBST *root,int aktualneMaksimum)
  119. //{
  120. // if(root!= nullptr)
  121. // {
  122. // if(root->val>aktualneMaksimum){
  123. // aktualneMaksimum=root->val;
  124. //
  125. // }
  126. // }
  127. //}
  128.  
  129. drzewoBST * nastepnik (drzewoBST *wezel)
  130. {
  131. drzewoBST * r;
  132. if(wezel)
  133. {
  134. if(wezel->right) return znajdzMin(wezel->right);
  135. else {
  136. r= wezel->up;
  137. while(r && (wezel==r->right))
  138. {
  139. wezel=r;
  140. r=r->up;
  141. }
  142. return r;
  143.  
  144.  
  145. }
  146. }
  147. return nullptr;
  148. }
  149. drzewoBST * poprzednik (drzewoBST *wezel)
  150. {
  151. drzewoBST *r;
  152. if(wezel)
  153. {
  154. if(wezel->left) return znajdzMin(wezel->left);
  155. else
  156. {
  157. r=wezel->up;
  158. while(r&&(wezel==r->left))
  159. {
  160. wezel=r;
  161. r=r->up;
  162. }
  163. return r;
  164. }
  165.  
  166. }return nullptr;
  167. }
  168.  
  169.  
  170. drzewoBST* szukaj(drzewoBST *root,int wartoscSzukana)
  171. {
  172. if(root!= nullptr)
  173. {
  174. if(root->val==wartoscSzukana)
  175. {
  176. return root;
  177. }
  178. if(wartoscSzukana>root->val)
  179. {
  180. szukaj(root->right,wartoscSzukana);
  181. }else szukaj(root->left,wartoscSzukana);
  182.  
  183. return nullptr;
  184. }
  185.  
  186. }
  187. //funkcja usuwająca liście tego drzewa
  188. void usunTo(drzewoBST *&root)
  189. {
  190. delete root;
  191. root= nullptr;
  192. }
  193.  
  194. void usunLiscie (drzewoBST *&root) // gdzieś się wykrzacza funkcja przy wypisywaniu
  195. {
  196. if(root!= nullptr)
  197. {
  198. if(root->left== nullptr&&root->right== nullptr)//gdy nie ma możliwości zejścia w dół
  199. {
  200. usunTo(root);
  201. }else
  202. {
  203. if(root->left!= nullptr)
  204. {
  205. usunLiscie(root->left);
  206. }
  207. if(root->right!= nullptr)
  208. {
  209. usunLiscie(root->right);
  210. }
  211. }
  212.  
  213. }
  214. }
  215.  
  216. //inorder ( wyświetlanie )
  217. void inorder(drzewoBST *&root)
  218. {
  219. if(root!= nullptr)
  220. {
  221. inorder(root->left);
  222. cout<<root->val<<" ";
  223. inorder(root->right);
  224. }else
  225. return;
  226. }
  227. int main() {
  228. drzewoBST * drzewo=nullptr;
  229. addBstNode(drzewo,12);
  230. addBstNode(drzewo,16);
  231. addBstNode(drzewo,25);
  232. addBstNode(drzewo,14);
  233. addBstNode(drzewo,3);
  234. addBstNode(drzewo,8);
  235. addBstNode(drzewo,22);
  236. addBstNode(drzewo,16);
  237. addBstNode(drzewo,10);
  238. addBstNode(drzewo,27);
  239. drzewoBST * drzewo2=nullptr;
  240. addBstNode(drzewo2,12);
  241. addBstNode(drzewo2,6);
  242. addBstNode(drzewo2,-5);
  243. addBstNode(drzewo2,11);
  244. addBstNode(drzewo2,3);
  245. addBstNode(drzewo2,8);
  246. addBstNode(drzewo2,22);
  247. addBstNode(drzewo2,16);
  248. addBstNode(drzewo2,10);
  249. addBstNode(drzewo2,27);
  250.  
  251.  
  252.  
  253. inorder(drzewo);
  254. cout<<endl;
  255. usunLiscie(drzewo);
  256. inorder(drzewo);
  257. cout<<endl;
  258. usunLiscie(drzewo2);
  259. inorder(drzewo2);
  260. cout<<endl;
  261. drzewoBST * nowe=nullptr;
  262. addBstNodeWithRememberUp(nowe,12);
  263. addBstNodeWithRememberUp(nowe,16);
  264. addBstNodeWithRememberUp(nowe,25);
  265. addBstNodeWithRememberUp(nowe,14);
  266. addBstNodeWithRememberUp(nowe,3);
  267. addBstNodeWithRememberUp(nowe,8);
  268. addBstNodeWithRememberUp(nowe,22);
  269. addBstNodeWithRememberUp(nowe,16);
  270. addBstNodeWithRememberUp(nowe,10);
  271. addBstNodeWithRememberUp(nowe,27);
  272. inorder(nowe);
  273. cout<<endl<<znajdzMin(nowe)->val<<endl<<znajdzMax(nowe)->val<<endl;
  274. /// TESTOWANIE POPRZEDNIKA I NASTĘPNIKA
  275. drzewoBST *drzewoBst= nullptr;
  276. addBstNodeWithRememberUp(drzewoBst,15);
  277. addBstNodeWithRememberUp(drzewoBst,6);
  278. addBstNodeWithRememberUp(drzewoBst,22);
  279. addBstNodeWithRememberUp(drzewoBst,-5);
  280. addBstNodeWithRememberUp(drzewoBst,14);
  281. addBstNodeWithRememberUp(drzewoBst,8);
  282. addBstNodeWithRememberUp(drzewoBst,12);
  283. addBstNodeWithRememberUp(drzewoBst,13);
  284. addBstNodeWithRememberUp(drzewoBst,18);
  285. addBstNodeWithRememberUp(drzewoBst,27);
  286. addBstNodeWithRememberUp(drzewoBst,17);
  287. inorder(drzewoBst);
  288. cout<<endl;
  289. cout<<drzewoBst->left->right->left->right->right->val<<endl;
  290. cout<<poprzednik(drzewoBst->left->right->left->right->right)->val<<endl;//ok
  291. cout<<drzewoBst->right->left->left->val<<endl;
  292. cout<<poprzednik(drzewoBst->right->left->left)->val<<endl;//ok
  293. cout<<poprzednik(drzewoBst)->val<<endl;
  294. cout<<nastepnik(drzewoBst)->val<<endl;
  295. cout<<nastepnik(drzewoBst->right->right);
  296. return 0;
  297. }
  298.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement