-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroj-novih-elemenata-stabla-bez-povecanja-visine.c
148 lines (132 loc) · 4.02 KB
/
broj-novih-elemenata-stabla-bez-povecanja-visine.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#ifndef DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#endif
#define MAX_TREE_STRING 100
#define MAX_NODES 100
// sirina i visina 2D polja koje se koristi za funkciju print_tree
#define WIDTH 80
#define HEIGHT 10
typedef struct node {
int key;
struct node *left;
struct node *right;
}Node;
int print_tree(Node *tree, int is_left, int offset, int depth, char s[HEIGHT][WIDTH]) {
char b[HEIGHT];
int i, left, right, width = 3;
if (!tree) return 0;
sprintf(b,"(%c)",tree->key);
left = print_tree(tree->left,1,offset,depth+1,s);
right = print_tree(tree->right,0,offset+left+width,depth+1,s);
for (i=0; i<width; i++)
s[depth][offset+left+i] = b[i];
if (depth) {
if (is_left) {
for (i=0; i<width+right; i++)
s[depth-1][offset+left+width/2+i] = '-';
} else {
for (i=0; i<left+width; i++)
s[depth-1][offset-width/2+i] = '-';
}
s[depth-1][offset+left+width/2] = '.';
}
return left+width+right;
}
Node *create_tree(char *tree_string) {
Node *queue[MAX_NODES];
Node *trenutni, *parent;
int tail, head, i;
tail = head = -1;
trenutni = parent = NULL;
for(i=0; i<(int)strlen(tree_string); i++) {
if (tree_string[i]!=';') {
if (tree_string[i]=='-') continue;
trenutni = (Node *)malloc(sizeof(Node));
trenutni->key = tree_string[i];
trenutni->left = trenutni->right = NULL;
queue[++tail] = trenutni;
if (parent && tree_string[i-1]==';') {
parent->left = trenutni;
} else if (i>0) {
parent->right = trenutni;
}
} else {
parent = queue[++head];
}
}
return queue[0];
}
/* vraca: visinu stabla + 1 */
int visina_stabla(Node *node){
if(!node)
return 0;
int lijevo = visina_stabla(node->left);
int desno = visina_stabla(node->right);
if(desno>lijevo)
return desno+1;
else
return lijevo+1;
}
/* vraca ukupan broj cvorova */
int broj_cvorova_u_stablu(Node *node){
if(!node)
return 0;
return broj_cvorova_u_stablu(node->right) +
broj_cvorova_u_stablu(node->left) +
1;
}
/* vraca broj cvorova koji se mogu
* dodati bez povecanja visine
*/
int broj_cvorova(Node *root) {
int najveci_moguci_broj_cvorova = 1;
int i;
for(i=0; i < visina_stabla(root); i++){
najveci_moguci_broj_cvorova<<=1;
}
najveci_moguci_broj_cvorova -= 1;
return najveci_moguci_broj_cvorova - broj_cvorova_u_stablu(root);
}
int main () {
Node *root=NULL;
int i, menu_choice;
char c, tree_string[MAX_TREE_STRING];
char print_format[6], empty_line[WIDTH], s[HEIGHT][WIDTH];
setbuf(stdout, NULL);
do {
menu_choice = 0;
DEBUG("\n1 Broj cvorova koje je moguce dodati bez povecanje visine (broj_cvorova)");
DEBUG("\n2 Kreiraj stablo \n3 Ispis \n4 Izlaz\n");
scanf("%d", &menu_choice);
switch (menu_choice) {
case 1:
printf("%d\n",broj_cvorova(root));
break;
case 2:
DEBUG("Unesite stablo kao niz alfanumerickih znakova odvojenih sa znakom ;\n");
scanf(" %s", tree_string);
root = create_tree(tree_string);
break;
case 3:
sprintf(print_format, "%%%ds", WIDTH-1);
for (i=0; i<HEIGHT; i++)
sprintf(s[i], print_format, " ");
print_tree(root,0,0,0,s);
sprintf(empty_line, print_format, " ");
for (i=0; i<HEIGHT; i++) {
if (strcmp(s[i],empty_line))
printf("%s\n", s[i]);
}
break;
case 4:
break;
default:
while((c = getchar()) != '\n' && c != EOF);
}
} while(menu_choice!=4);
return 0;
}