文章目录
#include<iostream>
#include<stack>
#include<queue>
#include<string.h>
using namespace std;
#define MaxSize 101
class BinTree
{
public:
BinTree();
void CreatTree(); //生成二叉树
void PreOrderTraverse2(int i=1); //先序非递归遍历
void InOrderTraverse2(int i=1); //中序非递归遍历
void PostOrderTraverse2(int i=1); //后序非递归遍历
void LevelOrder(); //层次遍历
void Visit(int i); //输出第i个位置的数据
int Depth(int i=1); //获得二叉树深度
int NodeCount(int i=1); //获得结点个数
int LeafCount(int i=1); //获得叶子结点个数
~BinTree();
private:
int* list; //结点存储的数据为整形数
int size; //记录结点的个数
};
BinTree::BinTree()
{
list = new int[MaxSize];
memset(list,0,MaxSize);
}
void BinTree::CreatTree() //生成二叉树
{
int n = 0;
cout << "请输入结点个数:";
cin >> n;
if(n<=0)
{
cout << "结点数不能小于1" << endl;
return;
}
while(n>MaxSize)
{
cout << "最大结点数为:" << MaxSize << endl;
cout << "请输入结点个数:";
cin >> n;
}
size = n;
cout << "请输入每个结点的数据:";
for(int i=0; i<n; ++i)
{
cin >> list[i];
}
}
void BinTree::PreOrderTraverse2(int i) //先序非递归遍历
{
if(!size)
{
return;
}
else
{
stack<int> s;
s.push(i); //根节点入栈
while(!s.empty())
{
i = s.top();
Visit(i);
s.pop();
if(list[2*i]!=0)
{
s.push(2*i+1); //先入右结点
}
if(list[2*i-1]!=0)
{
s.push(2*i); //再入左结点
}
}
}
}
void BinTree::InOrderTraverse2(int i) //中序非递归遍历
{
if(!size)
{
return;
}
else
{
int i = 1;
stack<int> s;
while(list[i-1]!=0||!s.empty())
{
if(list[i-1]!=0)
{
s.push(i);
i = 2*i;
}
else
{
Visit(list[s.top()-1]);
i = 2*s.top() + 1;
s.pop();
}
}
}
}
void BinTree::PostOrderTraverse2(int i) //后序非递归遍历
{
if(!size)
{
return;
}
else
{
stack<int> s;
int j = i;
s.push(i);
while(!s.empty())
{
i = s.top();
if(list[2*i-1]!=0&&2*i!=j&&2*i+1!=j)
{
s.push(2*i);
}
else
{
if(list[2*i-1]!=0&&2*i+1!=j)
{
s.push(2*i+1);
}
else
{
s.pop();
Visit(i);
j = i;
}
}
}
}
}
void BinTree::LevelOrder() //层次遍历
{
if(!size)
{
return;
}
else
{
queue<int> q;
int i = 1;
q.push(i);
while(!q.empty())
{
if(list[q.front()-1]!=0)
{
i = q.front();
q.push(2*i);
q.push(2*i+1);
Visit(i);
q.pop();
}
else
{
q.pop();
}
}
}
}
void BinTree::Visit(int i) //输出第i个位置的数据
{
if(list[i-1]!=0)
{
cout << list[i-1] << " ";
}
}
int BinTree::Depth(int i) //获得二叉树深度
{
if(!size)
{
return 0;
}
else
{
if(list[i-1]==0)
{
return 0;
}
else
{
int m = Depth(2*i);
int n = Depth(2*i+1);
return m>n?(m+1):(n+1);
}
}
}
int BinTree::NodeCount(int i) //获得结点个数
{
if(!size)
{
return 0;
}
else
{
if(list[i-1]==0)
{
return 0;
}
else
{
return NodeCount(2*i) + NodeCount(2*i+1) + 1;
}
}
}
int BinTree::LeafCount(int i) //获得叶子结点个数
{
if(!size)
{
return 0;
}
else
{
if(list[2*i-1]==0&&list[2*i]==0)
{
return 1;
}
else
{
if(list[2*i-1]!=0&&list[2*i]==0)
{
return LeafCount(2*i);
}
else if(list[2*i]!=0&&list[2*i-1]==0)
{
return LeafCount(2*i+1);
}
else
{
return LeafCount(2*i) + LeafCount(2*i+1);
}
}
}
}
BinTree::~BinTree()
{
delete[] list;
}
int main()
{
BinTree bt;
bt.CreatTree();
cout << "先序非递归遍历:";bt.PreOrderTraverse2();
cout << endl;
cout << "中序非递归遍历:";bt.InOrderTraverse2();
cout << endl;
cout << "后序非递归遍历:";bt.PostOrderTraverse2();
cout << endl;
cout << "层次遍历:";bt.LevelOrder();
cout << endl;
cout << "深度:" << bt.Depth();
cout << endl;
cout << "叶子结点数:" << bt.LeafCount();
cout << endl;
cout << "结点数:" << bt.NodeCount();
cout << endl;
}
测试结果: