AVL树又叫自平衡二叉查找树,在效率上不比红黑树,但是是红黑树的基础!下面是构造一棵AVL树的完整代码!
- //AVLTree.h
- //参考:http://www.cppblog.com/goodwin/archive/2011/08/08/152797.html
- #ifndef _AVLTREE_H_
- #define _AVLTREE_H_
- #include “Stack.h”
- #include “Queue.h”
- #define SAFE_CALL(p) p == NULL ? NULL : p
- template <typename T>
- T Max(T a,T b)
- {
- return (a)>(b) ? (a) : (b);
- }
- namespace _AVL_Tree_
- {
- #define Balance(a) abs((SAFE_CALL(a–>mpLeft)–>mHeigh) – (SAFE_CALL(a–>mpRight)–>mHeigh))
- #define isLeftHigh(a) ((SAFE_CALL(a–>mpLeft)–>mHeigh) > (SAFE_CALL(a–>mpRight)–>mHeigh)) ? true : false
- #define isLeftTree(a,b) ((a)–>mpLeft == (b)) ? true : false
- #define isRightTree(a,b) ((a)–>mpRight == (b)) ? true : false
- typedef enum _position_
- {
- enPosi_Root = 0,
- enPosi_Left,
- enPosi_Right,
- enPosi_Equal,
- enPosiMax
- }POSITION;
- typedef enum _rotal_type_
- {
- enRotalType_Anonymous,
- enRotalType_LeftLeft = 1,
- enRotalType_RightRight,
- enRotalType_LeftRight,
- enRotalType_RightLeft,
- enRotalType_MaxType
- }RotalType;
- template <typename T>
- class TreeNode
- {
- public:
- TreeNode()
- {
- mHeigh = 0;
- mElement = T();
- mpLeft = mpRight = mpParent = NULL;
- }
- ~TreeNode()
- {
- mElement = T();
- mpLeft = mpRight = mpParent = NULL;
- }
- T mElement;
- int mHeigh;
- TreeNode<T>* mpLeft;
- TreeNode<T>* mpRight;
- TreeNode<T>* mpParent;
- };
- template <typename T>
- class AVLTree
- {
- public:
- AVLTree();
- ~AVLTree();
- bool insert(const T& element);
- bool remove(const T& element);
- bool clear();
- //POSITION:返回找到的插入点的位置,s:返回在遍历过程中从树根到找到的插入点的路径(主要是用来旋转用)。
- POSITION find(const T& element,Stack<TreeNode<T>*>& s);
- //中序遍历
- bool inorderTravel(void);
- //先序遍历
- bool prevorederTravel(void);
- //中序遍历,并把遍历的结果放到栈中,用户销毁这棵树。
- bool inorderTravel(Stack<TreeNode<T>*>& s);
- //求一棵树的最大长度的路径。
- bool getLongestPath(const TreeNode<T>* pRoot,Queue<TreeNode<T>*>& q);
- //检查是否是AVL树。
- bool checkIsAVLTree(void);
- //把AVL树的信息丰收放到文件中
- void saveAVLTreeToFile(const char* pFileName);
- protected:
- TreeNode<T>* CreateNode(const T& element);
- TreeNode<T>* RightRightRotal(TreeNode<T>* pNode);
- TreeNode<T>* LeftLeftRotal(TreeNode<T>* pNode);
- TreeNode<T>* LeftRightRotal(TreeNode<T>* pNode);
- TreeNode<T>* RightLeftRotal(TreeNode<T>* pNode);
- RotalType checkRotalType(TreeNode<T>* pPParentNode,TreeNode<T>* pParentNode,TreeNode<T>* pNode);
- TreeNode<T>* rotalTree(TreeNode<T>* pPParentNode,TreeNode<T>* pParentNode,TreeNode<T>* pNode);
- //当要进行旋转时,判定需求旋转的类型。
- RotalType checkRotalType(TreeNode<T>* pRootNode);
- //对以pRootNode为根的进行旋转
- TreeNode<T>* rotalTree(TreeNode<T>* pRootNode);
- //对树进行平衡处理
- bool balanceTree(TreeNode<T>* pRootNode);
- //获取节点pNode的中序遍历【前驱】节点
- TreeNode<T>* getPrevNode(TreeNode<T>* pNode);
- //获取节点pNode的中序遍历【后继】节点
- TreeNode<T>* getNextNode(TreeNode<T>* pNode);
- private:
- TreeNode<T>* mpRoot;
- };
- template <typename T>
- AVLTree<T>::AVLTree()
- {
- mpRoot = new TreeNode<T>;
- if (!mpRoot) throw(“malloc memery failed.”);
- mpRoot–>mElement = –1;
- mpRoot–>mHeigh = 0;
- mpRoot–>mpLeft = mpRoot–>mpParent = mpRoot–>mpRight = NULL;
- }
- template <typename T>
- AVLTree<T>::~AVLTree()
- {
- this–>clear();
- delete this–>mpRoot;
- this–>mpRoot = NULL;
- }
- template <typename T>
- TreeNode<T>* AVLTree<T>::CreateNode(const T& element)
- {
- TreeNode<T>* pNode = new TreeNode<T>;
- if (pNode)
- {
- pNode–>mElement = element;
- pNode–>mpLeft = pNode–>mpRight = pNode–>mpParent = NULL;
- pNode–>mHeigh = 1;
- return pNode;
- }
- return NULL;
- }
- template <typename T>
- TreeNode<T>* AVLTree<T>::RightRightRotal(TreeNode<T>* pNode)
- {
- TreeNode<T>* pRightChild = pNode–>mpRight;
- //cout << “RightRightRotal mElement:” << pNode–>mElement << endl;
- pNode–>mHeigh = Max((SAFE_CALL(pNode–>mpLeft)–>mHeigh),(SAFE_CALL(pRightChild–>mpLeft)–>mHeigh)) + 1;
- pRightChild–>mHeigh = Max((SAFE_CALL(pRightChild–>mpRight)–>mHeigh),(pNode–>mHeigh)) + 1;
- pNode–>mpRight = pRightChild–>mpLeft;
- if (pRightChild–>mpLeft && pRightChild–>mpLeft–>mpParent)
- {
- pRightChild–>mpLeft–>mpParent = pNode;
- }
- pRightChild–>mpLeft = pNode;
- pNode–>mpParent = pRightChild;
- return pRightChild;
- }
- template <typename T>
- TreeNode<T>* AVLTree<T>::LeftLeftRotal(TreeNode<T>* pNode)
- {
- TreeNode<T>* pLeftChild = pNode–>mpLeft;
- //cout << “LeftLeftRotal mElement:” << pNode–>mElement << endl;
- pNode–>mHeigh = Max((SAFE_CALL(pLeftChild–>mpRight)–>mHeigh),(SAFE_CALL(pNode–>mpRight)–>mHeigh)) + 1;
- pLeftChild–>mHeigh = Max((SAFE_CALL(pLeftChild–>mpLeft)–>mHeigh),pNode–>mHeigh) + 1;
- pNode–>mpLeft = pLeftChild–>mpRight;
- if (pLeftChild–>mpRight && pLeftChild–>mpRight–>mpParent)
- {
- pLeftChild–>mpRight–>mpParent = pNode;
- }
- pLeftChild–>mpRight = pNode;
- pNode–>mpParent = pLeftChild;
- return pLeftChild;
- }
- template <typename T>
- TreeNode<T>* AVLTree<T>::LeftRightRotal(TreeNode<T>* pNode)
- {
- TreeNode<T>* pLeftChild = pNode–>mpLeft;
- TreeNode<T>* pTmp = pLeftChild;
- //左旋转
- pLeftChild = this–>RightRightRotal(pLeftChild);
- pNode–>mpLeft = pLeftChild;
- pLeftChild–>mpParent = pNode;
- pTmp–>mpParent = pLeftChild;
- //右旋转
- pNode =this–>LeftLeftRotal(pNode);
- return pNode;
- }
- template <typename T>
- TreeNode<T>* AVLTree<T>::RightLeftRotal(TreeNode<T>* pNode)
- {
- TreeNode<T>* pRightChild = pNode–>mpRight;
- TreeNode<T>* pTmp = pRightChild;
- //右旋转
- pRightChild = this–>LeftLeftRotal(pRightChild);
- pNode –>mpRight = pRightChild;
- pRightChild–>mpParent = pNode;
- pTmp–>mpParent = pRightChild;
- //左旋转
- pNode = this–>RightRightRotal(pNode);
- return pNode;
- }
- template <typename T>
- RotalType AVLTree<T>::checkRotalType(TreeNode<T>* pPParentNode,TreeNode<T>* pParentNode,TreeNode<T>* pNode)
- {
- bool bPLeft = false,bLeft = false;
- if (pPParentNode–>mpLeft == pParentNode)
- bPLeft = true;
- else
- bPLeft = false;
- if (pParentNode–>mpLeft == pNode)
- bLeft = true;
- else
- bLeft = false;
- RotalType type ;
- if (bPLeft && bLeft)
- type = enRotalType_LeftLeft;
- else if (!bPLeft && bLeft)
- type = enRotalType_RightLeft;
- else if(bPLeft && !bLeft)
- type = enRotalType_LeftRight;
- else if (!bPLeft && !bLeft )
- type = enRotalType_RightRight;
- return type;
- }
- template <typename T>
- TreeNode<T>* AVLTree<T>::rotalTree(TreeNode<T>* pPParentNode,TreeNode<T>* pParentNode,TreeNode<T>* pNode)
- {
- RotalType type = this–>checkRotalType(pPParentNode,pParentNode,pNode);
- switch (type)
- {
- case enRotalType_LeftLeft:
- {
- return this–>LeftLeftRotal(pPParentNode);
- }
- case enRotalType_RightRight:
- {
- return this–>RightRightRotal(pPParentNode);
- break;
- }
- case enRotalType_LeftRight:
- {
- return this–>LeftRightRotal(pPParentNode);
- break;
- }
- case enRotalType_RightLeft:
- {
- return this–>RightLeftRotal(pPParentNode);
- break;
- }
- }
- return NULL;
- }
- //当要进行旋转时,判定需求旋转的类型。
- template <typename T>
- RotalType AVLTree<T>::checkRotalType(TreeNode<T>* pRootNode)
- {
- RotalType type = enRotalType_Anonymous;
- if (Balance(pRootNode) >= 2)
- {
- bool bCLeft = false,bCCLeft = false;
- Queue<TreeNode<T>*> q;
- this–>getLongestPath(pRootNode,q);
- TreeNode<T>* pChildNode = q.front(); //先把树根出列
- pChildNode= q.front();
- bCLeft = (pRootNode–>mpLeft == pChildNode);
- TreeNode<T>* pCChildNode = q.front();
- bCCLeft = (pChildNode–>mpLeft == pCChildNode);
- q.clear();
- if (bCLeft && bCCLeft)
- type = enRotalType_LeftLeft;
- else if (!bCLeft && bCCLeft)
- type = enRotalType_RightLeft;
- else if(bCLeft && !bCCLeft)
- type = enRotalType_LeftRight;
- else if (!bCLeft && !bCCLeft )
- type = enRotalType_RightRight;
- }
- return type;
- }
- //对以pRootNode为根的进行旋转
- template <typename T>
- TreeNode<T>* AVLTree<T>::rotalTree(TreeNode<T>* pRootNode)
- {
- RotalType type = this–>checkRotalType(pRootNode);
- switch (type)
- {
- case enRotalType_Anonymous:
- {
- return NULL;
- }
- case enRotalType_LeftLeft:
- {
- return this–>LeftLeftRotal(pRootNode);
- }
- case enRotalType_RightRight:
- {
- return this–>RightRightRotal(pRootNode);
- }
- case enRotalType_LeftRight:
- {
- return this–>LeftRightRotal(pRootNode);
- }
- case enRotalType_RightLeft:
- {
- return this–>RightLeftRotal(pRootNode);
- }
- }
- return NULL;
- }
- //对树进行平衡处理
- template <typename T>
- bool AVLTree<T>::balanceTree(TreeNode<T>* pRootNode)
- {
- while (pRootNode && pRootNode != this–>mpRoot)
- {
- pRootNode–>mHeigh = Max((SAFE_CALL(pRootNode–>mpLeft)–>mHeigh) , (SAFE_CALL(pRootNode–>mpRight)–>mHeigh)) + 1;
- if (Balance(pRootNode) >= 2)
- {
- bool bLeft = false;
- TreeNode<T>* pPPNode = pRootNode–>mpParent;
- bLeft = (pPPNode–>mpLeft == pRootNode);
- pRootNode = this–>rotalTree(pRootNode);
- if (bLeft)
- pPPNode–>mpLeft = pRootNode;
- else
- pPPNode–>mpRight = pRootNode;
- pRootNode–>mpParent = pPPNode;
- }
- pRootNode = pRootNode–>mpParent;
- }
- return true;
- }
- //获取节点pNode的中序遍历【前驱】节点
- template <typename T>
- TreeNode<T>* AVLTree<T>::getPrevNode(TreeNode<T>* pNode)
- {
- TreeNode<T>* pTmpNode = pNode–>mpLeft;
- while (pTmpNode && pTmpNode–>mpRight)
- pTmpNode = pTmpNode–>mpRight;
- return pTmpNode;
- }
- //获取节点pNode的中序遍历【后继】节点
- template <typename T>
- TreeNode<T>* AVLTree<T>::getNextNode(TreeNode<T>* pNode)
- {
- TreeNode<T>* pTmpNode = pNode–>mpRight;
- while (pTmpNode && pTmpNode–>mpLeft)
- pTmpNode = pTmpNode–>mpLeft;
- return pTmpNode;
- }
- template <typename T>
- bool AVLTree<T>::insert(const T& element)
- {
- TreeNode<T>* pNode = NULL;
- TreeNode<T>* pInNode = NULL; //插入点节点
- Stack<TreeNode<T>*> s;
- POSITION pos = this–>find(element,s);
- switch (pos)
- {
- case enPosi_Equal:
- {
- static int iCount = 0;
- cout << “—-enPosi_Equal–iCount:”<<++iCount<<“–element:”<< element << endl;
- break;
- }
- case enPosi_Root:
- {
- pNode = this–>CreateNode(element);
- this–>mpRoot–>mpLeft = pNode;
- pNode–>mpParent = this–>mpRoot;
- break;
- }
- case enPosi_Left:
- {
- pNode = this–>CreateNode(element);
- pInNode = s.pop();
- pInNode–>mpLeft = pNode;
- pNode–>mpParent = pInNode;
- pInNode–>mHeigh = Max((SAFE_CALL(pInNode–>mpLeft)–>mHeigh) ,(SAFE_CALL(pInNode–>mpRight)–>mHeigh)) + 1;
- TreeNode<T>* pParentNode = pInNode–>mpParent;
- if (pParentNode && pParentNode != this–>mpRoot)
- {
- this–>balanceTree(pParentNode);
- }
- break;
- }
- case enPosi_Right:
- {
- pNode = this–>CreateNode(element);
- TreeNode<T>* pInNode = s.pop();
- pInNode–>mpRight = pNode;
- pNode–>mpParent = pInNode;
- pInNode–>mHeigh = Max((SAFE_CALL(pInNode–>mpRight)–>mHeigh) , (SAFE_CALL(pInNode–>mpLeft)–>mHeigh)) + 1;
- TreeNode<T>* pParentNode = pInNode–>mpParent;
- if (pParentNode && pParentNode != this–>mpRoot)
- {
- this–>balanceTree(pParentNode);
- }
- break;
- }
- }
- s.clear();
- return true;
- }
- template <typename T>
- bool AVLTree<T>::remove(const T& element)
- {
- TreeNode<T>* pDelNode = NULL; //插入点节点
- Stack<TreeNode<T>*> s;
- POSITION pos = this–>find(element,s);
- if (s.isEmpty()) goto OVER;
- switch (pos)
- {
- case enPosi_Equal:
- {
- pDelNode = s.pop();
- if (pDelNode–>mpLeft == NULL && pDelNode–>mpRight == NULL)
- {//1.删除的节点没有子树
- TreeNode<T>* pParentNode = pDelNode–>mpParent;
- if (pParentNode && isLeftTree(pParentNode,pDelNode))
- pParentNode–>mpLeft = NULL;
- else if(pParentNode && isRightTree(pParentNode,pDelNode))
- pParentNode–>mpRight = NULL;
- this–>balanceTree(pParentNode);
- delete pDelNode;
- pDelNode = NULL;
- }
- else if((pDelNode–>mpLeft && pDelNode–>mpRight == NULL) ||
- (pDelNode–>mpLeft == NULL && pDelNode–>mpRight))
- {//2.删除的节点有一子树
- TreeNode<T>* pChildNode = pDelNode–>mpLeft;
- if(!pChildNode)
- pChildNode = pDelNode–>mpRight;
- TreeNode<T>* pParentNode = pDelNode–>mpParent;
- if (pParentNode && isLeftTree(pParentNode,pDelNode))
- {// 2.1、删除的结点是其父节点的左子树
- pParentNode–>mpLeft = pChildNode;
- pChildNode–>mpParent = pParentNode;
- this–>balanceTree(pParentNode);
- }
- else if(pParentNode && isRightTree(pParentNode,pDelNode))
- {//2.2、删除的结点是其父节点的右子树
- pParentNode–>mpRight = pChildNode;
- pChildNode–>mpParent = pParentNode;
- this–>balanceTree(pParentNode);
- }
- delete pDelNode;
- pDelNode = NULL;
- }
- else if (pDelNode–>mpLeft && pDelNode–>mpRight)
- {//3、删除的节点有左右子树
- /*
- 有两种做法,要删除p的节点:
- 1.把p的左子树设成其父节点的左子树,把p的右子树设成其实左子树的最右边的叶子节点。
- 2.把p的【直接前驱(中序)】替换p,然后把【前驱】的左子树替换成【前驱】的右子树。
- 以下实现是利用第2种方法。
- */
- if (pDelNode–>mpLeft–>mHeigh >= pDelNode–>mpRight–>mHeigh)
- {//左子树比右子树高,就用pDelNode的【前驱】替换
- TreeNode<T>* pPrevNode = this–>getPrevNode(pDelNode);
- pDelNode–>mElement = pPrevNode–>mElement;
- TreeNode<T>* pParentNode = pPrevNode–>mpParent;
- pParentNode–>mpRight = pPrevNode–>mpLeft;
- if (pPrevNode–>mpLeft)
- pPrevNode–>mpLeft–>mpParent = pParentNode;
- this–>balanceTree(pParentNode);
- delete pPrevNode;
- pPrevNode = NULL;
- }
- else
- {//右子树比左子树高,就用pDelNode的【后继】替换
- TreeNode<T>* pNextNode = this–>getNextNode(pDelNode);
- pDelNode–>mElement = pNextNode–>mElement;
- TreeNode<T>* pParentNode = pNextNode–>mpParent;
- pParentNode–>mpLeft = pNextNode–>mpRight;
- if(pNextNode–>mpRight)
- pNextNode–>mpRight–>mpParent = pParentNode;
- this–>balanceTree(pParentNode);
- delete pNextNode;
- pNextNode = NULL;
- }
- }
- break;
- }
- default:
- {
- goto OVER;
- break;
- }
- }
- OVER:
- s.clear();
- return true;
- }
- template <typename T>
- bool AVLTree<T>::clear()
- {
- Stack<TreeNode<T>*> s;
- this–>inorderTravel(s);
- TreeNode<T>* pNode = NULL;
- while ((pNode= s.pop()) != NULL)
- delete pNode;
- return true;
- }
- //POSITION:返回找到的插入点的位置,s:返回在遍历过程中从树根到找到的插入点的路径(主要是用来旋转用)。
- template <typename T>
- POSITION AVLTree<T>::find(const T& element,Stack<TreeNode<T>* >& s)
- {
- TreeNode<T>* pNode = this–>mpRoot–>mpLeft;
- POSITION pos = enPosi_Root;
- while (pNode)
- {
- s.push(pNode);
- if (element == pNode–>mElement)
- {
- pos = enPosi_Equal;
- break;
- }
- else if (element < pNode–>mElement)
- {
- pos = enPosi_Left;
- if (pNode–>mpLeft)
- pNode = pNode–>mpLeft;
- else
- break;
- }
- else if (element > pNode–>mElement)
- {
- pos = enPosi_Right;
- if (pNode–>mpRight)
- pNode = pNode–>mpRight;
- else
- break;
- }
- }
- return pos;
- }
- //中序遍历
- template <typename T>
- bool AVLTree<T>::inorderTravel(void)
- {
- TreeNode<T>* pNode = this–>mpRoot–>mpLeft;
- if (pNode)
- {
- Stack<TreeNode<T>*> s;
- while (pNode || !s.isEmpty())
- {
- while (pNode)
- {
- s.push(pNode);
- pNode = pNode–>mpLeft;
- }
- pNode = s.pop();
- if (pNode)
- {
- cout << pNode–>mElement << ” “;
- pNode = pNode–>mpRight;
- }
- }
- }
- return true;
- }
- //先序遍历
- template <typename T>
- bool AVLTree<T>::prevorederTravel(void)
- {
- TreeNode<T>* pNode = this–>mpRoot–>mpLeft;
- if (pNode)
- {
- Stack<TreeNode<T>*> s;
- while (pNode || !s.isEmpty())
- {
- while (pNode)
- {
- cout << pNode–>mElement << ” “;
- s.push(pNode);
- pNode = pNode–>mpLeft;
- }
- pNode = s.pop();
- if (pNode)
- pNode = pNode–>mpRight;
- }
- }
- return true;
- }
- //中序遍历,并把遍历的结果放到栈中,用户销毁这棵树。
- template <typename T>
- bool AVLTree<T>::inorderTravel(Stack<TreeNode<T>*>& s)
- {
- TreeNode<T>* pNode = this–>mpRoot–>mpLeft;
- if (pNode)
- {
- Stack<TreeNode<T>*> sTmp;
- while (pNode || !sTmp.isEmpty())
- {
- while (pNode)
- {
- sTmp.push(pNode);
- pNode = pNode–>mpLeft;
- }
- pNode = sTmp.pop();
- if (pNode)
- {
- s.push(pNode);
- pNode = pNode–>mpRight;
- }
- }
- }
- return true;
- }
- //求一棵树的最大长度的路径。
- template <typename T>
- bool AVLTree<T>::getLongestPath(const TreeNode<T>* pRoot,Queue<TreeNode<T>*>& q)
- {
- if (!pRoot) return false;
- q.insert((TreeNode<T>*)(pRoot));
- if ((SAFE_CALL(pRoot–>mpLeft)–>mHeigh) >= (SAFE_CALL(pRoot–>mpRight)–>mHeigh))
- return getLongestPath(pRoot–>mpLeft,q);
- else
- return getLongestPath(pRoot–>mpRight,q);
- return true;
- }
- //检查是否是AVL树。
- template <typename T>
- bool AVLTree<T>::checkIsAVLTree(void)
- {
- Stack<TreeNode<T>*> s;
- this–>inorderTravel(s);
- TreeNode<T>* pNode = NULL;
- while ((pNode= s.pop()) != NULL)
- {
- cout << “[“ << pNode–>mElement<<“]” \
- <<” Height:” << pNode–>mHeigh \
- <<” Left:” << (SAFE_CALL(pNode–>mpLeft)–>mHeigh) \
- <<” Rigth:”<< (SAFE_CALL(pNode–>mpRight)–>mHeigh) \
- <<” bala:” << Balance(pNode) << endl;
- }
- return true;
- }
- #include <fstream>
- using namespace std;
- //把AVL树的信息丰收放到文件中
- template <typename T>
- void AVLTree<T>::saveAVLTreeToFile(const char* pFileName)
- {
- if (!pFileName) return;
- FILE* pFile = fopen(pFileName,“w”);
- if (!pFile)
- {
- cout << “open file failed.” << endl ;
- return ;
- }
- char tmpBuf[512] = {0};
- Stack<TreeNode<T>*> s;
- this–>inorderTravel(s);
- sprintf(tmpBuf,“TreeSize: %d\r\n”,s.size());
- fwrite(tmpBuf,strlen(tmpBuf),1,pFile);
- TreeNode<T>* pNode = NULL;
- int iMaxHeigh = 0;
- while ((pNode= s.pop()) != NULL)
- {
- if (pNode–>mHeigh > iMaxHeigh)
- {
- iMaxHeigh = pNode–>mHeigh;
- }
- sprintf(tmpBuf,“[%d]–Hegigh:%d–Left:%d–Right:%d–Bala:%d\r\n”,
- pNode–>mElement,
- pNode–>mHeigh,
- (SAFE_CALL(pNode–>mpLeft)–>mHeigh),
- (SAFE_CALL(pNode–>mpRight)–>mHeigh),
- Balance(pNode));
- fwrite(tmpBuf,strlen(tmpBuf),1,pFile);
- }
- sprintf(tmpBuf,“iMaxHeigh: %d\r\n”,iMaxHeigh);
- fwrite(tmpBuf,strlen(tmpBuf),1,pFile);
- fclose(pFile);
- pFile = NULL;
- }
- }
- #endif
/////////******************************测试函数*****************************************///////////////////////
- #include <stdexcept>
- #include <typeinfo>
- #include <iostream>
- #include <fstream>
- using namespace std;
- #include “AVLTree.h”
- #include “Stack.h”
- #include “Queue.h”
- using namespace _AVL_Tree_;
- #include <time.h>
- #define ARRY_SIZE 1000
- int main(int argc,char** argv)
- {
- AVLTree<int> tree;
- // int arr[] = {1,2,3};//RR
- // int arr[] = {3,2,1};//LL
- // int arr[] = {2,4,3};//RL
- // int arr[] = {4,2,3};//LR
- // int arr[] = {10,5,3,7,6,8,12,54,11}; //删除8后,还要再次平衡处理右子树
- // int arr[] = {100,50,30,70,60,80,120,540,110,51,75,112,85,78,71}; //删除 后,还要再次平衡处理左子树85,71,78,79
- int arr[ARRY_SIZE] = {0};
- srand((unsigned int)time(NULL));
- for (int i = 0; i < ARRY_SIZE; i++)
- {
- arr[i] = (rand() % ARRY_SIZE) + 1;
- }
- size_t size = sizeof(arr) / sizeof(int);
- for (size_t i = 0; i < size; i++)
- {
- tree.insert(arr[i]);
- }
- cout << “Befor Inorder:”;
- tree.inorderTravel();
- cout << “\nBefor Preorder:”;
- tree.prevorederTravel();
- tree.saveAVLTreeToFile(“AVLResult.txt”);
- cout << “\r\nAVLTree Result writes in file AVLResult.txt.” << endl;
- getchar();
- return 0;
- }
///////////////*****************************C++实现栈***************************************///////////////////////////////////////////
- //Stack.h
- //双链表栈数据结构C++模块的实现
- //理论知识参考《数据结构(C语言版)––严慰明》
- #ifndef _STACK_H_
- #define _STACK_H_
- #include <iostream>
- using namespace std;
- namespace _STACK_
- {
- //栈中的元素
- template <typename T>
- class StackNode
- {
- public:
- StackNode()
- {
- this–>mElement = 0;
- this–>mpNext = NULL;
- this–>mpPrev = NULL;
- }
- ~StackNode()
- {
- this–>mElement = 0;
- this–>mpNext = NULL;
- this–>mpPrev = NULL;
- }
- T& getElement(void)
- {
- return mElement;
- }
- //重载“<<“操作符,以便对栈节点输出,注意以friend重载“<<”操作符要加上“<>”
- friend ostream& operator << <>(ostream& ost,StackNode<T>& src)
- {
- ost << src.mElement;
- return ost;
- }
- //protected:
- T mElement; //存放的数据
- StackNode<T>* mpNext; //指向后继节点指针
- StackNode<T>* mpPrev; //指向前驱节点指针
- template <typename T>
- friend class Stack;
- };
- template <typename T>
- class Stack
- {
- public:
- Stack();
- ~Stack();
- bool push(const T& element);
- T pop(void);
- bool isEmpty(void);
- bool clear(void);
- int size();
- friend ostream& operator<< <>(ostream& ost,Stack<T>& src);
- protected:
- StackNode<T>* createNode(const T& element);
- protected:
- StackNode<T>* mpHead;
- StackNode<T>* mpTop;
- };
- template <typename T>
- Stack<T>::Stack()
- {
- T i = T();
- //创建一个带有头节点的双链栈,这个节点不存入任何数据
- this–>mpHead = this–>createNode(i);
- this–>mpTop = this–>mpHead;
- }
- template <typename T>
- Stack<T>::~Stack()
- {
- this–>clear();
- delete this–>mpHead;
- this–>mpHead = NULL;
- this–>mpTop = NULL;
- }
- template <typename T>
- StackNode<T>* Stack<T>::createNode(const T& element)
- {
- StackNode<T>* pNode = new StackNode<T>;
- if (pNode)
- {
- pNode–>mElement = element;
- pNode–>mpNext = NULL;
- pNode–>mpPrev = NULL;
- }
- return pNode;
- }
- template <typename T>
- bool Stack<T>::push(const T& element)
- {
- StackNode<T>* pNode = createNode(element);
- if(this–>mpHead–>mpNext == NULL)
- {
- this–>mpHead–>mpNext = pNode;
- pNode–>mpPrev = this–>mpHead;
- }
- else
- {
- this–>mpTop–>mpNext = pNode;
- pNode–>mpPrev = this–>mpTop;
- }
- this–>mpTop = pNode;
- return true;
- }
- template <typename T>
- T Stack<T>::pop(void)
- {
- if (this–>mpTop != this–>mpHead)
- {
- StackNode<T>* pNode = this–>mpTop;
- this–>mpTop = this–>mpTop–>mpPrev;
- T elem = pNode–>mElement;
- pNode –>mpNext = pNode–>mpPrev = NULL;
- delete pNode;
- pNode = NULL;
- return elem;
- }
- return (T)0;
- }
- template <typename T>
- bool Stack<T>::isEmpty(void)
- {
- return (this–>mpHead == this–>mpTop);
- }
- template <typename T>
- bool Stack<T>::clear(void)
- {
- StackNode<T>* pNode = this–>mpTop;
- while((pNode = this–>mpTop) != this–>mpHead)
- {
- this–>mpTop =pNode–>mpPrev;
- pNode–>mpNext = NULL;
- pNode–>mpPrev = NULL;
- delete pNode;
- pNode = NULL;
- }
- return true;
- }
- template <typename T>
- int Stack<T>::size()
- {
- int iCount = 0;
- StackNode<T>* pNode = this–>mpTop;
- while(pNode != this–>mpHead)
- {
- iCount++;
- pNode = pNode–>mpPrev;
- }
- return iCount;
- }
- template <typename T>
- ostream& operator<< <>(ostream& ost,Stack<T>& src)
- {
- StackNode<T>* pNode = src.mpTop;
- while( src.mpHead)
- {
- ost << *pNode << ” “;
- pNode = pNode–>mpPrev;
- }
- return ost;
- }
- }
- #endif
///////////////////////***************************************C++实现队列*********************************////////////////////////////////////////////////
- //Queue.h
- //双链表队列数据结构C++模块的实现
- //理论知识参考《数据结构(C语言版)––严慰明》
- #ifndef _QUEUE_H_
- #define _QUEUE_H_
- namespace _QUEUE_
- {
- //队列中的数据元素
- template <typename T>
- class QueueNode
- {
- public:
- QueueNode()
- {
- this–>mElement = 0;
- this–>mpNext = this–>mpPrev = NULL;
- }
- T mElement;
- QueueNode<T>* mpNext;
- QueueNode<T>* mpPrev;
- };
- template <typename T>
- class Queue
- {
- public:
- Queue()
- {
- QueueNode<T> * pNode = new QueueNode<T>;
- pNode–>mElement = T(–1);
- pNode–>mpNext = pNode–>mpPrev = NULL;
- this–>mpHead = this–>mpTail = pNode;
- }
- ~Queue()
- {
- this–>clear();
- delete this–>mpHead;
- this–>mpHead = this–>mpTail = NULL;
- }
- bool insert(T element);
- T front();
- T back();
- bool isEmpty(void);
- bool clear(void);
- int size();
- friend ostream& operator<< <>(ostream& ostr,const Queue<T>& q);
- private:
- QueueNode<T>* mpHead;
- QueueNode<T>* mpTail;
- };
- template <typename T>
- bool Queue<T>::insert(T element)
- {
- QueueNode<T> * pNode = new QueueNode<T>;
- if (pNode == NULL) return false;
- pNode–>mElement = element;
- this–>mpTail–>mpNext = pNode;
- pNode–>mpPrev = this–>mpTail;
- this–>mpTail = this–>mpTail–>mpNext;
- return true;
- }
- template <typename T>
- T Queue<T>::front()
- {
- T element = T();
- QueueNode<T>* pNode = NULL;
- if (!this–>isEmpty())
- {
- pNode = this–>mpHead–>mpNext;
- element = pNode–>mElement;
- this–>mpHead –>mpNext = pNode–>mpNext;
- if (pNode–>mpNext)
- pNode–>mpNext–>mpPrev = this–>mpHead;
- if (pNode == this–>mpTail)
- this–>mpTail = this–>mpHead;
- delete pNode;
- }
- return element;
- }
- template <typename T>
- T Queue<T>::back()
- {
- T element = T();
- QueueNode<T>* pNode = NULL;
- if (!this–>isEmpty())
- {
- pNode = this–>mpTail;
- element = pNode–>mElement;
- this–>mpTail = this–>mpTail–>mpPrev;
- this–>mpTail–>mpNext = NULL;
- delete pNode;
- }
- return element;
- }
- template <typename T>
- bool Queue<T>::isEmpty(void)
- {
- return (this–>mpTail == this–>mpHead);
- }
- template <typename T>
- bool Queue<T>::clear(void)
- {
- while (!this–>isEmpty())
- this–>back();
- return true;
- }
- template <typename T>
- int Queue<T>::size()
- {
- int iCount = 0;
- if (!this–>isEmpty())
- {
- QueueNode<T>* pNode = this–>mpTail;
- while (pNode != this–>mpHead)
- {
- iCount++;
- pNode = pNode–>mpPrev;
- }
- }
- return iCount;
- }
- template <typename T>
- ostream& operator<< <>(ostream& ostr,const Queue<T>& q)
- {
- QueueNode<T>* pNode = q.mpHead–>mpNext;
- while (pNode != NULL)
- {
- ostr << pNode–>mElement << “,”;
- pNode = pNode–>mpNext;
- }
- return ostr;
- }
- }
- #endif