Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct elem
- {
- int inf;
- elem* bal;
- elem* jobb;
- };
- elem* gyoker=NULL;
- ifstream f("binfa.be.txt");
- void beolvas(elem* &gyoker)
- {
- int x;
- if(f>>x)
- {
- if(x!=0)
- {
- gyoker=new elem;
- gyoker->inf=x;
- beolvas(gyoker->bal);
- beolvas(gyoker->jobb);
- }
- else gyoker=NULL;
- }
- }
- void preorder(elem* gyoker)
- {
- cout<<gyoker->inf<<" ";
- if(gyoker->bal!=NULL) preorder(gyoker->bal);
- if(gyoker->jobb!=NULL) preorder(gyoker->jobb);
- }
- void inorder(elem* gyoker)
- {
- if(gyoker->bal!=NULL) inorder(gyoker->bal);
- cout<<gyoker->inf<<" ";
- if(gyoker->jobb!=NULL) inorder(gyoker->jobb);
- }
- void postorder(elem* gyoker)
- {
- if(gyoker->bal!=NULL) postorder(gyoker->bal);
- if(gyoker->jobb!=NULL) postorder(gyoker->jobb);
- cout<<gyoker->inf<<" ";
- }
- int levelsz(elem* gyoker)
- {
- if(gyoker==NULL) return 0;
- else
- if(gyoker->bal==NULL && gyoker->jobb==NULL) return 1;
- else return levelsz(gyoker->bal)+levelsz(gyoker->jobb);
- }
- int max(int x,int y)
- {
- if (x>y) return x;
- else return y;
- }
- int magassag(elem* gyoker)
- {
- if (gyoker==NULL) return 0;
- else
- return 1+max(magassag(gyoker->bal),magassag(gyoker->jobb));
- }
- int main()
- {
- beolvas(gyoker);
- preorder(gyoker);
- cout<<endl;
- inorder(gyoker);
- cout<<endl;
- postorder(gyoker);
- cout<<endl;
- cout<<"Levelek szama:"<<levelsz(gyoker);
- cout<<endl;
- cout<<"A graf magassaga:"<<magassag(gyoker)-1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement