Вы находитесь на странице: 1из 7

Курс «Объектно-ориентированное программирование на C++»

Встреча №17

Тема: Бинарное дерево

Домашнее задание

Задание.

Реализовать базу данных ГАИ по штрафным квитанциям с помощью бинарного дерева.


Ключом будет служить номер автомашины, значением узла - список правонарушений.
Если квитанция добавляется в первый раз, то в дереве появляется новый узел, а в списке
данные по правонарушению; если нет, то данные заносятся в существующий список.
Необходимо также реализовать следующие операции:

Полная распечатка базы данных (по номерам машин и списку правонарушений,


числящихся за ними)

Распечатка данных по заданному номеру

Распечатка данных по диапазону номеров


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <time.h>
#include <cmath>
#include <math.h>
#include <string.h>
using namespace std;

/*Реализовать базу данных ГАИ по штрафным квитанциям с помощью бинарного дерева.


Ключом будет служить номер автомашины, значением узла - список правонарушений.
Если квитанция добавляется в первый раз, то в дереве появляется новый узел, а в списке
данные по правонарушению;
если нет, то данные заносятся в существующий список. Необходимо также реализовать
следующие операции:
Полная распечатка базы данных (по номерам машин и списку правонарушений, числящихся за
ними)
Распечатка данных по заданному номеру
Распечатка данных по диапазону номеров
*/

struct LNode
{
char* fine_list;
LNode* next;
};

