-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.c
144 lines (120 loc) · 2.66 KB
/
tree.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
#include <stdio.h>
#include <stdlib.h>
typedef struct ListaSeq {
int *v;
int top,max;
} ListaSeq;
ListaSeq * newLista(int max){
ListaSeq * aux = (ListaSeq *)malloc(sizeof(ListaSeq));
aux->top = 0;
aux->max = max;
aux->v = (int *)malloc(sizeof(int)*max);
return aux;
}
void printLSeq(ListaSeq *l){
printf("[");
for(int i=0;i<l->top;i++){
printf("%d",l->v[i]);
if(i<l->top-1){
printf(",");
}
}
printf("]\n");
}
void addLSeq(ListaSeq *l, int valor){
if(l->top==l->max){
printf("Lista cheia\n");
exit(0);
}
l->v[l->top++]=valor;
}
// binary tree implementation
typedef struct Node {
int value;
struct Node * right;
struct Node * left;
} Node;
Node * newNode(int value) {
Node * n = (Node*)malloc(sizeof(Node));
n->value = value;
n->right = NULL;
n->left = NULL;
return n;
}
typedef struct Tree {
Node * raiz;
} Tree;
Tree * newTree() {
Tree * t = (Tree*)malloc(sizeof(Tree));
t->raiz = NULL;
return t;
}
void addNode(Node * Node, int value) {
if(value > Node->value) {
if(Node->right == NULL)
Node->right = newNode(value);
else
addNode(Node->right, value);
} else {
if(Node->left == NULL)
Node->left = newNode(value);
else
addNode(Node->left, value);
}
}
void addNodeTree(Tree * t, int value) {
if(t->raiz == NULL) {
t->raiz = newNode(value);
} else {
addNode(t->raiz, value);
}
}
void printTree(Node * Node) {
if(Node==NULL) {
return;
}
printTree(Node->left);
printf("%d ", Node->value);
printTree(Node->right);
}
//treeSorts com lista sequencial e lista ligada
void TreeToVector(Node * Node, ListaSeq * l) {
if(Node==NULL) {
return;
}
TreeToVector(Node->left, l);
addLSeq(l, Node->value);
TreeToVector(Node->right, l);
}
void TreeSortSeq(ListaSeq * l) {
Tree * t = newTree();
for(int i=0; i<l->top; i++) {
addNodeTree(t, l->v[i]);
}
l->top = 0;
TreeToVector(t->raiz, l);
free(t);
}
int sizeTree(Node * Node) {
if (Node==NULL) {
return 0;
}
int left = sizeTree(Node->left);
int right = sizeTree(Node->right);
return 1+left+right;
}
int main() {
Tree * t = newTree();
ListaSeq * l = newLista(10);
for(int i=0; i<10; i++) {
addNodeTree(t, rand()%25);
addLSeq(l, rand()%30);
}
printf("Tree: \n");
printTree(t->raiz);
printf("\n\nLista sequencial: \n");
printLSeq(l);
TreeSortSeq(l);
printf("\nLista sequencial ordenada: \n");
printLSeq(l);
}