The balanced binary sort tree inserts a new element recursive algorithm, or more complex, especially the implementation of the code want to understand or to go step-by-step to manually execute the code. Personally understand this algorithm and look at the sample code is also a lot of effort, understanding level is still the primary stage. In short, it is necessary to do more to their own practice in order to better understand.
#include <stdio.h> #include <malloc.h> #define OK 1 #define ERROR 0 #define OVERFLOW-1 #define LH +1
Zogao #define EH 0//equal height #define RH-1//Right high typedef int STATUS;
typedef int TELEMTYPE;
typedef struct bstnode{telemtype data field int bf;//node balance factor struct Bstnode *lchild, *rchild;//left and right child pointer
}bstnode, *bstree;
Right-rotating void r_rotate (Bstree &p);
L-void L_rotate (Bstree &p);
Left balance treatment void leftbalance (Bstree &t);
The right balance deals with void rightbalance (Bstree &t);
The two-fork balance tree Inserts a new element int Insertavl (Bstree &t, Telemtype E, bool &taller);
The first sequence traverses the nodes of the binary tree status Preordertraverse (Bstree T, status (* Visit) (Telemtype e));
Print output node data Status Visit (Telemtype e);
int main () {int array[] = {30,39,21,25,24};
Bstree T = NULL;
BOOL Taller = true;
for (int i = 0; i < 5; i++) {Insertavl (T, Array[i], taller);
Status status = Preordertraverse (T, Visit);
printf ("%d\n", status);
return 0; } void R_rotate (Bstree &p{///*p The two-fork sort tree with the root of the handle, after which p points to the new root node, i.e. the root node of the left subtree before the rotation//processing.
Bstree LC; LC = p->lchild; LC Point P Zogen Node P->lchild = lc->rchild;
The right sub hung of LC is followed by the left subtree of p lc->rchild = p; p = LC;
P points to the new root node} void L_rotate (Bstree &p) {//to the left of the two-fork sort tree with *p root, and after processing p points to the new root node, which is the root node of the right subtree before the rotation//processing.
Bstree RC; rc = p->rchild; RC point P to the right Zishugen node P->rchild = rc->lchild;
The left sub-hung of RC is connected with P's right subtree rc->lchild = p; p = RC;
P points to the new root node} void Leftbalance (Bstree &t) {//to the left balance rotation of the two-fork tree with the root of pointer T, and///pointer t to the new root node at the end of this algorithm.
Bstree Lc,rd; LC = t->lchild; LC Point to *t Zogen node switch (LC->BF) {//Check the balance of the *t left subtree, and balance the case LH://The new node is inserted on the left subtree of the left child of *t, to be treated as a single right rotation T-&G
T;BF = LC->BF = EH;
R_rotate (T);
Break Case RH://new node inserted on the right subtree of *t's left child, to be treated as a double spin rd = lc->rchild;
Rd points to *t's left child's right son root switch (RD->BF) {//modify *t and its left child's balance factor case LH:T->BF = RH;
LC->BF = EH; Break
Case EH:T->BF = LC->BF = EH;
Break
Case RH:T->BF = EH;
LC->BF = LH;
} RD->BF = EH; L_rotate (T->lchild); The Zuozi of *t is treated as the r_rotate (T) of the left rotation balance;
*t for right rotation balance processing}} void Rightbalance (Bstree &t) {//Right balance rotation for a two-tree with the root of pointer T, and///pointer T points to a new root node at the end of this algorithm
Bstree Rc,rd; rc = t->rchild;
RC points to *t right Zishugen node switch (RC->BF) {//Check the balance of the right subtree of *t, and do the corresponding balance processing case RH://new node inserted in the right subtree of the right child of *t, to be treated as Tanzoo
T->BF = RC->BF = EH;
L_rotate (T);
Break Case LH://The new node is inserted on the left subtree of the right child of *t, to be treated as a double spin rd = rc->lchild;
Rd points to *t's right child's Zogen switch (RD->BF) {//modify *t and its right child's balance factor 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;
} RD->BF = EH; R_rotate (T->rchild); The right *t of the right sub-tree is treated l_rotatE (T); *t as a left-balance processing} int Insertavl (Bstree &t, Telemtype E, bool &taller) {//If there are no nodes in the balanced two-fork sort tree t that have the same key as E , a new node with a//data element of E is inserted and 1 is returned, otherwise 0 is returned.
If the two-fork sorting tree//Balance is lost because of insertion, the balance rotation is done, and the Boolean variable taller reflects t long height or not. if (!
T) {//Insert new node, tree "long height", set taller to 1 T = (bstree) malloc (sizeof (Bstnode));
T->data = e;
T->lchild = T->rchild = NULL;
T->BF = EH;
Taller = true;
}else{if (e = = T->data) {//the node that already exists in the tree and E has the same keyword is no longer inserted into taller = false;
return 0; if (E < T->data) {//should continue to search in the left subtree of *t if (!
Insertavl (T->lchild,e,taller))//not inserted return 0;
if (taller) {//has been inserted into the left subtree of the *t and the left subtree "long high" switch (T->BF)//Check the balance of *t
{Case LH://The original Supi right subtree is high and needs to be treated as left balance leftbalance (T); Taller = false;
Sign does not have a long high break; Case EH://The original left, right subtree and so on, now because of the higher left subtree to increase the tree T->BF = LH; Taller = true;
Sign long high break;
Case RH://The original right subtree is higher than the left son tree, now left, right subtree, such as high T->BF = EH; Taller = false;
Sign does not have a long high break; }else{//should continue searching in the right subtree of *t if (! Insertavl (T->rchild,e,taller))//not inserted {return 0; } if (taller) {//is inserted into the right subtree of T and the right subtree "long high" switch (T->BF)//Check the balance of T (case L) H:T->BF = EH;
Originally Supi right son Tree High, now left, right subtree and so high taller = false;
Break
Case EH://original Left, right subtree and so high, now because of the right subtree increased tree T->BF = RH;
Taller = true;
Break
Case RH://The original right subtree is higher than the left subtree, it needs to be treated as right balance rightbalance (T);
Taller = false;
}//switch}//else}//else} return 1; }//First-order traversal status Preordertraverse (Bstree T, status (* Visit) (Telemtype e)) {if (t) {Visit
(T->data); Preordertraverse (T->lchild, Visit);
Preordertraverse (T->rchild, Visit);
return OK;
}//Node access Status Visit (Telemtype e) {printf ("%d\n", e);
return OK; }
Running environment: Win7 flagship 32-bit, Microsoft Visual C + + 6.0
Reference: "Data structure (c language Edition)" Min Wu Weiming Editor