Posted By

sam085 on 01/06/11


Tagged


Versions (?)

zad4 - main


 / Published in: C++
 

  1. #include <iostream>
  2. #define header 2
  3.  
  4. #if header == 1
  5. #include "BTree_pointer.h"
  6. #elif header == 2
  7. #include "BTree_array.h"
  8. #else
  9. #include "ChildSiblingTree.h"
  10. #endif
  11.  
  12. using namespace std;
  13.  
  14. #if header == 1 || header == 2
  15. void binaryTree()
  16. {
  17. int rt, nd1, nd2, nd3, nd4, nd5, nd6, nd7;
  18. cout << "Inicijalizacija stabla..." << endl;
  19. cout<<"-------------------------------------------------" << endl;
  20. cout << "Vrijednost korjena: ";
  21. cin >> rt;
  22. btree myTree = init(rt);
  23.  
  24. cout << endl << "\nKreiranje cvorova..." << endl;
  25. cout << "Lijevo dijete korjena: ";
  26. cin >> nd1;
  27. createLeft(nd1, root(myTree), myTree);
  28. cout << "Desno dijete korjena: ";
  29. cin >> nd2;
  30. createRight(nd2, root(myTree), myTree);
  31. cout << "Lijevo dijete lijevog dijeteta korjena: ";
  32. cin >> nd3;
  33. createLeft(nd3, leftChild(root(myTree), myTree), myTree);
  34. cout << "Desno dijete lijevog dijeteta korjena: ";
  35. cin >> nd4;
  36. createRight(nd4, leftChild(root(myTree), myTree), myTree);
  37. cout << "Lijevo dijete desnog dijeteta korjena: ";
  38. cin >> nd5;
  39. createLeft(nd5, rightChild(root(myTree), myTree), myTree);
  40. cout << "Desno dijete desnog dijeteta korjena: ";
  41. cin >> nd6;
  42. createRight(nd6, rightChild(root(myTree), myTree), myTree);
  43.  
  44. cout << endl << "-------------------------------------------------" << endl;
  45. cout << "Ispis" << endl;
  46. cout << "korijen: " << label(root(myTree), myTree) << endl;
  47. cout << "1. razina: " << label(leftChild(root(myTree), myTree), myTree)<< ", ";
  48. cout << label(rightChild(root(myTree), myTree), myTree)<< endl;
  49. cout << "2. razina: " << label(leftChild(leftChild(root(myTree), myTree), myTree), myTree);
  50. cout << ", " << label(rightChild(leftChild(root(myTree), myTree), myTree), myTree);
  51. cout << ", " << label(leftChild(rightChild(root(myTree), myTree), myTree), myTree);
  52. cout << ", " << label(rightChild(rightChild(root(myTree), myTree), myTree), myTree)<< endl;
  53. cout << "-----------------------------------------------" << endl;
  54.  
  55. node tmp = leftChild(rightChild(root(myTree), myTree), myTree);
  56. cout << "Roditelj cvora 6 je: " << label(parrent(tmp, myTree), myTree) << endl;
  57. cout << "Brisanje cvora 3 i potomaka!" << endl;
  58. deleteNode(rightChild(root(myTree), myTree), myTree);
  59. cout << "Unesite novu vrijednost: ";
  60. cin >> nd7;
  61. changeLabel(nd7, leftChild(leftChild(root(myTree), myTree), myTree), myTree);
  62.  
  63. cout << endl << "-------------------------------------------------" << endl;
  64. cout << "Novo stanje" << endl;
  65. cout << "Korijen: " << label(root(myTree), myTree) << endl;
  66. cout << "1. razina: " << label(leftChild(root(myTree), myTree), myTree)<< endl;
  67. cout << "2. razina: " << label(leftChild(leftChild(root(myTree), myTree), myTree), myTree);
  68. cout << ", " << label(rightChild(leftChild(root(myTree), myTree), myTree), myTree) << endl;
  69. cout << "-----------------------------------------------" << endl;
  70. }
  71. #else
  72. void trie()
  73. {
  74. tree *myTree = new tree;
  75. init(1, myTree);
  76. cout << "cvor 2:" << endl;
  77. create(2, 1, myTree);
  78. cout << "cvor 3:" << endl;
  79. create(3, 2, myTree);
  80. cout << "cvor 4:" << endl;
  81. create(4, 2, myTree);
  82.  
  83. string newLabel;
  84. cout << "Nova oznaka za cvor 2:"; cin >> newLabel;
  85. changeLabel(newLabel, 2, myTree);
  86. label(3, myTree);
  87.  
  88. label(2, myTree);
  89. label(3, myTree);
  90. label(4, myTree);
  91. label(5, myTree);
  92.  
  93. cout << "prvo djete od 2: " << firstChild(2, myTree) << endl;
  94. cout << "roditelj od 3: " << parent(3, myTree) << endl;
  95. cout << "brat od 3: " << nextSibling(3, myTree) << endl;
  96. deleteNode(2, myTree);
  97. delete myTree;
  98. }
  99. #endif
  100. int main ()
  101. {
  102. #if header == 1 || header == 2
  103. binaryTree();
  104.  
  105. #else
  106. trie();
  107. #endif
  108.  
  109.  
  110. system("pause");
  111. return 0;
  112. }

Report this snippet  

You need to login to post a comment.