1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
| #include <stdio.h> #include <stdlib.h>
typedef struct AVLNode { int key; int height; struct AVLNode* left; struct AVLNode* right; } AVLNode;
int getHeight(AVLNode* node) { if (node == NULL) return 0; return node->height; }
int max(int a, int b) { return (a > b) ? a : b; }
AVLNode* createNode(int key) { AVLNode* newNode = (AVLNode*)malloc(sizeof(AVLNode)); newNode->key = key; newNode->height = 1; newNode->left = newNode->right = NULL; return newNode; }
AVLNode* rightRotate(AVLNode* A) { AVLNode* B = A->left; AVLNode* T2 = B->right;
B->right = A; A->left = T2;
A->height = max(getHeight(A->left), getHeight(A->right)) + 1; B->height = max(getHeight(B->left), getHeight(B->right)) + 1;
return B; }
AVLNode* leftRotate(AVLNode* A) { AVLNode* B = A->right; AVLNode* T2 = B->left;
B->left = A; A->right = T2;
A->height = max(getHeight(A->left), getHeight(A->right)) + 1; B->height = max(getHeight(B->left), getHeight(B->right)) + 1;
return B; }
int getBalanceFactor(AVLNode* node) { if (node == NULL) return 0; return getHeight(node->left) - getHeight(node->right); }
AVLNode* insertNode(AVLNode* root, int key) { if (root == NULL) return createNode(key);
if (key < root->key) root->left = insertNode(root->left, key); else if (key > root->key) root->right = insertNode(root->right, key); else return root;
root->height = 1 + max(getHeight(root->left), getHeight(root->right));
int balanceFactor = getBalanceFactor(root);
if (balanceFactor > 1 && key < root->left->key) return rightRotate(root);
if (balanceFactor < -1 && key > root->right->key) return leftRotate(root);
if (balanceFactor > 1 && key > root->left->key) { root->left = leftRotate(root->left); return rightRotate(root); }
if (balanceFactor < -1 && key < root->right->key) { root->right = rightRotate(root->right); return leftRotate(root); }
return root; }
AVLNode* searchNode(AVLNode* root, int key) { if (root == NULL || root->key == key) return root;
if (key < root->key) return searchNode(root->left, key); else return searchNode(root->right, key); }
AVLNode* findMinNode(AVLNode* root) { if (root == NULL) return NULL; if (root->left == NULL) return root; return findMinNode(root->left); }
AVLNode* deleteNode(AVLNode* root, int key) { if (root == NULL) return root;
if (key < root->key) root->left = deleteNode(root->left, key); else if (key > root->key) root->right = deleteNode(root->right, key); else { if (root->left == NULL || root->right == NULL) { AVLNode* temp = root->left ? root->left : root->right;
if (temp == NULL) { temp = root; root = NULL; } else *root = *temp;
free(temp); } else { AVLNode* minNode = findMinNode(root->right);
root->key = minNode->key;
root->right = deleteNode(root->right, minNode->key); } }
if (root == NULL) return root;
root->height = 1 + max(getHeight(root->left), getHeight(root->right));
int balanceFactor = getBalanceFactor(root);
if (balanceFactor > 1 && getBalanceFactor(root->left) >= 0) return rightRotate(root);
if (balanceFactor > 1 && getBalanceFactor(root->left) < 0) { root->left = leftRotate(root->left); return rightRotate(root); }
if (balanceFactor < -1 && getBalanceFactor(root->right) <= 0) return leftRotate(root);
if (balanceFactor < -1 && getBalanceFactor(root->right) > 0) { root->right = rightRotate(root->right); return leftRotate(root); }
return root; }
void inorderTraversal(AVLNode* root) { if (root == NULL) return; inorderTraversal(root->left); printf("%d ", root->key); inorderTraversal(root->right); }
int main() { AVLNode* root = NULL;
root = insertNode(root, 9); root = insertNode(root, 5); root = insertNode(root, 10); root = insertNode(root, 3); root = insertNode(root, 6); root = insertNode(root, 12);
printf("中序遍历结果:"); inorderTraversal(root); printf("\n");
root = deleteNode(root, 6); root = deleteNode(root, 10);
printf("中序遍历结果:"); inorderTraversal(root); printf("\n");
return 0; }
|