/ Published in: C++
Ophodnja stabla (preorder, inorder, postorder)
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
void Preorder(tr *T){ int pom = RootT(T); cout << pom << " "; if(FirstChildT(pom, T) != -1){ T->prvi = FirstChildT(pom, T); Preorder(T); } if(NextSiblingT(pom, T) != -1){ T->prvi = NextSiblingT(pom, T); Preorder(T); } } void Inorder(int t,tr *T){ if (FirstChildT(t,T)!=-1) Inorder(FirstChildT(t,T),T); cout <<t<<" "; if(FirstChildT(t,T)!=-1){ t=FirstChildT(t,T); while(NextSiblingT(t,T)!=-1){ t=NextSiblingT(t,T); Inorder(t,T); } } } void Postorder(tr *T){ int pom = RootT(T); if(FirstChildT(pom, T) != -1){ T->prvi = FirstChildT(pom, T); Postorder(T); } cout << pom << " "; if(NextSiblingT(pom, T) != -1){ T->prvi = NextSiblingT(pom, T); Postorder(T); } }