Balanced binary tree balance factor bf calculation of data structure

Source: Internet
Author: User

After reading the code of the balance binary tree in the book, we find that the wisdom of the predecessors is infinite. But because the most perfect code given at once makes people sometimes don't understand ...

Later, after careful scrutiny, only slowly discovered the mystery. At first, I did not know how the balance of the balance of binary tree bf was modified, and later found about the balance of binary tree The most important sentence: in the process of building a balanced binary tree, whenever a node is inserted, the first check whether the balance of the tree is broken by insertion, if, then find the smallest unbalanced subtree, The relationship is adjusted under the precondition of preserving binary sorting tree.

This sentence means: As long as the damage to the balance, immediately modify the binary tree re-balance, meaning that as long as the minimum balance of the tree can be modified to make the whole binary tree rebalance.

The following is an explanation of the code in the book that follows the code

[CPP] View plaincopy 1. #include "stdio.h" 2. #include "Stdlib.h" 3. #include "IO.h" 4. #include "math.h" 5.   
	#include "time.h" 6. 7. #define OK 1 8. #define ERROR 0 9. #define TRUE 1 10. #define FALSE 0 11.   
	#define MAXSIZE 100/* Storage space Initial allocation */12. typedef int STATUS;   
	/* Status is the type of the function whose value is the function result status code, such as OK, etc. */14. 15.16. /* Two fork tree two-fork linked list node structure definition */17. typedef struct BITNODE/* Node structure */18.   {. int data;     /* Node Data */20. int BF;     /* Balance factor for Node */21.    struct Bitnode *lchild, *rchild; /* Right and left child hands */22.  
	} Bitnode, *bitree;   
	23.24. 25./* Right-click on the two-fork sorting tree with P root, */26. /* After processing p points to the new root node, which is the roots node of the left subtree before the rotation process */27. void R_rotate (Bitree *p) 28.  
	{Bitree L; L= (*p)->lchild;     /* l points to P's Zogen node */31. (*p)->lchild=l->rchild;     /* The right sub-rime of L is connected to the left subtree of p */32.  
	L->rchild= (*P); *p=l; /* p points to the new root node */34.   
	} 35. 36./* The two-fork sorting tree with P root is left-handed, */37. /* ProcessingThen p points to the new root node, which is 0 */38 of the roots of the right subtree before the rotation process. void L_rotate (Bitree *p) 39.  
	{Bitree R; R= (*p)->rchild;     /* r points to P's right subtree node */42. (*p)->rchild=r->lchild;     /* The left sub-Rime of R is connected to the right subtree of p */43.  
	R->lchild= (*P); *p=r; /* p points to the new root node */45.   
	} 46. #define LH +1/* Left high */48. #define EH 0/* Equal height */49.   
	#define RH-1/* Right high */50. 51./* Make a left balance rotation for the two-fork tree that is the root of the pointer T Point (*/52). /* At the end of the algorithm, the pointer T points to the new root node */53. void Leftbalance (Bitree *t) 54.  
	{Bitree L,LR; L= (*t)->lchild;     /* l points to T's Zogen node */57.     Switch (L->BF) 58.          {/* Check the balance of the left subtree of T and make the appropriate balance */59.             Case LH:/* New node inserted in T left child's left child tree, to be single right-handed processing */60.  
	(*t)->bf=l->bf=eh;  
	R_rotate (T);  
	A. break;             Case RH:/* New node inserted in T's left child's right subtree, to be double-spin */64. lr=l->rchild;             /* LR points to the right sub-root of T's left child */65.             Switch (LR->BF) 66. {/* Modify T and its left child's flatScale factor */67.  
	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;.  
	A. break;             75.} 76.  
	lr->bf=eh; L_rotate (& (*t)->lchild);             /* The Zuozi of T is treated as a left-handed balance */78. R_rotate (T);     /* Right balance treatment for T */79. } 80.   
	} 81. 82./* The right balance rotation is handled for the two-fork tree that is the root of the pointer t Point, */83. /* At the end of the algorithm, the pointer T points to the new root node */84. void Rightbalance (Bitree *t) 85.  
	{Bitree r,rl; R= (*t)->rchild;     /* R points to the right subtree node of T */88.     Switch (R->BF) 89.      {/* Check the balance of the right subtree of T and make the appropriate balance */90.               Case RH:/* New node inserted in T right child's right subtree, for Tanzoo processing */91.  
	(*t)->bf=r->bf=eh;  
	L_rotate (T); "Break"; 
	94. Case LH:/* New node inserted in T right child's left sub-tree, to be double-spin */95. rl=r->lchild;               /* RL points to T's right child's Zogen */96.               Switch (RL->BF) 97.                 {/* Modify the balance factor for T and its right child */98.  
	Case RH: (*t)->bf=lh;  
	r->bf=eh;.  
	break;  
	101. Case EH: (*t)->bf=r->bf=eh;  
	102. Break;  
	103. Case LH: (*t)->bf=eh;  
	104. r->bf=rh;  
	break;               106.} 107.  
	rl->bf=eh; 108. R_rotate (& (*t)->rchild);               /* Right-balance the right sub-tree of T */109. L_rotate (T);     /* Left-hand balance treatment for T */110. } 111.   
	} 112. 113./* If there are no nodes with the same keyword in the balanced two-fork sort tree T, insert a/* 114. /* The data element is a new node of E and returns 1, otherwise 0 is returned. If the two forks are sorted by inserting the tree */115. /* Loss of balance, then balance rotation processing, Boolean variable taller reflect T length high or not. */116. Status Insertavl (bitree *t,int e,status *taller) 117. {   
	118. if (!*t) 119.          {/* Insert new node, tree "Long High", set taller to True */120.  
	*t= (bitree) malloc (sizeof (Bitnode)); 121. (*t)->data=e; (*t)->lchild= (*t)->rchild=null;  
	(*t)->bf=eh;  
	122. *taller=true;     123.} 124.     Else 125.         {126. if (e== (*t)->data) 127.             A node that already exists in the {/* tree and E has the same keyword is no longer inserted */128. *taller=false;  
	return FALSE;         129.} 130.         if (e< (*t)->data) 131.             {/* should continue to search in the left subtree of T */132. if (!                 Insertavl (& (*T)->lchild,e,taller)/* * NOT inserted/*/133.  
	return FALSE;                 134. if (*taller)/* is inserted into the left subtree of T and the left subtree is "tall" */135.                 Switch ((*t)-&GT;BF)/* Check the balance of T */136.                             {137. Case LH:/* Originally Supi Right sub-tree high, need to be left balanced */138. Leftbalance (T); *taller=false;  
	Break    139. Case EH:/* Originally left and right sub-tree, etc., now because of the increase in the left sub-tree to increase the tree */140.                         (*t)->bf=lh; *taller=true;  
	Break                             141. Case RH:/* Originally the right subtree is taller than the left subtree, now the left and right sub-trees are high */142. (*t)->bf=eh; *taller=false;  
	Break         143.} 144.         } 145.         Else 146.             {/* should continue to search in the right subtree of T */147. if (!                 Insertavl (& (*T)->rchild,e,taller)/* * NOT inserted/*/148.  
	return FALSE;                 149. if (*taller)/* is inserted into the right subtree of T and the right subtree is "long high"/150.                 Switch ((*t)-&GT;BF)/* Check the balance of T */151.                             Case LH:/* Originally Supi Right sub-tree high, now left, right subtree, such as high */153. (*t)->bf=eh;  *taller=false;  
	Break                             154. Case EH:/* Originally left and right subtree, etc., is now due to the increase in the right subtree to increase the tree */155. (*t)->bf=rh; *taller=true;  
	Break                             156. Case RH:/* The original right subtree is taller than the left subtree and needs to be treated as a right balance */157. Rightbalance (T); *taller=false;  
	Break    158.             } 159.     } 160.     } 161.  
	return TRUE;   
	162.} 163. 164. int main (void) 165.  
	{166. int i;  
	167. Int a[10]={3,2,1,4,5,6,7,10,9,8};  
	168. Bitree T=null;  
	169. Status Taller;     171. for (i=0;i<10;i++).  
	{172. INSERTAVL (&t,a[i],&taller);     173.} 174.  
	printf ("This sample suggests breakpoint tracking to view the balanced binary tree structure");  
	175. return 0;  
 176.}
1. With regard to the Insertavl method, it is necessary to note that it uses the idea of recursion, which modifies the balance factor from the bottom to the parent class, without calculating the bf for each node, only according to the height difference between the Zuozi and the right subtree. As long as a damage to the balance on the modification, so the number of balance factor can only be-2,-1, 0, 1, 2 the number of the value. So the current balance factor can be determined by the height difference before insertion and the position after insertion (left subtree or right subtree). If you break the balance, call the **balance function, adjust the balance, and set taller to false, because the balance has been adjusted and the height has not changed, so all fathers above this node do not have to modify their balance factor.

2. About the balance method, take leftbalance as an example to illustrate

① first, the reason for calling Leftbalance is that the depth of the left subtree before insertion is greater than the depth of the right subtree, and now the insertion is in the left subtree, so the depth of the Zuozi is more than 2 of the depth of the right subtree, i.e. the balance factor of the smallest unbalanced tree is 2

② because it is inserted in the left subtree of the vertex t of the smallest unbalanced tree, it is necessary to compare the symbol of the balance factor of the vertex T with the left subtree L, and if it is consistent, make a simple right rotation, and if not, make a left spin on its left subtree, and then rotate the smallest unbalanced tree t to the right. --that is, when the balance factor of the left dial hand tree L is 1 o'clock (LH), a simple right rotation is performed, and 1 (RH) is left-rotated and the smallest unbalanced tree T is rotated to the right.

③ about the change of the balance factor of the right rotation of the Zuozi with the left rotation of the first and the smallest unbalanced tree. Because it involves the left rotation of the subtree L, the right subtree LR of l will be affected, so there will be different changes depending on the balance factor of LR .


A. When the equilibrium factor of LR is LH (equivalent to 1), the equilibrium factor of T becomes -1,l and the balance of the balance becomes 0.


B.. When the equilibrium factor of LR is eh (equivalent to 0), the balance factor of T is changed into a 0,l of 0 .


c.. When the equilibrium factor of LR is RH (equivalent to-1), the equilibrium factor of T becomes 0,l and the balance becomes 1.



may think: This is only a special case, in fact, it is not. Because each rotation is affected by only a few points, the position of the other points changes, but is changed in the whole way, so the balance factor of the other points does not change. Rightbalance and leftbalance form symmetry, so they don't draw.

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.