193030

Zadacha izpit SAA Milen 3.3 (Binary tree)

Feb 6th, 2021 (edited)
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct vazel
  6. {
  7.     string zaglavie;
  8.     string avtor;
  9.     string izdatelstvo;
  10.     struct vazel* rchild;
  11.     struct vazel* lchild;
  12.  
  13. } *first = NULL;
  14.  
  15. void add(string zagl, string avt, string izd)
  16. {
  17.     struct vazel* t = root;
  18.     struct vazel* r = NULL, * p;
  19.    
  20.      if (first == NULL)
  21.     {
  22.         p = new vazel;
  23.         p->zaglavie = zagl;
  24.         p->avtor = avt;
  25.         p->izdatelstvo = izd;
  26.         p->lchild = p->rchild = NULL;
  27.         first = p;
  28.         return;
  29.     }
  30.     while (t != NULL)
  31.     {
  32.         r = t;
  33.         if (avt < t->data)
  34.             t = t->lchild;
  35.         else if (avt > t->data)
  36.             t = t->rchild;
  37.         else
  38.             return;
  39.     }
  40.     p = new vazel;
  41.     p->zaglavie = zagl;
  42.     p->avtor = avt;
  43.     p->izdatelstvo = izd;
  44.     p->lchild = p->rchild = NULL;
  45.     if (avt < r->avtor) r->lchild = p;
  46.     else r->rchild = p;
  47. }
  48.  
  49.  
  50. int main()
  51. {
  52.     string zaglavie, avtor, izdatelstvo;
  53.     add(zaglavie, avtor, izdatelstvo);
  54. }
  55.  
  56.  
Add Comment
Please, Sign In to add comment