/ Published in: C++
                    
                                        
Implementacija binarnog stabla pomocu polja
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
#include <iostream>
using namespace std;
struct element{
bool used;
int label;
};
struct tstablo
{
element stablo_polje[1000];
};
int ParentB(int n, tstablo *stablo_polje){
if (stablo_polje->stablo_polje[n].used==1)
return (n/2);
else
return (-1);
}
int LeftChildB(int n, tstablo *stablo_polje){
if (stablo_polje->stablo_polje[n].used==1)
if (stablo_polje->stablo_polje[n*2].used==1)
return (n*2);
else
return (-1);
else
return (-1);
}
int RightChildB(int n, tstablo *stablo_polje){
if (stablo_polje->stablo_polje[n].used==1)
if (stablo_polje->stablo_polje[n*2+1].used==1)
return (n*2+1);
else
return (-1);
else
return (-1);
}
int LabelB(int n, tstablo *stablo_polje){
if (stablo_polje->stablo_polje[n].used==1)
return ((int)stablo_polje->stablo_polje[n].label);
else
return (-1);
}
void ChangeLabelB(int x, int n, tstablo *stablo_polje){
if (stablo_polje->stablo_polje[n].used==1)
stablo_polje->stablo_polje[n].label=x;
else
cout<<"Navedeni cvor ne postoji."<<endl;
}
int RootB(tstablo *stablo_polje){
if (stablo_polje->stablo_polje[1].used==1)
return 1;
else
return (-1);
}
void CreateLeftB(int x, int n, tstablo *stablo_polje){
if (stablo_polje->stablo_polje[n].used==1)
if (stablo_polje->stablo_polje[n*2].used!=1){
stablo_polje->stablo_polje[n*2].label=x;
stablo_polje->stablo_polje[n*2].used=1;
}
else
cout<<"Pogreska! Lijevo dijete je vec kreirano! "<<endl;
else
cout<<"Navedeni cvor ne postoji."<<endl;
}
void CreateRightB(int x, int n, tstablo *stablo_polje){
if (stablo_polje->stablo_polje[n].used==1)
if (stablo_polje->stablo_polje[n*2+1].used!=1){
stablo_polje->stablo_polje[n*2+1].label=x;
stablo_polje->stablo_polje[n*2+1].used=1;
}
else
cout<<"Pogreska! Lijevo dijete je vec kreirano! "<<endl;
else
cout<<"Navedeni cvor ne postoji."<<endl;
}
void DeleteB(int n, tstablo *stablo_polje){
if(stablo_polje->stablo_polje[n].used==1)
{
if (stablo_polje->stablo_polje[n*2].used==1)
DeleteB(n*2, stablo_polje);
if (stablo_polje->stablo_polje[n*2+1].used==1)
DeleteB(n*2+1, stablo_polje);
stablo_polje->stablo_polje[n].used=0;
}
else
cout<<"Navedeni cvor ne postoji."<<endl;
}
void InitB(int x ,tstablo *stablo_polje) {
stablo_polje->stablo_polje[1].label=x;
stablo_polje->stablo_polje[1].used=1;
for (int i=2; i<1000; i++)
stablo_polje->stablo_polje[i].used=0;
}
Comments
 Subscribe to comments
                    Subscribe to comments
                
                