鱼C论坛

 找回密码
 立即注册
查看: 2823|回复: 4

[技术交流] C++版本的一个二叉搜索树(未平衡)

[复制链接]
发表于 2014-2-26 22:48:34 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 andalousie 于 2014-3-1 17:18 编辑

还以为自己要卡在BST这儿了呢。上回的错误找到了,因为我没有用引用类型,导致了我的变量用完直接被销毁了。:ton:
是在不幸,我的g++编译器出问题了,只能回归vc6.0了,也好。vc6.0的debug功能挺不错的。
queue.h
  1. #if !defined (QUEUE_H)
  2. #define QUEUE_H
  3. #include <cassert>

  4. const int maxPuts=16;

  5. template<typename T>
  6. class Queue
  7. {
  8. public:
  9.         Queue();
  10.         
  11.         T    pop()
  12.         {
  13.                 assert (_getIdx<_putIdx);
  14.                 ++_getIdx;
  15.                 return _arr [(_getIdx-1) % maxPuts];
  16.         }
  17.         
  18.         void push(T x)
  19.         {
  20.                 assert (_putIdx < maxPuts + _getIdx);
  21.                 _arr [_putIdx % maxPuts] = x;
  22.                 ++_putIdx;
  23.         }
  24.         
  25.         bool empty() { return _putIdx == _getIdx; }
  26. private:
  27.         T   _arr [maxPuts];
  28.         int _putIdx;
  29.         int _getIdx;
  30. };

  31. template<typename T>
  32. Queue<T>::Queue()
  33. : _putIdx(0),
  34.   _getIdx(0)
  35. {}

  36. #endif
复制代码
BST.h
  1. #if !defined (BST_H)
  2. #defind BST_H
  3. #include "queue.h"

  4. template<typename Index, typename Value>
  5. class BST
  6. {
  7. private:
  8.         struct Node
  9.         {
  10.                 Node(Index k, Value v, int n)
  11.                 :key(k), val(v), N(n), left(0), right(0)
  12.                 {}
  13.                 Index key;
  14.                 Value val;
  15.                 Node *left, *right;
  16.                 int   N;
  17.         };
  18.         Node *root;
  19. public:
  20.         BST():root(0){}
  21.         int   size()
  22.         {
  23.                 return size(root);
  24.         }

  25.         Index min()
  26.         {
  27.                 return min(root)->key;
  28.         }

  29.         Index floor(Index key)
  30.         {
  31.                 Node *x = floor(root, key);
  32.                 if(!x) return 0;
  33.                 return x->key;
  34.         }

  35.         Value get(Index key)
  36.         {
  37.                 return get(root, key);
  38.         }

  39.         void put(Index key, Value val)
  40.         { // Search for key. Update value if found; grow table if new.
  41.                 root = put(root, key, val);
  42.         }

  43.         Value select(int k)
  44.         {
  45.                 return select(root, k)->key;
  46.         }

  47.         int rank(Index key)
  48.         {
  49.                 return rank(key, root);
  50.         }

  51.         void delMin()
  52.         {
  53.                 root = delMin(root);
  54.         }

  55.         void del(Index key)
  56.         {
  57.                 root = del(root, key);
  58.         }

  59.         void iterate()
  60.         {
  61.                 Queue<Index> q;
  62.                 inorder(root, q);
  63.                 while(!q.empty())
  64.                 {
  65.                         Index key =  q.pop();
  66.                         std::cout << get(key);
  67.                 }
  68.         }
  69. private:
  70.         int   size(Node *x)
  71.         {
  72.                 if(!x) return 0;
  73.                 else   return x->N;
  74.         }

  75.         Node* min(Node* x)
  76.         {
  77.                 if(!(x->left)) return x;
  78.                 return min(x->left);
  79.         }

  80.         Node* floor(Node* x, Index& key)
  81.         {
  82.                 if(!x) return 0;
  83.                 int cmp = compare(key, x->key);

  84.                 if (cmp == 0) return x;

  85.                 if (cmp < 0) return floor(x->left, key);

  86.                 Node* t = floor(x->right, key);
  87.                 if(t) return t;
  88.                 else return x;
  89.         }
  90.         
  91.         Value get(Node *x, Index& key)
  92.         { // Return value associated with key in the subtree rooted at x;
  93.           // return null if key not present in subtree rooted at x.
  94.                 if (!x) return 0;
  95.                 int cmp = compare(key, x->key);
  96.                 if (cmp < 0) return get(x->left, key);
  97.                 else if (cmp > 0) return get(x->right, key);
  98.                 else return x->val;
  99.         }
  100.         
  101.         Node* put(Node* x, Index key, Value val)
  102.         {
  103.                 if(!x)
  104.                 {
  105.                         x = new Node(key, val, 1);
  106.                         return x;
  107.                 }
  108.                 int cmp = compare(key, x->key);
  109.                 if(cmp < 0)
  110.                         x->left  = put(x->left, key, val);
  111.                 else if(cmp > 0)
  112.                         x->right = put(x->right, key, val);
  113.                 else
  114.                         x->val = val;
  115.                 x->N = size(x->left) + size(x->right) + 1;
  116.                 return x;
  117.         }

  118.         Node* select(Node* x, int& k)
  119.         {   // Return Node containing key of rank k.
  120.                 if(!x) return 0;
  121.                 int t = size(x->left);
  122.                 if (t > k) return select(x->left, k);
  123.                 else if (t < k) return select(x->right, k-t-1);
  124.                 else return x;
  125.         }

  126.         int   rank(Index& key, Node* x)
  127.         {
  128.                 if(!x) return 0;
  129.                 int cmp = compare(key, x->key);
  130.                 if (cmp < 0) return rank(key, x->left);
  131.                 else if (cmp > 0) return 1 + size(x->left) + rank(key, x->right);
  132.                 else return size(x->left);
  133.         }

  134.         Node* delMin(Node* x)
  135.         {
  136.                 if (!(x->left)) return x->right;
  137.                 x->left = delMin(x->left);
  138.                 x->N = 1 + size(x->left) + size(x->right);
  139.                 return x;
  140.         }

  141.         Node* del(Node* x, Index& key)
  142.         {
  143.                 if(!x) return 0;
  144.                 int cmp = compare(key, x->key);
  145.                 /* search for key */
  146.                 if (cmp < 0) x->left = del(x->left, key);
  147.                 else if (cmp > 0) x->right = del(x->right, key);
  148.                 else
  149.                 {
  150.                         if (!(x->right)) return x->left;            //no right child
  151.                         if (!(x->left)) return x->right;           ;//no left child

  152.                         /* replace with successor */
  153.                         Node* t = x;
  154.                         x = min(t->right);
  155.                         x->right = delMin(t->right);
  156.                         x->left = t->left;
  157.                 }
  158.                 x->N = size(x->left) + size(x->right) + 1; //update subtree counts
  159.                 return x;
  160.         }
  161.         
  162.         int   compare(Index& v, Index& w) const
  163.         {
  164.                 if(v > w)      return  1;
  165.                 else if(v < w) return -1;
  166.                 return 0;
  167.         }

  168.         void inorder(Node* x, Queue<Index>& q)
  169.         {
  170.                 if(!x) return;
  171.                 inorder(x->left, q);
  172.                 q.push(x->key);
  173.                 inorder(x->right, q);
  174.         }
  175. };

  176. #endif
复制代码
main.cpp
  1. #include <iostream>
  2. #include "BST.h"

  3. int main()
  4. {
  5.         BST<int, int> bst;
  6.         bst.put(5, 1);
  7.         bst.put(9, 0);
  8.         bst.put(2, 3);
  9.         bst.put(1, 5);
  10.         bst.put(4, 2);
  11.         bst.put(8, 3);
  12.         std::cout << bst.rank(1);
  13.         std::cout << bst.rank(5);
  14.         std::cout << bst.rank(9);
  15.         std::cout << std::endl;
  16.         bst.iterate();
  17.         std::cout << std::endl;
  18.         bst.delMin();
  19.         bst.del(4);
  20.         bst.iterate();
  21.         return 0;
  22. }
复制代码
就是分享一下。也可能会有问题,大家提出来便是。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-2-27 00:13:31 | 显示全部楼层
错误找到了,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-27 00:15:11 | 显示全部楼层
谢谢分享经验
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-12 09:23:26 | 显示全部楼层
谢谢楼主分享!!!!!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-16 11:36:23 From FishC Mobile | 显示全部楼层
回帖是一种美德
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-7 18:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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