Last active
September 24, 2015 16:47
-
-
Save epson121/778608 to your computer and use it in GitHub Desktop.
binarno stablo -> pokazivaci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
struct cvor | |
{ | |
int oznaka; | |
int PrvoDijete; | |
int SljedeciBrat; | |
bool koristeno; | |
}; | |
struct tree | |
{ | |
cvor element[1000]; | |
int first; | |
}; | |
int pom; | |
bool nadjen = false; | |
int ParentB(int x, tree *stablo) | |
{ | |
if(x == stablo->first) | |
{ | |
nadjen = true; | |
return -1; | |
}; | |
for(int i=0; i<10000; i++) | |
{ | |
if(stablo->element[i].PrvoDijete==n && stablo->element[i].koristeno == true) | |
{ | |
nadjen = true; | |
cout << "Roditelj cvora: " << i << endl; | |
return i; | |
} | |
else if(stablo->element[i].SljedeciBrat==n && stablo->element[i].koristeno == true) | |
{ | |
ParentB(i,stablo); | |
}; | |
}; | |
if(nadjen == false) | |
{ | |
cout << "Nije nadjen (ili ste unijeli korijen)!" << endl; | |
return -1; | |
} | |
} | |
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<<"Error"<<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; | |
} | |
void CreateB(int x, int y, tree *stablo) | |
{ | |
int i; | |
if(stablo->element[y].PrvoDijete==-1) | |
{ | |
stablo->element[y].PrvoDijete=x; | |
stablo->element[x].PrvoDijete=-1; | |
stablo->element[x].SljedeciBrat=-1; | |
stablo->element[x].koristeno = true; | |
cout << "Label: "; | |
cin >> stablo->element[x].oznaka; | |
} | |
else | |
{ | |
i=stablo->element[y].PrvoDijete; | |
while(stablo->element[i].SljedeciBrat!=-1) | |
{ | |
i=stablo->element[i].SljedeciBrat; | |
}; | |
stablo->element[i].SljedeciBrat=x; | |
stablo->element[x].PrvoDijete=-1; | |
stablo->element[x].SljedeciBrat=-1; | |
stablo->element[x].koristeno = true; | |
cout << "Label: "; | |
cin >> stablo->element[x].label; | |
}; | |
}; | |
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++; | |
stablo->element[x].koristeno = true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment