/****************************************************************************************** * Test an AVL ******************************************************************************************/ template <typename T> void testAVL(int n) { AVL<T>* avl = new AVL<T>; while (avl->size() < n) { T e = dice((T)n*3); //[0, 3n)范围内的e switch (dice(3)) { case 0: { //查找,成功率 <= 33.3% printf("Searching for "); print(e); printf(" ...\n"); BinNodePosi(T) & p = avl->search(e); p ? printf("Found with"), print(p), printf("\n") : printf("Not found\n"); break; } case 1: { //删除,成功率 <= 33.3% printf("Removing "); print(e); printf(" ...\n"); avl->remove(e) ? printf("Done\n"), print(avl) : printf("Not exists\n"); break; } default: {//插入,成功率 == 100% printf("Inserting "); print(e); printf(" ...\n"); BinNodePosi(T) p = avl->insert(e); printf("Done with"), print(p), printf("\n"), print(avl); break; } } } while (avl->size() > 0) { T e = dice((T)n*3); //[0, 3n)范围内的e printf("Removing "); print(e); printf(" ...\n"); avl->remove(e) ? printf("Done\n"), print(avl) : printf("Not exists\n"); } release(avl); }
void testRedBlack(int n) { RedBlack<T>* rb = new RedBlack<T>; while (rb->size() < n) { T e = dice((T)n*3); //[0, 3n)范围内的e switch (dice(6)) { case 0: { //查找(概率 = 1/6) printf("Searching for "); print(e); printf(" ...\n"); BinNodePosi(T) p = rb->search(e); p ? printf("Found with"), print(p), printf("\n") : printf("Not found\n"); break; } case 1: case 2: { //删除(概率 = 2/6) printf("Removing "); print(e); printf(" ...\n"); rb->remove(e) ? printf("Done\n"), print(rb) : printf("Not exists\n"); break; } default: { //插入(概率 = 3/6) printf("Inserting "); print(e); printf(" ...\n"); BinNodePosi(T) v = rb->insert(e); printf("Done with"), print(v), printf("\n"), print(rb); break; } } } while (rb->size() > 0) { T e = dice((T)n*3); //[0, 3n)范围内的e printf("Removing "); print(e); printf(" ...\n"); rb->remove(e) ? printf("Done\n"), print(rb) : printf("Not exists\n"); } release(rb); }
void BinNode<T>::travLevel(VST& visit) { std::queue<BinNodePosi(T)> Q; Q.enqueue(this); while (!Q.empty()) { BinNodePosi(T) x = Q.dequeue(); visit(x->data); if (HasLChild(*x)) Q.enqueue(x->lChild); if (HasRChild(*x)) Q.enqueue(x->rChild); } }
/****************************************************************************************** * Test a BST ******************************************************************************************/ template <typename T> void testBST(int n) { BST<T>* bst = new BST<T>; while (bst->size() < n) { T e = dice((T)n*3); //[0, 3n)范围内的e switch (dice(3)) { case 0: { //查找,成功率 <= 33.3% printf("Searching for "); print(e); printf(" ... "); BinNodePosi(T) & p = bst->search(e); p ? printf("Found with"), print(p->data), printf("\n") : printf("not found\n"); break; } case 1: { //删除,成功率 <= 33.3% printf("Removing "); print(e); printf(" ... "); bst->remove(e) ? printf("Done\n"), print(bst) : printf("not exists\n"); break; } default: {//插入,成功率 == 100% printf("Inserting "); print(e); printf(" ... "); printf("Done with"), print(bst->insert(e)->data), printf("\n"), print(bst); break; } } } while (bst->size() > 0) { T e = dice((T)n*3); //[0, 3n)范围内的e printf("Removing "); print(e); printf(" ... "); bst->remove(e) ? printf("Done\n"), print(bst) : printf("not exists\n"); } release(bst); }
// 根据编码树对长为n的Bitmap串做Huffman解码 void decode ( HuffTree* tree, Bitmap* code, int n ) { BinNodePosi ( HuffChar ) x = tree->root(); for ( int i = 0; i < n; i++ ) { x = code->test ( i ) ? x->rc : x->lc; if ( IsLeaf ( *x ) ) { printf ( "%c", x->data.ch ); x = tree->root(); } } /*DSA*/if ( x != tree->root() ) printf ( "..." ); printf ( "\n" ); } //解出的明码,在此直接打印输出;实用中可改为根据需要返回上层调用者
void BinNode<T>::travPre(VST& visit) { BinNode<T*> x = this; std::stack<BinNodePosi(T)> s; if (x) s.push(x); while(!s.empty()) { vist(s.top()); x = s.top(); s.pop(); if(x->rChild) s.push(x->rChild); if(x->lChild) s.push(x->lChild); } }
void BinNode<T>::travIn(VST& visit) { BinNode<T*> x = this; std::stack<BinNodePosi(T)> s; while(x && !s.empty) { while(x) { if(x->lChild) { s.push(x->lChild); x = x->lChild; } } x = s.top(); vist(x); s.pop(); x = x->rChild; } }
/****************************************************************************************** * Test a BST ******************************************************************************************/ template <typename T> void testBST ( int n ) { BST<T> bst; while ( bst.size() < n ) bst.insert ( dice ( ( T ) n * 3 ) ); print ( bst ); //随机创建 bst.stretchToLPath(); print ( bst ); //伸直成撇 while ( !bst.empty() ) bst.remove ( bst.root()->data ); //清空 while ( bst.size() < n ) bst.insert ( dice ( ( T ) n * 3 ) ); print ( bst ); //随机创建 bst.stretchToRPath(); print ( bst ); //伸直成捺 while ( !bst.empty() ) bst.remove ( bst.root()->data ); //清空 while ( bst.size() < n ) { //随机插入、查询、删除 T e = dice ( ( T ) n * 3 ); //[0, 3n)范围内的e switch ( dice ( 3 ) ) { case 0: { //查找,成功率 <= 33.3% printf ( "Searching for " ); print ( e ); printf ( " ... " ); BinNodePosi(T) & p = bst.search ( e ); p ? printf ( "Found with" ), print ( p->data ), printf ( "\n" ) : printf ( "not found\n" ); break; } case 1: { //删除,成功率 <= 33.3% printf ( "Removing " ); print ( e ); printf ( " ... " ); bst.remove ( e ) ? printf ( "Done\n" ), print ( bst ) : printf ( "not exists\n" ); break; } default: {//插入,成功率 == 100% printf ( "Inserting " ); print ( e ); printf ( " ... " ); printf ( "Done with" ), print ( bst.insert ( e )->data ), printf ( "\n" ), print ( bst ); break; } } } while ( bst.size() > 0 ) { //清空 T e = dice ( ( T ) n * 3 ); //[0, 3n)范围内的e printf ( "Removing " ); print ( e ); printf ( " ... " ); bst.remove ( e ) ? printf ( "Done\n" ), print ( bst ) : printf ( "not exists\n" ); } }
#include "MyAVL.h" //理想平衡条件 #define Balanced(x) ( stature(x->lc) == stature(x->rc) ) //平衡因子 #define BalFac(x) ( stature(x->lc) - stature(x->rc) ) //AVL平衡条件 #define AclBalanced(x) ( (-2 < BalFac(x)) && (BalFac(x) < 2) ) //恢复平衡的调整方案,决定于失衡节点的更高孩子、更高孙子节点的方向 //在左、右孩子中取更高者, 在AVL平衡调整前,借此重构方案 template <typename T> BinNodePosi(T)& MyAVL<T>::tallerChild(BinNodePosi(T) &x){ if(stature(x->lc) > stature(x->rc) ) return x->lc; else if(stature(x->lc) < stature(x->rc)) return x->rc; else if(IsLChild(x)) return x->lc; else return x->rc; } //AVL树的节点插入 template <typename T> BinNodePosi(T) MyAVL<T>::insert(const T &e){ BinNodePosi(T) &x = search(e); if(x) return x; BinNodePosi(T) xx = x = new BinNode<T>(e, _hot); _size++; //此时, x的父亲_hot若增高, 则其祖父有可能失衡
#include "binTree.h" #include <stack> template <typename T> int BinTree<T>::updateHeight(BinNodePosi(T) x) { return x->height = 1 + max(stature(x->lChild), stature(x->rChild)); } template <typename T> void BinTree<T>::updateHeightAbove(BinNodePosi(T) x) { while (x) { updateHeight(x); x = x->parent; } } template <typename VST> void travPre(VST& e) { stack<VST> s; s.push(e); while (!s.empty()) }
template <typename T>//基于二叉树,以左堆形式实现的优先级队列 class PQ_LeftHeap : public PQ<T>, public BinTree<T> { public: void insert(T) ;//按照比较器确定的优先次序插入元素 T getMax() { return _root->data; } T delMax();//删除优先级最高的元素 }; template <typename T> static BinNodePosi(T) merge(BinNodePosi(T), BinNodePosi(T)); template <typename T> static BinNodePosi(T) merge( BinNodePosi(T) a, BinNodePosi(T) b ) { if (!a) return b;//递归基 if (!b) return a;//递归基 if (lt(a->data, b->data)) swap(b, a);//首先确保b不大 a->rc = merge(a->rc, b);//将a的右子堆与b合并 a->rc->parent = a;//更新父子关系 if ( ! a->lc || a->lc->npl < a->rc->npl )//若有必要 swap (a->lc, a->rc);//交换a的左右子堆,以确保右子堆的npl不大 a->npl = a->rc ? a->rc->npl + 1 : 1;//更新a的npl return a; } //insert template <typename T> void PQ_LeftHeap<T>::insert(T e) { //O(log n) BinNodePosi(T) v = new BinNode<T> (e);