Posted By


HeatPwnz on 01/11/14

Tagged


Statistics


Viewed 365 times
Favorited by 0 user(s)

opcenito_stablo.h


/ Published in: C++
Save to your folder(s)

header koristen za realizaciju opcenitog stabla


Copy this code and paste it in your HTML
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct elem {
  5. char oznaka;
  6. int firstchild,nextsibling;
  7. };
  8. struct tr {
  9. elem element[1000];
  10. int root;
  11. };
  12. typedef struct tr tree;
  13. typedef int node;
  14.  
  15. int RootT(tree *T){
  16. return T->root;
  17. }
  18.  
  19. int FirstChildT (int n, tree *T){
  20. return T->element[n].firstchild;
  21. }
  22. int NextSiblingT (int n, tree *T) {
  23. return T->element[n].nextsibling;
  24. }
  25.  
  26. int ParentT(int n, tree *T){
  27. if(n == T->root || T->element[n].oznaka == ' ' )
  28. return -1;
  29. for (int i=0; i<1000; i++) {
  30. if (FirstChildT(i, T)==n) return i;
  31. if (NextSiblingT(i, T)==n) return ParentT(i, T);
  32. }
  33. return -1;
  34. }
  35. char LabelT (int n, tree *T) {
  36. return T->element[n].oznaka;
  37. }
  38. int CreateT(char oznaka, int n, tree *T) {
  39. int i=0;
  40. while (LabelT(i, T)!=' ') i++;
  41. T->element[i].oznaka=oznaka;
  42. T->element[i].firstchild=-1;
  43. T->element[i].nextsibling=-1;
  44. if (T->element[n].firstchild!=-1) {
  45. n=FirstChildT(n, T);
  46. while (NextSiblingT(n, T)!=-1) n=NextSiblingT(n, T);
  47. T->element[n].nextsibling=i;
  48. }
  49. else T->element[n].firstchild=i;
  50. return i;
  51. }
  52. void ChangeLabelT(char oznaka, int n, tree *T) {
  53. T->element[n].oznaka=oznaka;
  54. }
  55.  
  56. bool DeleteT (int n, tree *T) {
  57. int roditelj, brat;
  58. roditelj = ParentT(n,T);
  59. brat = T->element[roditelj].firstchild;
  60.  
  61. if(T->element[n].oznaka == ' ') return false;
  62.  
  63. while(T->element[n].firstchild != -1){
  64. int dijete = T->element[n].firstchild;
  65. DeleteT(dijete,T);
  66. }
  67. if(brat == n)
  68. T->element[ParentT(n,T)].firstchild = T->element[n].nextsibling;
  69. else if(T->element[n].nextsibling != -1){
  70. T->element[brat].nextsibling = T->element[n].nextsibling;
  71. }
  72.  
  73. T->element[n].oznaka = ' ';
  74. return true;
  75. }
  76.  
  77. tree *InitT(char oznaka, tree *T) {
  78. T=new tree;
  79. T->root=0;
  80. T->element[0].oznaka=oznaka;
  81. T->element[0].firstchild=-1;
  82. T->element[0].nextsibling=-1;
  83. for (int i=1; i<1000; i++) {
  84. T->element[i].oznaka=' ';
  85. T->element[i].firstchild=-1;
  86. T->element[i].nextsibling=-1;
  87. }
  88. return T;
  89. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.