Skip to content

Instantly share code, notes, and snippets.

@epson121
Last active September 24, 2015 15:07
Show Gist options
  • Save epson121/766358 to your computer and use it in GitHub Desktop.
Save epson121/766358 to your computer and use it in GitHub Desktop.
Binarno stablo -> polja
#include <iostream>
using namespace std;
struct cvor
{
int oznaka;
int PrvoDijete;
int SljedeciBrat;
};
struct tree
{
cvor element[1000];
int first;
};
int ParentB(int x, tree *stablo)
{
if(x>0) return x-1;
else
{
if(x==0) return -1;
else cout<<"Cvor ne postoji"<<endl;
}
}
int FirstChildB(int x, tree *stablo)
{
if(x<0) return -1;
else
{
if(x>=0 && ((x+1)!=0)) return stablo->element[x].PrvoDijete;
else return -1;
}
}
int NextSiblingB(int x, tree *stablo)
{
if(x<0) return -1;
else
{
if(stablo->element[x].SljedeciBrat!=-1)return stablo->element[x].SljedeciBrat;
else return -1;
}
}
int LabelB(int x, tree *stablo)
{
if(x<0) cout<<"Pogreska"<<endl;
else
{
if(stablo->element[x].oznaka!=-1) return -1;
else return stablo->element[x].oznaka;
}
}
int RootB(tree *stablo)
{
return stablo->element[0].oznaka;
}
int CreateB(int x, int y, tree *stablo)
{
stablo->element[stablo->first].oznaka=x;
stablo->element[stablo->first].PrvoDijete=y;
stablo->first++;
}
int ChangeLabelB(int x, int y, tree *stablo)
{
if(x<0) return -1;
else stablo->element[y].oznaka=x;
}
int DeleteB(int x, tree *stablo)
{
if(x<0) return -1;
else
{
if(stablo->element[x].PrvoDijete>0) DeleteB(stablo->element[x].PrvoDijete,stablo);
else
{
stablo->element[x].oznaka=-1;
stablo->first--;
}
}
}
tree *InitB(int x,tree *stablo)
{
delete [] stablo->element;
stablo=new tree;
stablo->first=0;
stablo->element[x].oznaka=x;
stablo->first++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment