Posted By

Dragana_Mlikota on 01/13/14


Tagged

main


Versions (?)

main_funkcija


 / Published in: C++
 

main

  1. #include <iostream>
  2. #include "Tree_array.h"
  3. #include "Tree_order.h"
  4. #include "BinTree_array.h"
  5. //#include "BinTree_pointer.h"
  6.  
  7. using namespace std;
  8.  
  9. void dodavanje(tree * T){
  10. node parent;
  11. labeltype x;
  12. system("cls");
  13. cout << "Unesite roditelja kojem zelite dodati dijete: ";
  14. cin >> parent;
  15. cout << "Unesite vrijednost: ";
  16. cin >> x;
  17. CreateT(x, parent, T);
  18. }
  19.  
  20. void promjena(tree * T){
  21. node n;
  22. labeltype x;
  23. system("cls");
  24. cout << "Unesite cvor kojem zelite promijeniti vrijednost: ";
  25. cin >> n;
  26. cout << "Unesite vrijednost: ";
  27. cin >> x;
  28. ChangeLabelT(x, n, T);
  29. }
  30.  
  31. void brisanje(tree * T){
  32. node n;
  33. system("cls");
  34. cout << "Unesite cvor kojem zelite obrisati: ";
  35. cin >> n;
  36. DeleteT(n, T);
  37. }
  38.  
  39. void preorder (tree * T){
  40. system("cls");
  41. cout << "Red.br.\tLabel\tFirst child\tNext sibling" << endl;
  42. Preorder(0, T);
  43. system("pause");
  44. }
  45.  
  46. void inorder (tree * T){
  47. system("cls");
  48. cout << "Red.br.\tLabel\tFirst child\tNext sibling" << endl;
  49. Inorder(0, T);
  50. system("pause");
  51. }
  52.  
  53. void postorder (tree * T){
  54. system("cls");
  55. cout << "Red.br.\tLabel\tFirst child\tNext sibling" << endl;
  56. Postorder(0, T);
  57. system("pause");
  58. }
  59.  
  60. void ispis(tree * T){
  61. system("cls");
  62. cout << "Red.br.\tLabel\tFirst child\tNext sibling" << endl;
  63. for (int i=0; i<ARRAY_SIZE; i++){
  64. if (NextSiblingT(i, T) != -2){
  65. cout << i << "\t" << LabelT(i, T)
  66. << "\t" << FirstChildT(i, T)
  67. << "\t\t" << NextSiblingT(i, T) << endl;
  68. }
  69. }
  70. system("pause");
  71. }
  72.  
  73. void dodavanjeB(bnode n, labeltype value, btree BT){
  74. if (value<LabelB(n, BT)) {
  75. if (!testExistB(LeftChildB(n, BT), BT)) {
  76. CreateLeftB(value, n, BT);
  77. ChangeLabelB(value*2, LeftChildB(n, BT), BT);
  78. return;
  79. } else dodavanjeB(LeftChildB(n, BT), value, BT);
  80. }
  81. if (value>=LabelB(n, BT)) {
  82. if (!testExistB(RightChildB(n, BT), BT)) {
  83. CreateRightB(value, n, BT);
  84. ChangeLabelB(value*2, RightChildB(n, BT), BT);
  85. return;
  86. } else dodavanjeB(RightChildB(n, BT), value, BT);
  87. }
  88. }
  89.  
  90. void ispisB(bnode n, btree BT){
  91. cout << n << "\t" << LabelB(n, BT)
  92. << "\t" << LeftChildB(n, BT)
  93. << "\t\t" << RightChildB(n, BT) << endl;
  94. if (testExistB(RightChildB(n, BT), BT)) ispisB(RightChildB(n, BT), BT);
  95. if (testExistB(LeftChildB(n, BT), BT))ispisB(LeftChildB(n, BT), BT);
  96. }
  97.  
  98. labeltype * unosBr(){
  99. system("cls");
  100. int i=0, br;
  101. labeltype * polje = new labeltype[ARRAY_SIZE];
  102. cout << "Unesite niz brojeva zavrsno sa '0':\n";
  103. do{
  104. cout << i+1 << ": ";
  105. cin >> br;
  106. polje[i] = br;
  107. i++;
  108. }while (br!=0);
  109. return polje;
  110. }
  111.  
  112.  
  113. int main(){
  114. int izbor;
  115. labeltype * polje = NULL;
  116. tree * T = new tree;
  117. InitT(0, T);
  118. btree BT;
  119. BT = InitB(rand()%101, BT);
  120.  
  121. do{
  122. do{
  123. system("cls");
  124. cout << "IZBORNIK:\n";
  125. cout << "---------- Stablo - polje ----------\n";
  126. cout << "1. Dodaj cvor u stablo\n";
  127. cout << "2. Promijeni vrijednost\n";
  128. cout << "3. Brisanje cvora\n";
  129. cout << "4. Ispis - Preorder\n";
  130. cout << "5. Ispis - Inorder\n";
  131. cout << "6. Ispis - Postorder\n";
  132. cout << "7. Ispis\n";
  133. cout << "---------- Binarno stablo----------\n";
  134. cout << "8. Unos niza\n";
  135. cout << "9. Dodaj unose u stablo\n";
  136. cout << "10. Ispis\n";
  137. cout << "---------------------------------\n";
  138. cout << "0. Kraj\n\n";
  139. cout << "Izbor: ";
  140. cin >> izbor;
  141. }while(izbor<0 || izbor>12);
  142.  
  143. switch(izbor){
  144. case 1: dodavanje(T);
  145. break;
  146. case 2: promjena(T);
  147. break;
  148. case 3: brisanje(T);
  149. break;
  150. case 4: preorder (T);
  151. break;
  152. case 5: inorder(T);
  153. break;
  154. case 6: postorder(T);
  155. break;
  156. case 7: ispis(T);
  157. break;
  158. case 8: polje = unosBr();
  159. break;
  160. case 9: if (polje) {
  161. for (int i=0; i<ARRAY_SIZE && polje[i]!=0; i++){
  162. dodavanjeB(RootB(BT), polje[i], BT);
  163. }
  164. }
  165. else {
  166. cout << "Ne postoji niz koji bi se unio u stablo!\n\n";
  167. system("pause");
  168. }
  169. break;
  170. case 10: system("cls");
  171. cout << "Node\tLabel\tLeft child\tRight child" << endl;
  172. ispisB(RootB(BT), BT);
  173. system("pause");
  174. break;
  175.  
  176. }
  177. }while(izbor!=0);
  178.  
  179.  
  180. return 0;
  181. }

Report this snippet  

You need to login to post a comment.