Advertisement
szmelu

drzewa

Jun 8th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <ctime>
  6. using namespace std;
  7. string cr,cl,cp;
  8.  
  9.  
  10. struct node
  11. {
  12. int value;
  13. node *left;
  14. node *right;
  15. };
  16. void push(node **&tab, node *H, int &top)
  17. {
  18. top++;
  19. tab[top]=H;
  20.  
  21. }
  22. void pop(int &top)
  23. {
  24. top--;
  25. }
  26. void add(node *&H, int x)
  27. {
  28. if (H == NULL)
  29. {
  30. H = new node;
  31. H->left = NULL;
  32. H->right = NULL;
  33. H->value = x;
  34. }
  35. else
  36. {
  37. if ( x < H->value)
  38. add(H->left, x);
  39. else
  40. add(H->right, x);
  41. }
  42. }
  43. void add(node *&H, int x, node *&p)
  44. {
  45. if (H == NULL)
  46. {
  47. H = new node;
  48. H->left = NULL;
  49. H->right = NULL;
  50. H->value = x;
  51. p=H;
  52. }
  53. else
  54. {
  55. if ( x < H->value)
  56. add(H->left, x, p);
  57. else
  58. add(H->right, x, p);
  59. }
  60. }
  61. node* min(node *H)
  62. {
  63. if(H!=NULL)
  64. {
  65. node *temp=H;
  66. while(temp->left!=NULL)
  67. temp=temp->left;
  68. return temp;
  69. }
  70. }
  71. node* max(node *H)
  72. {
  73. if(H!=NULL)
  74. {
  75. node *temp=H;
  76. while(temp->right!=NULL)
  77. temp=temp->right;
  78. return temp;
  79. }
  80. }
  81. node* poprzednik(node *H, node*p)
  82. {
  83. if(H!=NULL)
  84. {
  85. node *temp=H;
  86.  
  87. while(temp->left!=p && temp->right!=p)
  88. {
  89. if(p->value<temp->value)
  90. temp=temp->left;
  91. else
  92. temp=temp->right;
  93. }
  94. return temp;
  95. }
  96. }
  97. node* nastepnik(node *H, node *p)
  98. {
  99. if(p->right)
  100. {
  101. node *temp=p->right;
  102. if(temp->left==NULL)
  103. {
  104. return temp;
  105. }
  106. else
  107. {
  108. while(temp->left!=NULL)
  109. temp=temp->left;
  110. return temp;
  111. }
  112. }
  113. }
  114. void removetree(node *&H, node *p)
  115. {
  116. if(H!=NULL)
  117. {
  118. node *temp=NULL;
  119. temp=poprzednik(H, p);
  120. if(temp->left==p)
  121. temp->left=NULL;
  122. else
  123. temp->right=NULL;
  124. }
  125. }
  126. void remove(node *&H, node *p)
  127. {
  128. if(H!=NULL)
  129. {
  130. if(H==p)
  131. {
  132. node *temp=nastepnik(H, p);
  133. remove(H,nastepnik(H, p));
  134. temp->left=H->left;
  135. temp->right=H->right;
  136. H=temp;
  137. }
  138. else
  139. {
  140. node *temp=poprzednik(H,p);
  141. if(temp->left==p)
  142. {
  143. if(p->left==NULL && p->right==NULL)
  144. temp->left=NULL;
  145. else if(p->left==NULL && p->right!=NULL)
  146. temp->left=p->right;
  147. else if(p->left!=NULL && p->right==NULL)
  148. temp->left=p->left;
  149. else if(p->left!=NULL && p->right!=NULL)
  150. {
  151. node *tmp2=nastepnik(H, p);
  152. remove(H, nastepnik(H,p));
  153. node *tmp=p->left;
  154. node *tmp1=p->right;
  155. tmp2->left=tmp;
  156. tmp2->right=tmp1;
  157. temp->left=tmp2;
  158. }
  159. }
  160. else
  161. {
  162. if(p->left==NULL && p->right==NULL)
  163. temp->right=NULL;
  164. else if(p->left==NULL && p->right!=NULL)
  165. temp->right=p->right;
  166. else if(p->left!=NULL && p->right==NULL)
  167. temp->right=p->left;
  168. else if(p->left!=NULL && p->right!=NULL)
  169. {
  170. node *tmp2=nastepnik(H,p);
  171. remove(H, nastepnik(H,p));
  172. node *tmp=p->left;
  173. node *tmp1=p->right;
  174. tmp2->left=tmp;
  175. tmp2->right=tmp1;
  176. temp->right=tmp2;
  177. }
  178. }
  179. }
  180. }
  181. }
  182. void lustro(node *&H)
  183. {
  184. if(H!=NULL)
  185. {
  186. lustro(H->left);
  187. lustro(H->right);
  188. node *temp=H->left;
  189. H->left=H->right;
  190. H->right=temp;
  191. }
  192. }
  193. void deleteleaf(node *&H, node **&tab, int &top)
  194. {
  195. if(H!=NULL)
  196. {
  197. deleteleaf(H->left, tab, top);
  198.  
  199. if(H->left==NULL && H->right==NULL)
  200. {
  201. push(tab, H, top);
  202. }
  203.  
  204. deleteleaf(H->right, tab, top);
  205. }
  206. }
  207. void leaf(node *&H, node **&tab, int &top)
  208. {
  209. if(H!=NULL)
  210. {
  211. while(top>=0)
  212. {
  213.  
  214. node *temp=tab[top];
  215. remove(H, temp);
  216. pop(top);
  217. }
  218. }
  219. }
  220.  
  221.  
  222.  
  223.  
  224. void inorder(node *H)
  225. {
  226. if(H)
  227. {
  228. inorder(H->left);
  229. cout<<H->value<<" ";
  230. inorder(H->right);
  231. }
  232. }
  233. void printBT(string sp, string sn, node * v)
  234. {
  235. string s;
  236.  
  237. if(v)
  238. {
  239. s = sp;
  240. if(sn == cr) s[s.length() - 2] = ' ';
  241. printBT(s + cp, cr, v->right);
  242.  
  243. s = s.substr(0,sp.length()-2);
  244. cout << s << sn << v->value << endl;
  245.  
  246. s = sp;
  247. if(sn == cl) s[s.length() - 2] = ' ';
  248. printBT(s + cp, cl, v->left);
  249. }
  250. }
  251. int main()
  252. {
  253. srand(time(NULL));
  254. node *H = NULL;
  255. node *p=NULL;
  256. node **tab=new node *[40];
  257. int top=-1;
  258. int i, k;
  259. cr = cl = cp = " ";
  260. cr[0] = 218; cr[1] = 196;
  261. cl[0] = 192; cl[1] = 196;
  262. cp[0] = 179;
  263. for(int i=0;i<40;i++)
  264. {
  265. if(i==3)
  266. add(H, rand()%100, p);
  267. add(H, rand()%100);
  268. }
  269.  
  270. printBT("","",H);
  271. lustro(H);
  272. inorder(H);
  273. printBT("","",H);
  274. system("pause");
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement