forked from huffz/Huffman_Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertabb.c
67 lines (60 loc) · 1.25 KB
/
insertabb.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
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
typedef struct binary_tree
{
long long int item;
struct binary_tree *left;
struct binary_tree *right;
}b_tree;
b_tree *create_b_tree(long long int item)
{
b_tree *new_tree = (b_tree *) malloc(sizeof(b_tree));
new_tree->item = item;
new_tree->left = new_tree->right = NULL;
return new_tree;
}
b_tree *create_empty_b()
{
return NULL;
}
long long int comparisons=0;
b_tree *insert(b_tree *tree, long long int item)
{
if(tree == NULL){
tree = create_b_tree(item);
comparisons++;
}else{
if(item >= tree->item){
tree->right = insert(tree->right, item);
comparisons++;
}else{
tree->left = insert(tree->left, item);
comparisons++;
}
}
return tree;
}
void free_tree(b_tree *tree)
{
if(tree == NULL)
return;
free_tree(tree->left);
free_tree(tree->right);
free(tree);
}
int main()
{
b_tree *tree = create_empty_b();
long long int n,size=0;
printf("num_nodes n_comparisons\n");
while(scanf("%lld", &n) != EOF)//le a entrada
{
tree = insert(tree, n);
printf("%lld %lld\n",size, comparisons);//imprime no inputabb.txt o tamanho da entrada e o num de comparisons
size++;//apos n ser adicionado o num de nodes da tree aumenta
comparisons=0;
}
free_tree(tree);
return 0;
}