информатики и радиоэлектроники
Бинарные деревья
Минск,2020
1)Задание
Разработать проект для обработки дерева поиска, каждый элемент
которого содержит целочисленный ключ и строку текста, содержащую,
например, ФИО и номер паспорта. В программе должны быть реализованы
следующие возможности:
- создание дерева;
- добавление новой записи;
- поиск информации по заданному ключу;
- удаление информации с заданным ключом;
- вывод информации;
- решение индивидуального задания;
- освобождение памяти при выходе из программы.
Задание варианта 5 : определить число узлов на каждом уровне дерева.
2)Код
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <conio.h>
#include <time.h>
int count = 0;
typedef struct {
char name[15];
char surname[15];
char patronymic[15];
int premia;
} School;
typedef struct TREE {
int key;
School sotr;
}TREE;
root->root = NULL;
root->key = 10;
tree = root;
return tree;
int index = 0;
char ch;
do
ch = _getch();
if (ch == 8)
str[index] = 0;
index--;
puts("Некорректный ввод");
system("pause");
str[i] = 0;
return 0;
if ((ch >= 'а' && ch <= 'я') || (ch >= 'А' && ch <= 'Я') || (ch == 'ё') || (ch == 'Ё'))
puts("Некорректный ввод");
system("pause");
str[i] = 0;
return 0;
printf("%c", ch);
str[index] = ch;
index++;
if (index < 0)
puts("Некорректный ввод");
system("pause");
str[i] = 0;
return 0;
if (str[0] == 'ё')
str[0] = 'Ё';
if (str[i] == 'Ё')
str[i] = 'ё';
return 1;
sotr->name[i] = 0;
sotr->surname[i] = 0;
sotr->patronymic[i] = 0;
}
do
system("cls");
} while (!get(sotr->surname));
do
} while (!get(sotr->name));
do
} while (!get(sotr->patronymic));
int premia = 0;
scanf_s("%d", &premia);
if (premia > 0)
sotr->premia = premia;
else
premia = 0;
printf("Повторите попытку\n");
system("pause");
count++;
}
TREE* kkk(TREE* tree, int key)
if (count == 0)
vvod_dereva(&tree->sotr);
return tree;
else
root2 = root1;
root1 = root1->left;
root1 = root1->right;
vvod_dereva(&node->sotr);
node->key = key;
node->root = root2;
node->left = node->right = NULL;
root2->left = node;
else
root2->right = node;
return tree;
length = k - length;
int i = 0;
printf(" ");
i++;
int number = 0;
number++;
length /= 10;
number = k - number;
int i = 0;
printf(" ");
i++;
space_int(root->key, 3);
printf("||");
int i = strlen(sotr->surname);
true_otobr(i, 15);
printf("||");
i = strlen(sotr->name);
true_otobr(i, 15);
printf("||");
i = strlen(sotr->patronymic);
true_otobr(i, 15);
printf("||");
space_int(sotr->premia, 7);
printf("||\n");
}
if (root)
p_tree(root->right);
print_sotr_school(&root->sotr, root);
p_tree(root->left);
if (root)
printf("%d\n", root->key);
else
{
printf("|-----------------------------------------------------------------------|\n");
printf("|------|------------------|----------------|------------------|---------|\n");
print_sotr_school(&root->sotr, root);
printf("|-----------------------------------------------------------------------|\n");
print++;
return print;
if (root)
del_t(root->left);
del_t(root->right);
free(root);
root = 0;
return root;
if (root)
return 0;
}
TREE* findmax(TREE* root)
if (root->left)
root = findmax(root->left);
return root;
if (root)
else
root->sotr = localMax->sotr;
root->key = localMax->key;
return del;
else if (root->left) {
if (root == root->root->left) {
root->root->left = root->left;
else {
root->root->right = root->left;
}
}
else if (root->right) {
if (root == root->root->right) {
root->root->right = root->right;
else {
root->root->left = root->right;
else {
if (root->root)
if (root == root->root->left) {
root->root->left = NULL;
else {
root->root->right = NULL;
del++;
count--;
free(root);
return del;
int fm(int n)
{
int choice;
choice = _getch();
if (choice == 0)
choice = _getch();
if (choice < n)
printf("%c", choice);
if (tree)
mas[level]++;
return *mas;
}
int main()
while (1)
system("cls");
int m;
puts("1)Создать дерево");
puts("2)Добавить запись");
puts("7)Вариант_5");
puts("8)Выйти из программы");
printf(">> ");
m = fm(9);
switch (m)
case 1:
tree = create_tree(tree);
break;
case 2:
if (!tree)
system("pause");
break;
system("pause");
break;
while (1)
srand(time(NULL));
continue;
else
kkk(tree, key);
if (countbefore == count)
continue;
else
break;
break;
case 3:
system("cls");
if (!tree)
printf("Дерево не создано\n");
system("pause");
else if (count == 0)
printf("Дерево пусто\n");
system("pause");
else
int key = 0;
system("cls");
scanf_s("%d", &key);
int del = 0;
if (del == 0)
printf("Неверный ключ\n");
else
printf("Запись удалена\n");
system("pause");
else
printf("Неверный ввод\n");
system("pause");
break;
case 4:
if (!tree)
printf("Дерево не создано\n");
system("pause");
else if (count == 0)
printf("Дерево пусто\n");
system("pause");
else
int key = 0;
system("cls");
scanf_s("%d", &key);
int print = 0;
printf("Неверный ключ\n");
system("pause");
else
printf("Неверный ввод\n");
system("pause");
break;
case 5:
if (!tree)
system("pause");
else if (count == 0)
printf("Дерево пустое...\n");
system("pause");
else
system("cls");
printf("|-------------------------------------------------------------------------|\n");
printf("| Ключ | Фамилия | Имя || Отчество | Премия
|\n");
printf("|-------|-----------------|-----------------|-----------------|-----------|\n");
p_tree(tree);
printf("|-------------------------------------------------------------------------|\n");
keys(tree, 0);
printf("\n\n");
system("pause");
break;
case 6:
system("cls");
if (!tree)
system("pause");
else
tree = del_t(tree);
count = 0;
printf("Дерево удалено\n");
system("pause");
break;
case 7:
if (tree == NULL)
{
system("cls");
printf("\n");
system("pause");
system("cls");
break;
else if (count == 0)
system("cls");
puts("Дерево пустое");
printf("\n");
system("pause");
system("cls");
break;
}
system("cls");
int mas[5];
mas[i] = 0;
int i = 0;
while (mas[i] != 0)
i++;
break;
case 8:
if (tree)
tree = del_t(tree);
count = 0;
system("cls");
return 0;
}
default:
printf("Неверный ввод\n");
system("pause");
break;
3)Консоль
Индивидуальное задание: