| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2098 人关注过本帖
标题:帮忙写个二叉树的程序
只看楼主 加入收藏
独行者123
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2008-5-1
收藏
 问题点数:0 回复次数:6 
帮忙写个二叉树的程序
写个C程序,实现以下的功能
1 建二叉树,实现前序遍历的非递归算法
2(1)求已建好的二叉树的结点数
(2)求叶子的结点数
(3)求度为1的结点数
(4)求二叉树的深度
刚学二叉树,对于一些算法及将算法转化为可运行的程序很迷糊,希望哪位能够写出完整的程序,让我能有个参考,便于自己理解,谢了
搜索更多相关主题的帖子: 二叉树 
2008-11-02 20:18
cfans1314
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2008-10-20
收藏
得分:0 
期待中!
我也不会,不能让你的帖子沉下去,谁会 ,就帮忙写一个啊
2008-11-03 07:04
geninsf009
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:613
专家分:95
注 册:2008-8-16
收藏
得分:0 
算法还是自己去实现好...
2008-11-03 10:24
ronaldowsy
Rank: 1
等 级:新手上路
帖 子:68
专家分:0
注 册:2008-10-20
收藏
得分:0 
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

typedef struct bitnode
{
char data;
struct bitnode *lchild, *rchild;
}bitnode, *bitree;

void createbitree(t,n)
bitnode ** t;
int *n;
{
char x;
bitnode *q;

*n=*n+1;
printf("\n       Input  %d  DATA:",*n);
x=getchar();
if(x!='\n') getchar();

if (x=='\n')
return;

q=(bitnode*)malloc(sizeof(bitnode));
q->data=x;
q->lchild=NULL;
q->rchild=NULL;
*t=q;

printf("      This Address is:   %o,    Data is:  %c,\n      Left Pointer is:   %o,       Right Pointer is:  %o",q,q->data,q->lchild,q->rchild);

createbitree(&q->lchild,n);
createbitree(&q->rchild,n);
return;
}

void visit(e)
bitnode *e;
{
printf("  Address:  %o,  Data:  %c,  Left Pointer:  %o,  Right Pointer:  %o\n",e,e->data,e->lchild,e->rchild);
}

void preordertraverse(t)
bitnode *t;
{
if(t)
{
visit(t);
preordertraverse(t->lchild);
preordertraverse(t->rchild);
return ;
}else return ;
}

void countleaf(t,c)
bitnode *t;
int *c;
{
    if(t!=NULL)
  {
      if (t->lchild==NULL && t->rchild==NULL)
      {*c=*c+1;
      }
   countleaf(t->lchild,c);
   countleaf(t->rchild,c);
  }
 return;
}

int treehigh(t)
bitnode *t;
{int lh,rh,h;
 if(t==NULL)
 h=0;
 else{
 lh=treehigh(t->lchild);
 rh=treehigh(t->rchild);
 h=(lh>rh ? lh:rh)+1;
      }
 return h;
}
main()
{
bitnode *t; int count=0;
int n=0;

printf("\n                  Please input TREE Data:\n");
createbitree(&t,&n);

printf("\n                  This is TREE Struct: \n");
preordertraverse(t);

countleaf(t,&count);
printf("\n       This TREE has %d leaves    ",count);

printf("    ,       High of The TREE is: %d\n",treehigh(t));
}


2008-11-03 13:32
safin8916
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-11-3
收藏
得分:0 
非递归前序遍历
void Preread()
{
    BTNode *P;
    struct
    {
        BTNode *pt;
        int tag;
    };St[MAXSIZE]
        int top=1;
        top++;
        St[top].pt=b;
        St[top].tag=1;
        while (top>-1)                        /*栈空时候循环*/
        {
            if(ST[top].tag==1)                 /*不能直接访问的情况*/
            {
                p=St[top].pt;
                top--;
                if(p!=NULL)
                {
                    top++;
                    St[top].pt=p->rchild;      /*右孩子结点进栈*/
                    St[top].tag=1;
                    top++;
                    St[top].pt=p->lchild;      /*左孩子结点进栈*/
                    St[top].tag=1;
                    top++;                     /*根结点进栈*/
                    St[top].pt=p;
                    St[top].tag=0;
                }
            }
            if (St[top].tag==0)              /*可以直接访问的情况*/
            {
                cout<<" "<<St[top].pt-.data<<;
                top--;
            }
        }
}   





