Advertisement
adwas33

Drzewa BST małe

May 7th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 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. void usunElement(drzewoBST *&wierzcholek)// nie dokończona
  228. {
  229. if(wierzcholek->left== nullptr&&wierzcholek->right == nullptr) // To jest wtedy liść
  230. {
  231.  
  232. delete wierzcholek;
  233. }else if(wierzcholek->left&&wierzcholek->right)
  234. {
  235. drzewoBST * minimumZPrawejGalezi= znajdzMin(wierzcholek->right);
  236. if(minimumZPrawejGalezi->right)
  237. {
  238. drzewoBST * gora=minimumZPrawejGalezi->up;
  239.  
  240. }
  241.  
  242. } else
  243. if(wierzcholek->left!= nullptr || wierzcholek -> right== nullptr)
  244. {
  245. drzewoBST *gora=wierzcholek->up;
  246. if(gora->left==wierzcholek)
  247. {
  248. gora->left=wierzcholek->left;
  249. wierzcholek->up=gora;
  250. delete wierzcholek;
  251. } else
  252. if(gora->right==wierzcholek)
  253. {
  254. gora->right=wierzcholek->right;
  255. wierzcholek->up=gora;
  256. delete wierzcholek;
  257. }
  258. }
  259. }
  260.  
  261.  
  262. void rotacjaWLewo(drzewoBST *&root,drzewoBST *&zamieniany)
  263. {
  264. drzewoBST * b,* parent;
  265. b=zamieniany->left;
  266. if(b== nullptr)
  267. {
  268. return;
  269. }
  270. parent=zamieniany->up;
  271. zamieniany->left=b->right;
  272. if(zamieniany!= nullptr)
  273. {
  274. zamieniany->left->up=zamieniany;
  275. }
  276. b->right=zamieniany;
  277. b->up=parent;
  278. zamieniany->up=b;
  279. if(parent== nullptr)
  280. {
  281. root=b;
  282. return;
  283. }
  284. if(parent->left==zamieniany)
  285. {
  286. parent->left=b;
  287. }else parent->right=b;
  288.  
  289.  
  290. }
  291.  
  292.  
  293.  
  294. int main() {
  295. drzewoBST * drzewo=nullptr;
  296. addBstNode(drzewo,12);
  297. addBstNode(drzewo,16);
  298. addBstNode(drzewo,25);
  299. addBstNode(drzewo,14);
  300. addBstNode(drzewo,3);
  301. addBstNode(drzewo,8);
  302. addBstNode(drzewo,22);
  303. addBstNode(drzewo,16);
  304. addBstNode(drzewo,10);
  305. addBstNode(drzewo,27);
  306. drzewoBST * drzewo2=nullptr;
  307. addBstNode(drzewo2,12);
  308. addBstNode(drzewo2,6);
  309. addBstNode(drzewo2,-5);
  310. addBstNode(drzewo2,11);
  311. addBstNode(drzewo2,3);
  312. addBstNode(drzewo2,8);
  313. addBstNode(drzewo2,22);
  314. addBstNode(drzewo2,16);
  315. addBstNode(drzewo2,10);
  316. addBstNode(drzewo2,27);
  317.  
  318.  
  319.  
  320. inorder(drzewo);
  321. cout<<endl;
  322. usunLiscie(drzewo);
  323. inorder(drzewo);
  324. cout<<endl;
  325.  
  326. usunLiscie(drzewo2);
  327. inorder(drzewo2);
  328. cout<<endl;
  329. drzewoBST * nowe=nullptr;
  330. addBstNodeWithRememberUp(nowe,12);
  331. addBstNodeWithRememberUp(nowe,16);
  332. addBstNodeWithRememberUp(nowe,25);
  333. addBstNodeWithRememberUp(nowe,14);
  334. addBstNodeWithRememberUp(nowe,3);
  335. addBstNodeWithRememberUp(nowe,8);
  336. addBstNodeWithRememberUp(nowe,22);
  337. addBstNodeWithRememberUp(nowe,16);
  338. addBstNodeWithRememberUp(nowe,10);
  339. addBstNodeWithRememberUp(nowe,27);
  340. inorder(nowe);
  341. cout<<endl<<znajdzMin(nowe)->val<<endl<<znajdzMax(nowe)->val<<endl;
  342. /// TESTOWANIE POPRZEDNIKA I NASTĘPNIKA
  343. drzewoBST *drzewoBst= nullptr;
  344. addBstNodeWithRememberUp(drzewoBst,15);
  345. addBstNodeWithRememberUp(drzewoBst,6);
  346. addBstNodeWithRememberUp(drzewoBst,22);
  347. addBstNodeWithRememberUp(drzewoBst,-5);
  348. addBstNodeWithRememberUp(drzewoBst,14);
  349. addBstNodeWithRememberUp(drzewoBst,8);
  350. addBstNodeWithRememberUp(drzewoBst,12);
  351. addBstNodeWithRememberUp(drzewoBst,13);
  352. addBstNodeWithRememberUp(drzewoBst,18);
  353. addBstNodeWithRememberUp(drzewoBst,27);
  354. addBstNodeWithRememberUp(drzewoBst,17);
  355. inorder(drzewoBst);
  356. cout<<endl;
  357. cout<<drzewoBst->left->right->left->right->right->val<<endl;
  358. cout<<poprzednik(drzewoBst->left->right->left->right->right)->val<<endl;//ok
  359. cout<<drzewoBst->right->left->left->val<<endl;
  360. cout<<poprzednik(drzewoBst->right->left->left)->val<<endl;//ok
  361. cout<<poprzednik(drzewoBst)->val<<endl;
  362. cout<<nastepnik(drzewoBst)->val<<endl;
  363. cout<<nastepnik(drzewoBst->right->right);
  364. return 0;
  365. }
  366.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement