Advertisement
RayaStark

Tree

Nov 17th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. struct tree
  4. {
  5. string ime;
  6. double sr_uspeh;
  7. tree *left;
  8. tree *right;
  9. };
  10. void CreateP(tree *T, string i, double x)
  11. {
  12. if(T != NULL)
  13. {
  14. if(x <= T->sr_uspeh)
  15. {
  16. if(T->left != NULL)
  17. {
  18. CreateP(T->left, i, x);
  19. }
  20. else
  21. {
  22. tree *z = new tree;
  23. z->ime = i;
  24. z->sr_uspeh = x;
  25. z->left = NULL;
  26. z->right = NULL;
  27. T->left = z;
  28. }
  29. }
  30. else
  31. {
  32. if(T->right != NULL)
  33. {
  34. CreateP(T->right, i, x);
  35. }
  36. else
  37. {
  38. tree *z = new tree;
  39. z->ime = i;
  40. z->sr_uspeh = x;
  41. z->left = NULL;
  42. z->right = NULL;
  43. T->right = z;
  44. }
  45. }
  46. }
  47. }
  48. void Print(tree *T)
  49. {
  50. if(T == NULL) return;
  51. Print(T->right);
  52. cout<<T->ime<<" - "<<T->sr_uspeh<<endl;
  53. Print(T->left);
  54. }
  55. int main()
  56. {
  57. int n;
  58. cout<<"Vuvedi broq uchenici: ";
  59. cin>>n;
  60. string ime;
  61. double sr_uspeh;
  62. tree *T = new tree;
  63. cout<<"Ime: ";
  64. cin>>ime;
  65. cout<<"Sreden uspeh: ";
  66. cin>>sr_uspeh;
  67. T->ime = ime;
  68. T->sr_uspeh = sr_uspeh;
  69. T->left = NULL;
  70. T->right = NULL;
  71. for(int i = 1; i < n; i++)
  72. {
  73. cout<<"Ime: ";
  74. cin>>ime;
  75. cout<<"Sreden uspeh: ";
  76. cin>>sr_uspeh;
  77. CreateP(T, ime, sr_uspeh);
  78. }
  79. Print(T);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement