/ Published in: C++
Implementacija algoritma ophođenja stabla (preorder, inorder, postorder) za kolegij Strukture podataka.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
void PreOrder(tree* T){ int node = T->root; cout << node << " "; if(T->array[node].firstchild != -1){ T->root = T->array[node].firstchild; PreOrder(T); } if(T->array[node].nextsibling != -1){ T->root = T->array[node].nextsibling; PreOrder(T); } } void InOrder(tree* T){ int node = T->root; if(T->array[node].firstchild != -1){ T->root = T->array[node].firstchild; InOrder(T); } int parent = ParentT(node, T); if(T->array[node].firstchild == -1) cout << node << " "; if(FirstChildT(parent, T) == node) cout << parent << " "; if(T->array[node].nextsibling != -1){ T->root = T->array[node].nextsibling; InOrder(T); } } void PostOrder(tree* T){ int node = T->root; if(T->array[node].firstchild != -1){ T->root = T->array[node].firstchild; PostOrder(T); } cout << node << " "; if(T->array[node].nextsibling != -1){ T->root = T->array[node].nextsibling; PostOrder(T); } }