急需一个二叉树的层次遍历算法

xiexiemenghun 2008-07-07 10:41:24
急需一个二叉树的层次遍历算法
...全文
6305 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
hnd201031000407 2011-11-01
  • 打赏
  • 举报
回复
这是我自己写的代码(用队列麻烦啊,直接用个结构数组方便多了):
T为已有的二叉树,n为二叉树的结点数;
int CreatLevel(BiTree T,int n){ //按层次遍历二叉树
BiTree queue[100],p; int i,a=0,k;
queue[0]=T;
for(i=1;i<n;){
k=i-1;
for(;a<=k;a++){
p=queue[a];
if(p->lchild){queue[i]=p->lchild; i++;}
if(p->rchild){queue[i]=p->rchild; i++;}
}
}
for(i=0;i<n;i++){
printf("%d ",queue[i]->data);
}
return 0;
}
RJGC_Bullet 2010-11-21
  • 打赏
  • 举报
回复
代码还是自己写写吧..现在要找工作,必须会写链表.
yunshouhu 2010-11-20
  • 打赏
  • 举报
回复
好 代码 先用来做作业先!
yuanlong_zheng 2009-05-12
  • 打赏
  • 举报
回复
很好,有帮助,相当不错的代码,我也喜欢
hnsmtql 2009-04-28
  • 打赏
  • 举报
回复
很好,有帮助,是个不错的代码,我喜欢
zhangbin55661 2008-08-27
  • 打赏
  • 举报
回复
#include"stdio.h"
#include"string.h"
#define Max 20 /*结点的最大个数*/
typedef struct node{
char data;
struct node *lchild,*rchild;
}BinTNode; /*自定义二叉树的结点类型*/
typedef BinTNode *BinTree; /*定义二叉树的指针*/
int NodeNum,leaf; /*NodeNum为结点数,leaf为叶子数*/
/*==========基于先序遍历算法创建二叉树============== */
/*=====要求输入先序序列,其中加入虚结点"#"以示空指针的位置========== */
BinTree CreatBinTree(void)
{
BinTree T;
char ch;
if((ch=getchar())=='#')
return(NULL); /*读入#,返回空指针 */
else{
T=(BinTNode *)malloc(sizeof(BinTNode)); /*生成结点*/
T->data=ch;
T->lchild=CreatBinTree(); /*构造左子树*/
T->rchild=CreatBinTree(); /*构造右子树*/
return(T);
}
}
/*========NLR 先序遍历============= */
void Preorder(BinTree T)
{
if(T) {
printf("%c",T->data); /*访问结点*/
Preorder(T->lchild); /*先序遍历左子树*/
Preorder(T->rchild); /*先序遍历右子树*/
}
}

/*========LNR 中序遍历===============*/
/*==============中序遍历================== */
void Inorder(BinTree T)
{
if(T) {
Inorder(T->lchild); /*中序遍历左子树*/
printf("%c",T->data); /*访问结点*/
Inorder(T->rchild); /*中序遍历右子树*/
}
}

/*==========LRN 后序遍历============ */
/*==============后序遍历===================*/
void Postorder(BinTree T)
{
if(T) {

Postorder(T->lchild); /*后序遍历左子树*/
Postorder(T->rchild); /*后序遍历右子树*/
printf("%c",T->data); /*访问结点*/
}
}

/*=====采用后序遍历求二叉树的深度、结点数及叶子数的递归算法======== */
int TreeDepth(BinTree T)
{
int hl,hr,max;
if(T){
hl=TreeDepth(T->lchild); /*求左深度*/
hr=TreeDepth(T->rchild); /*求右深度*/
max=hl>hr? hl:hr; /*取左右深度的最大值*/
NodeNum=NodeNum+1; /*求结点数*/
if(hl==0&&hr==0) leaf=leaf+1; /*若左右深度为0,即为叶子。*/
return(max+1);
}
else return(0);
}
/*====利用"先进先出"(FIFO)队列,按层次遍历二叉树==========*/
void Levelorder(BinTree T)
{
int front=0,rear=1;
BinTNode *cq[Max],*p; /*定义结点的指针数组cq */
cq[1]=T; /*根入队*/
while(front!=rear)
{
front=(front+1)%NodeNum;
p=cq[front]; /*出队 */
printf("%c",p->data); /*出队,输出结点的值 */
if(p->lchild!=NULL){
rear=(rear+1)%NodeNum;
cq[rear]=p->lchild; /*左子树入队 ( 指针替换)*/
}
if(p->rchild!=NULL){
rear=(rear+1)%NodeNum;
cq[rear]=p->rchild; /*右子树入队*/
}
}
}
/*==========主函数=================*/
void main()
{
BinTree root;
int i,depth;
printf("\n");
printf("Creat Bin_Tree; Input preorder:"); /*输入完全二叉树的先序序列,*/
/* 用#代表虚结点,如ABD###CE##F## */
root=CreatBinTree(); /*创建二叉树,返回根结点*/
do { /*从菜单中选择遍历方式,输入序号。*/
printf("\t********** select ************\n");
printf("\t1: Preorder Traversal\n");
printf("\t2: Iorder Traversal\n");
printf("\t3: Postorder traversal\n");
printf("\t4: PostTreeDepth,Node number,Leaf number\n");
printf("\t5: Level Depth\n"); /*按层次遍历之前,先选择4,求出该树的结点数*/
printf("\t0: Exit\n");
printf("\t*******************************\n");
scanf("%d",&i); /*输入菜单序号(0-5) */
switch (i){
case 1: printf("Print Bin_tree Preorder: ");
Preorder(root); /*先序遍历 */
break;
case 2: printf("Print Bin_Tree Inorder: ");
Inorder(root); /*中序遍历*/
break;
case 3: printf("Print Bin_Tree Postorder: ");
Postorder(root); /*后序遍历*/
break;
case 4: depth=TreeDepth(root); /*求树的深度及叶子数*/
printf("BinTree Depth=%d BinTree Node number=%d",depth,NodeNum);
printf(" BinTree Leaf number=%d",leaf);
break;
case 5: printf("LevePrint Bin_Tree: ");
Levelorder(root); /*按层次遍历 */
break;
default: exit(1);
}
printf("\n");
} while(i!=0);
}
birdas 2008-08-27
  • 打赏
  • 举报
