Posted By


8ivana8 on 01/18/15

Tagged


Statistics


Viewed 362 times
Favorited by 0 user(s)

algoritam ophođenja stabla_preorder_inorder_postorder


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

Implementacija algoritma ophođenja stabla (preorder, inorder, postorder) za kolegij Strukture podataka.


Copy this code and paste it in your HTML
  1. void PreOrder(tree* T){
  2. int node = T->root;
  3. cout << node << " ";
  4. if(T->array[node].firstchild != -1){
  5. T->root = T->array[node].firstchild;
  6. PreOrder(T);
  7. }
  8. if(T->array[node].nextsibling != -1){
  9. T->root = T->array[node].nextsibling;
  10. PreOrder(T);
  11. }
  12. }
  13.  
  14. void InOrder(tree* T){
  15. int node = T->root;
  16. if(T->array[node].firstchild != -1){
  17. T->root = T->array[node].firstchild;
  18. InOrder(T);
  19. }
  20. int parent = ParentT(node, T);
  21. if(T->array[node].firstchild == -1) cout << node << " ";
  22. if(FirstChildT(parent, T) == node) cout << parent << " ";
  23. if(T->array[node].nextsibling != -1){
  24. T->root = T->array[node].nextsibling;
  25. InOrder(T);
  26. }
  27. }
  28.  
  29. void PostOrder(tree* T){
  30. int node = T->root;
  31. if(T->array[node].firstchild != -1){
  32. T->root = T->array[node].firstchild;
  33. PostOrder(T);
  34. }
  35. cout << node << " ";
  36. if(T->array[node].nextsibling != -1){
  37. T->root = T->array[node].nextsibling;
  38. PostOrder(T);
  39. }
  40. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.