class List
{
LNode* ph, * pt;
int count;
public:
List()
{
ph = pt = NULL;
count = 0;
}
void Add(char* str)
{
if (ph == NULL)
{
LNode* temp = new LNode;
temp->fine_list = new char[strlen(str) + 1];
strcpy(temp->fine_list, str);
temp->next = NULL;
ph = pt = temp;
count++;
}
else
{
LNode* temp = new LNode;
temp->fine_list = new char[strlen(str) + 1];
strcpy(temp->fine_list, str);
temp->next = NULL;
pt->next = temp;
pt = temp;
count++;
}
}

void Add(int i, char* str) {

if (i - 1 > count) {
Add(str);
}
else if (i - 2 < 0) {
LNode* temp = new LNode;
temp->next = ph;
ph = temp;
ph->fine_list = new char[strlen(str) + 1];
strcpy(ph->fine_list, str);
count++;

}
else {
int tempi = count;
count = 0;
LNode* add = new LNode, * temp1 = ph, *temp2;
for (; i - 2 != count; count++) {
temp1 = temp1->next;
}
temp2 = temp1->next;
temp1->next = add;
add->next = temp2;
add->fine_list = new char[strlen(str) + 1];
strcpy(add->fine_list, str);
count = tempi + 1;
}
}

void Print()
{
LNode* temp = ph;
if (temp != NULL)
{
while (temp)
{
cout << temp->fine_list << " ";
temp = temp->next;
}
cout << endl;
}
else
{
cout << "List Empty" << endl;
}

}
void DelH()
{
if (ph != NULL)
{
LNode* temp = ph->next;
if (ph->fine_list != NULL)
delete[] ph->fine_list;
delete ph;
ph = temp;
count--;
}

void DelAll()
{
while (ph != NULL)
{
DelH();
}
}

char* Search(char* str) {


LNode* temp = ph;
while (temp->next) {
if (strcmp(temp->fine_list, str) == 0)
return temp->fine_list;
temp = temp->next;
}
return NULL;
}

bool ItEmpty() {
if (ph == NULL)
return 1;
return 0;
}

~List()
{
DelAll();
}
};

struct Node
{
int number;
char* name;
Node* parent, * left, * right;
List protocol;
};

class Tree
{
Node* root;

public:
Tree()
{
root = NULL;
}
Node* Add(int x, char* str)
{
if (root == NULL)
{
root = new Node;
root->number = x;
root->name = new char[strlen(str) + 1];
strcpy(root->name, str);
root->parent = NULL;
root->left = NULL;
root->right = NULL;
return root;
}
else
{
Node* temp = new Node;
temp->number = x;
temp->name = new char[strlen(str) + 1];
strcpy(temp->name, str);
temp->left = NULL;
temp->right = NULL;

Node* y = root;
Node* temp1 = NULL;

while (y != NULL)
{
temp1 = y;
if (y->number > x)
{
y = y->left;
}
else
{
y = y->right;
}

if (temp1->number > x)
{
temp1->left = temp;
}
else
{
temp1->right = temp;
}

temp->parent = temp1;
return temp;
}
}
Node* GetRoot()
{
return root;
}

void Print_A_B(Node* y, int a, int b) {


if (y != NULL) {
Print_A_B(y->left, a, b);
if (y->number >= a && y->number <= b) {
cout << y->number << " " << y->name << "\nProtocol: ";
y->protocol.Print();
}
Print_A_B(y->right, a, b);
}
}

void Print_Min(Node* y)
{
if (y != NULL)
{
Print_Min(y->left);
cout << y->number << " " << y->name << "\nProtocol: ";
y->protocol.Print();
Print_Min(y->right);
}
}

void Print_Max(Node* y)
{
if (y != NULL)
{
Print_Max(y->right);
cout << y->number << " " << y->name << "\nProtocol: ";
y->protocol.Print();
Print_Max(y->left);
}
}

void print(Node* y) {
cout << y->number << " " << y->name << "\nProtocol: ";
y->protocol.Print();
}

Node* Search(Node* y, int x)


{
while (y != NULL && x != y->number)
{
if (x < y->number)
{
y = y->left;
}
else
{
y = y->right;
}
}
return y;
}

Node* Min(Node* y)
{
if (y != NULL)
{
while (y->left != NULL)
{
y = y->left;
}
}
return y;
}

Node* Max(Node* y)
{
if (y != NULL)
{
while (y->right != NULL)
{
y = y->right;
}
}
return y;
}

Node* Next(Node* y)
{
Node* temp = NULL;
if (y != NULL)
{
if (y->right != NULL)
{
return Min(y->right);
}

temp = y->parent;
while (temp != NULL && y == temp->right)
{
y = temp;
temp = temp->parent;
}
}
return temp;
}

Node* Previous(Node* y)
{
Node* temp = NULL;
if (y != NULL)
{
if (y->left != NULL)
{
return Max(y->left);
}

temp = y->parent;
while (temp != NULL && y == temp->left)
{
y = temp;
temp = temp->parent;
}
}
return temp;
}

void addProtocol(int x, char* str) {


Node* temp = NULL;
temp = Search(root, x);
if (temp != NULL) {
temp->protocol.Add(str);
}
else {
temp = Add(x, str);
temp->protocol.Add(str);
}
}

void Delete(Node* y)
{
if (y == NULL) return;

Delete(y->left);
Delete(y->right);
y->protocol.DelAll();
delete[] y->name;
delete y;
}
};

void main()
{
int arr[] = { 6, 3, 9, 2, 4, 7, 10, 1, 5, 8};
Tree t;
char* name = new char[10];
for (int i = 0; i < 10; i++)
{
cin >> name;
t.Add(arr[i], name);
}

Node* y = t.Search(t.GetRoot(), 95);


y = t.Search(t.GetRoot(), 30);

y = t.Min(t.GetRoot());
t.print(y);
cout << endl;

y = t.Max(t.GetRoot());
t.print(y);
cout << endl;

y = t.Search(t.GetRoot(), 9);
y = t.Min(y);
t.print(y);
cout << endl;

y = t.Search(t.GetRoot(), 8);
y = t.Next(y);
t.print(y);
cout << endl;

y = t.Search(t.GetRoot(), 3);
y = t.Previous(y);
t.print(y);
cout << endl;

t.Print_Min(t.GetRoot());
cout << endl;

t.addProtocol(25, (char*)"404");
t.addProtocol(25, (char*)"404");
t.addProtocol(25, (char*)"404");
t.Print_Max(t.GetRoot());
cout << endl;

t.Print_A_B(t.GetRoot(), 1, 3);

t.Delete(t.GetRoot());
system("pause");

Вам также может понравиться