AVL
C语言中的指针总是一个让人头疼的东西,特别是把指针作为函数参数的时候,情况往往会更糟。有两种情况特别容易出现,一种是所谓的段错误,另一种是在函数调用过程中指针的值没有按照我们期望的那样发生变化。出现第一种情况的原因是对空指针进行了操作,而出现第二种情况往往是因为忽视了C语言函数传值调用的特点。
下面是一段创建平衡二叉树的代码,里面用到了大量指针,还有二重指针。希望能帮助大家加深对指针的理解。
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 struct node 5 { 6 int data; 7 int bf; 8 struct node *lchild; 9 struct node *rchild; 10 }; 11 12 void r_rotate(struct node **t); 13 void l_rotate(struct node **t); 14 int insert_node(struct node *t, int data); 15 void inorder_tra(struct node *t); 16 void refresh(struct node *t, int data); 17 18 struct node **ubal = NULL; //the unbalanced node 19 20 int main() 21 { 22 int num; //the number of nodes 23 int data; 24 int i; 25 struct node *T = NULL; 26 T = (struct node *)malloc(sizeof(struct node)); 27 if (!T) 28 { 29 printf ("fail to allocate space!\n"); 30 exit (1); 31 } 32 printf ("please input the number of nodes:\n"); 33 scanf ("%d", &num); 34 printf ("please input the value of nodes:\n"); 35 scanf ("%d", &data); 36 T->data = data; 37 T->bf = 0; 38 T->lchild = NULL; 39 T->rchild = NULL; //create the root node 40 for (i=0; i<num-1; i++) 41 { 42 scanf ("%d", &data); 43 ubal = NULL; 44 //if the insert node changes a subtree's height, refresh the value of bf 45 if (insert_node(T, data)) refresh(T, data); 46 47 if (ubal) //the new come node breaks the balance 48 { 49 if ((*ubal)->bf==2 && (*ubal)->lchild->bf==1) 50 { 51 (*ubal)->bf = 0; 52 (*ubal)->lchild->bf = 0; 53 r_rotate(ubal); 54 continue ; 55 } 56 if ((*ubal)->bf==2 && (*ubal)->lchild->bf==1) 57 { 58 if ((*ubal)->lchild->rchild->bf==1) 59 { 60 (*ubal)->bf = -1; 61 (*ubal)->lchild->bf = 0; 62 (*ubal)->lchild->rchild->bf = 0; 63 } 64 else 65 { 66 (*ubal)->bf = 0; 67 (*ubal)->lchild->bf = 1; 68 (*ubal)->lchild->rchild->bf = 0; 69 } 70 l_rotate(&(*ubal)->lchild); 71 r_rotate(ubal); 72 continue ; 73 } 74 if ((*ubal)->bf==-2 && (*ubal)->rchild->bf==-1) 75 { 76 (*ubal)->bf = 0; 77 (*ubal)->rchild->bf = 0; 78 l_rotate(ubal); 79 continue ; 80 } 81 if ((*ubal)->bf==-2 && (*ubal)->rchild->bf==1) 82 { 83 if ((*ubal)->rchild->lchild->bf==1) 84 { 85 (*ubal)->bf = 0; 86 (*ubal)->rchild->bf = -1; 87 (*ubal)->rchild->lchild->bf = 0; 88 } 89 else 90 { 91 (*ubal)->bf = 1; 92 (*ubal)->rchild->bf = 0; 93 (*ubal)->rchild->lchild->bf = 0; 94 } 95 r_rotate(&(*ubal)->rchild); 96 l_rotate(ubal); 97 continue ; 98 } 99 } 100 } 101 inorder_tra(T); //inorder traversal 102 return 0; 103 } 104 105 void r_rotate(struct node **t) 106 { 107 struct node *p; 108 p = (*t)->lchild; 109 (*t)->lchild = p->rchild; 110 p->rchild = *t; 111 *t = p; 112 } 113 114 void l_rotate(struct node **t) 115 { 116 struct node *p; 117 p = (*t)->rchild; 118 (*t)->rchild = p->lchild; 119 p->lchild= *t; 120 *t = p; 121 } 122 123 int insert_node(struct node *t, int data) 124 { 125 if (data<t->data) 126 { 127 if (t->lchild==NULL) 128 { 129 struct node *p = (struct node *)malloc(sizeof(struct node)); 130 if (!p) exit (1); 131 p->data = data; 132 p->bf = 0; 133 p->lchild = NULL; 134 p->rchild = NULL; 135 t->lchild = p; 136 return !t->rchild? 1:0; 137 } 138 else 139 { 140 return insert_node(t->lchild, data); 141 } 142 } 143 else 144 { 145 if (t->rchild==NULL) 146 { 147 struct node *p = (struct node *)malloc(sizeof(struct node)); 148 if (!p) exit (1); 149 p->data = data; 150 p->bf = 0; 151 p->lchild = NULL; 152 p->rchild = NULL; 153 t->rchild = p; 154 return !t->lchild? 1:0; 155 } 156 else 157 { 158 return insert_node(t->rchild, data); 159 } 160 } 161 } 162 163 void inorder_tra(struct node *t) 164 { 165 if (t->lchild!=NULL) inorder_tra(t->lchild); 166 printf ("%d ", t->data); 167 if (t->rchild!=NULL) inorder_tra(t->rchild); 168 return ; 169 } 170 171 void refresh(struct node *t, int data) 172 { 173 if (data<t->data) 174 { 175 t->bf += 1; 176 if (t->bf>=2) ubal = &t; 177 if (t->lchild->data==data) return ; 178 refresh(t->lchild, data); 179 } 180 else 181 { 182 t->bf += -1; 183 if (t->bf<=-2) ubal = &t; 184 if (t->rchild->data==data) return ; 185 refresh(t->rchild, data); 186 } 187 }
sample input:
5
88 70 61 96 120
sample output:
61 70 88 96 120
posted on 2015-08-11 21:57 songlingfei 阅读(217) 评论(0) 编辑 收藏 举报