Implementacija pokazivač





0
Date Submitted Tue. Jan. 4th, 2011 9:36 AM
Revision 1 of 1
Beginner antstefek
Tags podataka | Struktute | Z4
Comments 0 comments
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;
};



 

Antonio Štefek

Comments

There are currently no comments for this snippet.

Voting

Votes Up


Votes Down