1 #include <stdio.h>
2 #include <stdlib.h>
3
4 struct node{
5 int data;
6 struct node *left;
7 struct node *right;
8 };
9
10 struct node *creatNode(int value);
11 void insertNode(struct node **head, struct node *new);
12 //void insertRootNode(struct node **head, struct node *new);
13 struct node *insertRootNode(struct node *head, struct node *new);
14 void print(struct node *p);
15 void traverse(struct node *p, void (*fun)(struct node *));
16 struct node *search(struct node *head, int value);
17
18 struct node *findMin(struct node *head);
19 struct node *findMax(struct node *head);
20 struct node *deleteNode(struct node *head, int value);
21
22 int main(int argc, char *argv[])
23 {
24 int a[6];
25 struct node *head = NULL;
26 int i;
27 //srand(time(NULL));
28 for(i=0; i<6; i++)
29 {
30 a[i] = rand()%100;
31 printf("%d ", a[i]);
32 }
33 printf("\n");
34
35 for(i=0; i<6; i++)
36 //insertRootNode(&head, creatNode(a[i]));
37 head = insertRootNode(head, creatNode(a[i]));
38 // insertNode(&head, creatNode(100));
39 traverse(head, print);
40 printf("\n");
41 print(search(head, 93));
42 printf("\n");
43 //print(findMin(head));
44 //print(findMax(head));
45 traverse(deleteNode(head, 83), print);
46 return 0;
47 }
48
49 // 新建一个节点
50 struct node *creatNode(int value)
51 {
52 struct node *p;
53 p = (struct node *)malloc(sizeof(struct node));
54 p->data = value;
55 p->left = NULL; p->right = NULL;
56 return p;
57 }
58
59 // 标准插入,递归插入节点,构建二叉树
60 void insertNode_recursive(struct node **head, struct node *new)
61 {
62 struct node *p;
63 p = *head;
64 if(p == NULL)
65 *head = new;
66 else
67 {
68 if(new->data < p->data)
69 insertNode(&(p->left), new);
70 else
71 insertNode(&(p->right), new);
72 }
73 }
74
75 // 非递归插入节点,构建二叉树
76 void insertNode(struct node **head, struct node *new)
77 {
78 struct node *p;
79 p = *head;
80 if(p == NULL)
81 {
82 *head = new;
83 }
84 else
85 {
86 struct node *temp;
87 while(p != NULL)
88 {
89 temp = p;
90 p = (new->data < p->data) ? p->left : p->right;
91 }
92 if(new->data < temp->data)
93 temp->left = new;
94 else
95 temp->right = new;
96 }
97 }
98
99 // 节点打印函数,设置所需格式
100 void print(struct node *p)
101 {
102 printf("%d ", p->data);
103 }
104
105 // 递归遍历二叉树,打印输出
106 void traverse(struct node *p, void (*fun)(struct node *))
107 {
108 if(p == NULL)
109 return ;
110 traverse(p->left, fun);
111 (*fun)(p);
112 traverse(p->right, fun);
113 }
114
115 // 查找,递归
116 struct node *search_recursive(struct node *head, int value)
117 {
118 if(head == NULL)
119 return NULL;
120 if(head->data == value)
121 return head;
122 else if(head->data > value)
123 search(head->left, value);
124 else
125 search(head->right, value);
126 }
127
128 // 查找,非递归
129 struct node *search(struct node *head, int value)
130 {
131 struct node *p = head;
132 while(p != NULL)
133 {
134 if(p->data == value)
135 return p;
136 else if(p->data > value)
137 p = p->left;
138 else
139 p = p->right;
140 }
141 return NULL;
142 }
143
144 /* === 根插入的二叉树:左旋、右旋 === */
145
146 // 右旋
147 struct node *rotRight(struct node *head)
148 {
149 struct node *new;
150 new = head->left;
151 head->left = new->right;
152 new->right = head;
153 return new;
154 }
155
156 // 左旋
157 struct node *rotLeft(struct node *head)
158 {
159 struct node *new;
160 new = head->right;
161 head->right = new->left;
162 new->left = head;
163 return new;
164 }
165
166 // 根插入,递归,无返回值
167 /*
168 void insertRootNode(struct node **head, struct node *new)
169 {
170 struct node *p;
171 p = *head;
172 if(p == NULL)
173 *head = new;
174 else
175 {
176 if(new->data < p->data)
177 {
178 insertRootNode(&(p->left), new);
179 *head = rotRight(p);
180 }
181 else
182 {
183 insertRootNode(&(p->right), new);
184 *head = rotLeft(p);
185 }
186 }
187 }
188 */
189 // 根插入,递归,有返回值
190 struct node *insertRootNode(struct node *head, struct node *new)
191 {
192 if(head == NULL)
193 return new;
194 if(new->data < head->data)
195 {
196 head->left = insertRootNode(head->left, new);
197 head = rotRight(head);
198 }
199 else
200 {
201 head->right = insertRootNode(head->right, new);
202 head = rotLeft(head);
203 }
204 return head;
205 }
206 // 最小值,递归
207 struct node *findMin(struct node *head)
208 {
209 if(head==NULL || head->left==NULL)
210 return head;
211 findMin(head->left);
212 }
213 // 最大值,非递归
214 struct node *findMax(struct node *head)
215 {
216 if(head==NULL)
217 return head;
218 while(head->right != NULL)
219 head = head->right;
220 return head;
221 }
222 // 删除节点
223 struct node *deleteNode(struct node *head, int value)
224 {
225 if(head == NULL)
226 return head;
227 if(value < head->data)
228 head->left = deleteNode(head->left, value);
229 else if(value > head->data)
230 head->right = deleteNode(head->right, value);
231 else // 找到需要删除的节点
232 if(head->left && head->right) // 该节点有两个孩子
233 { // 直接将该节点的数值替换为其右子数中的最小值,然后删除该最小值的节点
234 struct node *temp = findMin(head->right);
235 head->data = temp->data;
236 head->right = deleteNode(head->right, temp->data);
237 }
238 else
239 {
240 if(head->left == NULL)
241 { // head只是一个 变量,它指向一块内存区域
242 struct node *del = head;
243 head = head->right;
244 free(del);
245 }
246 else if(head->right == NULL)
247 {
248 struct node *del = head;
249 head = head->left;
250 free(del);
251 }
252 else
253 free(head);
254 }
255 return head;
256 }
(C语言)BinarySrearchTree二叉搜索树 --- 标准插入(递归,非递归)、遍历(前,中,后序)、查找(递归,非递归)、根插入递归(左旋,右旋)、最小最大值、删除节点
最新推荐文章于 2024-02-03 08:42:37 发布