Я пытаюсь написать деструктор для моего двоичного дерева поиска, и я знаю, как рекурсивно прокручивать дерево, но я не знаю, как это сделать в деструкторе, чтобы каждый узел был удален.Деструктор для дерева двоичного поиска
Мой Заголовок является:
struct Node;
typedef string TreeType;
typedef Node * TreePtr;
//Defines a Node
struct Node
{
TreeType Info;
int countDuplicates = 1;
TreePtr Left, Right;
};
class Tree
{
public:
//Constructor
Tree();
//Destructor
~Tree();
//Retruns true if the tree is Empty
bool Empty();
//Inserts a Node into the tree
bool Insert(TreeType);
//Delete decides what type of delection needs to occur, then calls the correct Delete function
bool Delete(Node * , Node *);
//Deletes a leaf node from the tree
bool DeleteLeaf(TreePtr, TreePtr);
//Deletes a two child node from the tree
bool DeleteTwoChild(TreePtr);
//Deletes a one child node from the tree
bool DeleteOneChild(TreePtr, TreePtr);
//Finds a certain node in the tree
bool Find(TreeType);
//Calculates the height of the tree
int Height(TreePtr);
//Keeps a count of the nodes currently in the tree;
void Counter();
private:
//Prints the nodes to the output text file in order alphabetically
void InOrder(ofstream &,TreePtr);
//Defines a TreePtr called Root
TreePtr Root;
//Defines a TreePtr called Current
TreePtr Current;
//Defines a TreePtr called Parent
TreePtr Parent;
};
Мой конструктор:
Tree::Tree()
{
Root = NULL;
Current = NULL;
Parent = NULL;
}
Есть ли способ вызвать деструктор рекурсивно? Если нет, как мне пройти через каждый узел, чтобы удалить его.
Представьте себе, если вы могли бы вызвать деструктор рекурсивно. Деструктор работает с текущим объектом, являющимся «Деревом», а не «Узлом». И он не принимает никаких параметров. Итак, как он узнает, какой узел разрушить? – immibis