Revision: 38613
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at January 5, 2011 23:09 by mogrguric
Initial Code
struct element {
int label;
element *left, *right;
};
typedef element *node;
typedef element bt;
//funkcija vraca roditelja cvora n
node ParentB(node n, bt *tree){
if(n==tree)
return NULL;
node starac=NULL;
if(tree->left) {
if(tree->left==n)
return tree;
else
starac=ParentB(n,tree->left);
}
if(tree->right) {
if(tree->right==n)
return tree;
else
starac=ParentB(n,tree->right);
}
return starac;
}
//funkcija vraca lijevo dijete cvora n. Ako čvor n nema lijevog djeteta, onda vraća null-cvor
node LeftChildB(node n, bt *tree){
if(!n || !n->left)
return NULL;
return n->left;
}
//funkcija vraca desno dijete cvora n. Ako čvor n nema desnog djeteta, onda vraća null-cvor
node RightChildB(node n, bt *tree){
if(!n || !n->right)
return NULL;
return n->right;
}
//funkcija koja vraca oznaku (vrijednost) koju sadrži cvor n
int LabelB(node n, bt *tree){
return n->label;
}
//mijenja oznaku cvora n u stablu tree na vrijednost x
void ChangeLabelB(int x, node n, bt *tree){
if(!n)
return;
n->label=x;
}
//funkcija koja vraca korijen binarnog stabla T. Ako je stablo prazno,onda vraća null-cvor
node RootB(bt *tree){
if(!tree)
return NULL;
return tree;
}
//Procedura dodaje x kao lijevo dijete cvora n.
//Ako cvor n već ima lijevo dijete, onda se javlja poruka pogreske.
void CreateLeftB(int x, node n, bt *tree){
if(n->left) {
cout << "Pogreska!" << endl;
return; }
node novi=new element;
n->left=novi;
novi->label=x;
novi->left=NULL;
novi->right=NULL;
}
//Procedura dodaje x kao desno dijete cvora n.
//Ako cvor n već ima lijevo dijete, onda se javlja poruka pogreske.
void CreateRightB(int x, node n, bt *tree){
if(n->right) {
cout << "Pogreska!" << endl;
return; }
node novi=new element;
n->right=novi;
novi->label=x;
novi->left=NULL;
novi->right=NULL;
}
//procedura brise cvor n, a s njim i sve njegove potomke iz stabla tree
void DeleteB(node n, bt *tree){
static bool prvo=false;
if(!prvo) {
node starac=ParentB(n,tree);
if(starac->left==n)
starac->left=NULL;
else
starac->right=NULL;
prvo=true; }
if(n->left)
DeleteB(n->left,tree);
if(n->right)
DeleteB(n->right,tree);
delete n;
}
//inicijalizira stablo tree s korijenom x
bt* InitB(int x, bt *tree){
tree=new element;
tree->label=x;
tree->left=NULL;
tree->right=NULL;
return tree;
}
Initial URL
Initial Description
Initial Title
bstablo pokazivac
Initial Tags
Initial Language
C++