1、AVL树:
AVL树又称为高度平衡的二叉搜索树,是1962年有俄罗斯的数学家G.M.Adel'son-Vel'skii和E.M.Landis提出来的。它能保持二叉树的高度
平衡,尽量降低二叉树的高度,减少树的平均搜索长度。
2、AVL树的性质:1、左子树和右子树的高度之差的绝对值不超过1.
2、树中的每个左子树和右子树都是AVL树。
3、每个节点都有一个平衡因子(balance factor--bf),任一节点的平衡因子是-1,0,1。(每个节点的平衡因子等于右子树的高度减去左子树树的高度 )
3、AVL树的效率
一棵AVL树有N个节点,其高度可以保持在log2N,插入/删除/查找的时间复杂度也是log2N。
插入、删除、查找的实现源码:
#include#include using namespace std; template struct TreeNode { T Data; int height; TreeNode *left; TreeNode *right; TreeNode *parent; TreeNode(T data) :left(NULL) ,right(NULL) ,Data(data) ,parent(NULL) ,height(0) {} }; template class AVLtree { public: AVLtree() {} ~AVLtree() {} AVLtree(T *arr,size_t sz); void Insert(T data) { TreeNode *tmp = new TreeNode (data); _root = tmp; InsertAVLtree(_root,data); } void Search(T data) { TreeNode *tmp = new TreeNode (data); tmp = SearchAVLtree(_root,tmp); if(tmp) cout<<'Y'< * node = MinAVLtree(_root); cout< Data< * node = MaxAVLtree(_root); cout< Data< * node = MaxLeftAVLtree(_root); cout< Data< * node = MinRightAVLtree(_root); cout< Data< *tmp = new TreeNode (data); tmp = SearchAVLtree(_root,tmp); if (tmp) tmp = prevAVLtree(tmp); cout< Data< *tmp = new TreeNode (data); tmp = SearchAVLtree(_root,tmp); if (tmp) tmp = postAVLtree(tmp); cout< Data< *tmp = new TreeNode (data); tmp = SearchAVLtree(_root,tmp); if (tmp) _root= DeleteAVLtree(_root,tmp); } void Destroy() { DestroyAVLtree(_root); } void Mid() { MidOrder(_root); } void MidOrder(TreeNode *Root) { if(Root==NULL) return; MidOrder(Root->left); cout< Data<<" "; MidOrder(Root->right); } int Height(TreeNode *Root) { return HeightTree(Root); } int MAX(int h1, int h2) { return h1 > h2 ? h1 : h2; } void BitreeFloorOrder() { FloorOrder(_root); } protected: TreeNode * RightRightRotate(TreeNode *Node) { TreeNode *cur = Node->left; Node->left = cur->right; cur->right = Node; Node->height = MAX(Height(Node->left),Height(Node->right))+1; cur->height = MAX(Height(cur->left),Height(cur->right))+1; return cur; } TreeNode * LeftLeftRotate(TreeNode * Node) { TreeNode * cur=Node->right; Node->right=cur->left; cur->left=Node; Node->height = MAX(Height(Node->left),Height(Node->right))+1; cur->height = MAX(Height(cur->left),Height(cur->right))+1; return cur; } TreeNode * RightLeftRotate(TreeNode *Node) { Node->right = RightRightRotate(Node->right); return LeftLeftRotate(Node); } TreeNode * LeftRightRotate(TreeNode *Node) { Node->left = LeftLeftRotate(Node->left); return RightRightRotate(Node); } int HeightTree(TreeNode *Node) { if(Node != NULL) return Node->height; } TreeNode * InsertAVLtree(TreeNode *root,T key); TreeNode * SearchAVLtree(TreeNode *Root,TreeNode *Node); TreeNode * MinAVLtree(TreeNode * Root); TreeNode * MaxAVLtree(TreeNode * Root); TreeNode * MaxLeftAVLtree(TreeNode *Root); TreeNode * MinRightAVLtree(TreeNode *Root); TreeNode * prevAVLtree(TreeNode *Node); TreeNode * postAVLtree(TreeNode *Node); TreeNode * DeleteAVLtree(TreeNode *Root, TreeNode *Node); void DestroyAVLtree(TreeNode *Root); void FloorOrder(TreeNode *Root); private: TreeNode * _root; }; template AVLtree ::AVLtree(T *arr,size_t sz) { _root = new TreeNode (arr[0]); for (int i = 1; i < sz; i++) { _root = InsertAVLtree(_root,arr[i]); } } template TreeNode * AVLtree ::InsertAVLtree(TreeNode *root,T key) { if (root == NULL) root = new TreeNode (key); else { if (key < root->Data) { root->left = InsertAVLtree(root->left, key); if (Height(root->left) - Height(root->right) > 1) { if (key < root->left->Data) { root = RightRightRotate(root); } if (key > root->left->Data) { root = LeftRightRotate(root); } } } else if(key > root->Data) { root->right = InsertAVLtree(root->right, key); if (Height(root->right) - Height(root->left) > 1) { if (key > root->right->Data) { root = LeftLeftRotate(root); } if (key < root->right->Data) { root = RightLeftRotate(root); } } } } root->height = MAX(Height(root->left),Height(root->right))+1; return root; } template TreeNode * AVLtree ::SearchAVLtree(TreeNode * Root,TreeNode *Node) { TreeNode *cur=Root; while(cur&&cur->Data!=Node->Data) { if(cur->Data>Node->Data) { cur=cur->left; } else { cur=cur->right; } } if(cur) return cur; else return NULL; } template TreeNode * AVLtree ::MinAVLtree(TreeNode * Root) { TreeNode * cur=Root; while(cur->left) { cur=cur->left; } return cur; } template TreeNode * AVLtree ::MaxAVLtree(TreeNode * Root) { TreeNode * cur=Root; while(cur->right) { cur=cur->right; } return cur; } template TreeNode * AVLtree ::MaxLeftAVLtree(TreeNode *Root) { if (Root->left == NULL) return NULL; TreeNode * cur = Root->left; while(cur->right) { cur = cur->right; } return cur; } template TreeNode * AVLtree ::MinRightAVLtree(TreeNode *Root) { if (Root->right == NULL) return NULL; TreeNode * cur = Root->right; while(cur->left) { cur = cur->left; } return cur; } template TreeNode * AVLtree ::prevAVLtree(TreeNode *Node) { if (Node->left) return MaxLeftAVLtree(Node); TreeNode *P = Node->parent; if (Node->left == NULL && Node == P->right) { return Node->parent; } while (P && Node == P->left) { Node = P; P = P->parent; } return P; } template TreeNode * AVLtree ::postAVLtree(TreeNode *Node) { if (Node->right) return MinRightAVLtree(Node); TreeNode *P = Node->parent; if (Node->right == NULL && Node == P->left) { return Node->parent; } while (P && Node == P->right) { Node = P; P = P->parent; } return P; } template TreeNode * AVLtree ::DeleteAVLtree(TreeNode *Root, TreeNode *Node) { if (Node->Data < Root->Data) { Root->left = DeleteAVLtree(Root->left,Node); if (Height(Root->right) - Height(Root->left) > 1) { TreeNode *cur = Root->right; if (Height(cur->left) < Height(cur->right)) { Root = LeftLeftRotate(Root); } if (Height(cur->left) > Height(cur->right)) { Root = RightLeftRotate(Root); } } } else if (Node->Data > Root->Data) { Root->right = DeleteAVLtree(Root->right, Node); if (Height(Root->left) - Height(Root->right) > 1) { TreeNode *cur = Root->left; if (Height(cur->left) < Height(cur->right)) { Root = LeftRightRotate(Root); } if (Height(cur->left) > Height(cur->right)) { Root = RightRightRotate(Root); } } } else { if (Root->left && Root->right) { if (Height(Root->left) >= Height(Root->right)) { TreeNode *cur = prevAVLtree(Root); Root->Data = cur->Data; Root->left = DeleteAVLtree(Root->left,cur); } if (Height(Root->right) >= Height(Root->left)) { TreeNode *cur = postAVLtree(Root); Root->Data = cur->Data; Root->right = DeleteAVLtree(Root->right,cur); } } else { TreeNode *tmp = Root; Root = Root->left ? Root->left : Root->right; delete tmp; } } return Root; } template void AVLtree ::DestroyAVLtree(TreeNode *Root) { if(Root==NULL) return; if(Root->left) DestroyBStree(Root->left); if(Root->right) DestroyBStree(Root->right); delete Root; Root = NULL; } template void AVLtree ::FloorOrder(TreeNode *Root) { if (Root == NULL) return; queue *> q; TreeNode *cur = Root; q.push(cur); while (!q.empty()) { TreeNode *tmp = q.front(); cout< Data<<" "; q.pop(); if (tmp->left) { q.push(tmp->left); } if (tmp->right) { q.push(tmp->right); } } } void Test() { //int arr[] = {0,1,2,3,4,5,6,7,8,9}; //int arr2[] = {16,3,7,11,9,26,18,14,15}; int arr3[] = {9,8,7,6,5,4,3,2,1,0}; int sz = sizeof(arr3)/sizeof(arr3[0]); AVLtree a(arr3,sz); //a.Mid(); //a.BitreeFloorOrder(); //cout<