Posted By

akljaic on 01/19/15


Tagged

tree binary


Versions (?)

main_binarno


 / Published in: C++
 

URL: asdf

asdf

  1. #include <iostream>
  2. #include "opcenito1.h"
  3. #include "binarno_polje.h"
  4. //#include "binarno_pokazivac.h"
  5. #include "ophodjenje1.h"
  6. #include "pretrazivanje.h"
  7. #include "hrpa.h"
  8.  
  9. using namespace std;
  10.  
  11. int root,a,b,c,d,e,f,g,h,k,l;
  12.  
  13. void opcenito(){
  14. system("CLS");
  15. cout << "P R V O D I J E T E - S L J E D E C I B R A T" << endl
  16. << "-------------------------------------------------" << endl
  17. << "Kreiram stablo..." << endl
  18. << " 9 " << endl
  19. << " / \\ " << endl
  20. << " 4 10 " << endl
  21. << " / \\ " << endl
  22. << " 2 1 " << endl
  23. << " / | \\ " << endl
  24. << " 0 6 3 " << endl
  25. << " " << endl;
  26.  
  27. root = 9;
  28. tree *BT=InitT(root,BT);
  29. cout << "RootT: " << RootT(BT) << endl << endl;
  30. CreateT(4,RootT(BT),BT);
  31. CreateT(10,RootT(BT),BT);
  32. CreateT(2,4,BT);
  33. CreateT(1,10,BT);
  34. CreateT(0,2,BT);
  35. CreateT(6,2,BT);
  36. CreateT(3,2,BT);
  37.  
  38. cout << "Label FirstChild NextSibling" << endl;
  39. for(int i=0; i<=10; i++){
  40. cout << LabelT(i,BT)
  41. << "\t" << FirstChildT(i,BT)
  42. << "\t" << NextSiblingT(i,BT)
  43. << endl;
  44. }
  45. system("PAUSE");
  46. }
  47.  
  48. void ophodjenje(){
  49. system("CLS");
  50. cout << " O P H O D J E N J E S T A B L A "
  51. << "-------------------------------------" << endl
  52. << "Kreiram stablo..." << endl
  53. << " 1 " << endl
  54. << " / | \\ " << endl
  55. << " 2 3 4 " << endl
  56. << " / \\ \\ " << endl
  57. << " 5 6 7 " << endl
  58. << " / | | " << endl
  59. << " 8 9 10 " << endl
  60. << " " << endl;
  61.  
  62. root = 1;
  63. tree *BT=InitT(root,BT);
  64. cout << "RootT: " << RootT(BT) << endl << endl;
  65. CreateT(2,RootT(BT),BT);
  66. CreateT(3,RootT(BT),BT);
  67. CreateT(4,RootT(BT),BT);
  68. CreateT(5,3,BT);
  69. CreateT(6,3,BT);
  70. CreateT(7,4,BT);
  71. CreateT(8,5,BT);
  72. CreateT(9,5,BT);
  73. CreateT(10,6,BT);
  74.  
  75. cout << "Preorder: " << endl;
  76. preorder(BT);
  77. cout << endl;
  78. BT->first = root;
  79. cout << "Inorder: " << endl;
  80. inorder(BT);
  81. cout << endl;
  82. BT->first = root;
  83. cout << "Postorder: " << endl;
  84. postorder(BT);
  85. cout << endl;
  86. BT->first = root;
  87.  
  88. system("PAUSE");
  89. }
  90.  
  91. void pretrazivanje(){
  92. system("CLS");
  93. cout << "B I N A R N O P R E T R A Z I V A N J E" << endl
  94. << "----------------------------------------" << endl
  95. << "Kreiram stablo..." << endl
  96. << " 6 " << endl
  97. << " / \\ " << endl
  98. << " 3 7 " << endl
  99. << " / \\ \\ " << endl
  100. << " 2 4 9 " << endl
  101. << " / / " << endl
  102. << " 1 8 " << endl
  103. << " " << endl;
  104.  
  105. root = 6;
  106. btree *BT=InitB(root,BT);
  107. cout << "RootT: " << RootB(BT) << endl << endl;
  108. AddElement(3,RootB(BT),BT);
  109. AddElement(7,RootB(BT),BT);
  110. AddElement(2,3,BT);
  111. AddElement(4,3,BT);
  112. AddElement(9,7,BT);
  113. AddElement(1,2,BT);
  114. AddElement(8,9,BT);
  115.  
  116. int broj;
  117. char jos;
  118. do{
  119. cout << "Unesite broj koji zelite pretraziti: ";
  120. cin >> broj;
  121. if(Find(broj,RootB(BT),BT))
  122. cout << "Broj je pronadjen!" << endl;
  123. else
  124. cout << "Broj nije pronadjen!" << endl;
  125. cout << "Zelite opet pretraziti? (d/n) ";
  126. cin >> jos;
  127. }while(jos=='d');
  128.  
  129. system("PAUSE");
  130. }
  131.  
  132. void hrpa(){
  133. system("CLS");
  134. cout << " H E A P S O R T " << endl
  135. << "------------------------------" << endl
  136. << "Niz: 6, 2, 7, 5, 8, 2, 8, 9, 1" << endl;
  137.  
  138. int a[9];
  139. a[0] = 6;
  140. a[1] = 2;
  141. a[2] = 7;
  142. a[3] = 5;
  143. a[4] = 8;
  144. a[5] = 2;
  145. a[6] = 8;
  146. a[7] = 9;
  147. a[8] = 1;
  148.  
  149. build_maxheap(a,9);
  150. heapsort(a,9);
  151.  
  152. cout << "Sortirano: ";
  153. for(int i=1; i<=9; i++)
  154. cout << a[i] << " ";
  155. cout << endl;
  156. system("PAUSE");
  157. }
  158.  
  159. int main(){
  160. int izbor;
  161. do{
  162. system("CLS");
  163. cout << " B I N A R N O S T A B L O " << endl
  164. << "------------------------------------------------" << endl
  165. << "1. Prvo dijete - sljedeci brat - opcenito stablo" << endl
  166. << "2. Ophodjenje stabla" << endl
  167. << "3. Binarno pretrazivanje" << endl
  168. << "4. Hrpa (heap)" << endl
  169. << "9. Izlaz" << endl;
  170. cin >> izbor;
  171. switch(izbor){
  172. case 1:
  173. opcenito();
  174. break;
  175. case 2:
  176. ophodjenje();
  177. break;
  178. case 3:
  179. pretrazivanje();
  180. break;
  181. case 4:
  182. hrpa();
  183. break;
  184. }
  185. } while(izbor != 9);
  186. return 0;
  187. }

Report this snippet  

You need to login to post a comment.