|
|
|
Implementacija pokazivač
0
Implementacija binarnog stabla pomoću pokazivača.
struct telement{
int label, root;
telement *left, *right;
};
typedef struct telement *tcvor;
typedef struct telement *tstablo;
tcvor LeftChildB (tcvor cvor, tstablo stablo){
if (cvor->left!= NULL)
return cvor->left;
if (cvor->left == NULL)
cout<<"U stablu nema lijevog djeteta."<<endl;
};
tcvor RightChildB (tcvor cvor, tstablo stablo){
if (cvor->right!= NULL)
return cvor->right;
if (cvor->right == NULL)
cout<<"U stablu nema desnog djeteta."<<endl;
};
tcvor ParentB (tcvor cvor, tstablo stablo){
tcvor par;
if (LeftChildB(cvor, stablo)){
if (stablo->left == cvor) return stablo->left;
par = ParentB (cvor, stablo->left);
}
if (RightChildB(cvor, stablo)){
if (stablo->right == cvor) return stablo->right;
par = ParentB(cvor, stablo->right);
}
return par;
};
tcvor RootB (tstablo stablo){
if (stablo) return stablo;
else
cout<<"Greska!"<<endl;
};
int LabelB (tcvor cvor, tstablo stablo){
if(RootB (stablo)){
return cvor->label;
}
else
cout<<"Greska!"<<endl;
};
void ChangeLabelB (int label, tcvor cvor, tstablo stablo){
if (!RootB (stablo))
cout << "Taj cvor ne postoji, pa mu se ne moze promijeniti oznaka!" << endl;
else
stablo->label = label;
};
void CreateLeftB (int label, tcvor cvor, tstablo stablo){
tcvor novi = new telement;
if (LeftChildB(cvor, stablo)){
cout<<"Lijevo dijete vec postoji."<<endl;
return;
}
else{
novi->label = label;
novi->left = NULL;
novi->right = NULL;
cvor->left = novi;
}
};
void CreateRightB (int label, tcvor cvor, tstablo stablo){
tcvor novi = new telement;
if (RightChildB(cvor, stablo)){
cout<<"Desno dijete vec postoji."<<endl;
return;
}
else{
novi->label = label;
novi->left = NULL;
novi->right = NULL;
cvor->right = novi;
}
};
void DeleteB (tcvor cvor, tstablo stablo){
if (LeftChildB (cvor, stablo)) DeleteB (cvor->left, stablo);
if (RightChildB (cvor, stablo) ) DeleteB (cvor->right, stablo);
delete cvor;
};
void InitB (int root, tstablo stablo){
tcvor novi = new telement;
novi->root = root;
novi->left = NULL;
novi->right = NULL;
stablo = novi;
};




There are currently no comments for this snippet.