/ Published in: C++
Header file bstablo_pokazivac.h, odn. header file implementacija binarnog stabla pomoću pokazivaÄa
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
struct element{ labeltype oznaka; struct element *left,*right; }; typedef struct element *node; typedef struct element *tree; node ParentB(node n,tree stablo){ node i; int OK=0; stack S; MakeNullS(S); PushS(stablo,S); while((!IsEmptyS(S)) && (!OK)){ i=PopS(S); if (i->left!=n) && (i->right!=n)){ if (i->left!=0) PushS(i->left); if (i->right!=0) PushS(i->right); else OK=-1; } if(OK) return i; else return 0; } node LeftChildB(node n,tree stablo){ return n->left; } node RightChildB(node n,tree stablo){ return n->right; } labeltype LabelB(node n,tree stablo){ return n->oznaka; } void ChangeLabelB(labeltype x, node n,tree stablo){ n->oznaka=x; } node RootB(tree stablo){ return stablo; } void CreateLeftB(labeltype x,node n,tree stablo){ node l; if(n->left!=0){ l=(struct element *)malloc(sizeof(struct element)); l->left=l->right=0; l->oznaka=x; n->left=l; } else return; } void CreateRightB(labeltype x,node n,tree stablo){ node l; if (n->right!=0){ l=(struct element *)malloc(sizeof(struct element)); l->left=l->right=0; l->oznaka=x; n->right=l; } else return; } void DelB(node n, tree stablo){ if (n->left!=0) DelB((n->left,stablo); if (n->right!=0) DelB((n->right,stablo); free(n); } void DeleteB(node n,tree stablo){ node l; if (n->left!=0) DelB((n->left,stablo); if (n->right!=0) DelB((n->right,stablo); l=ParentB(n,stablo); if(l->left==n) l->left=0; else l->right=0; free(n); } void InitB(node x, tree *stablo){ node l; l=(struct element *)malloc(sizeof(struct element)); l->left=l->right=0; l->oznaka=x; (*stablo)=l; }