Advertisement
Guest User

laby AVL 1

a guest
May 23rd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.46 KB | None | 0 0
  1. // AVLTREE.cpp : Defines the entry point for the console application.
  2. //
  3. // BSTree.cpp: definiuje punkt wejścia dla aplikacji konsolowej.
  4. //
  5.  
  6. // Drzewo AVL
  7.  
  8. #include "stdafx.h"
  9. #include <iostream>
  10. #include <fstream>
  11. #include <string>
  12.  
  13. using namespace std;
  14.  
  15.  
  16.  
  17. struct node {
  18.     node *up;
  19.     node *left;
  20.     node *right;
  21.     int key;
  22.     int bf;
  23. };
  24.  
  25. void loadFile(node *& root);
  26.  
  27.  
  28. void printBT(string sp, string sn, node * root);
  29. node * minBST(node * root);
  30. node * maxBST(node * root);
  31. node * findBST(node * root, int value);
  32. void inorder(node * root);
  33. void saveFile(node *root);
  34. void addNode(node ** root, int value);
  35. node *create(int value);
  36. void deleteNode(node **root, int value);
  37. void removeBST(node * & root, int value);
  38.  
  39. int _tmain(int argc, _TCHAR* argv[])
  40. {
  41.  
  42.     node *root = new node;
  43.     root = NULL;
  44.     node *temp = new node;
  45.     temp = NULL;
  46.     int option, value;
  47.     do {
  48.         cout << "1. Add NOde" << endl;
  49.         cout << "2. Load File" << endl;
  50.         cout << "3. Store File" << endl;
  51.         cout << "4. Minimum" << endl;
  52.         cout << "5. Maximum" << endl;
  53.         cout << "6. Find value" << endl;
  54.         cout << "7. InOrder " << endl;
  55.         cout << "8. Usuwanie element" <<endl;
  56.         cout << "10. Print Tree" << endl;
  57.         cout << "\nPodaj opcje: ";
  58.         cin >> option;
  59.  
  60.  
  61.         switch (option) {
  62.         case 1:
  63.             cout << "Podaj liczbe: " << endl;
  64.             cin >> value;
  65.             addNode(&root, value);
  66.             cout << "root " << root << "wartość " << root->key;
  67.             break;
  68.         case 2:
  69.             loadFile(root);
  70.             break;
  71.         case 3:
  72.             saveFile(root);
  73.             break;
  74.         case 4:
  75.             cout << "Minimalny element to" << minBST(root)->key << endl;
  76.             break;
  77.         case 5:
  78.             cout << "Maksymalny element to" << maxBST(root)->key << endl;
  79.             break;
  80.         case 6:
  81.             cout << "Podaj liczbe: " << endl;
  82.             cin >> value;
  83.             temp = findBST(root, value);
  84.             if (temp != NULL) {
  85.                 cout << "Znaleziony element  " << temp->key << " pod adresem " << temp << endl;
  86.             }
  87.             else cout << "Nie ma takiego elementu";
  88.             break;
  89.         case 7:
  90.             inorder(root);
  91.             break;
  92.         case 8:
  93.             cout << "Podaj liczbe: " << endl;
  94.             cin >> value;
  95.             removeBST(root, value);
  96.             break;
  97.         case 10:
  98.              printBT(" "," " , root);
  99.              break;
  100.  
  101.         }
  102.  
  103.         cout << "\n\n";
  104.  
  105.     } while (option != 0);
  106.  
  107.  
  108.     return 0;
  109. }
  110.  
  111. void addNode(node ** root, int value) {
  112.     node *newNode = create(value);
  113.     node *temp ,*p;
  114.     temp=*root;
  115.     if (*root == NULL) {
  116.         *root = newNode;
  117.     }
  118.     else if (value < temp->key) {
  119.         if (temp->left) {
  120.             addNode(&temp->left, value);
  121.         }
  122.         else {
  123.             newNode->up = temp;
  124.             temp->left = newNode;
  125.         }
  126.     }
  127.     else if (value > temp->key) {
  128.         if (temp->right) {
  129.             addNode(&temp->right, value);
  130.         }
  131.         else {
  132.             newNode->up = temp;
  133.             temp->right = newNode;
  134.         }
  135.     }
  136.     int end=1;
  137.     p=temp;
  138.     temp=newNode;
  139.     // aktualnie p wskazuje na rodzica new Node
  140.     while (!p!=!end){   //xor
  141.         if(p->left==temp) p->bf+=1;
  142.         else if (p->right==temp)p->bf-=1;
  143.         if(p->bf==2 || p->bf==-2) end=0; // gdy nie zgodne wychodzi
  144.         else{
  145.             temp=p;
  146.         p=p->up;
  147.         }
  148.  
  149.     }
  150. }
  151.  
  152. void loadFile(node *&root) {
  153.     int idx;
  154.     fstream plik;
  155.     plik.open("AVLdata.txt", ios::in);
  156.     if (plik) {
  157.         while (!plik.eof()) {
  158.             plik >> idx;
  159.             addNode(&root, idx);
  160.         }
  161.         cout << "Plik wczytany" << endl;
  162.     }
  163.     plik.close();
  164. }
  165.  
  166. // rekurencyjny zapis do pliku IN ORDER
  167. void saveFile(node *root) {
  168.     ofstream plik;
  169.     plik.open("result.txt", ios::out | ios::app);
  170.  
  171.     if (root->left)
  172.         saveFile(root->left);
  173.  
  174.     plik << root->key << " ";
  175.  
  176.     if (root->right)
  177.         saveFile(root->right);
  178.  
  179.     plik.close();
  180. }
  181.  
  182. node * minBST(node * root) {
  183.     while (root->left) {
  184.         root = root->left;
  185.     }
  186.     return root;
  187. }
  188.  
  189. node * maxBST(node * root) {
  190.     while (root->right) {
  191.         root = root->right;
  192.     }
  193.     return root;
  194. }
  195.  
  196. node * findBST(node * root, int value)
  197. {
  198.     while (root) {
  199.         if (value == root->key)
  200.             return root;
  201.         else if (value < root->key)
  202.             root = root->left;
  203.         else if (value > root->key)
  204.             root = root->right;
  205.     }
  206.     return NULL;
  207. }
  208.  node *create(int value) {
  209.      node *newNode = new node;
  210.      newNode = new node;
  211.      newNode->up = NULL;
  212.      newNode->left = NULL;
  213.      newNode->right = NULL;
  214.      newNode->key = value;
  215.      return newNode;
  216. }
  217.  
  218. void printBT(string sp, string sn, node * root)
  219. {
  220.     string s;
  221.     string cr, cl, cp;
  222.     cr = cl = cp = "  ";
  223.     cr[0] = 218; cr[1] = 196;
  224.     cl[0] = 192; cl[1] = 196;
  225.     cp[0] = 179;
  226.  
  227.     if (root)
  228.     {
  229.         s = sp;
  230.         if (sn == cr) s[s.length() - 2] = ' ';
  231.         printBT(s + cp, cr, root->right);
  232.  
  233.         s = s.substr(0, sp.length() - 2);
  234.         cout << s << sn << root->key <<" "<<root->bf << endl;
  235.  
  236.         s = sp;
  237.         if (sn == cl) s[s.length() - 2] = ' ';
  238.         printBT(s + cp, cl, root->left);
  239.     }
  240. }
  241.  
  242. void inorder(node * root) {
  243.     if (root->left)
  244.         inorder(root->left);
  245.     cout << root->key << " ";
  246.     if (root->right)
  247.         inorder(root->right);
  248.  
  249. }
  250.  
  251.  
  252.  
  253. void removeBST(node * & root, int value)
  254. {
  255.     node *A = findBST(root, value);
  256.     node * B, *C;
  257.  
  258.     if (A)
  259.     {
  260.        
  261.  
  262.         //Jesli A ma obu potomków to B= nastepnik A ( tu zawsze min od A->right)
  263.         //jak ma jednego lub mniej to B=A
  264.        
  265.         if (A->left && A->right) B = minBST(A->right);
  266.         else B = A;
  267.        
  268.         // C wskazuje syna B lub NULL
  269.         if (B->left) C = B->left;
  270.         else C = B->right;
  271.  
  272.         // Jeśli syn B istnieje, to zastąpi C w drzewie dowiąznie up
  273.         if (C) C->up = B->up;
  274.  
  275.         // B zostaje zastąpione przez C w drzewie
  276.         if (!B->up) root = C;
  277.         else if (B == B->up->left) B->up->left = C;
  278.             else  B->up->right = C;
  279.  
  280.         // Jeśli B jest następnikiem A, to kopiujemy dane
  281.         if (B != A) A->key = B->key;
  282.  
  283.         delete B;
  284.  
  285.     }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement