平衡二叉树

 

/*******  avl.h **********/

《平衡二叉树》
《平衡二叉树》

 1 /***********************************************************/
 2 /* Copyright (C) SA14226214, USTC, 2014-2015               */
 3 /*                                                         */
 4 /*  FILE NAME             :  avl.h                         */
 5 /*  PRINCIPAL AUTHOR      :  GaoZhipeng                    */
 6 /*  SUBSYSTEM NAME        :  AVL_Tree                      */
 7 /*  MODULE NAME           :  AVL_Tree                      */
 8 /*  LANGUAGE              :  C                             */
 9 /*  TARGET ENVIRONMENT    :  ANY                           */
10 /*  DATE OF FIRST RELEASE :  2015/05/09                    */
11 /*  DESCRIPTION           :  This is a bst_tree program    */
12 /***********************************************************/
13 
14 /*
15  *Revision log:
16  *
17  *Ceated by GaoZhipeng, 2015/05/09
18  *     
19  */
20 
21 #ifndef _AVL_TREE_H
22 #define _AVL_TREE_H
23 
24 #define ElementType int 
25 
26 struct AvlNode;
27 typedef struct AvlNode *Position;
28 typedef struct AvlNode *AvlTree;
29 
30 
31 AvlTree MakeEmpty( AvlTree T );
32 Position Find( ElementType X, AvlTree T );
33 Position FindMin( AvlTree T );
34 Position FindMax( AvlTree T );
35 AvlTree Insert( ElementType X, AvlTree T );
36 //AvlTree Delete( ElementType X, AvlTree T );
37 //ElementType Retrieve( Position P );
38 
39 static Position SingleRotateWithLeft( Position K2 );
40 static Position SingleRotateWithRight( Position K1 );
41 static Position DoubleRotateWithLeft( Position K3 );
42 static Position DoubleRotateWithRight( Position K1 );
43 
44 
45 #endif

View Code

 

/******** avl.c **********/

《平衡二叉树》
《平衡二叉树》

  1 /***********************************************************/
  2 /* Copyright (C) SA14226214, USTC, 2014-2015               */
  3 /*                                                         */
  4 /*  FILE NAME             :  avl.c                         */
  5 /*  PRINCIPAL AUTHOR      :  GaoZhipeng                    */
  6 /*  SUBSYSTEM NAME        :  AVL_Tree                      */
  7 /*  MODULE NAME           :  AVL_Tree                      */
  8 /*  LANGUAGE              :  C                             */
  9 /*  TARGET ENVIRONMENT    :  ANY                           */
 10 /*  DATE OF FIRST RELEASE :  2015/05/09                    */
 11 /*  DESCRIPTION           :  This is a bst_tree program    */
 12 /***********************************************************/
 13 
 14 /*
 15  *Revision log:
 16  *
 17  *Ceated by GaoZhipeng, 2015/05/09
 18  *     
 19  */
 20 
 21 #include<stdio.h>
 22 #include<malloc.h>
 23 #include"avl.h"
 24 
 25 struct AvlNode
 26 {
 27     ElementType Element;
 28     AvlTree Left;
 29     AvlTree Right;
 30     int Height;
 31 };
 32 
 33 static int 
 34 Height( Position P ) 
 35 {
 36     if(P == NULL) 
 37     {
 38         return -1;
 39     }
 40     else return P->Height;
 41 }
 42 
 43 
 44 AvlTree MakeEmpty( AvlTree T )
 45 {
 46     if(T != NULL) 
 47     {
 48         MakeEmpty(T->Left);
 49         MakeEmpty(T->Right);
 50         free(T);
 51     }
 52     return NULL;
 53 }
 54 
 55 
 56 Position 
 57 Find( ElementType X, AvlTree T ) 
 58 {
 59     if(T == NULL) 
 60     {
 61         return NULL;
 62     }
 63     if(T->Element < X) 
 64     {
 65         Find(X, T->Left);
 66     }
 67     else if(T->Element > X)
 68     {
 69         Find(X, T->Right);
 70     }
 71     else return T;
 72 }
 73 
 74 
 75 Position 
 76 FindMin( AvlTree T )
 77 {
 78     if(T == NULL)
 79     {    
 80         return NULL;
 81     }
 82     else 
 83     {
 84         if(T->Left == NULL) 
 85         {
 86             return T;
 87         }
 88         else 
 89         {
 90             FindMin( T->Left );
 91         }
 92     }
 93 }
 94 
 95 Position 
 96 FindMax( AvlTree T)
 97 {
 98     if(T == NULL) 
 99     {
100         return NULL;
101     }
102     else 
103     {
104         if(T->Right == NULL) 
105         {
106             return T;
107         }
108         else 
109         {
110             FindMax(T->Right);
111         }
112     }
113 }
114 
115 
116 AvlTree 
117 Insert( ElementType X, AvlTree T ) 
118 {
119     if(T == NULL) 
120     {
121         T = malloc( sizeof(struct AvlNode) );
122         if(T == NULL)
123         {
124             printf("Out of space!!!");
125         }
126         else 
127         {
128             T->Element = X;
129             T->Height = 0;
130             T->Left = T->Right = NULL;
131         }
132     }
133     else if (X < T->Element) 
134     {
135         T->Left = Insert(X, T->Left);
136         if( Height(T->Left) - Height(T->Right) == 2 )
137         {
138             if( X < T->Left->Element ) 
139             {
140                 T = SingleRotateWithLeft(T);
141             }
142             else 
143             {
144                 T = DoubleRotateWithLeft(T);
145             }
146         }
147     }
148     else if (X > T->Element) 
149     {
150         T->Right = Insert(X, T->Right);
151         if( Height(T->Right) - Height(T->Left) == 2) 
152         {
153             if(X > T->Right->Element)
154             {
155                 T = SingleRotateWithRight(T);
156             } 
157             else 
158             {
159                 T = DoubleRotateWithRight(T);
160             }
161         }
162     }
163 
164     T->Height = Max(Height(T->Left), Height(T->Right)) + 1;
165     return T;
166 }
167 
168 static Position 
169 SingleRotateWithLeft( Position K2 )
170 {
171     Position K1;
172      
173     K1 = K2->Left;
174     K2->Left = K1->Right;
175     K1->Right = K2;
176     
177     K2->Height = Max( Height(K2->Left), Height(K2->Right) ) + 1;
178     K1->Height = Max( Height(K1->Left), Height(K1->Left) ) + 1;
179     return K1;
180 }
181 
182 static Position 
183 SingleRotateWithRight( Position K1 ) 
184 {
185     Position K2;
186 
187     K2 = K1->Right;
188     K1->Left = K2->Right;
189     K2->Right = K1;
190 
191     K1->Height = Max( Height(K1->Left), Height(K1->Right) ) + 1;
192     K2->Height = Max( Height(K2->Left), Height(K2->Right) ) + 1;
193 
194     return K2;
195 }
196 
197 
198 static Position 
199 DoubleRotateWithLeft(Position K3)
200 {
201     Position K1; 
202 
203     K1 = K3->Left;
204     K3->Left = SingleRotateWithRight( K1 );
205 
206     return SingleRotateWithLeft(K3);
207 }
208 
209 static Position 
210 DoubleRotateWithRight(Position K1) 
211 {
212     Position K3;
213 
214     K3 = K1->Right;
215     K1->Right = SingleRotateWithLeft(K3);
216 
217     return SingleRotateWithRight(K1);
218 }
219 
220 int Max( int H1, int H2 )
221 {
222     return H1>H2 ? : H1, H2;
223 }

View Code

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/beyond-Acm/p/4428350.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