Endil

lab6

Jul 1st, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.41 KB | None | 0 0
  1. // lab6.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <vector>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10.  
  11. template <typename T>
  12. struct entry {
  13.     int key;
  14.     T value;
  15. };
  16.  
  17. template <typename T>
  18. struct node {
  19.     int key;
  20.     T value;
  21.     node<T> *left, *right;
  22.     int height;
  23. };
  24.  
  25. template <typename T>
  26. class bst {
  27.  
  28. private:
  29.     node<T>* root;
  30.     int node_count;
  31.  
  32. protected:
  33.     void height_fix() {
  34.         this.height = max(this->left.height, this->right.height) + 1;
  35.         return;
  36.     }
  37.  
  38.     node<T>* rotate_r() {
  39.         node<T>* new_root = this.left;
  40.         this.left = new_root.right;
  41.         new_root.right = this;
  42.         this.height_fix();
  43.         new_root.hight_fix();
  44.         return new_root;
  45.     }
  46.  
  47.     node<T>* rotate_l() {
  48.         node<T>* new_root = this.right;
  49.         this.right = new_root.left;
  50.         new_root.left = this;
  51.         this.height_fix();
  52.         new_root.hight_fix();
  53.         return new_root;
  54.     }
  55.  
  56.     node<T>* balance() {
  57.         this.height_fix();
  58.         int bfactor = this->left.height - this->right.height;
  59.         if (bfactor == 2) {
  60.             if (this->right->left.height > this->right->right.height)
  61.                 this.right = this->right.rotate_r;
  62.             return this.rotate_l();
  63.         }
  64.         if (bfactor == -2) {
  65.             if (this->right->left.height < this->right->right.height)
  66.                 this.left = this->left.rotate_l;
  67.             return this.rotate_r();
  68.         }
  69.         return this;
  70.     }
  71.  
  72.     node<T>* balance(const vector<node<T>* > path) {
  73.         for (int i = path.size() - 2; i + 1; i--)
  74.             path[i].left == path[i + 1] ? path[i].left = path[i + 1].balance() : path[i].right = path[i + 1].balance();
  75.         root = root.balance();
  76.         return root;
  77.     }
  78.  
  79. public:
  80.     bst() {
  81.         root = NULL;
  82.         node_count = 0;
  83.     }
  84.  
  85.     void insert(const int key, const T value) {
  86.         node<T>* ins;
  87.         ins.key = key;
  88.         ins.value = value;
  89.         ins.left = ins.right = NULL;
  90.         ins.height = 1;
  91.         if (!cur) {
  92.             root = ins;
  93.             node_count = 1;
  94.             return;
  95.         }
  96.         node<T>* cur = root;
  97.         vector<node<T>* > path;
  98.         while (!cur) {
  99.             path.push_back(cur);
  100.             cur.key > key ? cur = cur->left : cur = cur->right;
  101.         }
  102.         path.back.key > key ? path.back.left = ins : path.back.right = ins;
  103.         root = this.balance(path&);
  104.         node_count++;
  105.         return;
  106.     }
  107.  
  108.     node<T>* find(const int key, node<T>* cur = root) {
  109.         if (!cur)
  110.             return NULL;
  111.         if (key == cur.key)
  112.             return cur;
  113.         key < cur.key ? find(key, cur.left) : find(key, cur.right);
  114.     }
  115.    
  116.     node<T>* find(const node<T>* target, node<T>* cur = root) {
  117.         if (cur.left == target || cur.right == target)
  118.             return cur;
  119.         target.key < cur.key ? find(target, cur.left) : find(target, cur.right);
  120.     }
  121.  
  122.     void remove(const int key) {
  123.         if (!node_count)
  124.             return;
  125.         node<T>* cur = root;
  126.         vector<node<T>* > path;
  127.         do {
  128.             path.push_back(cur);
  129.             cur.key > key ? cur = cur->left : cur = cur->right;
  130.         } while (key != cur.key && !cur);
  131.         if (!cur)
  132.             return;
  133.         if (!cur.left || !cur.right) {
  134.             if (cur.left = cur.right) {
  135.                 path.back.left == cur ? path.back.left = NULL : path.back.right = NULL;
  136.                 delete cur;
  137.                 root = this.balance(path&);
  138.                 return;
  139.             }
  140.             if (!cur.left)
  141.                 path.back.left == cur ? path.back.left = cur.left : path.back.right = cur.left;
  142.             else
  143.                 path.back.left == cur ? path.back.left = cur.right : path.back.right = cur.right;
  144.             delete cur;
  145.             root = this.balance(path&);
  146.             return;
  147.         }
  148.         node<T> cur_p = path.back;
  149.         node<T> max = max.left;
  150.         do {
  151.             path.push_back(max);
  152.             max = max.right;
  153.         } while (!cur);
  154.         path.back.right = NULL;
  155.         cur_p.left == cur ? cur_p.left = max : cur_p.right = max;
  156.         max.left = cur.left;
  157.         max.right = cur.right;
  158.         delete cur;
  159.         root = this.balance(path&);
  160.         return;
  161.     }
  162.  
  163.     /*void remove(int key) {
  164.         if (!node_count)
  165.             return;
  166.         node<T>* del = find(key);
  167.         if (!del)
  168.             return;
  169.         node<T>* del_p = find(del);
  170.         if (!del.left || !del.right) {
  171.             if (del.left == del.right) {
  172.                 del_p.left == del ? del_p.left = NULL : del_p.right = NULL;
  173.                 delete del;
  174.                 return;
  175.             }
  176.             if (!del.left)
  177.                 del_p.left == del ? del_p.left = del.right : del_p.right = del.right;
  178.             else
  179.                 del_p.left == del ? del_p.left = del.left : del_p.right = del.left;
  180.             delete del;
  181.             return;
  182.         }
  183.         node<T> *cur = del.left, *cur_p = del;
  184.         while (cur->right) {
  185.             cur_p = cur;
  186.             cur = cur.right;
  187.         }
  188.         cur_p.right = cur.left;
  189.         del_p.left == del ? del_p.left = cur : del_p.right = cur;
  190.         cur.left = del.left;
  191.         cur.right = del.right;
  192.         delete del;
  193.     }
  194.  
  195.     void insert(int key, T value) {
  196.         node<T> *cur = new node<T>;
  197.         cur.key = key;
  198.         cur.value = value;
  199.         cur.left = cur.right = NULL;
  200.         if (!root) {
  201.             root = cur;
  202.             node_count = 1;
  203.             return;
  204.         }
  205.         node<T>* cur_xst = root;
  206.         while (true){
  207.             if (key == cur_xst.key) {
  208.                 cur_xst.value = value;
  209.                 break;
  210.             }
  211.             if (key < cur_xst.key) {
  212.                 if (!cur_xst.left) {
  213.                     cur_xst.left = cur;
  214.                     break;
  215.                 }
  216.                 else
  217.                     cur_xst = cur_xst.left;
  218.             else {
  219.                 if (!cur_xst.right) {
  220.                     cur_xst.right = cur;
  221.                     break;
  222.                 }
  223.                 else
  224.                     cur_xst = cur_xst.right;
  225.             }
  226.             }
  227.         }
  228.         node_count++;
  229.     }
  230.  
  231.     void insert(vector v, int idx_b = 0, int idx_e = v.size()-1) {
  232.         int avg = (idx_b + idx_e) /2;
  233.         this.insert(v[avg].key, v[avg].value);
  234.         this.insert(v, idx_b, avg);
  235.         this.insert(v, avg, idx_e);
  236.     }*/
  237. };
  238.  
  239. int _tmain(int argc, _TCHAR* argv[])
  240. {
  241.     bst<int> t;
  242.     for (int i = 1; i <= 6; i++) {
  243.         t.insert(i, i);
  244.     }
  245.     t.remove(5);
  246.     cout << t.find(3).value << endl;
  247.  
  248.     return 0;
  249. }
Advertisement
Add Comment
Please, Sign In to add comment