/*我可以为你提供一个非递归的前序遍历的算法。但是如果你不是要考研的话,其实没必要掌握这个。只要那简单的递归算法就好了!~ */
2008-11-03 17:22
ldy1204
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2008-9-1
收藏
得分:0 
#include"stdio.h"
#include"stdlib.h"
#define leng sizeof(bitnode)
#define OK 1
#define OVERFLOW -1
#define ERROR 0
#define stack_init_size 100
#define MAXSIZE 110
#define term 7
typedef char telemtype;
typedef struct bitnode   //二叉树的二叉链表存储表示
{
    telemtype data;
    struct bitnode *lchild,*rchild; //左右孩子指针
}bitnode,*bitree;
typedef struct{
    bitree base;
    bitree top;
    int stacksize;
}sqstack;
typedef struct{
    bitnode *base;
    int front;
    int rear;
}sqqueue;
char bitreenode[MAXSIZE];
int initstack(sqstack *s)
{
    s->base=(bitree)malloc(stack_init_size*leng);
    if(!s->base) exit (OVERFLOW);
    s->top=s->base;
    s->stacksize=stack_init_size;
    return OK;
}
int gettop(sqstack s,bitnode *e)
{
    if(s.top==s.base) return ERROR;
    *e=*(s.top-1);
    return OK;
}
int push(sqstack *s,bitnode *e)
{
    if (s->top-s->base>=s->stacksize)
    {
        s->base=(bitree)realloc(s->base,(MAXSIZE)*leng);
        if(!s->base) exit (OVERFLOW);
        s->top=s->base+s->stacksize;
        s->stacksize=MAXSIZE;
    }
    *s->top++=*e;
    return OK;
}
int pop(sqstack *s,bitree * e)
{
    if(s->top==s->base) return ERROR;
    *e=--s->top;
    return OK;
}
int stackempty(sqstack *s)
{
    if(s->top==s->base)  return 1;
    return 0;
}
int initqueue(sqqueue *q)
{
    q->base=(bitnode *)malloc(MAXSIZE*leng);
    if(!q->base) exit(OVERFLOW);
    q->front=q->rear=0;
    return OK;
}
int enqueue(sqqueue *q,bitnode *e)
{
    if((q->rear+1)%MAXSIZE==q->front) return ERROR;
    q->base[q->rear]=*e;
    q->rear=(q->rear+1)%MAXSIZE;
    return OK;
}
int dequeue(sqqueue *q,bitree e)
{
    if(q->front==q->rear) return ERROR;
    *e=q->base[q->front];
    q->front=(q->front+1)%MAXSIZE;
    return OK;
}
int queueempty(sqqueue *q)
{
    if(q->front==q->rear) return 1;
    return 0;
}
int visit(telemtype e)
{
    printf("%c",e);
    return OK;
}
int creatbitree(bitree *t)
{  
    static i=0;
    if(bitreenode[i]==' ') {*t=NULL; i++;}
  else
  {
      if(!(*t=(bitree)malloc(leng))) exit(OVERFLOW);        
      (*t)->data=bitreenode[i];
      i++;
      creatbitree(&(*t)->lchild);//递归构造左子树
      creatbitree(&(*t)->rchild); //递归构造右子树
  }
  return OK;
}
int preordertraverse1(bitree t,int(*visit)(telemtype e)) //先序遍历的递归算法
{
    if(t)
    {
        if(visit((t)->data))        //访问根结点
            if(preordertraverse1((t)->lchild,visit))   
                if(preordertraverse1((t)->rchild,visit))    return OK;    
                return ERROR;
    }
    else return OK;
}
int preordertraverse2(bitree t,int(*visit)(telemtype e)) //先序遍历的非递归算法
{    
    sqstack s;
    bitree p;
    initstack(&s);
    p=t;
    while (p||!stackempty(&s))
    {
        if(p){
            push(&s,p);   //根指针进栈
            if(!visit(p->data)) return ERROR;  
            p=p->lchild;   //访问左孩子
        }
        else{
            pop(&s,&p);
            p=p->rchild;  //访问右孩子
        }
    }
    return OK;
}
int inordertraverse1(bitree t,int(*visit)(telemtype e))
{
    if(t)
    {
        if(inordertraverse1(t->lchild,visit))
            if(visit(t->data))
                if(inordertraverse1(t->rchild,visit)) return OK;
                return ERROR;
    }
    else return OK;
}
int inordertraverse2(bitree t,int(*visit)(telemtype e))
{
    bitree p;
    sqstack s;
    initstack(&s);
    p=t;
    while(p||!stackempty(&s))
    {
        if(p)
        {
            push(&s,p);
            p=p->lchild;/*向左走到尽头*/
        }
        else{
            pop(&s,&p);
            if(!visit(p->data)) return ERROR;
            p=p->rchild;
        }
    }
    return OK;
}
int postordertraverse(bitree t,int(*visit)(telemtype e))
{
    if(t)
    {
        if(postordertraverse(t->lchild,visit))
            if(postordertraverse(t->rchild,visit))
                if(visit(t->data)) return OK;
                return ERROR;
    }
    else return OK;
}
void levelordertraverse(bitree t,int(*visit)(telemtype e))
{
    sqqueue q;
    bitree a;
    a=(bitree)malloc(leng);
    if (t)
    {   
        initqueue(&q);
        enqueue(&q,t);
        while(!queueempty(&q))
        {
            dequeue(&q,a);
            visit(a->data);
            if(a->lchild) enqueue(&q,a->lchild);
            if(a->rchild) enqueue(&q,a->rchild);
        }
    }
}
int bitreedegree1(bitree t,int* a,int * b,int* c)
{
    if(!t) return ERROR;
    if(t->lchild&&t->rchild) ++(*a);
    else if(t->lchild||t->rchild)  ++(*b);
        else ++(*c);
        bitreedegree1(t->lchild,a,b,c);
        bitreedegree1(t->rchild,a,b,c);
    return OK;
}
int bitreedegree2(bitree t,int* a,int * b,int* c)
{    
    bitree p=t;
    sqstack s;
    initstack(&s);
    while (p||!stackempty(&s))
    {
        if (p)
        {
            push(&s,p);
            if(p->lchild&&p->rchild) ++(*a);
            else if(p->lchild||p->rchild) ++(*b);
            else ++(*c);
            p=p->lchild;
        }
        else{
            pop(&s,&p);
            p=p->rchild;
        }
    }
    return OK;
}
int bitreedepth(bitree t)
{
    int i,j;
    if(!t) return 0;
    if(t->lchild) i=bitreedepth(t->lchild);
    else i=0;
    if(t->rchild) j=bitreedepth(t->rchild);
    else j=0;
    return (i>=j)?(i+1):(j+1);
}
void main()
{
    int a,k,two=0,one=0,zero=0,depth;
    bitree t;
    static char c[40];
    char *menu[]={//选择菜单
            "\n\n\t\t1:preorder traverse\n",
            "\t\t2:inorder traverse\n",
            "\t\t3:postorder traverse\n",
            "\t\t4:datas' degree \n",
            "\t\t5:levelorder traverse\n",
            "\t\t6:bitree' depth\n",
            "\t\t0:exit\n",
            "\n\tselect[0-6]:"
    };
    t= (bitree)malloc(sizeof(bitnode));
    t=NULL;
    printf("\nnow create a tree,please input a preorder bitree:");
    gets(bitreenode);
    creatbitree(&t);
        for(k=0;k<=term;k++)
            printf("%s",menu[k]);
        scanf("%d",&a);   
            while(1){//循环
            switch(a)
            {
              case 1:printf("\nthis is the recursion arithmetic:\n");
                preordertraverse1(t,visit);
                printf("\nthis is not the recursion arithmetic:\n");
                   preordertraverse2(t,visit);
                   break;
              case 2:    printf("\nthis is the recursion arithmetic:\n");
                  inordertraverse1(t,visit);    
                  printf("\nthis is not the recursion arithmetic:\n");
                  inordertraverse2(t,visit);
                  break;
              case 3:    printf("\nthis is the recursion arithmetic:\n");
                  postordertraverse(t,visit);break;
              case 4:     printf("this is the recursion arithmetic:\n");
                  bitreedegree1(t,&two,&one,&zero);
                  printf("number of degree 2:%d\n",two);
                  printf("number of degree 1:%d\n",one);
                  printf("number of degree 0:%d\n",zero);
                  two=0;one=0;zero=0;
                  printf("this is not the recursion arithmetic:\n");
                  bitreedegree2(t,&two,&one,&zero);
                  printf("number of degree 2:%d\n",two);
                  printf("number of degree 1:%d\n",one);
                  printf("number of degree 0:%d\n",zero);
                      break;
              case 5: levelordertraverse(t,visit);break;
              case 6: depth=bitreedepth(t);
                  printf("tree depth is:%d",depth);
                  break;
              case 0: exit(0) ;
              default:printf("\ninput the wrong choice,retry(0-7)");
            }
            printf("\ninput your choice(0-6):");
            scanf("%d",&a);
            }
}
2008-11-06 21:56
ldy1204
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2008-9-1
收藏
得分:0 
上个程序题目如下:
实验(三)  实现二叉树的如下运算:
      1.输入带空二叉树(信息)的先序遍历序列,生成一棵二叉树;(若结点为字符类型,用空格表示空二叉树;若结点为整数类型,
用0(零)表示空二叉树)
      2.作前序遍历,分别用递归算法、非递归算法实现;
      3.作中序遍历,分别用递归算法、非递归算法实现;
      4.作后序遍历,用递归算法实现;
      5.求度分别为0、1、2的结点的数目,分别用递归算法、非递归
算法实现;
     6.按层次遍历二叉树(提示:使用一个队列实现);
     7.求二叉树的高度(深度);
2008-11-06 21:57
快速回复:帮忙写个二叉树的程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029938 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved