⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tree1.h

📁 大学计算机专业课程中数据结构各章的算法设计
💻 H
字号:
#include<iostream.h>
#include"TreeNode1.h"
class Tree1
{
public:
	TreeNode1 *root;
	Tree1(char *str="");
	~Tree1();
	TreeNode1 *create(char *str);
	void destroy(TreeNode1 *p);
	void preorder();
	void inorder();
	void postorder();
};
void Tree1::preorder()
{
	cout<<"先序遍历二叉树";
	root->preorderChild(root);
	cout<<endl;
}
void Tree1::inorder()
{
	cout<<"中序遍历二叉树";
	root->inorderChild(root);
	cout<<endl;
}
void Tree1::postorder()
{
	cout<<"后序遍历二叉树";
	root->postorderChild(root);
	cout<<endl;
}
Tree1::Tree1(char *str)
{
	root=NULL;
	if(str!="")
	{
		cout<<"创建二叉树"<<endl;
		root=create(str);
		cout<<endl;
	}
}
///建立二叉树
TreeNode1*Tree1::create(char *str)
{
	TreeNode1 *p=NULL;
	static int i=0;
	cout<<"调用"<<str[i]<<endl;
	if(str[i]!='.')
	{
		p=new TreeNode1(str[i]);
		i++;
		p->left=create(str);
		p->right=create(str);
	}
	else
		i++;
	if(p==NULL)
		cout<<"返回NULL"<<endl;
	else
        cout<<"返回"<<p->data<<endl;
	if(str[i]=='\0')
		i=0;
	return p;
}
//撤消二叉树
Tree1::~Tree1()
{
	cout<<"撤消二叉树";
	destroy(root);
	root=NULL;
	cout<<endl;
}
void Tree1::destroy(TreeNode1 *p)
{
	if(p!=NULL)
	{
		destroy(p->left);
        destroy(p->right);
		cout<<p->data<<" ";
		delete p;
	}
}

⌨️ 快捷键说明

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