/ Published in: C++
binarno
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include<iostream> using namespace std; struct pnode{ int label; pnode *left,*right; }; pnode *InitB(int n,pnode *T){ T = new pnode; T->left = T->right = NULL; T->label = n; return T; } pnode *RootB(pnode *T){ return T; } void ChangeLabelB(int x,pnode *T){ T->label = x; } pnode *ParentB(int n,pnode *T){ if(T->label==n) return NULL; if(T->left){ if(T->left->label==n) return T; ParentB(n,T->left); } if(T->right){ if(T->right->label==n) return T; ParentB(n,T->right); } } pnode *LeftChildB(pnode *T){ return T->left; } pnode *RightChildB(pnode *T){ return T->right; } int LabelB(pnode *T){ return T->label; } void CreateLeftB(int x,pnode *T){ if(T->left) cout << "Lijevo dijete već postoji!" << endl; else{ pnode *novi = new pnode; novi->label = x; novi->left = NULL; novi->right = NULL; T->left = novi; } } void CreateRightB(int x,pnode *T){ if(T->right) cout << "Desno dijete već postoji!" << endl; else{ pnode *novi = new pnode; novi->label = x; novi->left = NULL; novi->right = NULL; T->right = novi; } } void DeleteB(pnode *P,pnode *T){ pnode *erase = P; if(P!=T){ pnode *roditelj = ParentB(P->label,T); if(roditelj->left==P) roditelj->left = NULL; else roditelj->right = NULL; } if(P->left) DeleteB(P->left,T); if(P->right) DeleteB(P->right,T); delete erase; }