回复
不错的东西
qkhhxkj102 2008-08-27
  • 打赏
  • 举报
回复
收藏
dghh159 2008-08-27
  • 打赏
  • 举报
回复
很好,马克。
baiyizhujian 2008-08-07
  • 打赏
  • 举报
回复

void Levelorder(BinTree T)
{/*按层次遍历二叉树T*/
int i,j;
BinTNode *q[20],*p; /*q[20]用于模拟队列,存储入队的结点*/
p=T;
if(p!=NULL)
{
i=1;q[i]=p;j=2;
} /*i为队首位置,j为队尾位置*/
while(i!=j)
{
p=q[i];
printf("%3c",p->data); /*访问队首元素*/
if (p->lchild!=NULL)
{
q[j]=p->lchild;
j++;
}
/*若队首元素左链域不为空,则将其入队列*/
if (p->rchild!=NULL)
{
q[j]=p->rchild;
j++;
}
/*若队首元素右链域不为空,则将其入队列*/
i++; /*将队首移到下一个位置*/
}
}
sdjinshiyu 2008-07-30
  • 打赏
  • 举报
回复
温习一下……
  • 打赏
  • 举报
回复
up iu_81
ack2me 2008-07-30
  • 打赏
  • 举报
回复
算法的话就是 将树的根节点进队列,在出队列的时候,再将这个根节点的左右子树的根节点进队列,这样就可以做到按层次遍历了。
讲白了就是借助队列这个数据结构。
wangdeqie 2008-07-30
  • 打赏
  • 举报
回复

//二叉树层次遍历
<template<class T>
void LevelOrder(BinaryTreeNode<T> *t)
{
LinkedQueue<BinaryTreeNode<T>* > Q;
while(t)
{
Visit(t);
if(t->LeftChild)
Q.Add(t->LeftChild);
if(t->RightChild)
Q.Add(t->RightChild);
try{Q.Delete(t);}
catch(OutOfBounds){return;}
}
}
aaajj 2008-07-30
  • 打赏
  • 举报
回复
用队列
小明的程序 2008-07-29
  • 打赏
  • 举报
回复
编写按层次(同一层从左至右)遍历二叉树的算
法。
void LayerOrder(Bitree T)//层序遍历二叉树
{  InitQueue(Q);
   EnQueue(Q,T);
  while(!QueueEmpty(Q)) {
    DeQueue(Q,p);
 visit(p);
    if(p->lchild) EnQueue(Q,p->lchild);
 if(p->rchild) EnQueue(Q,p->rchild);
 }
}
ChanDragon 2008-07-08
  • 打赏
  • 举报
回复
我也温故一下。。。
xiexiemenghun 2008-07-08
  • 打赏
  • 举报
回复
xiexieni
iu_81 2008-07-07
  • 打赏
  • 举报
回复
//二叉树层次遍历算法
#include <stdio.h>
#include <alloc.h>
#define MaxSize 1000
typedef char ElemType;
typedef struct node
{
ElemType data;
struct node *lchild;
struct node *rchild;
} BTNode;
//创建二叉树
void CreateBTNode(BTNode *&b,char *str)
{
BTNode *St[MaxSize],*p=NULL;
int top=-1,k,j=0;
char ch;
b=NULL;
ch=str[j];
while(ch!='\0')
{
switch(ch)
{
case '(':top++;St[top]=p;k=1;break;
case ')':top--;break;
case ',':k=2;break;
default:p=(BTNode *)malloc(sizeof(BTNode));
p->data=ch;p->lchild=p->rchild=NULL;
if(b==NULL) b=p;
else
{
switch(k)
{
case 1:St[top]->lchild=p;break;
case 2:St[top]->rchild=p;break;
}
}
}
j++;
ch=str[j];
}
}
//层次遍历算法
void LevelOrder(BTNode *b)
{
BTNode *p;
BTNode *qu[MaxSize];
int front,rear;
front=rear=-1;
rear++;
qu[rear]=b;
while(front!=rear)
{
front=(front+1)%MaxSize;
p=qu[front];
printf("%c ",p->data);
if(p->lchild!=NULL)
{
rear=(rear+1)%MaxSize;
qu[rear]=p->lchild;
}
if(p->rchild!=NULL)
{
rear=(rear+1)%MaxSize;
qu[rear]=p->rchild;
}
}
}

//主函数
int main()
{
BTNode *b,*h;
char s[MaxSize];//A(B(D(,G)),C(E,F))
printf("请输入二叉树括号表示法字符串:\n");
while(scanf("%s",&s))
{
CreateBTNode(b,s);
printf("层次遍历算法的访问次序为:");
LevelOrder(b);
printf("\n请输入二叉树括号表示法字符串:\n");
}
return 0;
}

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