The re-modification made me very depressed. I have already analyzed the text in detail, and I think the analysis is very embarrassing.
Writing and saving failed, and a lot of work was in vain. I am really disappointed with the csdn blog. It is very hard to draw pictures in best4c, but it is also in vain.
Sorry, now let's put the source code for implementation here. If a reader asks me to re-write the algorithm analysis and add a piece, I will continue.
Now, let's make a few changes. The AVL balancing tree is implemented on the basis of the Binary Search Tree, the difference between a binary search tree and a binary search tree is that insert and delete operations.
Insert and delete are recursive,
1. For each recursive insertion, you must determine whether the left and right subtree of the current node is balanced. If the current node is not balanced, the rotation is adjusted. The rotation is divided into left single rotation, Left Double rotation, and right single rotation, and the right double rotation. the four adjustments are implemented by a single function.
2. For each Delete, if the Left and Right nodes of the deleted node are empty:
If the left child is empty, connect it with the right child; otherwise, connect it with the left child.
If the left and right children are not empty
The minimum node exchange value of the current node and Its right subtree, and then recursively deletes the minimum node of the right subtree, for every node in the route to be deleted, a balance adjustment (if necessary) is required. This adjustment is similar to the one in 1, but I put it in a separate function avl_rebanlence (t).
It can be seen that the design and implementation of the AVL Tree is quite elegant, and all functions are implemented in modules.
/*** AVL tree implementation is divided into two files: avltree. h and avltree. c ****/
/** Avltree. h **/
/**********************************
* Author: Felix
* Last Update: Sat Jan 12 06:12:15 est 2008
* Address me and exchange our idea ^ _ ^, QQ: 349871604, e-mail: lzqziliao2004@163.com
* Description:
*
*
*
*/
# Ifndef ___ avltree ___
# Define ___ avltree ___
# Include <stdio. h>
# Include <stdlib. h>
Typedef int avl_element;
Extern struct avl_treenode;
Typedef struct avl_treenode * avl_tree;
Typedef struct avl_treenode * avl_position;
Avl_tree avl_makeempty (avl_tree t );
Avl_position avl_find (avl_element E, avl_tree t );
Avl_position avl_findmin (avl_tree t );
Avl_position avl_findmax (avl_tree t );
Avl_tree avl_delete (avl_element E, avl_tree t );
Avl_tree avl_insert (avl_element E, avl_tree t );
Avl_element avl_retrieve (avl_position P );
# Endif
/** Avltree. c **/
/**********************************
* Author: Felix
* Last Update: Sat Jan 12 06:12:15 est 2008
* Address me and exchange our idea ^ _ ^, QQ: 349871604, e-mail: lzqziliao2004@163.com
* Description:
*
*
*
*/
# Include "avltree. H"
# Define max (A, B) (A> B )? A: B)
Struct avl_treenode
{
Int height;
Avl_element E;
Avl_tree left;
Avl_tree right;
};
Static int height (avl_position P)
{
If (p) return p-> height;
Return-1;
}
/* Rotate function */
Avl_position avl_singlerotateleft (avl_position P)
{
Avl_position TMP;
TMP = p-> left;
P-> left = TMP-> right;
TMP-> right = P;
P-> Height = max (height (p-> left), height (p-> right) + 1;
TMP-> Height = max (height (TMP-> left), p-> height );
Return TMP;
}
Avl_position avl_singlerotateright (avl_position P)
{
Avl_position TMP;
TMP = p-> right;
P-> right = TMP-> left;
TMP-> left = P;
P-> Height = max (height (p-> left), height (p-> right) + 1;
TMP-> Height = max (p-> height, height (TMP-> right ));
Return TMP;
}
Avl_position avl_doublerotateright (avl_position P)
{
P-> right = avl_singlerotateleft (p-> right );
Return avl_singlerotateright (P );
}
Avl_position avl_doublerotateleft (avl_position P)
{
P-> left = avl_singlerotateright (p-> left );
Return avl_singlerotateleft (P );
}
Avl_tree avl_makeempty (avl_tree T)
{
If (t)
{
Avl_makeempty (t-> left );
Avl_makeempty (t-> right );
Free (t );
}
Return NULL;
}
Avl_position avl_find (avl_element E, avl_tree T)
{
If (t)
{
If (E> T-> E) return avl_find (E, T-> right );
Else if (E <t-> E) return avl_find (E, T-> left );
Else return (avl_position) T;
}
Else return NULL;
}
/* Find the minimum element */
Avl_position avl_findmin (avl_tree T)
{
If (t)
While (t-> left) t = T-> left;
Return (avl_position) T;
}
/* Find the maximum element */
Avl_position avl_findmax (avl_tree T)
{
If (t)
While (t-> right) t = T-> right;
Return (avl_position) T;
}
Static avl_tree avl_rebalance (avl_position P)
{
If (height (p-> left)-height (p-> right) = 2)
{
If (height (p-> left)> height (p-> left-> right)/* ll **/
{
Return avl_singlerotateleft (P );
}
Else/* LR */
{
Return avl_doublerotateleft (P );
}
}
Else
If (height (p-> left)-height (p-> right) =-2)
{
If (height (p-> right)> height (p-> right-> left)/* RR **/
{
Return avl_singlerotateright (P );
}/* Rl */
Else
{
Return avl_doublerotateright (P );
}
}
}
Static avl_tree avl_deletemin (avl_tree T)
{
If (t-> left ){
T = avl_deletemin (t-> left );
Return avl_rebalance (t );
}
Else
{
Free (t );
Return NULL;
}
}
Avl_tree avl_delete (avl_element E, avl_tree T)
{
Avl_position P;
If (t)
{
If (E> T-> E) T-> right = avl_delete (E, T-> right );
Else if (E <t-> E) T-> left = avl_delete (E, T-> left );
/* Find It */
Else if (t-> left & T-> right)
{
P = avl_findmin (t-> right );
T-> E = p-> E;
T-> right = avl_deletemin (t-> right );
}
Else
{
P = T;
If (t-> left = NULL) t = T-> right;
Else t = T-> left;
Free (P );
}
}
Return avl_rebalance (t );
}
Avl_tree avl_insert (avl_element E, avl_tree T)
{
If (t = NULL)/* Insert as leaf child */
{
T = (avl_tree) malloc (sizeof (struct avl_treenode ));
T-> E = E;
T-> left = T-> right = NULL;
T-> Height = 0;
}
Else
If (E> T-> E)
{
T-> right = avl_insert (E, T-> right );
If (height (t-> right)-height (t-> left) = 2)
If (E> T-> right-> E) t = avl_singlerotateright (t );
Else t = avl_doublerotateright (t );
} Else
If (E <t-> E)
{
T-> left = avl_insert (E, T-> left );
If (height (t-> left)-height (t-> right) = 2)
If (E <t-> left-> E) t = avl_singlerotateleft (t );
Else t = avl_doublerotateleft (t );
}
T-> Height = max (t-> left), height (t-> right) + 1;
Return T;
}
Avl_element avl_retrieve (avl_position P)
{
Return p-> E;
}
/** The test file includes testavl. c menu_c.h menu_c.c and the drawbittree. h/drawbittree. c implemented by the preceding file (11) (article (12 **/
/** Testavl. c **/
/* The vgalib database may be unstable.
* Author: Felix
* Create Date: Tue Jan 15 20:14:10 est 2008
* Last Update: Tue Jan 15 20:14:10 est 2008
* Description:
*
*/
# Include "menu_c.h"
# Include <stdio. h>
# Include <stdlib. h>
# Include "avltree. H"
# Include "drawbittree. H"
Struct avl_treenode
{
Int height;
Avl_element E;
Avl_tree left;
Avl_tree right;
};
/***** Define some function to show the tree *******/
Int getkey (void * t)
{
Return (avl_tree) T)-> E;
}
Void * getchild (void * t, int Mode)
{
If (mode = db_leftchild) Return (avl_tree) T)-> left;
Else return (avl_tree) T)-> right;
}
/*****************/
Int main ()
{
Int N;
Avl_tree T;
Avl_position P;
T = avl_makeempty (null );
While (select ())
{
Switch (selection)
{
Case '1 ':
Printf ("INTEGER:> ");
Scanf ("% d", & N );
T = avl_insert (n, t );
Break;
Case '2 ':
Printf ("integer to delete:> ");
Scanf ("% d", & N );
If (! Avl_find (n, t) printf ("sorry, no integer match ");
Else {
T = avl_delete (n, t );
Printf ("deletion done ");
}
Break;
Case '3 ':
While (P = avl_findmin (t ))
{
Printf ("% d", avl_retrieve (P), t );
T = avl_delete (avl_retrieve (P), t );
}
Break;
Case '4 ':
While (P = avl_findmax (t ))
{
Printf ("% d", avl_retrieve (p ));
T = avl_delete (avl_retrieve (P), t );
}
Break;
Case '5 ':
If (! (P = avl_findmax (t) printf ("sorry, the tree is null ");
Else
Printf ("% d", avl_retrieve (p ));
Break;
Case '6 ':
If (! (P = avl_findmin (t) printf ("sorry, the tree is null ");
Else
Printf ("% d", avl_retrieve (p ));
Break;
Case '7 ':
Db_drawbittree (T, getchild, getkey );
Break;
Case '9 ':
System ("less ../avl_tree.h ");
Break;
Default: break;
}
}
Return 0;
}
/** Menu_c.h **/
/*///////////////////////
* Author: Felix
* Create Date: Fri Jan 11 03:45:18 est 2008
* Last Update: Fri Jan 11 03:45:18 est 2008
* Description:
*
*
*/////////////////////
# Include <stdio. h>
# Ifndef _ menu ____
# DEFINE _ menu ____
# Define select () (___ sel = ___ select ())! = '0 ')
# Define selection ___ Sel
Char ___ sel;
Char ___ select ();
/*
Define the menu:
*/
# Endif
/** Menu_c.c **/
/*///////////////////////
* Author: Felix
* Create Date: Fri Jan 11 03:45:18 est 2008
* Last Update: Fri Jan 11 03:45:18 est 2008
* Description:
*
*
*/////////////////////
# Include "menu_c.h"
Char * ___ menu [] = {
"1. Insert a integer .",
"2. delete an integer from the tree .",
"3. sort and show from small to big (will delete the tree )",
"4. sort and show from big to small (will delete the tree )",
"5. Show the Max ",
"6. Show the min", "9. Print the file st_tree.h ",
"7. Show the tree ",
"0. Exit ",
Null
};
Void ___ showmenu ()
{
Printf ("Please select:/N ");
Char ** P = ___ menu;
While (* P) printf ("% s/n", * P ++ );
Printf (":> ");
}
Char ___ select ()
{
Char C;
___ Showmenu ();
While (C = getchar () = '/N ');
Return C;
}
/** Implementation of drawbittree. h and drawbittree. C :***/
Http://blog.csdn.net/liuzongqiang/archive/2008/01/16/2046194.aspx