/ Published in: C++
Implementacija binarnog stabla pomoću pokazivaÄa za kolegij Strukture podataka.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
struct pok_element{ int label; pok_element* left, * right; }; pok_element* InitB(int x, pok_element* T){ T = new pok_element; T->left = NULL; T->right = NULL; T->label = x; return T; } pok_element* RootB(pok_element* T){ return T; } int LabelB(pok_element* T){ return T->label; } void ChangeLabelB(int x, pok_element* T){ T->label = x; } pok_element* ParentB(int n, pok_element* 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); } } pok_element* LeftChildB(pok_element* T){ return T->left; } pok_element* RightChildB(pok_element* T){ return T->right; } void CreateLeftB(int x, pok_element* T){ if(T->left) cout << "Cvor vec ima dijete!" << endl; else{ pok_element* novi = new pok_element; novi->label = x; novi->left = NULL; novi->right = NULL; T->left = novi; } } void CreateRightB(int x, pok_element* T){ if(T->right) cout << "Cvor vec ima dijete!" << endl; else{ pok_element* novi = new pok_element; novi->label = x; novi->left = NULL; novi->right = NULL; T->right = novi; } } void DeleteB(pok_element* n, pok_element* T){ if(n->left != NULL) DeleteB(n->left, T); if(n->right != NULL) DeleteB(n->right, T); delete n; }