鱼C论坛

 找回密码
 立即注册
查看: 1523|回复: 0

[技术交流] 一个不太成熟的伸展树

[复制链接]
发表于 2014-5-1 00:02:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
源代码
  1. #include <iostream>
  2. #include <string>

  3. template<typename Index, typename Value>
  4. class BST
  5. {
  6. protected:
  7.         struct Node
  8.         {
  9.                 Node (Index k, Value v)
  10.                 :key(k), val(v), left(0), right(0)
  11.                 {}
  12.                 Index key;                  // key
  13.         Value val;                  // associated data
  14.         Node *left, *right;         // left and right subtrees
  15.         };
  16. public:
  17.         BST(): root(0) {}

  18.         bool contains(Index& key) {
  19.                 return get(key) != 0;
  20.         }

  21.         // return value associated with the given key
  22.         // if no such value, return null
  23.         Value get(Index key) {
  24.                 root = splay(root, key);
  25.                 int cmp = compare(key, root->key);
  26.                 if (!cmp) return root->val;
  27.                 else      return 0; //Error for std::string
  28.         }

  29.         /*************************************************************************
  30.         *  splay insertion
  31.         *************************************************************************/
  32.         void put(Index key, Value val) {
  33.                 // splay key to root
  34.                 if (!root)
  35.                 {
  36.                         root = new Node(key, val);
  37.                         return;
  38.                 }

  39.                 root = splay(root, key);

  40.                 int cmp = compare(key, root->key);

  41.                 // Insert new node at root
  42.                 if (cmp)
  43.                 {
  44.                         Node * n = new Node(key, val);
  45.                         if (cmp < 0)
  46.                         {
  47.                                 n->left     = root->left;
  48.                                 n->right    = root;
  49.                                 root->left  = 0;
  50.                         }
  51.                         else
  52.                         {
  53.                                 n->right    = root->right;
  54.                                 n->left     = root;
  55.                                 root->right = 0;
  56.                         }
  57.                         root = n;
  58.                 }

  59.                 // It was a duplicate key. Simply replace the value
  60.                 else
  61.                         root->val = val;
  62.         }

  63.         /*************************************************************************
  64.         *  splay insertion
  65.         *************************************************************************/
  66.         /* This splays the key, then does a slightly modified Hibbard deletion on
  67.          * the root (if it is the node to be deleted; if it is not, the key was
  68.          * not in the tree). The modification is that rather than swapping the
  69.          * root (call it node A) with its successor, it's successor (call it Node B)
  70.          * is moved to the root position by splaying for the deletion key in A's
  71.          * right subtree. Finally, A's right child is made the new root's right
  72.          * child.
  73.          */
  74.         void remove(Index key) {
  75.                 if (!root) return; // empty tree

  76.                 root = splay(root, key);

  77.                 if (compare(key, root->key)) return;
  78.                 // It wasn't in the tree to remove

  79.                 if (!root->left)
  80.                         root = root->right;
  81.                 else
  82.                 {
  83.                         Node * x = root->right;
  84.                         root = root->left;
  85.                         splay(root, key);
  86.                         root->right = x;
  87.                 }
  88.         }

  89.         int size() { return size(root); }
  90.         int height() { return height(root); }
  91. private:
  92.         Node * root;
  93.         int size(Node * x) {
  94.                 if (!x) return 0;
  95.                 return 1 + size(x->left) + size(x->right);
  96.         }
  97.         int height(Node * x) {
  98.                 if (!x) return 0;
  99.                 return 1 + (height(x->left) > height(x->right)
  100.                                           ? height(x->left) : height(x->right));
  101.         }

  102.         /*************************************************************************
  103.         *  splay function
  104.         *************************************************************************/
  105.         // splay key in the tree rooted at Node h. If a node with that key exists,
  106.         //   it is splayed to the root of the tree. If it does not, the last node
  107.         //   along the search path for the key is splayed to the root.
  108.         Node * splay(Node * h, Index key)
  109.         {
  110.                 if (!h) return 0;

  111.                 int cmp1 = compare(key, h->key);

  112.                 if (!cmp1) return h;

  113.                 bool left = cmp1 < 0 ? true : false;
  114.                 Node * x = left ? h->left : h->right;
  115.                 // key not in tree, so we're done
  116.                 if (!x) return h;

  117.                 int cmp2 = compare(key, x->key);
  118.                 if      (cmp2 < 0)
  119.                 {
  120.                         x->left = splay(x->left, key);
  121.                         if (left)
  122.                                 h = rotateRight(h);
  123.                         else if (x->left)
  124.                                 x = rotateRight(x);
  125.                 }
  126.                 else if (cmp2 > 0)
  127.                 {
  128.                         x->right = splay(x->right, key);
  129.                         if (!left)
  130.                                 h = rotateLeft(h);
  131.                         else if (x->right)
  132.                                 x = rotateLeft(x);
  133.                 }

  134.                 x = left ? h->left : h->right; //Revise
  135.                 if (!x) return h;
  136.                 return left ? rotateRight(h) : rotateLeft(h);
  137.         }

  138.         /*************************************************************************
  139.         *  helper BST functions
  140.         *************************************************************************/

  141.         // right rotate
  142.         Node * rotateRight(Node * h) {
  143.                 Node * x = h->left;
  144.                 h->left  = x->right;
  145.                 x->right = h;
  146.                 return x;
  147.         }

  148.         // left rotate
  149.         Node * rotateLeft(Node * h) {
  150.                 Node * x = h->right;
  151.                 h->right = x->left;
  152.                 x->left  = h;
  153.                 return x;
  154.         }

  155.         int   compare(Index& v, Index& w) const
  156.         {
  157.                 if     (v > w) return  1;
  158.                 else if(v < w) return -1;
  159.                 else           return  0;
  160.         }
  161. };

  162. int main()
  163. {
  164.         BST<std::string, std::string> st;

  165.         st.put("www.cs.princeton.edu",   "128.112.136.11");
  166.         st.put("www.cs.princeton.edu",   "128.112.136.35");
  167.         st.put("www.princeton.edu",      "128.112.130.211");
  168.         st.put("www.math.princeton.edu", "128.112.18.11");
  169.         st.put("www.yale.edu",           "130.132.51.8");
  170.         st.put("www.amazon.com",         "207.171.163.90");
  171.         st.put("www.simpsons.com",       "209.123.16.34");
  172.         st.put("www.stanford.edu",       "171.67.16.120");
  173.         st.put("www.google.com",         "64.233.161.99");

  174.         std::cout << st.get("www.cs.princeton.edu") << std::endl;
  175.         std::cout << st.get("www.princeton.edu") << std::endl;
  176.         st.remove("www.yale.edu");
  177.         st.remove("www.princeton.edu");
  178. }
复制代码


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 15:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表