Data Structure and algorithm problems AVL binary balancing tree
The AVL Tree is essentially a binary search tree, which features:
The first is a binary search tree.
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
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; // The void inordertree (BSTNode * & root) {if (root) {inordertree (root-> lchild ); cout <root-> key <","; inordertree (root-> rchild) ;}// traverse void preordertree (BSTNode * & root) in the forward order) {if (root) {cout <root-> key <","; preordertree (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-> rchild; switch (rd-> bf) {c Ase 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; case 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 ;}