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 <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 ;}
Data Structure and algorithm problems AVL binary balancing tree