-
Notifications
You must be signed in to change notification settings - Fork 0
/
LLtreelog.c
210 lines (175 loc) · 3.46 KB
/
LLtreelog.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
#include "LLtree.h"
// Initialize the stack
void initStack(Stack* s) {
s->top = NULL;
return;
}
// Push a tree node onto the stack
void push(Stack* s, node* node) {
StackNode* newStackNode = (StackNode*)malloc(sizeof(StackNode));
newStackNode->data = node;
newStackNode->next = s->top;
s->top = newStackNode;
return;
}
// Check if the stack is empty
int isEmpty(Stack* s) {
return s->top == NULL;
}
// Pop a node from the stack
node* pop(Stack* s) {
if (isEmpty(s)) {
return NULL;
}
StackNode* temp = s->top;
node* node = temp->data;
s->top = s->top->next;
free(temp);
return node;
}
node* peek(Stack* s){
if (isEmpty(s)) {
return NULL;
}
return s->top->data;
}
void init_BST(BST *t){
*t=NULL;
return;
}
void insert_bstree(BST *t, int d){
node *nn=(BST)malloc(sizeof(node));
nn->data=d;
nn->lchild=nn->rchild=NULL;
if(*t==NULL){
*t=nn;
return;
}
node *p=*t,*q=NULL;
while(p){
q=p;
if(p->data > d)
p=p->lchild;
else if(p->data < d)
p=p->rchild;
else{
free(nn);
return;
}
}
if(q->data > d)
q->lchild=nn;
else if(q->data < d)
q->rchild=nn;
return;
}
BST createNode(int d){
struct node *nn=(BST)malloc(sizeof(node));
nn->data=d;
nn->lchild=nn->rchild=NULL;
return nn;
}
BST recursive_insert(BST *t, int d){
if(*t==NULL){
return createNode(d);
}
node *p=*t;
if(p->data == d)
return p;
if(p->data > d)
p->lchild = recursive_insert(&p->lchild, d);
else if(p->data < d)
p->rchild = recursive_insert(&p->rchild,d);
return p;
}
void preorder(BST t){
if(!t)
return ;
printf("%d ",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
void non_recursive_preorder(BST t){
if (t == NULL) {
return;
}
node *p = t;
Stack s;
initStack(&s);
while (p || !isEmpty(&s)) {
if(p){
printf("%d ",p->data);
push(&s,p);
p=p->lchild;
}
else{
p = pop(&s);
p = p->rchild;
}
}
return;
}
void inorder(BST t){
if(!t)
return ;
inorder(t->lchild);
printf("%d ",t->data);
inorder(t->rchild);
}
void non_recursive_inorder(BST t){
if (t == NULL) {
return;
}
node *p = t;
Stack s;
initStack(&s);
while (p || !isEmpty(&s)) {
if(p){
//printf("%d->",p->data);
push(&s,p);
p=p->lchild;
}
else{
p = pop(&s);
printf("%d ",p->data);
p = p->rchild;
}
}
return;
}
void postorder(BST t){
if(!t)
return ;
postorder(t->lchild);
postorder(t->rchild);
printf("%d ",t->data);
}
void non_recursive_postorder(BST t){
if (t == NULL) {
return;
}
Stack s1, s2;
initStack(&s1);
initStack(&s2);
push(&s1, t);
while (!isEmpty(&s1)) {
node *p = pop(&s1);
push(&s2, p);
// Push left and right children into s1
if (p->lchild) {
push(&s1, p->lchild);
}
if (p->rchild) {
push(&s1, p->rchild);
}
}
// Now print all nodes from s2 which are in post-order
while (!isEmpty(&s2)) {
node *p = pop(&s2);
printf("%d ", p->data);
}
return;
}