Professional Documents
Culture Documents
АСДП КН1022В ПЗ13 Гасюк
АСДП КН1022В ПЗ13 Гасюк
ЗВІТ
Харків-2023
2
пошуку.
Завдання №1.
Варіант 6
Код програми
#include <conio.h>
#include <stdlib.h>
#include <iostream>
#include <stack>
class CtreeNode
int key;
int info;
public:
key = k;
info = info_c;
~CtreeNode() {}
3
};
cout << "\n Додається елемент (" << n << ";" << inf << ")\n";
int flag = 1;
p = ptr;
while (flag)
if (n < p->key)
q = p->LLink; //a3
if (q == NULL)
else
if (q == NULL)
flag = 0;
cout << "Створення новий елемент (" << n << "," << inf << ")\n";
cout << "Оновлення зв’яків: " << p->key << "->LLink = " << q->key << endl;
p->LLink = q;
else
p = q;
q = p->RLink; //a4
if (q == NULL)
else
if (q == NULL)
flag = 0;
cout << "Створюється новий елемент (" << n << "," << inf << ")\n ";
cout << "Оновлення зв’яків: " << p->key << "->RLink = " << q->key << endl;
p->RLink = q;
else
p = q;
else if (n == p->key)
}
5
if (ptr)
PrintTree(ptr->RLink, n + 3);
PrintTree(ptr->LLink, n + 3);
if (ptr != NULL)
DeleteTree(ptr->LLink);
DeleteTree(ptr->RLink);
delete (ptr);
if (ptr == nullptr)
return 0;
int count = 0;
6
count++;
cout << "Recursive: Node " << ptr->key << " is greater than " << threshold << endl;
return count;
int count = 0;
while (ptr)
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;
if (k < ptr->key)
if (ptr->LLink == nullptr)
ptr->LLink = newNode;
else
InsertNodeWithKey(k, ptr->LLink);
if (ptr->RLink == nullptr)
ptr->RLink = newNode;
else
InsertNodeWithKey(k, ptr->RLink);
8
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;
if (rand_elem == 'n')
else
if (rand_elem == 'n')
else
}
10
root->PrintTree(root, 5);
int threshold = 6;
root->CompareAndInsert(threshold, root);
cout << "Дерево після додавання вузла з ключем K:" << endl;
root->PrintTree(root, 5);
root->DeleteTree(root);
return 0;
}
11