关于二叉树的知识点汇总

来源:互联网 发布:58中国经纪人网络平台 编辑:程序博客网 时间:2024/06/05 16:41
#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");
}
原创粉丝点击