С++
Доброго времени суток. Я уже довольно часто задаю вопросы здесь и каждый раз мне помогают. Надеюсь и в этот раз мне смогут помочь. Задача заключается в то, что надо создать шаблонный класс бинарного дерева на С++. Просто реализацию бинарного дерева я написал. Вот она:

#include <iostream>
int tabs = 0;
int kol_vo = 0;
struct Branch
{
int Data;
Branch* LeftBranch;
Branch* RightBranch;
};
void Add(int aData, Branch*& aBranch)
{
if (!aBranch)
{
aBranch = new Branch;
aBranch->Data = aData;
aBranch->LeftBranch = 0;
aBranch->RightBranch = 0;
return;
}
else
if (aBranch->Data > aData)
{
Add(aData, aBranch->LeftBranch);
}
else
{
Add(aData, aBranch->RightBranch);
};
}
void print(Branch* aBranch)
{
if (!aBranch) return;
tabs += 5;
print(aBranch->LeftBranch);
for (int i = 0; i < tabs; i++) std::cout « " ";
std::cout « aBranch->Data «std:: endl;
print(aBranch->RightBranch);
tabs-= 5;
return;
}
void pr_obh(Branch*& aBranch)
{
if (NULL == aBranch) return;
std::cout « aBranch->Data « std::endl;
pr_obh(aBranch->LeftBranch);
pr_obh(aBranch->RightBranch);
}
void add_elem(int aData, Branch*& aBranch)
{
if (!aBranch)
{
aBranch = new Branch;
aBranch->Data = aData;
aBranch->LeftBranch = 0;
aBranch->RightBranch = 0;
return;
}
else
{
if (aData < aBranch->Data)
{
add_elem(aData, aBranch->LeftBranch);
}
else if (aData > aBranch->Data)
{
add_elem(aData, aBranch->RightBranch);
}
}
}
void is_Empty(Branch*& aBranch)
{
if (!aBranch)
{
std::cout « "The tree is empty";
}
else
{
std::cout « "The tree is not empty";
}
}
void FreeTree(Branch* aBranch)
{
if (!aBranch) return;
FreeTree(aBranch->LeftBranch);
FreeTree(aBranch->RightBranch);
delete aBranch;
return;
}
Branch* del_elem(Branch*& aBranch, int aData)
{
if (aBranch == NULL)
return aBranch;
if (aData == aBranch->Data)
{
Branch* tmp;
if (aBranch->RightBranch == NULL)
tmp = aBranch->LeftBranch;
else
{
Branch* ptr = aBranch->RightBranch;
if (ptr->LeftBranch == NULL)
{
ptr->LeftBranch = aBranch->LeftBranch;
tmp = ptr;
}
else
{
Branch* pmin = ptr->LeftBranch;
while (pmin->LeftBranch != NULL)
{
ptr = pmin;
pmin = ptr->LeftBranch;
}
ptr->LeftBranch = pmin->RightBranch;
pmin->LeftBranch = aBranch->LeftBranch;
pmin->RightBranch = aBranch->RightBranch;
tmp = pmin;
}
}
delete aBranch;
return tmp;
}
else if (aData < aBranch->Data)
aBranch->LeftBranch = del_elem(aBranch->LeftBranch, aData);
else
aBranch->RightBranch = del_elem(aBranch->RightBranch, aData);
return aBranch;
}
int main()
{
setlocale(LC_ALL, "rus");
Branch* Root = 0;
int vel;
int element;
int k;
std::cout « "Enter the number of elements for the future tree: ";
std::cin » vel;
std::cout «std:: endl;
std::cout « std::endl;
for (int i = 0; i < vel; i++)
{
Add(rand() % 100, Root);
}
std::cout « std::endl;
std::cout « "Binary tree: " « std::endl;
print(Root);
std::cout « std::endl;
std::cout « "Direct traversal of the binary tree: " « std::endl;
pr_obh(Root);
std::cout « std::endl;
std::string instruction;
std::cout « "add - 01, delete - 02, exit - 00" « std::endl;
std::cin » instruction;
while (instruction != "00")
{
if(instruction == "01")
{
std::cout « "Adding a new element to the binary tree:" « std::endl;
std::cout « "Enter a new element: ";
std::cin » instruction;
std::cin.ignore();
if(instruction == "01" || instruction == "02" || instruction == "00")
{
continue;
}
else
{
element = atoi(instruction.c_str());
instruction = "01";
}
add_elem(element, Root);
std::cout « "Binary tree: " « std::endl;
print(Root);
std::cout « std::endl;
}
else if(instruction == "02")
{
std::cout « "Removing an element from a binary tree:" « std::endl;
std::cout « "Enter the element to delete: ";
std::cin » instruction;
std::cin.ignore();
if(instruction == "01" || instruction == "02" || instruction == "00")
{
continue;
}
else
{
element = atoi(instruction.c_str());
instruction = "02";
}
del_elem(Root, element);
std::cout « "Binary tree: " « std::endl;
print(Root);
std::cout « std::endl;
}
}
FreeTree(Root);
return 0;
}
Очень надеюсь, что мне смогут помочь или хотя бы подробно объяснят, как это сделать. Даю все баллы! ​
Please enter comments
Please enter your name.
Please enter the correct email address.
You must agree before submitting.

Answers & Comments


Copyright © 2024 SCHOLAR.TIPS - All rights reserved.