|
|
|
implementacija pomocu polja
#include<iostream>
struct element{
int label;
int used;
};
struct bs {
struct element elements[10001];
};
typedef struct bs btree;
typedef int node;
node ParentB(node n,btree *t){
if(n=1){
return(-1);
}
else{
return((int)n/2);
}
}
node LeftChildB(node n,btree *t){
if(t->elements[n*2].used==1){
return n*2;
}
else{
return -1;
}
}
node RightChildB(node n,btree *t){
if(t->elements[n*2+1].used==1){
return (n*2+1);
}
else{
return -1;
}
}
int LabelB(node n,btree *t){
if(t->elements[n].used==1){
return (t->elements[n].label);
}
else{
// cout<<"Uneseni cvor ne postoji u stablu"<<endl;
}
}
void ChangeLabelB(int x, node n, btree *t){
if(t->elements[n].used==1){
t->elements[n].label=x;
}
else{
//cout<<"Uneseni cvor ne postoji u stablu"<<endl;
}
}
node RootB(btree *t){
if(t->elements[1].used==1){
return (t->elements[1].label);
}
else{
return -1;
}
}
void CreateLeftB(int x, node n, btree *t){
if(t->elements[n*2].used==1){
cout<<"Uneseni cvor već ima lijevo dijete"<<endl;
exit (0);
}
else{
t->elements[n*2].label=x;
t->elements[n*2].used=1;
}
}
void CreateRightB(int x, node n, btree *t){
if(t->elements[n*2+1].used==1){
cout<<"Uneseni cvor već ima lijevo dijete"<<endl;
exit (0);
}
else{
t->elements[n*2+1].label=x;
t->elements[n*2+1].used=1;
}
}
void DeleteB(node n, btree *t){
if(t->elements[n*2].used==1){
DeleteB(n*2, t);
}
if(t->elements[n*2+1].used==1){
DeleteB(n*2+1, t);
}
t->elements[n].used=0;
}
void InitB(int x, btree *t){
int i;
t->elements[1].label=x;
t->elements[1].used=1;
for(i=2;i<10000;i++){
t->elements[i].used=0;
}
}
int ExistLeftChildB(node n,btree *t){
if(t->elements[n*2].used==1){
return 1;
}
else{
return -1;
}
}
int ExistRightChildB(node n,btree *t){
if(t->elements[n*2+1].used==1){
return 1;
}
else{
return -1;
}
}




There are currently no comments for this snippet.