Advertisement
GerexD

binfa din

Dec 6th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. struct elem
  5. {
  6. int inf;
  7. elem* bal;
  8. elem* jobb;
  9. };
  10. elem* gyoker=NULL;
  11. ifstream f("binfa.be.txt");
  12. void beolvas(elem* &gyoker)
  13. {
  14. int x;
  15. if(f>>x)
  16. {
  17. if(x!=0)
  18. {
  19. gyoker=new elem;
  20. gyoker->inf=x;
  21. beolvas(gyoker->bal);
  22. beolvas(gyoker->jobb);
  23. }
  24. else gyoker=NULL;
  25. }
  26. }
  27. void preorder(elem* gyoker)
  28. {
  29. cout<<gyoker->inf<<" ";
  30. if(gyoker->bal!=NULL) preorder(gyoker->bal);
  31. if(gyoker->jobb!=NULL) preorder(gyoker->jobb);
  32. }
  33. void inorder(elem* gyoker)
  34. {
  35. if(gyoker->bal!=NULL) inorder(gyoker->bal);
  36. cout<<gyoker->inf<<" ";
  37. if(gyoker->jobb!=NULL) inorder(gyoker->jobb);
  38. }
  39. void postorder(elem* gyoker)
  40. {
  41. if(gyoker->bal!=NULL) postorder(gyoker->bal);
  42. if(gyoker->jobb!=NULL) postorder(gyoker->jobb);
  43. cout<<gyoker->inf<<" ";
  44. }
  45. int levelsz(elem* gyoker)
  46. {
  47. if(gyoker==NULL) return 0;
  48. else
  49. if(gyoker->bal==NULL && gyoker->jobb==NULL) return 1;
  50. else return levelsz(gyoker->bal)+levelsz(gyoker->jobb);
  51. }
  52. int max(int x,int y)
  53. {
  54. if (x>y) return x;
  55. else return y;
  56. }
  57. int magassag(elem* gyoker)
  58. {
  59. if (gyoker==NULL) return 0;
  60. else
  61. return 1+max(magassag(gyoker->bal),magassag(gyoker->jobb));
  62. }
  63.  
  64. int main()
  65. {
  66. beolvas(gyoker);
  67. preorder(gyoker);
  68. cout<<endl;
  69. inorder(gyoker);
  70. cout<<endl;
  71. postorder(gyoker);
  72. cout<<endl;
  73. cout<<"Levelek szama:"<<levelsz(gyoker);
  74. cout<<endl;
  75. cout<<"A graf magassaga:"<<magassag(gyoker)-1;
  76.  
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement