binarytree.h

来自「家族树程序,实现家族树功能~数据结构实验~」· C头文件 代码 · 共 71 行

H
71
字号
#include "BinaryNode.h"

template <class Entry>
class Binary_tree {
public:

Binary_node<Entry> *root;

Binary_tree()
{
    root = new Binary_node<Entry>();
}

Binary_tree(Entry data)
{
	root = new Binary_node<Entry>(data);
}

~Binary_tree()
{
	delete root;
	root = NULL;
}

void clear( )
{
	root = new Binary_node<Entry>();
}

bool empty() const
{
    return root->data == NULL;
}

void preorder(void (*visit)(Entry &))
{
	recursive_preorder(root, visit);
}

void postorder(void (*visit)(Entry &))
{
	recursive_postorder(root, visit);
}

private:

void recursive_preorder(Binary_node<Entry> *sub_root, void (*visit)(Entry &))
{
	if (sub_root != NULL)
	{
		visit(sub_root->data);
		for (int i = 0; i < TreeDegree; i++)
		{
			recursive_preorder(sub_root->children[i], visit);
		}
	}
}

void recursive_postorder(Binary_node<Entry> *sub_root, void (*visit)(Entry &))
{
	if (sub_root != NULL)
	{
		for (int i = 0; i < TreeDegree; i++)
		{
			recursive_preorder(sub_root->children[i], visit);
		}
		visit(sub_root->data);
	}
}

};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?