-
Notifications
You must be signed in to change notification settings - Fork 1
/
arvoreteste.c
188 lines (152 loc) · 3.21 KB
/
arvoreteste.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct pilha{
int tam_atual;
char items[300];
};
typedef struct pilha pilha;
pilha* criarpilha()
{
pilha* nova_pilha= malloc(sizeof(pilha));
nova_pilha->tam_atual=0;
return nova_pilha;
}
int checar_vazia(pilha *p)
{
if(p->tam_atual==0)
{
return 1;
}
else
{
return 0;
}
}
//add item a pilha
void push(pilha* p, char item)
{
if(p->tam_atual==300)
{
printf("stack overflow");
}
else
{
p->items[p->tam_atual++] = item;
}
}
//remover item do topo
int pop(pilha *p)
{
if(checar_vazia(p))
{
printf("stack underflow");
return -1;
}
else
{
return p->items[--p->tam_atual];
}
}
char peek(pilha *p)
{
if(checar_vazia(p))
{
printf("stack underflow");
return -1;
}
else
{
return p->items[p->tam_atual-1];
}
}
typedef struct Node Node;
struct Node {
int data;
Node *left, *right;
};
/* Helper function that allocates a new node */
Node* newNode(int data)
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->left = node->right = NULL;
return (node);
}
/* This funtcion is here just to test */
void preOrder(Node* node)
{
if (node == NULL)
return;
printf("%d ", node->data);
preOrder(node->left);
preOrder(node->right);
}
// functin to return the index of close parenthesis
int findIndex(char str[], int si, int ei)
{
if (si > ei)
return -1;
// Inbuilt stack
pilha *s;
s=criarpilha();
char t;
for (int i = si; i <= ei; i++) {
// if open parenthesis, push it
if (str[i] == '(')
push(s, str[i]);
// if close parenthesis
else if (str[i] == ')') {
if (t=peek(s) == '(') {
pop(s);
// if stack is empty, this is
// the required index
if (checar_vazia(s))
return i;
}
}
}
// if not found return -1
return -1;
}
// function to construct tree from string
Node* treeFromString(char str[], int si, int ei)
{
// Base case
if (si > ei)
return NULL;
// new root
Node* root = newNode(str[si] - '0');
int index = -1;
// if next char is '(' find the index of
// its complement ')'
if (si + 1 <= ei && str[si + 1] == '(')
index = findIndex(str, si + 1, ei);
// if index found
if (index != -1) {
// call for left subtree
root->left = treeFromString(str, si + 2, index - 1);
// call for right subtree
root->right = treeFromString(str, index + 2, ei - 1);
}
return root;
}
// Driver Code
int main()
{
char str[100] = "(4(2(3)(1)) (6(5)))";
char certa[100];
int i, j=0;
for ( i = 1; i < strlen(str)-1; i++)
{
if(str[i]!=' ')
{
certa[j]=str[i];
j++;
}
}
certa[j]='\0';
Node* root = treeFromString(certa, 0, strlen(certa) - 1);
preOrder(root);
printf("\n");
}