平衡二叉树主要涉及四种情况:左左、右右、左右、右左
四种调整
左左时:
//进行左旋 TreeNode* AvlTree::singleLeft(TreeNode* node1) { TreeNode* node2 = node1->lson; node1->lson = node2->rson; node2->rson = node1; return node2; }
//进行右旋 TreeNode* AvlTree::singleRight(TreeNode* node1) { TreeNode* node2 = node1->rson; node1->rson = node2->lson; node2->lson = node1; return node2; }右左
//先左旋右孩子在进行左旋 TreeNode* AvlTree::doubleRL(TreeNode* node1) { TreeNode* node2 = node1->rson; node1->rson = singleLeft(node2); return singleRight(node1); }
//先左旋左边孩子在右旋 TreeNode* AvlTree::doubleLR(TreeNode* node1) { TreeNode* node2 = node1->lson; node1->lson = singleRight(node2); return singleLeft(node1); }
#include <iostream> #include <stdlib.h> #include <stdio.h> using namespace std; #define max(a,b) (((a) > (b)) ? (a) : (b)) class TreeNode { public: TreeNode():lson(NULL), rson(NULL), hight(0){} virtual ~TreeNode(){} int data; int hight; TreeNode* lson; TreeNode* rson; }; TreeNode* root; class AvlTree { public: AvlTree(){} virtual ~AvlTree(){} void insertAvlTree(TreeNode** node, int data); void searchNode(TreeNode* tree); void balanceTree(TreeNode** node); private: int getBalance(TreeNode* node); int getHight(TreeNode* node); TreeNode* singleLeft(TreeNode* node1); //左左情况 TreeNode* singleRight(TreeNode* node1); //右右情况 TreeNode* doubleLR(TreeNode* node1); //左右情况 TreeNode* doubleRL(TreeNode* node1); //右左情况 }; int AvlTree::getHight(TreeNode* node) { int hight = 0; if(node) { hight = max(getHight(node->lson), getHight(node->rson)) +1; } return hight; } int AvlTree::getBalance(TreeNode* node) { if(node == NULL) return 0; if(node) return getHight(node->lson) - getHight(node->rson); } //进行左旋 TreeNode* AvlTree::singleLeft(TreeNode* node1) { TreeNode* node2 = node1->lson; node1->lson = node2->rson; node2->rson = node1; return node2; } //进行右旋 TreeNode* AvlTree::singleRight(TreeNode* node1) { TreeNode* node2 = node1->rson; node1->rson = node2->lson; node2->lson = node1; return node2; } //先左旋左边孩子在右旋 TreeNode* AvlTree::doubleLR(TreeNode* node1) { TreeNode* node2 = node1->lson; node1->lson = singleRight(node2); return singleLeft(node1); } //先左旋右孩子在进行左旋 TreeNode* AvlTree::doubleRL(TreeNode* node1) { TreeNode* node2 = node1->rson; node1->rson = singleLeft(node2); return singleRight(node1); } //create the balance tree void AvlTree::balanceTree(TreeNode** node) { int hight = getBalance(*node); if(hight > 1) { if(getBalance((*node)->lson) > 0) *node = singleLeft(*node); else *node = doubleLR(*node); } else if(hight < -1) { if(getBalance((*node)->rson) < 0) *node = singleRight(*node); else *node = doubleRL(*node); } } //add node void AvlTree::insertAvlTree(TreeNode** node, int key) { if(*node == NULL) { *node = new TreeNode(); (*node)->data = key; (*node)->lson = (*node)->rson = NULL; } else if((*node)->data > key) { insertAvlTree(&((*node)->lson), key); } else if((*node)->data < key) { insertAvlTree(&((*node)->rson), key); } } void AvlTree::searchNode(TreeNode* node) { if(node == NULL) return; if(node) { printf("%d\n", node->data); } searchNode(node->lson); searchNode(node->rson); } int main(int argc, char *argv[]) { AvlTree tree; tree.insertAvlTree(&root, 24); tree.insertAvlTree(&root, 33); tree.insertAvlTree(&root, 26); tree.insertAvlTree(&root, 29); tree.insertAvlTree(&root, 12); tree.insertAvlTree(&root, 27); tree.insertAvlTree(&root, 18); tree.insertAvlTree(&root, 28); tree.insertAvlTree(&root, 15); printf("the tree:\n"); tree.searchNode(root); tree.balanceTree(&root); printf("balance tree:\n"); tree.searchNode(root); return 0; }