我正在尝试为二进制搜索树编写析构函数,我知道如何递归遍历树,但我不知道如何在析构函数中执行此操作,以便删除每个节点。
我的标题是:
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;
}
有没有办法递归调用析构函数?如果没有,我如何遍历每个节点以删除它。
答案 0 :(得分:4)
1078 4.9E-324 seconds, (6.176E-321 ms)
1079 4.9E-324 seconds, (3.09E-321 ms)
1080 0.0 seconds, (1.54E-321 ms)
1081 0.0 seconds, (7.7E-322 ms)
1082 0.0 seconds, (3.85E-322 ms)
1083 0.0 seconds, (1.93E-322 ms)
1084 0.0 seconds, (1.0E-322 ms)
1085 0.0 seconds, (4.9E-323 ms)
1086 0.0 seconds, (2.5E-323 ms)
1087 0.0 seconds, (1.0E-323 ms)
1088 0.0 seconds, (4.9E-324 ms)
答案 1 :(得分:1)
你需要两个析构函数:
In [2]: dic1 = {4:1, 7:2, 8:3, 9:4}
In [3]: new_dict = {k: v * 2 for k, v in dic1.iteritems()} # dic1.items() for Python 3
In [4]: new_dict
Out[4]: {4: 2, 7: 4, 8: 6, 9: 8}
和
Tree::~Tree()
{
delete Root;
}
但你真的不需要两节课。每Node::~Node()
{
delete Left;
delete Right;
}
都是一棵树。
答案 2 :(得分:0)
当您致电delete
或您的Tree
进入生命周期结束时(从某个区块退出,例如最后的示例),您必须delete
Tree
} Node
和delete
运算符将调用析构函数,最后请参见示例。
当调用Tree析构函数时,这将使Tree完全从内存中消失。
试试这个:
#include <iostream>
using namespace std;
class Node {
Node *left, *right;
public:
Node(Node *l, Node *r);
~Node();
};
class Tree {
Node *root;
public:
Tree(Node *rt);
~Tree();
};
Tree::Tree(Node *rt):root(rt) {
cout << "new Tree with root node at " << rt << endl;
}
Tree::~Tree() {
cout << "Destructor of Tree" << endl;
if (root) delete root;
}
Node::Node(Node *l, Node *r):left(l), right(r) {
cout << "Node@"
<< this
<< "(left:" << l
<< ", right:" << r << ")"
<< endl;
}
Node::~Node() {
cout << "~Node@" << this
<< endl;
if (left) delete left;
if (right) delete right;
}
int main() {
Tree t(
new Node(
new Node(
new Node(
new Node(0, 0),
0),
0),
new Node(0, new Node(0, 0))));
}
答案 3 :(得分:0)
这是我的实现。一棵树等于一个TreeNode。
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
~TreeNode() {
delete left;
delete right;
}
...
};
只需删除树的根节点,然后将递归删除整个树。
TreeNode* root = new TreeNode(2);
delete root;
您可能已经知道删除的作用。
使用delete删除C ++类对象的内存时, 对象的析构函数在对象的内存被调用之前被调用 释放(如果对象具有析构函数)。
因此,在treeNode的析构函数中,您只需要破坏您手动分配的左右指针即可。您无需担心节点本身的重新分配。