opcenito stablo


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



Copy this code and paste it in your HTML
  1. struct elem {
  2. char label;
  3. int firstchild, nextsibling;
  4. };
  5.  
  6. struct tr {
  7. elem elements [10000];
  8. int first;
  9. };
  10.  
  11. int ParentT (int n, tr *tree) {
  12. if (n==0) return -1;
  13. else return a-1;
  14. }
  15.  
  16. int FirstChildT (int n, tr *tree) {
  17. if (n>=0) return tree->elements[n].firstchild;
  18. else return -1;
  19. }
  20.  
  21. int NextSiblingT (int n, tr *tree) {
  22. if (tree->elements[n].nextsibling!=-1) return tree->elements[n].nextsibling;
  23. else return -1;
  24. }
  25.  
  26. int LabelT (int n, tr *tree) {
  27. return tree->elements[n].label;
  28. }
  29.  
  30. int RootT (tr *tree) {
  31. return tree->elements[0].label;
  32. }
  33.  
  34. int CreateT (int x, int n, tr *tree) {
  35. tree->elements[tree->first].label=x;
  36. tree->elements[tree->first].firstchild=n;
  37. tree->first++;
  38. }
  39.  
  40. int ChangeLabelT (int x, int n, tr *tree) {
  41. tree->elements[n].label=x;
  42. }
  43.  
  44. int DeleteT (int n, tr *tree) {
  45. if (tree->elements[n].firstchild>0)
  46. DeleteT (tree->elements[n].firstchild, tree);
  47. else {
  48. tree->elements[n].label=-1;
  49. tree->first--;
  50. }
  51. }
  52.  
  53. tr *InitT (int x, tr *tree) {
  54. delete[]tree->elements;
  55. tree = new tr;
  56. tree->first=0;
  57. tree->elements[x].label=x;
  58. tree->first++;
  59.  
  60. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.