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
| #ifndef _INCLUDE_BASE #define _INCLUDE_BASE
#include <stdio.h> #include <stdlib.h>
#define true 1 #define false 0 #endif
typedef int avl_elementType;
typedef struct avl { avl_elementType data; struct avl *left; struct avl *right; avl_elementType hight; }AVL,*PAVL;
void RotateWithDoubleRight(PAVL *avl); void RotateWithDoubleLeft(PAVL *avl); void RotateWithLeftRight(PAVL *avl); void RotateWithRightLeft(PAVL *avl); void InsertNode(PAVL *avl,int data); int NodeHigh(PAVL avl); int compareHigh(PAVL a,PAVL b); void LeftBalance(PAVL *avl); void RightBalance(PAVL *avl); void print_inorder(PAVL avl); void print_preorder(PAVL avl); void print_houorder(PAVL avl); void print_inhight(PAVL avl);
void main() { PAVL root = (PAVL)malloc(sizeof(PAVL));
InsertNode(&root,2); InsertNode(&root,1); InsertNode(&root,-1);
InsertNode(&root,6); InsertNode(&root,7); InsertNode(&root,5); printf("中序排列节点的高度:"); print_inhight(root); printf("\n"); printf("中序排列:"); print_inorder(root); printf("\n"); printf("前序排列:"); print_preorder(root); }
void InsertNode(PAVL *avl,int data) { if(!(*avl)) { PAVL node = (PAVL)malloc(sizeof(PAVL)); node->data = data; node->hight = 0; *avl = node; return ; }
if(data > (*avl)->data) { InsertNode(&((*avl)->right),data); if (NodeHeight((*avl)->right)-NodeHeight((*avl)->left)==2) RightBalance(avl); }else{ InsertNode(&((*avl)->left),data); printf("相减为:%d",NodeHeight((*avl)->left)-NodeHeight((*avl)->right)); if (NodeHeight((*avl)->left)-NodeHeight((*avl)->right)==2) LeftBalance(avl); } (*avl)->hight = compareHigh((*avl)->left,(*avl)->right) + 1 ; printf("当前节点:%d , 高度:%d , left-高度:%d,right-高度:%d \n",(*avl)->data,(*avl)->hight,NodeHeight((*avl)->left),NodeHeight((*avl)->right)); }
int NodeHeight(PAVL avl) { return avl == NULL ? -1 : avl->hight; }
int compareHigh(PAVL a,PAVL b) { if(!a || !b) return 0; return a->hight > b->hight ? a->hight : b->hight; }
void LeftBalance(PAVL *avl) { if(NodeHeight((*avl)->left->left) - NodeHeight((*avl)->left->right) != -1) { RotateWithDoubleLeft(avl); }else { RotateWithLeftRight(avl); } }
void RightBalance(PAVL *avl) { if(NodeHeight((*avl)->right->right) - NodeHeight((*avl)->right->left) != -1) { RotateWithDoubleRight(avl); }else { RotateWithRightLeft(avl); } }
void RotateWithDoubleLeft(PAVL *avl) { PAVL p = *avl; PAVL q = p->left; p->left = q->right; q->right = p;
p->hight = compareHigh(p->left,p->right) +1 ; q->hight = compareHigh(q->left,q->right) +1 ;
*avl = q; }
void RotateWithDoubleRight(PAVL *avl) { PAVL p = *avl; PAVL q = p->right; p->right = q->left; q->left = p;
p->hight = compareHigh(p->left,p->right) +1 ; q->hight = compareHigh(q->left,q->right) +1 ;
*avl = q; }
void RotateWithLeftRight(PAVL *avl) { printf("\nLR\n"); RotateWithDoubleRight(&((*avl)->left)); RotateWithDoubleLeft(avl); }
void RotateWithRightLeft(PAVL *avl) { printf("\nRL\n"); RotateWithDoubleLeft(&((*avl)->right)); RotateWithDoubleRight(avl); }
void print_inorder(PAVL avl) { if(avl) { print_inorder(avl->left); printf("%d - ",avl->data); print_inorder(avl->right); } }
void print_inhight(PAVL avl) { if(avl) { print_inhight(avl->left); printf("%d - ",avl->hight); print_inhight(avl->right); } }
void print_preorder(PAVL avl) { if(avl) { printf("%d - ",avl->data); print_preorder(avl->left); print_preorder(avl->right); } }
void print_houorder(PAVL avl) { if(avl) { print_houorder(avl->left); print_houorder(avl->right); printf("%d - ",avl->data); } }
|