注册 登录
编程论坛 数据结构与算法

求改善,输出全部树分支的数据,高手帮帮忙!也帮忙增加些其他操作吧,新手上路!!!

hy247767221 发布于 2011-10-27 16:38, 606 次点击
#include<stdio.h>
#include<stdlib.h>

#define MAX 100

typedef struct Tree
{
    char info;
    struct Tree *llink, *rlink;
}Tree;

Tree *creat_tree();
Tree *set_up(char str[]);
void print_node(Tree *ptr);
void print_tree(Tree *ptr);

int main()
{
    Tree *root;
    root = creat_tree();
    print_tree(root);
    putchar(10);
    return 0;
}

Tree *creat_tree()
{
    Tree *root;
    char str[MAX];
    printf("Input the sring: ");
    gets(str);
    root = set_up(str);
    return root;
}

int i = 0;
Tree *set_up(char str[])
{
    Tree *ptr;
    if(str[i] == '\0')
        return NULL;
    if(str[i] != '0')
    {
        ptr = (Tree *)malloc(sizeof(Tree));
        ptr->info = str[i];
        i ++;
        ptr->llink = set_up(str);
        i ++;
        ptr->rlink = set_up(str);
        i ++;
        return ptr;
    }
    else
        return NULL;
}

int count;
void print_node(Tree *ptr)
{
    if(ptr == NULL)
        return;
    printf("%d\t%u\t%c\t%u\t%u\n", ++ count, ptr, ptr->info, ptr->llink, ptr->rlink);
    if(ptr->llink != NULL)
        print_node(ptr->llink);
    if(ptr->rlink != NULL)
        print_node(ptr->rlink);
}

void print_tree(Tree *ptr)
{
    count = 0;
    if(ptr == NULL)
        return;
    printf("Structure of the binary tree: \n");
    printf("Number\tAddress\tInfo\tLlink\tRlink\n");
    print_node(ptr);
}
0 回复
1