Advertisement
Tooster

binTree

Aug 29th, 2015
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. /*  ========================
  8.         binary Tree class - just mention about author
  9.         by Tooster
  10.     ========================
  11. */
  12. template<class T>
  13. class binTree {
  14.     // #===---
  15.     //  Struktura węzła
  16.     // #===---
  17.     struct Node {
  18.         T data;
  19.         Node *left;
  20.         Node *right;
  21.         // #===---
  22.         //  konstruktur
  23.         // #===---
  24.         Node(T v = 0) : data(v), left(NULL), right(NULL) {}
  25.     };
  26.     Node* root;
  27.     long long size_v = 0;
  28.     long long sum_v = 0;    // !!!
  29.  
  30.     // #===---
  31.     //  metody:
  32.     //  isEmpty()   OK      = bool      - czy puste
  33.     //  size()      OK      = long long - rozmiar
  34.     //  sum()       OK  !!! = long long - zwraca sume (niebezpieczne ze względu na szablon)
  35.     //  add()       OK      = ---       - dodaje do drzewa
  36.     //  left()      OK      = T         - zwraca najmniejszy element
  37.     //  right()     OK      = T         - zwraca najwiekszy element
  38.     //  remLeft()   OK      = T         - usuwa najmniejszy
  39.     //  remRight()  OK      = T         - usuwa najwiekszy
  40.     // #===---
  41. public:
  42.     binTree() { root = NULL; }
  43.  
  44.     bool isEmpty() {
  45.         if (root == NULL) return true;
  46.         return false;
  47.     }
  48.  
  49.     long long size()const {
  50.         return (long long)size_v;
  51.     }
  52.     // !!!
  53.     long long sum()const {
  54.         return (long long)sum_v;
  55.     }
  56.  
  57.     void add(T v) {
  58.         size_v++;
  59.         sum_v += v;      // !!!
  60.  
  61.         if (isEmpty()) {
  62.             root = new Node;
  63.             root->data = v;
  64.             return;
  65.         }
  66.         Node *tmp = root;
  67.         Node *newNode = new Node;
  68.         newNode->data = v;
  69.         while (true) {
  70.             if (v < tmp->data) {
  71.                 if (tmp->left == NULL) { tmp->left = newNode;       return; }
  72.                 else tmp = tmp->left;
  73.             }
  74.             else {
  75.                 if (tmp->right == NULL) { tmp->right = newNode;     return; }
  76.                 else tmp = tmp->right;
  77.             }
  78.         }
  79.     }
  80.  
  81.     T left() {
  82.         if (isEmpty()) return NULL;
  83.         Node* tmp = root;
  84.         while (tmp->left != NULL) {
  85.             tmp = tmp->left;
  86.         }
  87.         return tmp->data;
  88.     }
  89.  
  90.     T right() {
  91.         if (isEmpty()) return NULL;
  92.         Node* tmp = root;
  93.         while (tmp->right != NULL) {
  94.             tmp = tmp->right;
  95.         }
  96.         return tmp->data;
  97.     }
  98.  
  99.     void remLeft() {
  100.         if (isEmpty()) return;
  101.         Node* tmp = root;
  102.         Node* parent = tmp;
  103.         // #===
  104.         //  ustawienie nowego korzenia - jeśli okarze się, że korzeń nie ma lewej gałęzi
  105.         // #===
  106.         if (root->left == NULL) {
  107.             root = tmp->right;
  108.             size_v--;
  109.             sum_v -= tmp->data;      // !!!
  110.             delete tmp;
  111.             return;
  112.         }
  113.         // #===---
  114.         //  przepina gałąź
  115.         // #===---
  116.         while (tmp->left != NULL) {
  117.             parent = tmp;
  118.             tmp = tmp->left;
  119.         }
  120.         parent->left = tmp->right;
  121.         size_v--;
  122.         sum_v -= tmp->data;         // !!!
  123.         delete tmp;
  124.         return;
  125.     }
  126.  
  127.     void remRight() {
  128.         if (isEmpty()) return;
  129.         Node* tmp = root;
  130.         Node* parent = tmp;
  131.  
  132.         if (root->right == NULL) {
  133.             root = tmp->left;
  134.             size_v--;
  135.             sum_v -= tmp->data;      // !!!
  136.             delete tmp;
  137.             return;
  138.         }
  139.  
  140.         while (tmp->right != NULL) {
  141.             parent = tmp;
  142.             tmp = tmp->right;
  143.         }
  144.         parent->right = tmp->left;
  145.         size_v--;
  146.         sum_v -= tmp->data;         // !!!
  147.         delete tmp;
  148.         return;
  149.     }
  150. };
  151.  
  152. // #===---
  153. //  Program kontrolny
  154. // #===---
  155. int main() {
  156.     binTree<int> drzewo;
  157.     printf("# dommands: add <a> / left / right /size /sum / remL / remR/ info/ empty/ stop #\n");
  158.     string cmd;
  159.     int a;
  160.     do {
  161.         printf("> ");
  162.         cin >> cmd;
  163.         if (cmd == "info") {
  164.             cout << " ============\n";
  165.             printf(" left: %d\n", drzewo.left());
  166.             printf(" right: %d\n", drzewo.right());
  167.             printf(" size: %d\n", drzewo.size());
  168.             printf(" sum: %d\n", drzewo.sum());
  169.         }
  170.         else if (cmd == "add") {
  171.             cin >> a;
  172.             drzewo.add(a);
  173.         }
  174.         else if (cmd == "empty") printf(" empty= %d\n", drzewo.isEmpty());
  175.         else if (cmd == "left") printf(" left= %d\n", drzewo.left());
  176.         else if (cmd == "right") printf(" right= %d\n", drzewo.right());
  177.         else if (cmd == "size") printf(" size= %d\n", drzewo.size());
  178.         else if (cmd == "sum") printf(" sum= %d\n", drzewo.sum());
  179.         else if (cmd == "remL") { printf(" removed= %d\n", drzewo.left());  drzewo.remLeft(); }
  180.         else if (cmd == "remR") { printf(" removed= %d\n", drzewo.right());  drzewo.remRight(); }
  181.         else printf("# Nieznana komenda\n");
  182.     } while (cmd != "stop");
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement