shude.c

来自「这是个数据结构的算法(二叉树的实现)」· C语言 代码 · 共 58 行

C
58
字号
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct tree
{
    char info;
    struct tree *left,*right;
};

struct tree *createtree(struct tree *root,struct tree *r,char info)
{
    if(!r)
    {
        r=(struct tree *)malloc(sizeof(struct tree));
        if(!r)
        {
            printf("Out of Memory");
            exit(0);
        }
        r->left=NULL;
        r->right=NULL;
        r->info=info;
        if(!root)return r;
        if(info<root->info)root->left=r;
        else root->right=r;
        return r;
    }
    if(info<r->info)
    createtree(r,r->left,info);
    else
    createtree(r,r->right,info);
    return root;
}
void print_tree(struct tree *r)
{
    if(!r)return;
    print_tree(r->right);
    printf("%c",r->info);
    print_tree(r->left);
}



int main(void)
{
    char a[10];
    root=NULL;
    do
    {
        printf("Enter a letter:");
        gets(a);
        root=createtree(root,root,*a);
    }while(*a);
    print_tree(root);
    getch();
    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?