Data Structure and algorithm problems AVL binary balancing tree,

Source: Internet
Author: User

Data Structure and algorithm problems AVL binary balancing tree,

The AVL Tree is a binary search tree with a balance condition. This balance condition must be maintained, and it must ensure that the depth of the tree is O (logN ).



An AVL Tree is a binary search tree with a maximum height difference of 1 between the left subtree and the right subtree of each node. (The height of the empty tree is defined as-1 ).


After insertion, only the node balances from the insertion point to the Root Node path may be changed, because only the child trees of these nodes may change. When we go up to the root path along this path and update the balance information, we can find a node whose new balance breaks the AVL condition. We will point out how to rebalance the tree on the first such node (I .e. the deepest node) and prove that this new balance ensures that the entire tree meets AVL features.



Let's call this node a That must be rebalance. Because any node has a maximum of two sons, the height difference between the two Subtrees at the point is 2 in height imbalance. It is easy to see that such imbalance may occur in the following four situations:

1. Insert the left subtree of the Left son of a once.

2. Insert the right subtree of the Left son of

3. Insert the left subtree of the right son of a once.

4. Insert the right subtree of the right son of




The first case is when the insertion occurs outside the tree (that is, the left-left case or the right-right case ).Single RotationAnd complete the adjustment. The second case is insert in the "internal" situation (that is, the left-right case or the right-left case ).Double Rotation.

The AVL Tree is essentially a binary search tree, which features:



  1. The first is a binary search tree.

  2. With a balance condition: the absolute value (equilibrium factor) of the height difference between left and right subtree of each node is 1 at most.


# Include <iostream> using namespace std; const int LH = 1; const int EH = 0; const int RH =-1; bool TRUE = 1; bool FALSE = 0; typedef struct BSTNode {int key; int bf; BSTNode * lchild, * rchild;} BSTNode; // traverse void inordertree (BSTNode * & root) in the middle order {if (root) {inordertree (root-> lchild); cout <root-> key <","; inordertree (root-> rchild );}} // traverse void preordertree (BSTNode * & root) {if (root) {cout <root-> key <","; preorder Tree (root-> lchild); preordertree (root-> rchild) ;}}// right-hand void R_Rotate (BSTNode * & p) {BSTNode * lc = p-> lchild; p-> lchild = lc-> rchild; lc-> rchild = p; p = lc;} // left-hand void L_Rotate (BSTNode * & p) {BSTNode * rc = p-> rchild; p-> rchild = rc-> lchild; rc-> lchild = p; p = rc;} void LeftBalance (BSTNode * & T) {BSTNode * lc = T-> lchild; switch (lc-> bf) {case LH: T-> bf = lc-> bf = EH; R_Rotate (T); break; case RH: BSTNode * rd = lc-> rchil D; switch (rd-> bf) {case LH: T-> bf = RH; lc-> bf = EH; break; case EH: t-> bf = lc-> bf = EH; lc-> bf = LH; break;} rd-> bf = EH; L_Rotate (T-> lchild ); // left-handed R_Rotate (T); break ;}} void RightBalance (BSTNode * & T) {BSTNode * rc = T-> rchild; switch (rc-> bf) {case RH: T-> bf = rc-> bf = EH; L_Rotate (T); break; case LH: BSTNode * ld = rc-> lchild; switch (ld-> bf) {case RH: T-> bf = LH; rc-> bf = EH; break; case EH: t-> bf = rc-> bf = EH; break; cas E LH: T-> bf = EH; rc-> bf = RH; break;} ld-> bf = EH; R_Rotate (T-> rchild); L_Rotate (T ); break;} int insertAVL (BSTNode * & t, int e, bool & taller) {if (! T) {t = new BSTNode; t-> key = e; t-> lchild = t-> rchild = NULL; t-> bf = EH; taller = TRUE ;} else {if (e = t-> key) {taller = FALSE; return 0;} if (e <t-> key) {if (! InsertAVL (t-> lchild, e, taller) return 0; if (taller) {switch (t-> bf) {case LH: LeftBalance (t); taller = FALSE; break; case EH: t-> bf = LH; taller = TRUE; break; case RH: t-> bf = EH; taller = FALSE; break ;}}} else {if (! InsertAVL (t-> rchild, e, taller) return 0; if (taller) {switch (t-> bf) {case RH: RightBalance (t); taller = FALSE; break; case EH: t-> bf = RH; taller = TRUE; break; case LH: t-> bf = EH; taller = FALSE; break ;}}}} return 1;} BSTNode * search (BSTNode * t, int key) {BSTNode * p = t; while (p) {if (p-> key = key) return p; else if (p-> key <key) p = p-> rchild; elsep = p-> lchild;} return p;} int main () {BSTNode * root = NULL; BSTNode * r; bool taller = FALSE; int array [] = {13, 24, 37, 90, 53}; for (int I = 0; I <5; I ++) insertAVL (root, array [I], taller); cout <"inorder traverse... "<endl; inordertree (root); cout <endl; cout <" preorder traverse... "<endl; preordertree (root); cout <endl; cout <" search key... "<endl; r = search (root, 37); if (r) {cout <r-> key <endl ;} else {cout <"not find" <endl;} system ("pause"); return 0 ;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.