二叉树问题
来源:互联网 发布:血清甘油三酯增高 知乎 编辑:程序博客网 时间:2024/05/14 06:03
#include<iostream.h>
#include<malloc.h>
#define maxnode 1000
typedef char elemtype;
typedef struct bintnode
{
elemtype data;
struct bintnode *lchild,*rchild,*parent;
}bintnode,*bintree;
void createbintree(bintree *t) //构建一个二叉树链表
{
char ch;
cin>>ch;
if(ch=='0') *t='/0';
else
{
*t=(bintnode*)malloc(sizeof(bintnode));
(*t)->data=ch;
createbintree(&(*t)->lchild);
createbintree(&(*t)->rchild);
}
}
void preorderout(bintree t) //先序序列的输出
{
if(t)
{
cout<<t->data;
preorderout(t->lchild);
preorderout(t->rchild);
}
}
void inorderout(bintree t) //中序序列的输出
{
if(t)
{
inorderout(t->lchild);
cout<<t->data;
inorderout(t->rchild);
}
}
void postorderout(bintree t) //后序序列的输出
{
if(t)
{
postorderout(t->lchild);
postorderout(t->rchild);
cout<<t->data;
}
}
void leveorder(bintree t) //层次遍历的输出
{
bintree queue[maxnode];
int front,rear;
if(t=='/0') return;
front=-1;
rear=0;
queue[rear]=t;
while(front!=rear)
{
front++;
cout<<queue[front]->data;
if(queue[front]->lchild!='/0')
{
rear++;
queue[rear]=queue[front]->lchild;
}
if(queue[front]->rchild !='/0')
{
rear++;
queue[rear]=queue[front]->rchild ;
}
}
}
//主函数
void main()
{
bintree bt;
cout<<"输入二叉树:";
createbintree(&bt);
cout<<"输出先序遍历:";
preorderout(bt);
cout<<endl;
cout<<"输出中序遍历:";
inorderout(bt);
cout<<endl;
cout<<"输出后序遍历:";
postorderout(bt);
cout<<endl;
cout<<"层次遍历:";
leveorder(bt);
cout<<endl;
}
- 二叉树遍历问题
- 数据结构-二叉树 问题
- 二叉树遍历问题
- 二叉树问题
- 二叉树的问题
- 二叉树问题
- 二叉树相关问题
- 二叉树相关问题
- 二叉树问题
- 二叉树路径问题
- 二叉树问题
- 关于二叉树问题
- 二叉树递归问题
- 二叉树相关问题
- 二叉树相关问题
- 二叉树深度问题
- 二叉树相关问题
- 二叉树问题1
- 终于到了
- 习题总结 5.29 night
- 如何在photoshop里创作一个web 2.0风格的页面
- DoD模型中各层的协议
- 在main的前后执行程序
- 二叉树问题
- 开源工作的参与是不是可以从测试开始?
- “晨曦计划”我的第一讲
- 个人主页
- 这地方不错~
- 跟我快乐学C++系列:(4)理解函数的调用
- JefferyZhao教导我们...
- 为啥这没个动态语言的讨论区,比如Python
- 一道简单的关于override的测试题