一、结果显示
二、BinaryTree.h
#include <iostream>
using namespace std;
class BinaryTree
{
public:
struct BinaryNode
{
int data;
BinaryNode *leftChild;
BinaryNode *rightChild;
};
bool FindData(BinaryNode *&Tree,int data) //查找是否存在某元素
{
//cin>>data;
if(Tree == NULL)
return false;
else if(data < Tree->data)
FindData(Tree->leftChild,data);
else if(data > Tree->data)
FindData(Tree->rightChild,data);
else if(data = Tree->data)
return true;
else
return false;
}
void Delete(BinaryNode *&Tree) //删除整棵树
{
if(Tree)
{
Delete(Tree->leftChild);
Delete(Tree->rightChild);
delete Tree;
Tree = NULL;
}
}
void isEmpty(BinaryNode *&Tree) //判断是否为空树
{
if(Tree == NULL)
cout<<"树为空!!!"<<endl;
else
cout<<"树不为空!!!"<<endl;
}
BinaryNode* CreateTree() //创建树
{
int data;
cin>>data;
BinaryNode *Tree;
if(data == -1)
return NULL;
else
{
Tree = new BinaryNode;
Tree->data = data;
Tree->leftChild = CreateTree();
Tree->rightChild = CreateTree();
}
return Tree;
}
void PreTravel(BinaryNode *Tree) //前序遍历树
{
if(Tree)
{
cout<<Tree->data<<" ";
PreTravel(Tree->leftChild);
PreTravel(Tree->rightChild);
}
}
void PostTravel(BinaryNode *Tree) //后序遍历树
{
if(Tree)
{
PostTravel(Tree->leftChild);
PostTravel(Tree->rightChild);
cout<<Tree->data<<" ";
}
}
void InorderTravel(BinaryNode *Tree) //中序遍历树
{
if(Tree)
{
InorderTravel(Tree->leftChild);
cout<<Tree->data<<" ";
InorderTravel(Tree->rightChild);
}
}
void Insert(BinaryNode *&Tree,int data) //在树中插入某元素
{
if(Tree == NULL)
{
Tree = new BinaryNode;
Tree->data = data;
Tree->leftChild = NULL;
Tree->rightChild = NULL;
}
else if(data <= Tree->data)
Insert(Tree->leftChild,data);
else if(data > Tree->data)
Insert(Tree->rightChild,data);
}
int FindMin(BinaryNode *&Tree) //找到树中最小的元素值
{
if(Tree == NULL)
return -1;
else if(Tree->leftChild == NULL)
return Tree->data;
else
return FindMin(Tree->leftChild);
}
int FindMax(BinaryNode *&Tree) //找到树中最大的元素值
{
if(Tree == NULL)
return -1;
else if(Tree->rightChild == NULL)
return Tree->data;
else
return FindMax(Tree->rightChild);
}
void Remove(BinaryNode *&Tree,int data) //移除树中某元素
{
if(NULL == Tree)
return;
else if(data < Tree->data)
Remove(Tree->leftChild,data);
else if(data > Tree->data)
Remove(Tree->rightChild,data);
else if(Tree->leftChild && Tree->rightChild)
{
Tree->data = FindMin(Tree->rightChild);
Remove(Tree->rightChild,data);
}
else
{
BinaryNode *&node = Tree;
Tree = Tree->leftChild?Tree->leftChild:Tree->rightChild;
delete node;
node = NULL;
}
}
BinaryNode *Tree;
};
三、main()
#include "BinaryTree.h"
int main()
{
BinaryTree::BinaryNode* tree;
BinaryTree Tree;
cout<<"请输入所创建树的数据:";
tree = Tree.CreateTree();
cout<<"前序遍历:";
Tree.PreTravel(tree);
cout<<endl;
cout<<"后序遍历:";
Tree.PostTravel(tree);
cout<<endl;
cout<<"中序遍历:";
Tree.InorderTravel(tree);
cout<<endl;
cout<<"插入9:";
Tree.Insert(tree,9);
cout<<endl;
cout<<"中序遍历:";
Tree.InorderTravel(tree);
cout<<endl;
cout<<"是否存在数字1:";
if(Tree.FindData(tree,1))
cout<<"存在!!"<<endl;
else
cout<<"不存在!!"<<endl;
cout<<"找到最小的数字:"<<Tree.FindMin(tree)<<endl;
cout<<"找到最大的数字:"<<Tree.FindMax(tree)<<endl;
cout<<"移除数字1:";
Tree.Remove(tree,1);
cout<<endl;
cout<<"中序遍历:";
Tree.InorderTravel(tree);
cout<<endl;
cout<<"删除整个树!!"<<endl;
Tree.Delete(tree);
Tree.isEmpty(tree);
return 0;
}