#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<vector>
#define OK 1
#define MaxSize 1001
#include<stack>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode* lchild, * rchild;
}BiTNode, * BiTtree;
//按先序次序输入二叉树中结点的值,生成二叉树T
void CreatBiTree(BiTtree& T)
{
char ch;
cin >> ch;
if (ch == '#')
T = NULL;
else
{
T = new BiTNode;
T->data = ch;
cout << "请输入" << ch << "的左孩子:";
CreatBiTree(T->lchild);
cout << "请输入" << ch << "的右孩子:";
CreatBiTree(T->rchild);
}
}
//先序遍历
int PreOrderTraverse(BiTtree T)
{
if (T == NULL)
return OK; //空二叉树
else
{
cout << T->data << " "; //访问跟结点
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}
//中序遍历
int InOrderTraverse(BiTtree T)
{
if (T == NULL)
return OK;
else
{
InOrderTraverse(T->lchild);
cout << T->data << " ";
InOrderTraverse(T->rchild);
}
}
//后序遍历
int PostOrderTraverse(BiTtree T)
{
if (T == NULL)
return OK;
else {
PostOrderTraverse(T->lchild);
PostOrderTraverse(T->rchild);
cout << T->data << " ";
}
}
//层序遍历
void level(BiTtree T)
{
int front, rear;
front = rear = 0;
BiTtree que[100];
BiTtree p;
if (T)
{
rear = (rear + 1) % 100;
que[rear] = T;
while (front != rear)
{
front = (front + 1) % 100;
p = que[front];
cout << p->data;
if (p->lchild)
{
rear = (rear + 1) % 100;
que[rear] = p->lchild;
}
if (p->rchild)
{
rear = (rear + 1) % 100;
que[rear] = p->rchild;
}
}
}
}
//深度
int getdepth(BiTtree T)
{
int l, r;
if (!T)
{
return;
}
l = getdepth(T->lchild);
r = getdepth(T->rchild);
if (l > r)
{
return l + 1;
}
else
return r + 1;
}
//叶子结点数
void CountLeaf(BiTtree root, int& count)//采用先序遍历的递归算法
{
if (root != NULL) //非空二叉树条件
{
if (!root->lchild && !root->rchild)//是叶子结点则统计并打印
{
count++;
//cout<<root->data<<endl;
}
if (root->lchild)
CountLeaf(root->lchild, count); //递归遍历左子树;
if (root->rchild)
CountLeaf(root->rchild, count); //递归遍历右子树;
}
}
// 非递归前序遍历
void PreOrder1(BiTNode* root)
{
stack<BiTNode*>s; //生成一个栈
if (root != NULL) //如果传的root节点不为空,则将root入栈
{
s.push(root);
}
while (!s.empty()) //只要栈里还有节点
{
BiTNode* top = s.top(); //保存一下栈顶节点
s.pop(); //栈顶节点退栈
cout << top->data << " "; //打印出刚才的数据
if (top->rchild) //右孩子存在就入栈
{
s.push(top->rchild);
}
if (top->lchild) //左孩子存在就入栈
{
s.push(top->lchild);
}
}
cout << endl;
}
//非递归中序
vector<int> PreOrder2(BiTNode* root)
{
vector<int> v;//存储访问出元素
stack<BiTNode*> st;//栈——临时储存节点
BiTNode* cur = root;//用于游历整棵树的指针
while (!st.empty() || cur)
{
//遍历当前树的所有最左侧节点
while (cur)
{
st.push(cur);
cur = cur->lchild;
}
//取出栈顶节点
BiTNode* tmp = st.top();
st.pop();
v.push_back(cur->data);
//前序遍历右树
cur = tmp->rchild;//更新root为栈顶节点的右树
}
return v;
}
//非递归后续
vector<int> PreOrder3(BiTNode* root)
{
vector<int> v;//存储访问出元素
stack<BiTNode*> st;//栈——临时储存节点
BiTNode* cur = root;//用于游历整棵树的指针
BiTNode* prev = nullptr;
while (!st.empty() || cur)
{
//遍历当前树的所有最左侧节点
while (cur)
{
st.push(cur);
cur = cur->lchild;
}
BiTNode* tmp = st.top();
if (tmp->rchild == prev || tmp->rchild == nullptr)
{
st.pop();
v.push_back(tmp->data);
prev = tmp;
cur = nullptr;//下一次循环直接当空树访问下一个栈中节点
}
else
{
cur = tmp->rchild;//更新root为栈顶节点的右树
}
}
return v;
}
void show_help()
{
cout << "|------------------------------------------" << endl;
cout << "| 0.创建二叉树 " << endl;
cout << "| 1.先序遍历二叉树 " << endl;
cout << "| 2.中序遍历二叉树 " << endl;
cout << "| 3.后序遍历二叉树 " << endl;
cout << "| 4.二叉树的层序遍历 " << endl;
cout << "| 5.二叉树的深度 " << endl;
cout << "| 6.二叉树的叶子结点数 " << endl;
cout << "| 7.二叉树的前序遍历(非递归) " << endl;
cout << "| 8.二叉树的中序遍历(非递归) " << endl;
cout << "| 9.二叉树的后序遍历(非递归) " << endl;
cout << "|------------------------------------------" << endl;
}
int main()
{
BiTtree root;
int operate_code;
show_help();
cout << "请建立二叉树并输入二叉树的根结点" << endl;
CreatBiTree(root);
while (1)
{
cout << "请选择功能 " << endl;
cin >> operate_code;
int count = 0;
if (operate_code == 1)
{
if (root)
{
cout << "先序输出的结果为:";
PreOrderTraverse(root);
cout << endl;
}
else
cout << "该二叉树为空" << endl;
}
else if (operate_code == 2)
{
if (root)
{
cout << "中序输出的结果为:";
InOrderTraverse(root);
cout << endl;
}
else
cout << "该二叉树为空" << endl;
}
else if (operate_code == 3)
{
if (root)
{
cout << "后序输出的结果为:";
PostOrderTraverse(root);
cout << endl;
}
else
cout << "该二叉树为空" << endl;
}
else if (operate_code == 4)
{
cout << "二叉树的层序遍历是";
level(root);
cout << endl;
}
else if (operate_code == 5)
{
cout << "二叉树的深度是" << getdepth(root) << endl;
}
else if (operate_code == 6)
{
CountLeaf(root, count);
cout << "二叉树的叶子节点个数为" << count << endl;
}
else if (operate_code == 7) {
PreOrder1(root);
cout <<"非递归前序遍历是" << endl;
}
else if (operate_code == 8) {
cout << "非递归中序遍历是" << endl;
}
else if(operate_code == 9) {
PreOrder3(root);
cout << "非递归后序遍历是" << endl;
}
}
}