/ Published in: C++
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
struct element { int label; element *left,*right; }; typedef element *cvor; typedef element bt; bt* InitB(int x, bt* T){ T = new element; T->label = x; T->left = NULL; T->right = NULL; return T; }//initB void CreateLeftB(int x, cvor n, bt* T){ if(n->left) { cout << "Greska" << endl << endl; return; } cvor novi = new element; n->left = novi; novi->label = x; novi->left = NULL; novi->right = NULL; }//CreateLeftB void CreateRightB(int x, cvor n, bt* T){ if(n->right) { cout << "Greska" << endl << endl; return; } cvor novi = new element; n->right = novi; novi->label = x; novi->left = NULL; novi->right = NULL; }//createRightB cvor ParentB(cvor n, bt* T){ if (n == T) return NULL; cvor roditelj = NULL; if (T->left){ if (T->left == n) return T; else roditelj = ParentB(n, T->left); } if(T->right){ if (T->right == n) return T; else roditelj = ParentB(n, T->right); } return roditelj; }//parentB int LabelB(cvor n, bt* T){ return n->label; }//labelB void ChangeLabelB(int x, cvor n, bt* T){ if(!n) return; n->label = x; }//changeLabelB cvor LeftChildB(cvor n, bt* T){ if(!n || !n->left) return NULL; return n->left; }//leftCildB cvor RightChildB(cvor n, bt* T){ if(!n || !n->right) return NULL; return n->right; }//rightCildB cvor RootB(bt* T){ if(!T) return NULL; return T; }//rootB void DeleteB(cvor n, bt* T){ static bool jednom = false; if(!jednom) { cvor roditelj = ParentB(n, T); if(roditelj->left == n) roditelj->left = NULL; else roditelj->right = NULL; jednom = true; } if(n->left) DeleteB(n->left, T); if(n->right) DeleteB(n->right, T); delete n; }//deleteB