#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <stack>
#define ERROR 0
#define OK 1
using namespace std;
typedef int ElemType;
typedef char Status;
typedef void Void;
typedef int Int;
typedef struct node
{
ElemType data;
struct node *right; //右子树
struct node *left; //左子树
} Binode, *BiTree;
BiTree CreatBitree(); //建立二叉树
Void preOder(BiTree T); //先序遍历
Void inOrder( BiTree T);//中序遍历
Void postorde( BiTree T); //后序遍历
Int Count(BiTree T); //计算结点数
Int Height(BiTree T); //求树的高度
Int Max(ElemType x,ElemType y); //判断左右子树哪个大
Void Insert(BiTree T); //非递归中序遍历
int main()
{
BiTree root;
printf("请输入各结点数\n");
root = CreatBitree();
printf("先序遍历\n");
preOder(root);
printf("\n中序遍历\n");
inOrder(root);
printf("\n后序遍历\n");
postorde(root);
printf("\n输出叶子结点数\n");
int index = Count(root);
printf("%d", index);
printf("\n输出二叉树的高度\n");
int height = Height(root);
printf("%d\n", height);
printf("\n输出中序遍历--非递归算法\n");
Insert(root);
return 0;
}
BiTree CreatBitree()
{
BiTree T;
Status ch;
scanf("%c", &ch);
if(ch == ' ')
{
T = NULL;
}
else
{
T = (BiTree)malloc(sizeof(Binode));
T->data = ch;
T->left = CreatBitree();
T->right = CreatBitree();
}
return T;
}
Void preOder(BiTree T)
{
if(T)
{
printf("%c ", T->data);
preOder(T->left);
preOder(T->right);
}
}
Void inOrder( BiTree T)
{
if(T)
{
inOrder(T->left);
printf("%c ", T->data);
inOrder(T->right);
}
}
Void postorde( BiTree T)
{
if(T)
{
postorde(T->left);
postorde(T->right);
printf("%c ", T->data);
}
}
Int Count(BiTree T)
{
if(!T)
return 0;
else if(T->left == NULL&&T->right == NULL)
return 1;
return Count(T->left) + Count(T->right);
}
//求树的高度
Int Height(BiTree T)
{
if(!T)
return 0;
else if(T->left == NULL && T->right == NULL)
return 1;
else
return Max(Height(T->left),Height(T->right));
}
Int Max(ElemType x,ElemType y)
{
return x > y ? x+1 : y+1;
}
//用非递归的方法遍历二叉树
Void Insert(BiTree T)
{
stack<BiTree>s; // 用到栈
BiTree p = T, cur; //遍历的指针变量
while(p) //树非空
{
cur = p;
while(cur->left) //p非空 栈为空
{
s.push(cur); //p入栈,p从左遍历
cur = cur->left;
}
if(cur)
printf("%c ", cur->data);
if(cur->right)
p = cur->right;
else
{
while(!s.empty()&&!cur->right)
{
cur = s.top(); // 将cur赋值为栈顶指针
s.pop(); //删除栈顶指针
printf("%c ", cur->data); //输出分支最左端的孩子
}
}
p = cur ->right;
}
}
#include <malloc.h>
#include <stdlib.h>
#include <stack>
#define ERROR 0
#define OK 1
using namespace std;
typedef int ElemType;
typedef char Status;
typedef void Void;
typedef int Int;
typedef struct node
{
ElemType data;
struct node *right; //右子树
struct node *left; //左子树
} Binode, *BiTree;
BiTree CreatBitree(); //建立二叉树
Void preOder(BiTree T); //先序遍历
Void inOrder( BiTree T);//中序遍历
Void postorde( BiTree T); //后序遍历
Int Count(BiTree T); //计算结点数
Int Height(BiTree T); //求树的高度
Int Max(ElemType x,ElemType y); //判断左右子树哪个大
Void Insert(BiTree T); //非递归中序遍历
int main()
{
BiTree root;
printf("请输入各结点数\n");
root = CreatBitree();
printf("先序遍历\n");
preOder(root);
printf("\n中序遍历\n");
inOrder(root);
printf("\n后序遍历\n");
postorde(root);
printf("\n输出叶子结点数\n");
int index = Count(root);
printf("%d", index);
printf("\n输出二叉树的高度\n");
int height = Height(root);
printf("%d\n", height);
printf("\n输出中序遍历--非递归算法\n");
Insert(root);
return 0;
}
BiTree CreatBitree()
{
BiTree T;
Status ch;
scanf("%c", &ch);
if(ch == ' ')
{
T = NULL;
}
else
{
T = (BiTree)malloc(sizeof(Binode));
T->data = ch;
T->left = CreatBitree();
T->right = CreatBitree();
}
return T;
}
Void preOder(BiTree T)
{
if(T)
{
printf("%c ", T->data);
preOder(T->left);
preOder(T->right);
}
}
Void inOrder( BiTree T)
{
if(T)
{
inOrder(T->left);
printf("%c ", T->data);
inOrder(T->right);
}
}
Void postorde( BiTree T)
{
if(T)
{
postorde(T->left);
postorde(T->right);
printf("%c ", T->data);
}
}
Int Count(BiTree T)
{
if(!T)
return 0;
else if(T->left == NULL&&T->right == NULL)
return 1;
return Count(T->left) + Count(T->right);
}
//求树的高度
Int Height(BiTree T)
{
if(!T)
return 0;
else if(T->left == NULL && T->right == NULL)
return 1;
else
return Max(Height(T->left),Height(T->right));
}
Int Max(ElemType x,ElemType y)
{
return x > y ? x+1 : y+1;
}
//用非递归的方法遍历二叉树
Void Insert(BiTree T)
{
stack<BiTree>s; // 用到栈
BiTree p = T, cur; //遍历的指针变量
while(p) //树非空
{
cur = p;
while(cur->left) //p非空 栈为空
{
s.push(cur); //p入栈,p从左遍历
cur = cur->left;
}
if(cur)
printf("%c ", cur->data);
if(cur->right)
p = cur->right;
else
{
while(!s.empty()&&!cur->right)
{
cur = s.top(); // 将cur赋值为栈顶指针
s.pop(); //删除栈顶指针
printf("%c ", cur->data); //输出分支最左端的孩子
}
}
p = cur ->right;
}
}