English 中文(简体)
使用元素数组创建二叉树
原标题:Create a binary tree using an array of elements

因此,我试图使用给定的数组创建一个二叉树,然后按顺序、预序和后序打印结果,但似乎我在某个地方犯了一个错误,但据我所知,一切都应该正常。

这是它打印出来的结果

1 2 3 4 5 6 7 8 9 10  #inorder
10 9 8 7 6 5 4 3 2 1  #postorder
1 2 3 4 5 6 7 8 9 10  #preorder

到目前为止,我就是这么做的。有人能指出这个错误,也许还能就如何修复它提出建议吗?

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

typedef struct node_s node_t;
struct node_s
{
    int key;
    node_t *left,*right;
};
node_t *new_node(int val)
{
    node_t *new;
    new= malloc(sizeof (node_t));
    if(new==NULL)
    {
        exit(1);
    }
    new->key=val;
    new->left=NULL;
    new->right=NULL;
    return new;
}
void print_inorder(node_t *root)
{
    node_t *new=root;
    if (new != NULL) {
        print_inorder(new->left);
        printf("%d ",new->key);
        print_inorder(new->right);
    }
    return;

}
void print_postorder(node_t *root)
{
    node_t *new=root;
    if (new != NULL) {
        print_postorder(new->left);
        print_postorder(new->right);
        printf("%d ",new->key);
    }
    return;

}
void print_preorder(node_t *root)
{
    node_t *new=root;
    if (new != NULL) {
        printf("%d ",new->key);
        print_preorder(new->left);
        print_preorder(new->right);
    }
    return;

}
node_t *insert_node(node_t *root,int val)
{
     if(root==NULL)
            return new_node(val);
     else
     {
         if(val<root->left)
             root->left= insert_node(root->left,val);
         else if(val>root->left)
             root->right= insert_node(root->right,val);
     }
    return root;
}
int main() {

    int n;
    int v[]={1,2,3,4,5,6,7,8,9,10};
    node_t *root=NULL;
    FILE *file;
    file= fopen("file","r");
    if(file==NULL)
    {
        exit(1);
    }
    for (int i = 0; i < 10; ++i) {
        root= insert_node(root,v[i]);
    }

    print_inorder(root);
    printf("
");
    print_postorder(root);
    printf("
");
    print_preorder(root);

    return 0;
}
问题回答

在构建二叉树时,建议添加一个特殊字符作为节点的终止。

这是我的答案

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

typedef struct node_s node_t;
struct node_s
{
    char key;
    node_t *left,*right;
};


/*
node_t *new_node(char val)
{
    node_t *new;
    new= malloc(sizeof (node_t));
    if(new==NULL)
    {
        exit(1);
    }
    new->key=val;
    new->left=NULL;
    new->right=NULL;
    return new;
}
*/

void print_inorder(node_t *root)
{
    node_t *new=root;
    if (new == NULL)
       return;
    print_inorder(new->left);
    printf("%c ",new->key);
    print_inorder(new->right);

}
void print_postorder(node_t *root)
{
    node_t *new=root;
    if (new == NULL)
        return;
    print_postorder(new->left);
    print_postorder(new->right);
    printf("%c ",new->key);
    

}
void print_preorder(node_t *root)
{
    node_t *new=root;
    if (new == NULL)
        return;
    printf("%c ",new->key);
    print_preorder(new->left);
    print_preorder(new->right);
    

}
void insert_node(node_t** root)
{
    char v[]={ A , B , # , D , # , # , C , # , # };
    static int i = 0;
    if(v[i] ==  # ){
        *root = NULL;
        i++;
    }
    else{
        *root = (node_t*)malloc(sizeof(node_t));
        if(NULL == *root){
            printf("No space
");
            exit(1);
        }
        (*root)->key = v[i];
        i++;
        insert_node(&(*root) -> left);
        insert_node(&(*root) -> right);
    }
}
int main() {

    node_t *root=NULL;
    
//    FILE *file;
//    file= fopen("file","r");
//    if(file==NULL)
//    {
//        exit(1);
//    }    

    insert_node(&root);

    printf("preorder:  ");
    print_preorder(root);
    printf("
");

    printf("inorder:   ");
    print_inorder(root);
    printf("
");

    printf("postorder: ");
    print_postorder(root);
    printf("
");

    return 0;
}

运行它将输出:

preorder:  A B D C 
inorder:   B D A C 
postorder: D B C A 




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签