C语言创建一个二叉树

如何创建一个二叉树,先序遍历,中序遍历。

#include <stdio.h>
#include <stdlib.h>

#include<bits/stdc++.h>
#define NULLKEY '?'

typedef struct btnode
{
    char data;
    struct btnode *lchild,*rchild;
}btnode,*bitree;
//创建一个二叉树

bitree preCreateBitree(bitree &root)
{
    char ch;
    scanf("%c",&ch);
    if(ch==NULLKEY)
    {
        root=NULL;
        return(root);
    }
    else
    {
        root=(bitree)malloc(sizeof(btnode));
        root->data=ch;
        preCreateBitree(root->lchild);
        preCreateBitree(root->rchild);
        return(root);
    }
}

void fsearch(bitree root)
{
    if(root==NULL)
        return ;
    else
    {
        printf("%c",root->data); //先序遍历每个结点的值
        fsearch(root->lchild);
        fsearch(root->rchild);
    }
}

void msearch(bitree root)
{
    if(root==NULL)
        return ;
    else
    {
        msearch(root->lchild);
        printf("%c",root->data); //中序遍历每个结点的值
        msearch(root->rchild);
    }
}

int main()
{
    bitree root;
    root = preCreateBitree(root);
    printf("first:");
    fsearch(root);
    printf("\n");
    printf("middle:");
    msearch(root);
    printf("\n");
}
  • 11
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值