forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtree.c
More file actions
150 lines (124 loc) · 3.1 KB
/
tree.c
File metadata and controls
150 lines (124 loc) · 3.1 KB
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
// Copyright (c) 2014-2019 K Team. All Rights Reserved.
#include <stdlib.h>
#include <stdio.h>
struct treeNode {
int value;
struct treeNode *left;
struct treeNode *right;
};
struct listNode {
int value;
struct listNode *next;
};
struct stackNode {
struct treeNode *value;
struct stackNode *next;
};
struct listNode* tree_to_list_recursive(struct treeNode *t, struct listNode *l)
{
struct listNode *ln;
if (t == NULL)
return l;
ln = (struct listNode *) malloc(sizeof(struct listNode));
ln->value = t->value;
ln->next = tree_to_list_recursive(t->right, l);
printf("%d ", t->value);
l = tree_to_list_recursive(t->left, ln);
free(t);
return l;
}
struct listNode* tree_to_list_iterative(struct treeNode *t)
{
struct listNode *l;
struct stackNode *s;
if (t == NULL)
return NULL;
l = NULL;
s = (struct stackNode *) malloc(sizeof(struct stackNode));
s->value = t;
s->next = NULL;
while (s != NULL) {
struct treeNode *tn;
struct listNode *ln;
struct stackNode *sn;
sn = s;
s = s->next ;
tn = sn->value;
free(sn) ;
if (tn->left != NULL) {
sn = (struct stackNode *) malloc(sizeof(struct stackNode));
sn->value = tn->left;
sn->next = s;
s = sn;
}
if (tn->right != NULL) {
sn = (struct stackNode *) malloc(sizeof(struct stackNode));
sn->value = tn;
sn->next = s;
s = sn;
sn = (struct stackNode *) malloc(sizeof(struct stackNode));
sn->value = tn->right;
sn->next = s;
s = sn;
tn->left = NULL;
tn->right = NULL;
}
else {
ln = (struct listNode *) malloc(sizeof(struct listNode));
ln->value = tn->value;
ln->next = l;
l = ln;
printf("%d ", ln->value);
free(tn);
}
}
return l;
}
struct treeNode* create()
{
struct treeNode* root;
root = (struct treeNode*) malloc(sizeof(struct treeNode));
root->value = 4;
root->left = (struct treeNode*) malloc(sizeof(struct treeNode));
root->left->value = 2;
root->left->left = (struct treeNode*) malloc(sizeof(struct treeNode));
root->left->left->value = 1;
root->left->left->left = NULL;
root->left->left->right = NULL;
root->left->right = (struct treeNode*) malloc(sizeof(struct treeNode));
root->left->right->value = 3;
root->left->right->left = NULL;
root->left->right->right = NULL;
root->right = (struct treeNode*) malloc(sizeof(struct treeNode));
root->right->value = 6;
root->right->left = (struct treeNode*) malloc(sizeof(struct treeNode));
root->right->left->value = 5;
root->right->left->left = NULL;
root->right->left->right = NULL;
root->right->right = (struct treeNode*) malloc(sizeof(struct treeNode));
root->right->right->value = 7;
root->right->right->left = NULL;
root->right->right->right = NULL;
return root;
}
void destroy(struct listNode* x)
{
struct listNode *y;
while(x) {
y = x->next;
free(x);
x = y;
}
}
int main()
{
struct treeNode* t;
struct listNode* l;
t = create();
l = tree_to_list_recursive(t, NULL);
destroy(l);
t = create();
l = tree_to_list_iterative(t);
destroy(l);
return 0;
}