伸展树
1 #include <stdio.h> 2 #include <stdlib.h> 3 //author : pgdnxu@qq.com 4 5 struct NODE 6 { 7 NODE *left; 8 NODE *right; 9 NODE *pa; 10 int data; 11 }; 12 13 struct BST 14 { 15 NODE *root; 16 int nodenum; 17 }; 18 19 NODE *search(int data,NODE *n) 20 { 21 if(NULL == n) 22 return NULL; 23 if(data < n->data) 24 return search(data,n->left); 25 else if(data > n->data) 26 return search(data,n->right); 27 else 28 return n; 29 } 30 NODE *find(int data,BST *t) 31 { 32 if(NULL == t) 33 return NULL; 34 return search(data,t->root); 35 } 36 37 NODE *insert(int data,NODE **n,NODE *p) 38 { 39 if(NULL == (*n)) 40 { 41 (*n) = (NODE*)malloc(sizeof(NODE)); 42 (*n)->left = NULL; 43 (*n)->right = NULL; 44 (*n)->pa = p; 45 (*n)->data = data; 46 return *n; 47 } 48 if(data < (*n)->data) 49 { 50 //left 51 return insert(data,&((*n)->left),*n); 52 } 53 else 54 { 55 return insert(data,&((*n)->right),*n); 56 } 57 } 58 59 NODE *addNode(BST **t,int data) 60 { 61 if(NULL == (*t)) 62 { 63 (*t) = (BST*)malloc(sizeof(BST)); 64 (*t)->root = NULL; 65 (*t)->nodenum = 0; 66 } 67 (*t)->nodenum++; 68 return insert(data,&((*t)->root),NULL); 69 } 70 void init(BST **t,int data[],int num) 71 { 72 int i; 73 NODE *tmp; 74 for(i=0;i<num;i++) {="" tmp="addNode(t,data[i]);" }="" void="" printtree(node="" *n,int="" deep)="" int="" i;="" for(i="0;i<deep;i++)" printf("="" ");="" if(null="=" n)="" printf("null\n");="" else="" if(n-="">pa == NULL) 75 printf("%d--|\n",n->data); 76 else 77 printf("%d(p:%d)--|\n",n->data,n->pa->data); 78 if(NULL != n) 79 { 80 printTree(n->left,deep+1); 81 printTree(n->right,deep+1); 82 } 83 } 84 85 void splay(NODE *n) 86 { 87 //error 88 if(NULL == n) 89 return; 90 NODE *nPa = n->pa; 91 NODE *nPaPa = nPa == NULL ? NULL : nPa->pa; 92 NODE *nPaPaPa=NULL; 93 int lr = -1; 94 if(nPaPa != NULL) 95 nPaPaPa = nPaPa->pa; 96 if(nPaPaPa != NULL) 97 { 98 if(nPaPa == nPaPaPa->left) 99 { 100 //left 101 lr = 0; 102 } 103 else 104 { 105 //right 106 lr = 1; 107 } 108 } 109 //root return 110 if(nPa == NULL) 111 { 112 return; 113 } 114 else if(nPaPa == NULL && n == nPa->left) 115 { 116 //zig 117 printf("proc: zig\n"); 118 nPa->left = n->right; 119 if(n->right != NULL) 120 n->right->pa = nPa; 121 n->pa = nPa->pa; 122 nPa->pa = n; 123 n->right = nPa; 124 } 125 else if(nPaPa == NULL && n == nPa->right) 126 { 127 //zag 128 printf("proc: zag\n"); 129 nPa->right = n->left; 130 if(n->left != NULL) 131 n->left->pa = nPa; 132 n->pa = nPa->pa; 133 nPa->pa = n; 134 n->left = nPa; 135 } 136 else if(n == nPa->left && nPa == nPaPa->left) 137 { 138 //zig-zig 139 printf("proc: zig-zig\n"); 140 141 nPaPa->left = nPa->right; 142 nPa->pa = nPaPa->pa; 143 nPaPa->pa = nPa; 144 nPa->right = nPaPa; 145 146 nPa->left = n->right; 147 if(n->right != NULL) 148 n->right->pa = nPa; 149 n->pa = nPa->pa; 150 nPa->pa = n; 151 n->right = nPa; 152 153 if(lr != -1) 154 { 155 if(lr == 0) 156 nPaPaPa->left = n; 157 else if(lr == 1) 158 nPaPaPa->right = n; 159 } 160 if(n->pa != NULL) 161 splay(n); 162 } 163 else if(n == nPa->right && nPa == nPaPa->right) 164 { 165 //zag-zag 166 printf("proc: zag-zag\n"); 167 168 nPaPa->right = nPa->left; 169 nPa->pa = nPaPa->pa; 170 nPaPa->pa = nPa; 171 nPa->left = nPaPa; 172 173 nPa->right = n->left; 174 if(n->left != NULL) 175 n->left->pa = nPa; 176 n->pa = nPa->pa; 177 nPa->pa = n; 178 n->left = nPa; 179 180 if(lr != -1) 181 { 182 if(lr == 0) 183 nPaPaPa->left = n; 184 else if(lr == 1) 185 nPaPaPa->right = n; 186 } 187 if(n->pa != NULL) 188 splay(n); 189 } 190 else if(n == nPa->left && nPa == nPaPa->right) 191 { 192 //zig-zag 193 printf("proc: zig-zag\n"); 194 nPa->left = n->right; 195 n->pa = nPa->pa; 196 nPa->pa = n; 197 n->right = nPa; 198 199 nPaPa->right = n->left; 200 if(n->left != NULL) 201 n->left->pa = nPaPa; 202 n->pa = nPaPa->pa; 203 nPaPa->pa = n; 204 n->left = nPaPa; 205 206 if(lr != -1) 207 { 208 if(lr == 0) 209 nPaPaPa->left = n; 210 else if(lr == 1) 211 nPaPaPa->right = n; 212 } 213 if(n->pa != NULL) 214 splay(n); 215 } 216 else if(n == nPa->right && nPa == nPaPa->left) 217 { 218 //zag-zig 219 printf("proc: zag-zig\n"); 220 nPa->right = n->left; 221 n->pa = nPa->pa; 222 nPa->pa = n; 223 n->left = nPa; 224 225 nPaPa->left = n->right; 226 if(n->right != NULL) 227 n->right->pa = nPaPa; 228 n->pa = nPaPa->pa; 229 nPaPa->pa = n; 230 n->right = nPaPa; 231 232 if(lr != -1) 233 { 234 if(lr == 0) 235 nPaPaPa->left = n; 236 else if(lr == 1) 237 nPaPaPa->right = n; 238 } 239 if(n->pa != NULL) 240 splay(n); 241 } 242 } 243 244 NODE *sfind(int data,BST *t) 245 { 246 if(NULL == t) 247 return NULL; 248 NODE *tar = search(data,t->root); 249 if(tar != NULL) 250 { 251 splay(tar); 252 t->root = tar; 253 } 254 else 255 printf("tar is NULL!\n"); 256 return tar; 257 } 258 259 260 int main() 261 { 262 int a[] = {6,5,4,3,2,1}; 263 BST *tree; 264 init(&tree,a,6); 265 266 //case 1 267 printf("========= case 1 =========\n"); 268 printTree(tree->root,0); 269 sfind(1,tree); 270 printf("--------------------------\n"); 271 printTree(tree->root,0); 272 printf("==========================\n"); 273 //case 2 274 printf("========= case 2 =========\n"); 275 printTree(tree->root,0); 276 sfind(2,tree); 277 printf("--------------------------\n"); 278 printTree(tree->root,0); 279 printf("==========================\n"); 280 281 return 0; 282 } 283 </num;i++)></stdlib.h></stdio.h>