树及其三种遍历

本文介绍了一种创建二叉树的方法,并实现了三种不同的遍历方式:中序遍历、先序遍历和后序遍历。通过这些遍历方式可以更好地理解和操作二叉树结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/***********************/
/****Author:Guo Junling*/

/********Date:2004.11.2*/

/*Description:Create a tree and travelsal
              it in three different ways*/
/***********************/


#i nclude <stdio.h>
#i nclude <stdlib.h>
#i nclude <conio.h>
#define MAX 100


/*define a tree*/

typedef struct tnode
{
    int data;
    struct tnode *lchild,*rchild;
}TNODE;

void create();
TNODE *insert(int );      /*插入结点函数*/
void inorder(TNODE *);    /*中序遍历函数*/
void preorder(TNODE *);   /*先序遍历函数*/
void postorder(TNODE *);  /*后序遍历函数*/

/*define a global variable*/

TNODE *root=NULL;

/*main function*/

void main()
{
    create();
    printf("----------INORDER------------/n");
    inorder(root);
    printf("/n-----------------------------/n/n/n");
    printf("----------PREORDER-----------/n");
    preorder(root);
    printf("/n-----------------------------/n/n/n");
    printf("----------POSTORDER----------/n");
    postorder(root);
    printf("/n-----------------------------/n/n/n");
getch();
}

/*function:travelsal of the tree in inorder*/

void inorder(TNODE *ptr)
{
    if(ptr!=NULL)
    {
        inorder(ptr->lchild);
        printf("%d ",ptr->data);
        inorder(ptr->rchild);
    }
}


/*function:travelsal of the tree in preorder*/

void preorder(TNODE *ptr)
{
    if(ptr!=NULL)
    {
        printf("%d ",ptr->data);
        preorder(ptr->lchild);
        preorder(ptr->rchild);
    }
}

/*function:travelsal of the tree in postorder*/

void postorder(TNODE *ptr)
{
    if(ptr!=NULL)
    {
        postorder(ptr->lchild);
        postorder(ptr->rchild);
        printf("%d ",ptr->data);
    }
}

/*Function:Create a linked list*/

void create()
{
    int n,i;
    int k[MAX];
    printf("please input the node number of your tree:/n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&k[i]);/*输入节点信息,并保存到一个数组中去*/
        insert(k[i]);     /*将节点插入到树中去*/
    }
}


/*function:Insert  a node into a tree*/

TNODE  *insert(int m)
{
    TNODE *p1,*p2;
    if(root==NULL)
    {
        root=(TNODE *)malloc(sizeof(TNODE));
        root->data=m;  /*树根*/
        root->lchild=root->rchild=NULL;
    }
else 
   {
        p1=root;
        while(m!=p1->data)
        {
            if((m<p1->data)&&(p1->lchild!=NULL))  p1=p1->lchild;
            else if(m>(p1->data)&&(p1->rchild!=NULL))  p1=p1->rchild;
            else if(m<(p1->data)&&(p1->lchild==NULL))
             {
                p2=(TNODE *)malloc(sizeof(TNODE));
                p2->data=m;
                p2->lchild=p2->rchild=NULL;
                p1->lchild=p2;
             }
        else if((m>p1->data)&&(p1->rchild==NULL))
             {
                p2=(TNODE *)malloc(sizeof(TNODE));
                p2->data=m;
                p2->lchild=p2->rchild=NULL;
                p1->rchild=p2;
             }
        }
    }
    return(root);
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值