-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
136 lines (124 loc) · 3.02 KB
/
main.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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max_length 30
#define max_base 20
typedef struct tree
{
int count;
int n;
struct tree *l_branch, *r_branch;
} tree_t;
void tree_fill(tree_t **);
void add_leaf(tree_t *, int);
void tree_output(tree_t *);
void tree_find(tree_t *, int);
int atoi_change();
int num_check();
int main(int argc, char** argv)
{
tree_t *my_tree;
int ident = 0 , find;
my_tree = NULL;
do
{
tree_fill(&my_tree);
puts("Want to continue?");
ident = atoi_change();
} while(ident);
tree_output(my_tree);
puts("What you'd like to find?");
find = num_check();
tree_find(my_tree, find);
return 0;
}
void tree_fill(tree_t **my_tree)
{
int num;
printf("Enter the value of the new node: ");
num = num_check();
if (!(*my_tree)) {
*my_tree = (tree_t*)calloc(1, sizeof(tree_t));
(*my_tree)->count = num;
(*my_tree)->n = 1;
} else {
add_leaf(*my_tree, num);
}
}
void add_leaf(tree_t *leaf, int num)
{
if (num > leaf->count) {
if (leaf->r_branch) {
add_leaf(leaf->r_branch, num);
return;
} else {
leaf->r_branch = (tree_t*)calloc(1, sizeof(tree_t));
leaf->r_branch->count = num;
leaf->r_branch->n = (leaf->n*2)+1;
return;
}
} else if (num < leaf->count) {
if (leaf->l_branch) {
add_leaf(leaf->l_branch, num);
return;
} else {
leaf->l_branch = (tree_t*)calloc(1, sizeof(tree_t));
leaf->l_branch->count = num;
leaf->l_branch->n = (leaf->n*2);
return;
}
} else if (num == leaf->count) {
printf("There's already such a leaf. Please, repeat input.\n");
}
return;
}
void tree_output(tree_t *my_tree)
{ if(my_tree)
{ printf("Node contains : %d , count number %d\n", my_tree->count, my_tree->n);
if (my_tree->l_branch) tree_output(my_tree->l_branch);
if (my_tree->r_branch) tree_output(my_tree->r_branch);
}
}
void tree_find(tree_t *my_tree, int find)
{
if(my_tree->count == find) printf("Node contains : %d , count number %d\n", my_tree->count, my_tree->n);
else if (my_tree->count < find)
{
if(my_tree->l_branch) tree_find(my_tree->l_branch, find);
else puts("There's not such a number!");
}
else
{
if(my_tree->r_branch) tree_find(my_tree->r_branch, find);
else puts("There's not such a number!");
}
}
int atoi_change()
{
int identifier;
char string[128];
while(1)
{
printf("(yes - 1/no - 0) ");
identifier = num_check();
if(identifier == 1 || identifier == 0) { break; }
puts("It's not correct. Please, input data again.");
}
return identifier;
}
int num_check()
{
char buffer[max_length], *bufferPointer;
int num;
bufferPointer = buffer;
do {
fgets(buffer, max_length, stdin);
num = strtol(buffer, &bufferPointer, 10);
if (*bufferPointer != '\n') {
printf("Incorrect value. Try again: ");
continue;
}
break;
} while (1);
return num;
}