⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shude.c

📁 这是个数据结构的算法(二叉树的实现)
💻 C
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -