[LOG] Size Balanced Tree
来源:互联网 发布:淘宝买话费没发货 编辑:程序博客网 时间:2024/05/17 22:53
一个SBT模板。(平衡BST)
据说插入效率比红黑树还高= =
其中Maintain函数的效率可以进一步改进
(话说也就一个Maintain能优化其它的都跟标准BST一毛一样= =)
想用的话直接拷贝过去用把= =
API都很清楚把= =
//KeyType must be comparable!!!!!!!//overload both operator '>' and '<'//or you will get a compilation errortemplate<typename KeyType,typename ValType>class Size_Balanced_Tree { struct node { int Size;//important!!! //cnt the number of child nodes KeyType key; ValType val; node *lchild, *rchild; node(KeyType k, ValType v) { lchild = 0, rchild = 0; Size = 0; val = v; key = k; } }; node *root; inline int SizeOf(node *&target) { return target ? target->Size : 0; } void Lrotate(node *&target) { node *temp = target->lchild; target->lchild = temp->rchild; temp->rchild = target; temp->Size = target->Size; target->Size = SizeOf(target->lchild) + SizeOf(target->rchild) + 1; target = temp; } void Rrotate(node *&target) { node *temp = target->rchild; target->rchild = temp->lchild; temp->lchild = target; temp->Size = target->Size; target->Size = SizeOf(target->lchild) + SizeOf(target->rchild) + 1; target = temp; } void Maintain(node *& target) { if (target->lchild != NULL) { if (SizeOf(target->rchild) < SizeOf(target->lchild->lchild)) Lrotate(target); else if (SizeOf(target->rchild) < SizeOf(target->lchild->rchild)) { Rrotate(target->lchild); Lrotate(target); }Maintain(target->lchild); } if (target->rchild != NULL) { if (SizeOf(target->lchild) < SizeOf(target->rchild->rchild)) Rrotate(target); else if (SizeOf(target->lchild) < SizeOf(target->rchild->lchild)) { Lrotate(target->rchild); Rrotate(target); }Maintain(target->rchild); } return; } void insert(node *&target, KeyType &key, ValType &val) { if (!target) { target = new node(key, val); return; } target->Size++; if (key > target->key)insert(target->rchild, key, val); else if (key != target->key)insert(target->lchild, key, val); else return; Maintain(target); } ValType query(node *&target, KeyType key) { if (target == NULL) return 0; if (target->key == key)return target->val; else if (target->key < key)return query(target->rchild, key); else return query(target->lchild, key); }public: Size_Balanced_Tree() { root = 0; }//returns the value of the node with the max key in the whole tree ValType max() { node *scout = root; while (scout->rchild != NULL) scout = scout->rchild; return scout->val; }//= = ValType min() { node *scout = root; while (scout->lchild != NULL) scout = scout->lchild; return scount->val; }//returns the rank(from min) of the key in the tree int rank(KeyType key) { node *scout = root; int r = 0; while (scout->key != key) { if (scout->key < key) { r += scout->lchild->Size + 1; scout = scout->rchild; } else if (scout->key > key)scout = scout->lchild; } r += scout->lchild->Size + 1; return r; }//anything not clear= =? void insert(KeyType key, ValType val) { insert(root, key, val); } ValType query(KeyType key) { return query(root, key); }};
0 0
- [LOG] Size Balanced Tree
- Size Balanced Tree
- Size Balanced Tree
- 陈启峰 Size Balanced Tree
- Size Balanced Tree
- SBT(Size balanced tree)
- Size Balanced Tree模板
- size balanced tree
- Size Balanced Tree
- Size Balanced Tree(SBT)
- 数据结构-SBT(Size Balanced Tree)
- SBT模版(Size Balanced Tree)
- 【算法笔记】Size Balanced Tree
- [转载] SBT(Size Balanced Tree)
- Size Balanced Tree Sbt模板
- SB tree (Size Balanced Tree)
- 陈启峰 Size Balanced Tree (程序)
- sbt-size balanced tree学习笔记
- PHP Ajax 跨域问题最佳解决方案
- 20个常用的linux命令
- 一般函数指针和类的成员函数指针
- Linux 内核启动及文件系统加载过程
- java多线程的创建方法和线程的状态
- [LOG] Size Balanced Tree
- PHP+Ajax+Datatables实现分页
- 图论与代数系统学习笔记
- 142. Linked List Cycle II
- 剑指offer——斐波那契数列多种方法实现
- TIJ读书笔记05-this关键字
- javase第一天练习
- 编译界面程序时提示没有包含指定头文件globalhmi.h
- 有序链表