Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "tree.h"
  3. tree::~tree()
  4. {
  5. del(root);
  6. root=0;
  7. curr=0;
  8. }
  9.  
  10. void tree:: del(tree_node *root)
  11. {
  12. if(!root) return;
  13. if(root->child)
  14. del(root->child);
  15. if(root->brother)
  16. del(root->brother);
  17. delete root;
  18. }
  19.  
  20. int tree :: operator < (const tree & b)
  21. {
  22. if (*root<*b.root) return 1;
  23. return 0;
  24. }
  25.  
  26. int tree :: operator > (const tree & b)
  27. {
  28. if (*root>*b.root) return 1;
  29. return 0;
  30. }
  31.  
  32. int tree :: operator == (const tree & b)
  33. {
  34. if (*root==*b.root) return 1;
  35. return 0;
  36. }
  37.  
  38. void tree::insert(tree_node *root, tree_node *add)
  39. {
  40. if (!root->child)
  41. root->child=add;
  42. else
  43. {
  44. if (*root<*add)
  45. {
  46. if (*root<*root->child) insert(root->child,add);
  47. else
  48. {
  49. if (!root->child->brother) root->child->brother=add;
  50. else
  51. {
  52. if (!(*root->child->brother>*root))
  53. {
  54. if (!root->child->brother->brother) root->child->brother->brother=add;
  55. else insert(root->child->brother->brother,add);
  56. }
  57. else insert(root->child->brother,add);
  58. }
  59. }
  60. }
  61. if (*add==*root)
  62. {
  63. if (*root->child==*root) insert(root->child,add);
  64. else
  65. {
  66. if (*root->child<*root)
  67. {
  68.  
  69. if (!root->child->brother) root->child->brother=add;
  70. else
  71. {
  72. if (*root->child->brother==*root) insert(root->child->brother,add);
  73. else
  74. {
  75. add->brother=root->child->brother;
  76. root->child->brother=add;
  77. }
  78. }
  79. }
  80. else
  81. {
  82. add->brother=root->child;
  83. root->child=add;
  84. }
  85. }
  86. }
  87. if (*root>*add)
  88. {
  89. if (*root->child<*root) insert(root->child,add);
  90. else
  91. {
  92. add->brother=root->child;
  93. root->child=add;
  94. }
  95. }
  96. }
  97. }
  98.  
  99. int tree::read(FILE *fp,int n1,int n2,int n3)
  100. {
  101. int i;
  102. root= new tree_node();
  103. if((root->read(fp,n2,n3))<0) {delete root;root=0;return -1;}
  104. curr=root;
  105. for(i=1;i<n1;i++)
  106. { curr=new tree_node();
  107. if(!curr){ return -1;}
  108. if((curr)->read(fp,n2,n3)<0)
  109. {
  110. delete curr;
  111. curr=0;
  112. return -1;
  113. }
  114. insert(root,curr);
  115. }
  116. curr=root;
  117. return 0;
  118. }
  119.  
  120. void tree:: print(FILE *fp,tree_node *root,int level,int kol_level,int n2,int n3)
  121. {
  122. int i;
  123. if(!root) return;
  124. if(level>kol_level) return;
  125. for(i=0;i<2*level;i++)
  126. printf(" ");
  127. root->print(fp,level,n2,n3);
  128. if (root->child) print(fp,root->child,level+1,kol_level,n2,n3);
  129. if(root->brother) print(fp,root->brother,level,kol_level,n2,n3);
  130. return;
  131. }
  132.  
  133. int tree::go_to_child()
  134. {
  135. if(!curr) return 1;
  136. if(!(curr->child)) return 2;
  137. curr=curr->child;
  138. return 0;
  139. }
  140.  
  141. int tree::go_to_brother()
  142. {
  143. if(!curr) return 1;
  144. if(!(curr->brother)) return 2;
  145. curr=curr->brother;
  146. return 0;
  147. }
  148.  
  149. int tree::go_to_root()
  150. {
  151. curr=root;
  152. return 0;
  153. }
  154.  
  155. int tree::del_child()
  156. {
  157. tree_node *tmp;
  158. if(!curr) return 1;
  159. if(!(curr->child)) return 2;
  160. if(curr->child->child) return 3;
  161. tmp=curr->child;
  162. curr->child=curr->child->brother;
  163. delete tmp;
  164. return 0;
  165. }
  166.  
  167. int tree::del_brother()
  168. {
  169. tree_node *tmp;
  170. if(!curr) return 1;
  171. if(!(curr->brother)) return 2;
  172. if(curr->brother->child) return 3;
  173. tmp=curr->brother;
  174. curr->brother=curr->brother->brother;
  175. delete tmp;
  176. return 0;
  177. }
  178.  
  179. int tree::del_root()
  180. {
  181. if(!root) return 1;
  182. if(root->child) return 2;
  183. delete root;
  184. root=0;
  185. curr=0;
  186. return 0;
  187. }
  188.  
  189. int tree::del_child_subtree()
  190. {
  191. tree_node *tmp;
  192. if(!curr) return 1;
  193. if(!curr->child) return 2;
  194. tmp=curr->child;
  195. curr->child=curr->child->brother;
  196. tmp->brother=0;
  197. del(tmp);
  198. return 0;
  199. }
  200.  
  201. int tree::del_brother_subtree()
  202. {
  203. tree_node *tmp;
  204. if(!curr) return 1;
  205. if(!curr->brother) return 2;
  206. tmp=curr->brother;
  207. curr->brother=curr->brother->brother;
  208. tmp->brother=0;
  209. del(tmp);
  210. return 0;
  211. }
  212.  
  213. int tree::add_child(tree_node *add)
  214. {
  215. if(!curr) return 1;
  216. if(!curr->child) curr->child=add;
  217. else
  218. {
  219. add->brother=curr->child;
  220. curr->child=add;
  221. }
  222. return 0;
  223. }
  224.  
  225. int tree::add_brother(tree_node *add)
  226. {
  227. if(!curr) return 1;
  228. if(curr==root) return 2;
  229. if(!curr->brother) curr->brother=add;
  230. else
  231. {
  232. add->brother=curr->brother;
  233. curr->brother=add;
  234. }
  235. return 0;
  236. }
  237.  
  238. int tree::add_root(tree_node *add)
  239. {
  240. if(!root)
  241. {
  242. root=add;
  243. curr=add;
  244. }
  245. else return 1;
  246. return 0;
  247. }
  248.  
  249. void tree::print_current(FILE *fp,int n2,int n3)
  250. {
  251. if(!curr) return;
  252. curr->print(fp,0,n2,n3);
  253. }
  254.  
  255.  
  256. void tree:: menu()
  257. {
  258. char *p;
  259. char s[LEN];
  260. int k,n1,n2,n3;
  261. print_menu_tree();
  262. while(fgets(s,LEN,stdin))
  263. {
  264.  
  265. k=strtol(s,&p,10);
  266. if(p==s) continue;
  267. switch(k)
  268. {
  269. case -1: return ;
  270. case 0:
  271. {
  272. if(curr) curr->menu();
  273. else printf("Нет текущего элемента\n");
  274. break;
  275. }
  276. case 1:
  277. { printf("Введите максимальный уровнь дерева и кол-во элементов списка и стека для печати:\n");
  278. if(scanf("%d %d %d",&n1,&n2,&n3)!=3)
  279. {
  280. printf("Error\n");
  281. break;
  282. }
  283. print(stdout,root,0,n1,n2,n3);
  284. break;
  285. }
  286. case 2:
  287. { int r=go_to_child();
  288. if(r==1) printf("Нет текущего элемента\nНевозможно перейти к потомку\n");
  289. if(r==2) printf("Нет потомка\nНевозможно перейти к потомку\n");
  290. break;
  291. }
  292. case 3:
  293. { int r=go_to_brother();
  294. if(r==1) printf("Нет текущего элемента\nНевозможно перейти к брату\n");
  295. if(r==2) printf("Нет брата\nНевозможно перейти к брату\n");
  296. break;
  297. }
  298. case 4:
  299. {
  300. go_to_root();
  301. break;
  302. }
  303. case 5:
  304. { int r=del_child();
  305. if(r==1) printf("Нет текущего элемента\nНевозможно удалить потомка\n");
  306. if(r==2) printf("Нет потомка\nНевозможно удалить потомка\n");
  307. if(r==3) printf("Потомок не является концевой вершиной\nНевозможно удалить потомка\n");
  308. break;
  309. }
  310. case 6:
  311. { int r=del_brother();
  312. if(r==1) printf("Нет текущего элемента\nНевозможно удалить брата\n");
  313. if(r==2) printf("Нет брата\nНевозможно удалить потомка\n");
  314. if(r==3) printf("Брат не является концевой вершиной\nНевозможно удалить потомка\n");
  315. break;
  316. }
  317. case 7:
  318. { int r=del_root();
  319. if(r==1) printf("Нет корня\nНевозможно удалить корень\n");
  320. if(r==2) printf("У корня есть потомки\nНевозможно удалить корень\n");
  321. break;
  322. }
  323. case 8:
  324. { int r=del_child_subtree();
  325. if(r==1) printf("Нет корня\nНевозможно удалить поддерево потомка\n");
  326. if(r==2) printf("Нет потомка\nНевозможно удалить поддерево потомка\n");
  327. break;
  328. }
  329. case 9:
  330. { int r=del_brother_subtree();
  331. if(r==1) printf("Нет корня\nНевозможно удалить поддерево брата\n");
  332. if(r==2) printf("Нет брата\nНевозможно удалить поддерево брата\n");
  333. break;
  334. }
  335. case 10:
  336. {
  337. tree_node * p=new tree_node();
  338. if(!p)
  339. {
  340. printf("Error\n");
  341. break;
  342. }
  343. printf("Введите добавляемый элемент:\n");
  344. if(p->read(stdin,1,1)<0)
  345. {
  346. printf("Error read\n");
  347. delete p;
  348. break ;
  349. }
  350. if((add_child(p))!=0)
  351. {
  352. printf("Нет текущего элемента\nНевозможно добавить потомка\n");
  353. delete p;
  354. }
  355. break;
  356. }
  357. case 11:
  358. {
  359. tree_node * p=new tree_node();
  360. if(!p)
  361. {
  362. printf("Error\n");
  363. break;
  364. }
  365. printf("Введите добавляемый элемент:\n");
  366. if(p->read(stdin,1,1)<0)
  367. {
  368. printf("Error read\n");
  369. delete p;
  370. break ;
  371. }
  372. int r=add_brother(p);
  373. if(r==1)
  374. {
  375. printf("Нет текущего элемента\nНевозможно добавить брата\n");
  376. delete p;
  377. }
  378. if(r==2)
  379. {
  380. printf("Текущий элемент-корень дерева\nНевозможно добавить брата\n");
  381. delete p;
  382. }
  383. break;
  384. }
  385. case 12:
  386. {
  387. tree_node * p=new tree_node();
  388. if(!p)
  389. {
  390. printf("Error\n");
  391. break;
  392. }
  393. printf("Введите добавляемый элемент:\n");
  394. if(p->read(stdin,1,1)<0)
  395. {
  396. printf("Error read\n");
  397. delete p;
  398. break ;
  399. }
  400. if((add_root(p))!=0)
  401. {
  402. printf("Корень уже существует\nНевозможно добавить корень\n");
  403. delete p;
  404. }
  405. break;
  406. }
  407. case 13:
  408. {
  409. printf("Введите кол-во элементов (список,стек) для печати:\n");
  410. if(scanf("%d %d",&n2,&n3)!=2)
  411. {
  412. printf("Error\n");
  413. break;
  414. }
  415. print_current(stdout,n2,n3);
  416. break;
  417.  
  418. }
  419. default: { printf("Error number\n");}
  420.  
  421. }
  422. print_menu_tree();
  423. }
  424. return ;
  425. }
  426.  
  427. void print_menu_tree()
  428. {
  429. printf("В классе TREE доступны следующие функции:\n");
  430. printf("-1-выйти\n");
  431. printf("0-вызвать меню от текущего элемента\n");
  432. printf("1-печать\n");
  433. printf("2-перейти к потомку\n");
  434. printf("3-перейти к брату\n");
  435. printf("4-перейти к корню дерева\n");
  436. printf("5-удалить потомка\n");
  437. printf("6-удалить брата\n");
  438. printf("7-удалить корень\n");
  439. printf("8-удалить поддерево потомка\n");
  440. printf("9-удалить поддерево брата\n");
  441. printf("10-добавить потомка\n");
  442. printf("11-добавить брата\n");
  443. printf("12-добавить корень\n");
  444. printf("13-печать текущего элемента\n");
  445. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement