Posted By

Dragana_Mlikota on 01/12/14


Tagged

header polje stablo


Versions (?)

Tree_array.h


 / Published in: C++
 

stablo header polje

  1. #define ARRAY_SIZE 10000
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. typedef int labeltype;
  6. typedef int node;
  7.  
  8. struct elem{
  9. labeltype label;
  10. node firstchild,nextsibling;
  11. };
  12.  
  13. struct tr{
  14. struct elem elements[ARRAY_SIZE];
  15. node first;
  16. };
  17.  
  18. typedef struct tr tree;
  19.  
  20. bool nodeTest(node n, tree * T){
  21. if (T->elements[n].nextsibling == -2) {
  22. cout << "Taj cvor ne postoji!\n\n";
  23. system("pause");
  24. return false;
  25. }
  26. return true;
  27. }
  28.  
  29. node ParentT(node n, tree * T){
  30. if (n==T->first) return -1;
  31. else {
  32. for(int i=0;i<ARRAY_SIZE;i++){
  33. if (T->elements[i].firstchild==n) return i;
  34. else if (T->elements[i].nextsibling==n) ParentT(i, T);
  35. }
  36. }
  37. }
  38.  
  39. node FirstChildT(node n, tree * T){
  40. return T->elements[n].firstchild;
  41. }
  42.  
  43. node NextSiblingT(node n, tree * T){
  44. return T->elements[n].nextsibling;
  45. }
  46.  
  47. node LabelT(node n, tree * T){
  48. return T->elements[n].label;
  49. }
  50.  
  51. node RootT(tree * T){
  52. return T->first;
  53. }
  54.  
  55. void CreateT(labeltype x, node n, tree * T){
  56. if (!nodeTest(n, T)) return;
  57. for (int i=0; i<ARRAY_SIZE; i++){
  58. if (T->elements[i].nextsibling == -2) {
  59. T->elements[i].label = x;
  60. T->elements[i].firstchild = -1;
  61. T->elements[i].nextsibling = -1;
  62. if (T->elements[n].firstchild == -1) T->elements[n].firstchild = i;
  63. else {
  64. node current = T->elements[n].firstchild;
  65. node lastChild;
  66. while (T->elements[current].nextsibling != -1) current = T->elements[current].nextsibling;
  67. T->elements[current].nextsibling = i;
  68. }
  69. break;
  70. }
  71. }
  72. }
  73.  
  74. void ChangeLabelT(labeltype x, node n, tree * T){
  75. if (!nodeTest(n, T)) return;
  76. T->elements[n].label = x;
  77. }
  78.  
  79. bool RouteToRoot(node p, node n, tree * T){
  80. if (ParentT(n, T) == T->first) return true;
  81. if (ParentT(n, T) == p) return false;
  82. RouteToRoot(p, ParentT(n, T), T);
  83. }
  84.  
  85. void DeleteT(node n, tree * T){
  86. if (!nodeTest(n, T)) return;
  87. for (int i=1; i<ARRAY_SIZE; i++){
  88. if (T->elements[i].nextsibling == -2) continue;
  89. if (!RouteToRoot(n, i, T)) {
  90. cout << i << endl;
  91. T->elements[i].nextsibling = -2;
  92. }
  93. }
  94. T->elements[n].nextsibling = -2;
  95. system("pause");
  96. }
  97.  
  98. void InitT(labeltype x, tree * T){
  99. //T = new tree;
  100. T->first = 0;
  101. T->elements[RootT(T)].label = x;
  102. T->elements[RootT(T)].firstchild = -1;
  103. T->elements[RootT(T)].nextsibling = -1;
  104. for (int i=1; i<ARRAY_SIZE; i++){
  105. T->elements[i].nextsibling = -2;
  106. }
  107. }

Report this snippet  

You need to login to post a comment.