二叉树的遍历及应用
来源:互联网 发布:淘宝网商城炉具大全 编辑:程序博客网 时间:2024/06/11 03:34
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define MAX 100
int count=0;
typedef struct tree
{
char ch;
struct tree *Lchild,*Rchild;
}*TREE;
typedef struct Seqstack
{
TREE p[MAX];
int top;
}Seqstack;
void create(TREE *root);
void print1(TREE root);
void print2(TREE root);
void print3(TREE root);
void printtree(TREE root,int h);
int tall(TREE root);
void cal(TREE root);
Seqstack *Init();
int Empty(Seqstack *s);
int Push(Seqstack *s,TREE root);
TREE Pop(Seqstack *s);
void print4(TREE root);
void print5(TREE root);
void print6(TREE root);
void adjust(char ch,TREE root);
int main()
{
TREE root=NULL;
int x;
char ch;
create(&root);
printf("递归先序遍历:");
print1(root);
printf("\n递归中序遍历:");
print2(root);
printf("\n递归后序遍历:");
print3(root);
printf("\n非递归先序遍历:");
print4(root);
printf("\n非递归中序遍历:");
print5(root);
printf("\n层次遍历:");
print6(root);
printf("\n");
printf("请输入要查找的节点:");
fflush(stdin);
scanf("%c",&ch);
printf("双亲节点:");
adjust(ch,root);
printf("\n叶子节点:");
cal(root);
printf("\n叶子节点数:");
printf("%d\n",count);
x=tall(root);
printf("树的高度:");
printf("%d\n",x);
printtree(root,x);
return 0;
}
void print4(TREE root)
{
Seqstack *s;
TREE p;
s=Init();
p=root;
while(p!=NULL||!Empty(s))
{
while(p!=NULL)
{
printf("%c",p->ch);
Push(s,p);
p=p->Lchild;
}
if(!Empty(s))
{
p=Pop(s);
p=p->Rchild;
}
}
}
void print6(TREE root)
{
Seqstack *s;
TREE p;
s=Init();
p=root;
Push(s,p);
while(!Empty(s))
{
if(!Empty(s))
{
p=Pop(s);
if(p->Rchild!=NULL)
Push(s,p->Rchild);
if(p->Lchild!=NULL)
Push(s,p->Lchild);
}
printf("%c",p->ch);
}
}
void print5(TREE root)
{
Seqstack *s;
TREE p;
s=Init();
p=root;
while(p!=NULL||!Empty(s))
{
while(p!=NULL)
{
Push(s,p);
p=p->Lchild;
}
if(!Empty(s))
{
p=Pop(s);
printf("%c",p->ch);
p=p->Rchild;
}
}
}
Seqstack *Init()
{
Seqstack *s;
s=(Seqstack *)malloc(MAX*sizeof(Seqstack));
s->top=-1;
return s;
}
int Empty(Seqstack *s)
{
if(s->top==-1) return 1;
return 0;
}
int Push(Seqstack *s,TREE root)
{
if(s->top==MAX-1) return 0;
else
{
s->top++;
s->p[s->top]=root;
return 1;
}
}
TREE Pop(Seqstack *s)
{
TREE p;
if(Empty(s)) return 0;
else
{
//printf("%d ",s->p[s->top]->Rchild);
p=s->p[s->top];
s->top--;
return p;
}
}
void create(TREE *root)
{
char ch;
ch=getchar();
if(ch==' ') *root=NULL;
else
{
*root=(TREE)malloc(sizeof(TREE));
(*root)->ch=ch;
create(&((*root)->Lchild));
create(&((*root)->Rchild));
}
}
void print1(TREE root)
{
if(root)
{
printf("%c",root->ch);
print1(root->Lchild);
print1(root->Rchild);
}
}
void print2(TREE root)
{
if(root)
{
print2(root->Lchild);
printf("%c",root->ch);
print2(root->Rchild);
}
}
void print3(TREE root)
{
if(root)
{
print3(root->Lchild);
print3(root->Rchild);
printf("%c ",root->ch);
}
}
void printtree(TREE root,int h)
{
int i;
if(root==NULL) return;
printtree(root->Rchild,h+1);
for(i=0;i<h;i++) printf(" ");
printf("%c\n",root->ch);
printtree(root->Lchild,h+1);
}
int tall(TREE root)
{
int x=0,y=0;
if(root==NULL) return 0;
else
{
x=tall(root->Rchild);
y=tall(root->Lchild);
if(x>y) return x+1;
else return y+1;
}
}
void adjust(char ch,TREE root)
{
if(root)
{
if(root->Lchild!=NULL)
{
if(root->Lchild->ch==ch)
printf("%c",root->ch);
}
if(root->Rchild!=NULL)
{
if(root->Rchild->ch==ch)
printf("%c",root->ch);
}
adjust(ch,root->Lchild);
adjust(ch,root->Rchild);
}
}
void cal(TREE root)
{
int i,j;
if(root)
{
if(root->Lchild==NULL&&root->Rchild==NULL)
{
count++;
printf("%c",root->ch);
}
cal(root->Lchild);
cal(root->Rchild);
}
}
#include <malloc.h>
#include <string.h>
#define MAX 100
int count=0;
typedef struct tree
{
char ch;
struct tree *Lchild,*Rchild;
}*TREE;
typedef struct Seqstack
{
TREE p[MAX];
int top;
}Seqstack;
void create(TREE *root);
void print1(TREE root);
void print2(TREE root);
void print3(TREE root);
void printtree(TREE root,int h);
int tall(TREE root);
void cal(TREE root);
Seqstack *Init();
int Empty(Seqstack *s);
int Push(Seqstack *s,TREE root);
TREE Pop(Seqstack *s);
void print4(TREE root);
void print5(TREE root);
void print6(TREE root);
void adjust(char ch,TREE root);
int main()
{
TREE root=NULL;
int x;
char ch;
create(&root);
printf("递归先序遍历:");
print1(root);
printf("\n递归中序遍历:");
print2(root);
printf("\n递归后序遍历:");
print3(root);
printf("\n非递归先序遍历:");
print4(root);
printf("\n非递归中序遍历:");
print5(root);
printf("\n层次遍历:");
print6(root);
printf("\n");
printf("请输入要查找的节点:");
fflush(stdin);
scanf("%c",&ch);
printf("双亲节点:");
adjust(ch,root);
printf("\n叶子节点:");
cal(root);
printf("\n叶子节点数:");
printf("%d\n",count);
x=tall(root);
printf("树的高度:");
printf("%d\n",x);
printtree(root,x);
return 0;
}
void print4(TREE root)
{
Seqstack *s;
TREE p;
s=Init();
p=root;
while(p!=NULL||!Empty(s))
{
while(p!=NULL)
{
printf("%c",p->ch);
Push(s,p);
p=p->Lchild;
}
if(!Empty(s))
{
p=Pop(s);
p=p->Rchild;
}
}
}
void print6(TREE root)
{
Seqstack *s;
TREE p;
s=Init();
p=root;
Push(s,p);
while(!Empty(s))
{
if(!Empty(s))
{
p=Pop(s);
if(p->Rchild!=NULL)
Push(s,p->Rchild);
if(p->Lchild!=NULL)
Push(s,p->Lchild);
}
printf("%c",p->ch);
}
}
void print5(TREE root)
{
Seqstack *s;
TREE p;
s=Init();
p=root;
while(p!=NULL||!Empty(s))
{
while(p!=NULL)
{
Push(s,p);
p=p->Lchild;
}
if(!Empty(s))
{
p=Pop(s);
printf("%c",p->ch);
p=p->Rchild;
}
}
}
Seqstack *Init()
{
Seqstack *s;
s=(Seqstack *)malloc(MAX*sizeof(Seqstack));
s->top=-1;
return s;
}
int Empty(Seqstack *s)
{
if(s->top==-1) return 1;
return 0;
}
int Push(Seqstack *s,TREE root)
{
if(s->top==MAX-1) return 0;
else
{
s->top++;
s->p[s->top]=root;
return 1;
}
}
TREE Pop(Seqstack *s)
{
TREE p;
if(Empty(s)) return 0;
else
{
//printf("%d ",s->p[s->top]->Rchild);
p=s->p[s->top];
s->top--;
return p;
}
}
void create(TREE *root)
{
char ch;
ch=getchar();
if(ch==' ') *root=NULL;
else
{
*root=(TREE)malloc(sizeof(TREE));
(*root)->ch=ch;
create(&((*root)->Lchild));
create(&((*root)->Rchild));
}
}
void print1(TREE root)
{
if(root)
{
printf("%c",root->ch);
print1(root->Lchild);
print1(root->Rchild);
}
}
void print2(TREE root)
{
if(root)
{
print2(root->Lchild);
printf("%c",root->ch);
print2(root->Rchild);
}
}
void print3(TREE root)
{
if(root)
{
print3(root->Lchild);
print3(root->Rchild);
printf("%c ",root->ch);
}
}
void printtree(TREE root,int h)
{
int i;
if(root==NULL) return;
printtree(root->Rchild,h+1);
for(i=0;i<h;i++) printf(" ");
printf("%c\n",root->ch);
printtree(root->Lchild,h+1);
}
int tall(TREE root)
{
int x=0,y=0;
if(root==NULL) return 0;
else
{
x=tall(root->Rchild);
y=tall(root->Lchild);
if(x>y) return x+1;
else return y+1;
}
}
void adjust(char ch,TREE root)
{
if(root)
{
if(root->Lchild!=NULL)
{
if(root->Lchild->ch==ch)
printf("%c",root->ch);
}
if(root->Rchild!=NULL)
{
if(root->Rchild->ch==ch)
printf("%c",root->ch);
}
adjust(ch,root->Lchild);
adjust(ch,root->Rchild);
}
}
void cal(TREE root)
{
int i,j;
if(root)
{
if(root->Lchild==NULL&&root->Rchild==NULL)
{
count++;
printf("%c",root->ch);
}
cal(root->Lchild);
cal(root->Rchild);
}
}
0 0
- 二叉树的遍历及应用
- 二叉树的遍历应用
- 二叉树遍历的应用
- 遍历二叉树的应用
- 二叉树遍历的应用
- 二叉树遍历及简单应用
- 二叉树及其应用-二叉树的遍历及哈夫曼树-数据结构上机实验
- 二叉树的应用—二叉树遍历的应用
- 遍历二叉树应用
- 二叉树的遍历的应用
- 二叉树遍历算法的应用
- 线索二叉树的遍历应用
- 二叉树的遍历及其应用
- 二叉树遍历算法的应用
- 二叉树遍历算法的应用
- 【笔记】遍历二叉树的应用
- 树的遍历及应用
- 二叉树及二叉树的应用
- C#4.0初探:dynamic 关键字
- 欧拉计划003:最大质因数
- web.xml is missing and <failOnMissingWebXml> is set to true解决方法
- keil mdk 编译弹窗错误:browse information of one or more files is not available
- ZCMU—1668
- 二叉树的遍历及应用
- Linux下编写 makefile 详细教程
- QualComm Android boot recovery vertify
- 欢迎使用CSDN-markdown编辑器
- 【Centos7】安装nginx
- mongDB使用小技巧
- poj_1523 SPF(求割顶)
- STL容器解析之Vector
- java导出pdf文件工具类