数据结构学习笔记-平衡二叉树实现
2012-12-14 16:08
525 查看
#ifndef _CAVLTREE_H_ #define _CAVLTREE_H_ const int LH = 1; const int EH = 0; const int RH = -1; typedef struct BiTNode { int nData; int nBalanceFactor; struct BiTNode* pLChild, *pRChild; } BiTNode, *BiTree; class CAVLTree { public: CAVLTree(); virtual ~CAVLTree(); public: bool InsertAVL( int e, bool* taller ); void TraverseTree(); private: bool InsertAVL( BiTree& pBiTree, int e, bool* taller ); void TraverseTree( BiTree& pBiTree ); void DestroyTree( BiTree& pBiTree ); private: void LeftBalance( BiTree& pBiTree ); void RightBalance( BiTree& pBiTree ); void R_Rotate( BiTree& pBiTree ); void L_Rotate( BiTree& pBiTree ); private: BiTree m_pBiTree; }; #endif // _CAVLTREE_H_
#include <stdio.h> #include "AVLTree.h" CAVLTree::CAVLTree() : m_pBiTree( NULL ) { } CAVLTree::~CAVLTree() { DestroyTree( m_pBiTree ); } void CAVLTree::R_Rotate( BiTree& pBiTree ) { BiTree pLTree; pLTree = pBiTree->pLChild; pBiTree->pLChild = pLTree->pRChild; pLTree->pRChild = pBiTree; pBiTree = pLTree; } void CAVLTree::L_Rotate( BiTree& pBiTree ) { BiTree pRTree; pRTree = pBiTree->pRChild; pBiTree->pRChild = pRTree->pLChild; pRTree->pLChild = pBiTree; pBiTree = pRTree; } void CAVLTree::LeftBalance( BiTree& pBiTree ) { BiTree pLeft, pLeftR; pLeft = pBiTree->pLChild; switch( pLeft->nBalanceFactor ) { case LH: pBiTree->nBalanceFactor = pLeft->nBalanceFactor = EH; R_Rotate( pBiTree ); break; case RH: pLeftR = pLeft->pRChild; switch( pLeftR->nBalanceFactor ) { case LH: pBiTree->nBalanceFactor = RH; pLeft->nBalanceFactor = EH; break; case EH: pBiTree->nBalanceFactor = pLeft->nBalanceFactor = EH; break; case RH: pBiTree->nBalanceFactor = EH; pLeft->nBalanceFactor = LH; break; } pLeftR->nBalanceFactor = EH; L_Rotate( pBiTree->pLChild ); R_Rotate( pBiTree ); break; } } void CAVLTree::RightBalance( BiTree& pBiTree ) { BiTree pRight, pRightL; pRight = pBiTree->pRChild; switch( pRight->nBalanceFactor ) { case RH: pBiTree->nBalanceFactor = pRight->nBalanceFactor = EH; L_Rotate( pBiTree ); break; case LH: pRightL = pRight->pLChild; switch( pRightL->nBalanceFactor ) { case RH: pBiTree->nBalanceFactor = RH; pRight->nBalanceFactor = EH; break; case EH: pBiTree->nBalanceFactor = pRight->nBalanceFactor = EH; break; case LH: pBiTree->nBalanceFactor = EH; pRight->nBalanceFactor = LH; break; } pRightL->nBalanceFactor = EH; R_Rotate( pBiTree->pRChild ); L_Rotate( pBiTree ); break; } } bool CAVLTree::InsertAVL( BiTree& pBiTree, int e, bool* taller ) { if ( !pBiTree ) { pBiTree = new BiTNode; pBiTree->nData = e; pBiTree->pLChild = pBiTree->pRChild = NULL; pBiTree->nBalanceFactor = EH; *taller = true; } else { if ( e == pBiTree->nData ) { *taller = false; return false; } if ( e < pBiTree->nData ) { if ( !InsertAVL( pBiTree->pLChild, e, taller )) { return false; } if ( *taller ) { switch( pBiTree->nBalanceFactor ) { case LH: LeftBalance( pBiTree ); *taller = false; break; case EH: pBiTree->nBalanceFactor = LH; *taller = true; break; case RH: pBiTree->nBalanceFactor = EH; *taller = false; break; } } } else { if ( !InsertAVL( pBiTree->pRChild, e, taller )) { return false; } if ( *taller ) { switch( pBiTree->nBalanceFactor ) { case RH: RightBalance( pBiTree ); *taller = false; break; case EH: pBiTree->nBalanceFactor = RH; *taller = true; break; case LH: pBiTree->nBalanceFactor = EH; *taller = false; break; } } } } return true; } bool CAVLTree::InsertAVL( int e, bool* taller ) { return InsertAVL( m_pBiTree, e, taller ); } void CAVLTree::DestroyTree( BiTree& pBiTree ) { if ( NULL == pBiTree ) { return; } if ( NULL != pBiTree->pLChild ) { DestroyTree( pBiTree->pLChild ); } if ( NULL != pBiTree->pRChild ) { DestroyTree( pBiTree->pRChild ); } delete pBiTree; pBiTree = NULL; } void CAVLTree::TraverseTree( BiTree& pBiTree ) { if ( NULL == pBiTree ) { return; } if ( NULL != pBiTree->pLChild ) { TraverseTree( pBiTree->pLChild ); } printf("%d ", pBiTree->nData ); if ( NULL != pBiTree->pRChild ) { TraverseTree( pBiTree->pRChild ); } } void CAVLTree::TraverseTree() { TraverseTree( m_pBiTree ); }
#include <stdio.h>#include "AVLTree.h"int main(int argc, char* argv[]){// Test Dataint nSrcArry[20] ={8,3,6,5,9,56,32,21,48,57,16,25,23,29,35,36,50,1,2,4};printf("Source Data Array: \n");for ( int i = 0; i < 20; i++ ){printf("%d, ", nSrcArry[i]);if ( 9 == i%10 ){printf("\n");}}CAVLTree oAVLTree;bool bHeigh = false;for ( i = 0; i < 20; i++ ){oAVLTree.InsertAVL( nSrcArry[i], &bHeigh );}printf("Sorted Data Array: \n");oAVLTree.TraverseTree();printf("\n");return 0;}
相关文章推荐
- 高级数据结构设计--并查集及实现学习笔记(有趣篇)
- python数据结构学习笔记-2016-10-28-02-使用链表实现稀疏矩阵
- sizzle.js学习笔记利用闭包模拟实现数据结构:字典(Map)
- python数据结构学习笔记-2016-11-05-01-队列ADT及其实现
- 数据结构学习笔记之链表分析与实现(一)
- 数据结构学习笔记-栈的顺序存储(C语言实现)
- 严蔚敏版数据结构学习笔记和代码实现
- Linux IGMP PROXY 学习笔记 之 一 igmp proxy相关的数据结构及igmp proxy的大致实现要点
- 数据结构学习笔记-队列的链式存储(C语言实现)
- 数据结构学习笔记-栈的链式存储(C语言实现)
- 高级数据结构设计--并查集及实现学习笔记(有趣篇)
- 数据结构学习笔记-循环链表(C语言实现)
- 数据结构学习笔记9——区分叶结点与分支结点的二叉树实现方案一
- python数据结构学习笔记-2016-10-27-02-使用单链表实现包ADT
- 前端攻城狮学习笔记三:实现一个URI解析方法,把url里#之后的参数解析成指定的数据结构。
- 数据结构学习笔记-线性表链式存储(C语言实现)
- 数据结构学习笔记5——双链表的实现
- 【C++数据结构学习笔记---线性表】用数组实现线性表
- 线性表的两种实现方法-数据结构学习笔记2.1
- 【C++数据结构学习笔记---栈】用数组实现栈