-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2024.4.4.exercise.c
186 lines (176 loc) · 2.97 KB
/
2024.4.4.exercise.c
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
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int datatype;
//typedef struct list
//{
// datatype data;
// struct list* next;
//}list;
//
//typedef struct queue
//{
// list* front;
// list* rear;
//}queue;
//
//void initqueue(queue* q)
//{
// q->front = q->rear = (list*)malloc(sizeof(list));
// if (q->front == NULL)
// {
// printf("error\n");
// return;
// }
// q->front->next = NULL;
//}
//
//bool isempty(queue* q)
//{
// if (q->front == q->rear || q->front->next == NULL)
// {
// return true;
// }
// return false;
//}
//
//datatype outqueue(queue* q)
//{
// if (isempty(q))
// {
// printf("empty0\n");
// exit(1);
// }
// datatype temp;
// list* pointer;
// pointer = q->front->next;
// temp = pointer->data;
// q->front->next = pointer->next;
// if (q->front->next == NULL)
// {
// q->rear = q->front;
// }
// free(pointer);
// pointer = NULL;
// return temp;
//}
//
//void enqueue(queue* q)
//{
// printf("输入入队元素的值\n");
// datatype element;
// scanf("%d", &element);
// list* newnode = (list*)malloc(sizeof(list));
// if (newnode == NULL)
// {
// printf("error\n");
// return;
// }
// newnode->data = element;
// newnode->next = NULL;
// q->rear->next = newnode;
// q->rear = newnode;
//}
//
//int main()
//{
// queue q;
// initqueue(&q);
// bool flag = isempty(&q);
// if (!flag)
// {
// printf("error\n");
// return -1;
// }
// int n;
// printf("有几个元素要入队?\n");
// scanf("%d", &n);
// int i;
// for (i = 0; i < n; i++)
// {
// enqueue(&q);
// }
// printf("有多少元素要出队?\n");
// scanf("%d", &n);
// for (i = 0; i < n; i++)
// {
// printf("%d ", outqueue(&q));
// }
// return 0;
//}
typedef struct tree
{
int number;
datatype data;
struct tree* left;
struct tree* right;
}tree;
tree* creatroot()
{
tree* root = (tree*)malloc(sizeof(tree));
if (root == NULL)
{
printf("NULL\n");
exit(1);
}
printf("请输入节点值\n");
datatype element;
scanf("%d", &element);
root->number = 1;
root->data = element;
root->left = NULL;
root->right = NULL;
return root;
}
tree* creat(tree* root)
{
datatype data;
scanf("%d", &data);
getchar();
if (data != 0)
{
root = (tree*)malloc(sizeof(tree));
if (root == NULL)
{
printf("NULL\n");
exit(1);
}
root->data = data;
root->left = NULL;
root->right = NULL;
root->left = creat(root->left);
root->right = creat(root->right);
}
return root;
}
void freetree(tree* root)
{
if (root != NULL)
{
freetree(root->left);
freetree(root->right);
free(root);
}
}
void visitroot(tree* root)
{
printf("%d ", root->data);
}
void preorder(tree* root)
{
if (root != NULL)
{
visitroot(root);
preorder(root->left);
preorder(root->right);//前端遍历
}
}
int main()
{
tree* root = (tree*)malloc(sizeof(tree*));
root = creat(root);
preorder(root);
freetree(root);
return 0;
}