看到网上完整的AVL树操作集较少,索性自己写了一个,望大佬指教!
不多废话,上代码:
AVLTREE.h头文件
1 #pragma once 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <assert.h> 5 6 struct AVLNode; 7 typedef struct AVLNode* Position; 8 typedef struct AVLNode* AVLTree; 9 typedef int ElementType; 10 11 //清空AVL树 12 void MakeEmpty(AVLTree T); 13 //查找 14 Position Find(AVLTree T, ElementType X); 15 //递归实现查找最大元位置 16 Position FindMin(AVLTree T); 17 //非递归实现查找最小元位置 18 Position FindMax(AVLTree T); 19 //插入 20 AVLTree Insert(AVLTree T, ElementType X); 21 //删除 22 AVLTree Delete(AVLTree T, ElementType X); 23 //遍历 24 ElementType Retrieve(Position P); 25 //打印AVL树 26 void PrintTree(AVLTree T); 27 28 struct AVLNode 29 { 30 ElementType Data; 31 AVLTree Left; 32 AVLTree Right; 33 int Height; 34 };
AVLTREE.c源文件
1 #define _CRT_SECURE_NO_WARNINGS 2 #include "AVLTREE.h" 3 4 static int Height(Position P); 5 static int Max(int a, int b); 6 static Position SingleRotationWithLeft(Position K2); 7 static Position doubleRotationWithLeft(Position K3); 8 static Position SingleRotationWithRight(Position K2); 9 static Position doubleRotationWithRight(Position K3); 10 11 //计算AVL节点的高度 12 static int Height(Position P) 13 { 14 if (P == NULL) 15 { 16 return -1; 17 } 18 else 19 { 20 return P->Height; 21 } 22 } 23 24 //清空AVL树 25 void MakeEmpty(AVLTree T) 26 { 27 if (T != NULL)//递归终止 28 { 29 MakeEmpty(T->Left); 30 MakeEmpty(T->Right); 31 free(T); 32 } 33 } 34 35 //查找 36 Position Find(AVLTree T, ElementType X) 37 { 38 if (T == NULL) 39 { 40 printf("Element %d is not found!\n", X); 41 } 42 else if (X < T->Data) 43 { 44 return Find(T->Left, X); 45 } 46 else if (X > T->Data) 47 { 48 return Find(T->Right, X); 49 } 50 //不然就是找到了,不进行任何操作 51 return T; 52 } 53 54 //递归实现查找最小元位置 55 Position FindMin(AVLTree T) 56 { 57 if (T == NULL) 58 { 59 return NULL; 60 } 61 else if (T->Left == NULL) 62 { 63 return T; 64 } 65 else 66 { 67 return FindMin(T->Left); 68 } 69 } 70 71 //非递归实现查找最大元位置 72 Position FindMax(AVLTree T) 73 { 74 if (T != NULL) 75 { 76 while (T->Right != NULL) 77 { 78 T = T->Right; 79 } 80 } 81 return T; 82 } 83 84 //返回两者之间的较大值 85 static int Max(int a, int b) 86 { 87 return (a > b ? a : b); 88 } 89 90 //左单旋转(LL型状态) 91 static Position SingleRotationWithLeft(Position K2) 92 { 93 Position K1; 94 95 K1 = K2->Left; 96 K2->Left = K1->Right; 97 K1->Right = K2; 98 99 K2->Height = Max(Height(K2->Left), Height(K2->Right)) + 1; 100 K1->Height = Max(Height(K1->Left), K2->Height) + 1; 101 102 return K1;//新的根节点 103 } 104 105 //左双旋转(LR型状态)先左旋在右旋 106 static Position doubleRotationWithLeft(Position K3) 107 { 108 K3->Left = SingleRotationWithRight(K3->Left); 109 return SingleRotationWithLeft(K3); 110 } 111 112 //右单旋转(RR型状态) 113 static Position SingleRotationWithRight(Position K2) 114 { 115 Position K1; 116 117 K1 = K2->Right; 118 K2->Right = K1->Left; 119 K1->Left = K2; 120 121 K2->Height = Max(Height(K2->Left), Height(K2->Right)) + 1; 122 K1->Height = Max(Height(K1->Right), K2->Height) + 1; 123 124 return K1;//新的根节点 125 } 126 127 //右双旋转(RL型状态)先右旋在左旋 128 static Position doubleRotationWithRight(Position K3) 129 { 130 K3->Right = SingleRotationWithLeft(K3->Right); 131 return SingleRotationWithRight(K3); 132 } 133 134 //插入 135 AVLTree Insert(AVLTree T, ElementType X) 136 { 137 if (T == NULL) 138 { 139 T = malloc(sizeof(struct AVLNode)); 140 assert(T); 141 142 T->Data = X; 143 T->Height = 0; 144 T->Left = T->Right = NULL; 145 } 146 else if (X < T->Data) 147 { 148 T->Left = Insert(T->Left, X); 149 if (Height(T->Left) - Height(T->Right) == 2) 150 { 151 if (X < T->Left->Data) 152 { 153 T = SingleRotationWithLeft(T); 154 } 155 else 156 { 157 T = doubleRotationWithLeft(T); 158 } 159 } 160 } 161 else if (X > T->Data) 162 { 163 T->Right = Insert(T->Right, X); 164 if (Height(T->Right) - Height(T->Left) == 2) 165 { 166 if (X > T->Right->Data) 167 { 168 T = SingleRotationWithRight(T); 169 } 170 else 171 { 172 T = doubleRotationWithRight(T); 173 } 174 } 175 } 176 /*else x 已经在树中,我们什么都不做*/ 177 178 T->Height = Max(Height(T->Left), Height(T->Right)) + 1; 179 return T; 180 } 181 182 //删除 183 AVLTree Delete(AVLTree T, ElementType X) 184 { 185 Position TmpCell; 186 if (T == NULL) 187 { 188 printf("Element not found!"); 189 } 190 else if (X < T->Data)//往左走 191 { 192 T->Left = Delete(T->Left, X); 193 if (Height(T->Left) - Height(T->Right) == 2) 194 { 195 if (X < T->Left->Data) 196 { 197 T = SingleRotationWithLeft(T); 198 } 199 else 200 { 201 T = doubleRotationWithLeft(T); 202 } 203 } 204 } 205 else if (X > T->Data)//往右走 206 { 207 T->Right = Delete(T->Right, X); 208 if (Height(T->Right) - Height(T->Left) == 2) 209 { 210 if (X > T->Right->Data) 211 { 212 T = SingleRotationWithRight(T); 213 } 214 else 215 { 216 T = doubleRotationWithRight(T); 217 } 218 } 219 } 220 else if (X == T->Data)//如果就是要删除的节点 221 { 222 if (T->Left && T->Right)//有两个儿子 223 { 224 if (Height(T->Left) > Height(T->Right)) 225 { 226 Position min = FindMax(T->Left); 227 T->Data = min->Data; 228 T->Left = Delete(T->Left, min->Data); 229 } 230 else 231 { 232 Position max = FindMin(T->Right); 233 T->Data = max->Data; 234 T->Right = Delete(T->Right, max->Data); 235 } 236 } 237 else//有一个或零个儿子 238 { 239 TmpCell = T; 240 if (T->Left == NULL) 241 { 242 T = T->Right; 243 } 244 else if (T->Right == NULL) 245 { 246 T = T->Left; 247 } 248 free(TmpCell); 249 } 250 } 251 252 return T; 253 } 254 255 //遍历 256 ElementType Retrieve(Position P) 257 { 258 return P->Data; 259 } 260 261 //打印AVL树 262 void PrintTree(AVLTree T) 263 { 264 if (T != NULL) 265 { 266 PrintTree(T->Left); 267 printf("%d ", T->Data); 268 PrintTree(T->Right); 269 } 270 }
main.c源文件
1 #define _CRT_SECURE_NO_WARNINGS 2 #include "AVLTREE.h" 3 4 int main() 5 { 6 AVLTree T = NULL; 7 Position P; 8 9 //插入1 ~ 30 10 for (int i = 1; i <= 30; i++) 11 { 12 T = Insert(T, i); 13 } 14 PrintTree(T); 15 printf("\n"); 16 17 P = FindMin(T); 18 printf("\n最小元为:%d\n", P->Data); 19 P = FindMax(T); 20 printf("\n最大元为:%d\n", P->Data); 21 22 printf("--------------------------------------------------------------------------------"); 23 24 //删除其中的18 25 printf("\n删除其中的18\n\n"); 26 T = Delete(T, 18); 27 PrintTree(T); 28 printf("\n\n"); 29 30 //查找18 31 P = Find(T, 18); 32 33 //清空AVL树 34 MakeEmpty(T); 35 if (T != P) 36 { 37 printf("\n清空完毕!\n"); 38 } 39 40 system("pause"); 41 return 0; 42 }
标签:Right,AVLTree,C语言,AVL,Height,Position,数据结构,Data,Left From: https://www.cnblogs.com/Aeternus/p/17032215.html