You are on page 1of 12

Міністерство освіти і науки України

НАЦІОНАЛЬНИЙ ТЕХНІЧНИЙ УНІВЕРСИТЕТ


«ХАРКІВСЬКИЙ ПОЛІТЕХНІЧНИЙ ІНСТИТУТ»

Навчально-науковий інститут комп'ютерних наук та інформаційних технологій

Кафедра «Обчислювальна техніка та програмування»

ЗВІТ

з дисципліни "АЛГОРИТМИ ТА СТРУКТУРИ ДАНИХ"

про виконання Практичної роботи №13

АЛГОРИТМИ ОБРОБКИ ДЕРЕВ

Виконав: студент групи КН-1022В


Гасюк М.О.

Перевірив: Доцент ОТП


Бречко В.О.

Харків-2023
2

Мета: набути навичок та закріпити знання при виконанні операцій

пошуку.

Завдання №1.

Варіант 6
Код програми

#include <conio.h>

#include <stdlib.h>

#include <iostream>

#include <stack>

using namespace std;

class CtreeNode

int key;

int info;

CtreeNode *LLink, *RLink;

public:

CtreeNode() { key = 0; LLink = RLink = NULL; }

CtreeNode(int k, int info_c)

key = k;

info = info_c;

LLink = RLink = NULL;

~CtreeNode() {}
3

void Insert(int number, int info, CtreeNode *ptr);

void PrintTree(CtreeNode *ptr, int n);

void DeleteTree(CtreeNode *ptr);

int CountNodesGreaterThanRecursive(int threshold, CtreeNode *ptr);

int CountNodesGreaterThanStack(int threshold, CtreeNode *ptr);

void InsertNodeWithKey(int k, CtreeNode *ptr);

void CompareAndInsert(int threshold, CtreeNode *ptr);

};

void CtreeNode::Insert(int n, int inf, CtreeNode *ptr)

cout << "\n Додається елемент (" << n << ";" << inf << ")\n";

int flag = 1;

CtreeNode *p, *q;

p = ptr;

while (flag)

if (n < p->key)

q = p->LLink; //a3

cout << "Знайден вузол " << p->key << endl

<< "Перехід уліво";

if (q == NULL)

cout << "NULL" << endl;

else

cout << q->key << endl;

if (q == NULL)

flag = 0;

cout << "Створення новий елемент (" << n << "," << inf << ")\n";

q = new CtreeNode(n, inf);


4

cout << "Оновлення зв’яків: " << p->key << "->LLink = " << q->key << endl;

p->LLink = q;

else

p = q;

else if (n > p->key)

q = p->RLink; //a4

cout << "Знайден вузе " << p->key << endl

<< "Перехід управо";

if (q == NULL)

cout << "NULL" << endl;

else

cout << q->key << endl;

if (q == NULL)

flag = 0;

cout << "Створюється новий елемент (" << n << "," << inf << ")\n ";

q = new CtreeNode(n, inf);

cout << "Оновлення зв’яків: " << p->key << "->RLink = " << q->key << endl;

p->RLink = q;

else

p = q;

else if (n == p->key)

cout << "Такий елемент вже існує" << endl;

flag = 0; //вихід із циклу WHILE

}
5

void CtreeNode::PrintTree(CtreeNode *ptr, int n)

if (ptr)

PrintTree(ptr->RLink, n + 3);

for (int i = 1; i < n; i++)

cout << " ";

cout << ptr->key << endl;

PrintTree(ptr->LLink, n + 3);

void CtreeNode::DeleteTree(CtreeNode *ptr)

if (ptr != NULL)

DeleteTree(ptr->LLink);

DeleteTree(ptr->RLink);

delete (ptr);

int CtreeNode::CountNodesGreaterThanRecursive(int threshold, CtreeNode *ptr)

if (ptr == nullptr)

return 0;

int count = 0;
6

if (ptr->key > threshold)

count++;

cout << "Recursive: Node " << ptr->key << " is greater than " << threshold << endl;

count += CountNodesGreaterThanRecursive(threshold, ptr->LLink);

count += CountNodesGreaterThanRecursive(threshold, ptr->RLink);

return count;

int CtreeNode::CountNodesGreaterThanStack(int threshold, CtreeNode *ptr)

stack<CtreeNode *> nodeStack;

int count = 0;

while (ptr || !nodeStack.empty())

while (ptr)

if (ptr->key > threshold)

count++;

cout << "Stack: Node " << ptr->key << " is greater than " << threshold << endl;

nodeStack.push(ptr);

ptr = ptr->LLink;

ptr = nodeStack.top();
7

nodeStack.pop();

ptr = ptr->RLink;

return count;

void CtreeNode::InsertNodeWithKey(int k, CtreeNode *ptr)

if (k < ptr->key)

if (ptr->LLink == nullptr)

CtreeNode *newNode = new CtreeNode(k, 0);

ptr->LLink = newNode;

else

InsertNodeWithKey(k, ptr->LLink);

else if (k > ptr->key)

if (ptr->RLink == nullptr)

CtreeNode *newNode = new CtreeNode(k, 0);

ptr->RLink = newNode;

else

InsertNodeWithKey(k, ptr->RLink);
8

void CtreeNode::CompareAndInsert(int threshold, CtreeNode *ptr)

int nodesCountRecursive = CountNodesGreaterThanRecursive(threshold, ptr);

int nodesCountStack = CountNodesGreaterThanStack(threshold, ptr);

cout << "Кількість вузлів, ключ яких більший за " << threshold << " (рекурсивно): " << nodesCountRecursive << endl;

cout << "Кількість вузлів, ключ яких більший за " << threshold << " (з використанням стека): " << nodesCountStack <<
endl;

if (nodesCountRecursive == nodesCountStack)

int K = nodesCountRecursive;

cout << "Результати обох алгоритмів співпадають. Вузол з ключем " << K << " буде доданий до дерева." << endl;

InsertNodeWithKey(K, ptr);

else

cout << "Результати обох алгоритмів не співпадають. Новий вузол не доданий." << endl;

int main()

setlocale(LC_ALL, "Rus");

int k_elem;

char rand_elem;

cout << " Скількм елементів додавати? ";

cin >> k_elem;


9

cout << " Генерувати випадкові елементы? (y/n) ";

cin >> rand_elem;

int num, inf;

if (rand_elem == 'n')

cout << endl

<< "Індекс: ";

cin >> num;

cout << "Значення: ";

cin >> inf;

else

num = rand() % 100 + 1;

inf = rand() % 100 + 1;

CtreeNode *root = new CtreeNode(num, inf);

for (int tr_i = 1; tr_i < k_elem; tr_i++)

if (rand_elem == 'n')

cout << endl

<< "Индекс: ";

cin >> num;

cout << "Значение: ";

cin >> inf;

else

num = rand() % 100 + 1;

inf = rand() % 100 + 1;

}
10

root->Insert(num, inf, root);

root->PrintTree(root, 5);

int threshold = 6;

root->CompareAndInsert(threshold, root);

cout << "Дерево після додавання вузла з ключем K:" << endl;

root->PrintTree(root, 5);

root->DeleteTree(root);

return 0;

}
11

Рисунок 1.1 – результат програми


12

Рисунок 1.2 – результат програми

You might also like