Balanced binary tree implementation algorithm
/* First, the balanced binary tree is a binary sorting tree. Its basic idea is: when a node is inserted during the construction of the binary sorting tree, first, check whether the tree balance is damaged due to insertion. If yes, find the minimum unbalanced tree and perform adaptive rotation to make it a new balanced binary tree. */# Include
# Include
# Define LH 1 # define EH 0 # define RH-1 using namespace std; typedef struct BTNode {int data; int BF; // balance factor (balance factor) struct BTNode * lchild, * rchild;} BTNode, * BTree; void R_Rotate (BTree * p) // right rotation of the binary sorting tree with p as the root node {BTree L; L = (* p) -> lchild; (* p)-> lchild = L-> rchild; L-> rchild = (* p); * p = L; // p points to the new root node} void L_Rotate (BTree * p) // rotate left the binary sorting tree where p is the root node {BTree R; R = (* p) -> rchild; (* p)-> rchild = R-> lchild; R-> lchild = (* p); * p = R;} void Left Balance (BTree * T) {BTree L, Lr; L = (* T)-> lchild; switch (L-> BF) {// check the left subtree Balance of T, and perform corresponding balancing processing case LH: // Insert the new node into the left subtree of the child t, and perform single-right-hand processing (* T) -> BF = L-> BF = EH; R_Rotate (T); break; case RH: // The newly inserted node is on the right tree of the left child of T, perform double spin processing Lr = L-> rchild; switch (Lr-> BF) {case LH :( * T)-> BF = RH; L-> BF = EH; break; case EH :( * T)-> BF = L-> BF = EH; break; case RH :( * T)-> BF = EH; L-> BF = LH; break;} Lr-> BF = EH; L_Rotate (& (* T)-> lchild); R_Rotate (T) ;}} void RightBalance (BTree * T) {BTree R, rl; R = (* T)-> rchild; switch (R-> BF) {case RH: // The new node is inserted in the right tree of the right child of T, do a single left-hand processing (* T)-> BF = R-> BF = EH; L_Rotate (T); break; case LH: // The new node is inserted in the left Tree of the right child of T, and the dual-Rotation Processing Rl = R-> lchild; switch (Rl-> BF) {case LH :( * T) -> BF = EH; R-> BF = RH; break; case EH :( * T)-> BF = R-> BF = EH; break; case RH :( * T) -> BF = LH; R-> BF = EH; break;} Rl-> BF = EH; R_Rotate (& (* T)-> rchild ); rochelle rotate (T) ;}} bool InsertAVL (BTree * T, int e, bool * taller) // variable taller response T Length height or not {if (! * T) {* T = (BTree) malloc (sizeof (BTNode); (* T)-> data = e; (* T)-> lchild = (* T) -> rchild = NULL; (* T)-> BF = EH; * taller = true;} else {if (e = (* T)-> data) // do not insert {* taller = false; return false;} if (e <(* T)-> data) {if (! InsertAVL (& (* T)-> lchild, e, taller) // return false is not inserted; if (* taller) // to insert the left subtree, and the left subtree becomes higher {switch (* T)-> BF) {case LH: // The original left subtree is higher than the right subtree and requires left-balanced processing LeftBalance (T ); * taller = false; break; case EH: // The height of the left and right subtree. The height of the Left subtree increases (* T)-> BF = LH; * taller = true; break; case RH: // the original right subtree is higher than the left subtree, and the height (* T) of the left and right subtree (* T)-> BF = EH; * taller = false; break ;}} else {// search for if (! InsertAVL (& (* T)-> rchild, e, taller) return false; if (* taller) // Insert the right subtree, and the height of the right subtree {switch (* T)-> BF) {case LH: // The original height of the Left subtree is higher than that of the right subtree, and the height of the left and right subtree is now (* T) -> BF = EH; * taller = false; break; case EH: // height of the original left and right subtree; now the height of the right subtree (* T)-> BF = RH; * taller = true; break; case RH: // originally, the right subtree is higher than the left subtree. Now, you need to perform right balancing (T); * taller = false; break ;}}}return true;} bool Find (BTree T, int key) {if (! T) return false; else if (T-> data = key) return true; else if (T-> data
Rchild, key); elsereturn Find (T-> lchild, key);} void Output (BTree T) {if (T) {printf ("% d ", t-> data); if (T-> lchild | T-> rchild) {printf ("); Output (T-> lchild); printf (", "); Output (T-> rchild); printf (") ") ;}} int main (int argc, char * argv []) {int I; int A [] = {,}; BTree T = NULL; bool taller; for (I = 0; I