首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 二叉树遍历之递归算法
【标  题】:二叉树遍历之递归算法
【关键字】:
【来  源】:http://blog.csdn.net/jitom515/archive/2006/11/10/1377606.aspx

二叉树遍历之递归算法

#include <stdio.h>
#include <malloc.h>

typedef struct node{
        int data;
        struct node *lchild,*rchild;
}*treetp,tree;

treetp create (treetp t,int c);
void print1(treetp);
void print2(treetp);
void print3(treetp);

int number=0;

int main(void)
{
        treetp t=NULL, r=NULL;
        r=create (t,0);
        printf("\nqianxubianli: ");
        print1 (r);
        printf("\nzhongxubianli: ");
        print2 (r);
        printf("\nhouxubianli: ");
        print3 (r);
        printf("\n");

        return 0;
}

treetp create(treetp t,int c)
{
        treetp p,di;

        do{
                scanf("%d",&c);
                if (t==NULL)
                {
                        t=(treetp)malloc(sizeof(tree));
                        t->lchild=t->rchild=0;
                        t->data=c;
                }
                else
                {
                        p=t;
                        while(p!=NULL)
                        {
                                di=p;
                                if(c < (p->data))
                                        p=p->lchild;
                                else
                                        p=p->rchild;
                        }
                        if(c<(di->data))
                        {
                                treetp NEWdi=(treetp) malloc(sizeof(tree));
                                NEWdi->lchild=NEWdi->rchild=0;
                                NEWdi->data=c;
                                di->lchild=NEWdi;
                        }
                        else
                        {
                                treetp NEWdi=(treetp) malloc(sizeof(tree));
                                NEWdi->lchild=NEWdi->rchild=0;
                                NEWdi->data=c;
                                di->rchild=NEWdi;
                        }
                }
                ++number;
        }while(c!=0);

        printf("yezi de shuliang:%d\n",number);
        return t;
}

void print1(treetp t)
{
        if (t!=NULL)
        {
                printf("%d ",t->data);
                print1(t->lchild);
                print1(t->rchild);
        }
}

void print2(treetp t)
{
        if (t!=NULL)
        {
                print2(t->lchild);
                printf("%d ",t->data);
                print2(t->rchild);
        }
}

void print3(treetp t)
{
        if (t!=NULL)
        {
                print3(t->lchild);
                print3(t->rchild);
                printf("%d ",t->data);
        }

三天不练手生(数据结构):【上一篇】
二叉树遍历的非递归算法:【下一篇】
【相关文章】
没有相关文章
【随机文章】
  • 配置英特尔® RAID控制器
  • 一个塑料期货交易程序分析图程序(PHP+JAVA)
  • 使SWT的Table根据TableItem显示Tooltip
  • 女秘书PK老板
  • 供应链管理新视角:极速供应链
  • 从带库中存取磁带的方法(hp)
  • 一套CSS, DIV模版
  • Windows-菜单(二)
  • windows 下的信号机制初探
  • 宽带无线接入技术发展前景七大趋势
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.