我有一个编程课作业,今晚 CDT 晚上 8 点到期,但我遇到了麻烦。我们将通过读取文件来获取以下数字的列表:
9 30 20 40 35 22 48 36 37 38
将它们放在一个数组中(很简单),然后使用 C 将它们读入二叉搜索树。列表中的第一个数字是树中元素的数量。其余的放置在以下结构中:
typedef struct node_struct {
    int data;
    struct node_struct* left;
    struct node_struct* right;
} Node;
我想我已经完成了第一部分。把使用 fscanf 中的东西(我没有选择使用这种方法,我更喜欢 fgets),在数组的每个成员上调用一个插入函数,然后在插入函数内部调用一个“createNode”函数。
问题是,我只让一名成员加入 BST。此外,BST 必须满足条件node->left->data <= node->data < node->right->data……换句话说,节点在树中必须是有序的。
这是我到目前为止所拥有的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// def BST node struct
typedef struct node_struct {
    int data;
    struct node_struct* left;
    struct node_struct* right;
} Node;
// prototypes
Node* createNode(int data);
Node* bstInsert(Node* root, int data);
// helper function prototypes
void padding(char ch, int n);
void displayTree(Node* root, int depth);
int main(int argc, char **argv)
{
    FILE *in = NULL;
    int num_read, count=0, array_size = 0;
    if(argc != 2){
        printf("hw3 <input-file>\n");
        return 1;
    }
    in = fopen(argv[1], "r");
    if(in == NULL){
        printf("File can not be opened.\n");
        return 2;
    }
    // read in the first line to get the array size
    fscanf(in, "%d", &array_size);
    // declare the array
    int array[array_size];  
    // read from the second line to get each element of the array
    while(!feof(in)){
        fscanf(in, "%d", &num_read);
        array[count] = num_read;
        count++;
    }
    fclose(in);
    if (array_size != count) {
        printf("data error. Make sure the first line specifies the correct number of elements.");
        return 3;
    }
    Node *root1 = NULL, *root2 = NULL, *root3 = NULL;
    int i;
    // task1: construct a bst from the unsorted array
    printf("=== task1: construct a bst from the unsorted array ===\n");
    for (i = 0; i < array_size; i++) {
        root1 = bstInsert(root1, array[i]);
    }
    displayTree(root1, 0);
    return 0;
}   
Node* bstInsert(Node* root, int data) {
    if(root == NULL){
        root = createNode(data);
        if(root != NULL){
            root= createNode(data);
        }
        else{
            printf("%d not inserted, no memory available.\n", data);
        }
    }
    Node* current, previous, right;
    current = root;
    previous = root->left;
    next = root->right;
    else{
        if(previous->data <= current->data){
                }
     }
     return root;
}
Node* createNode(int data) {
    // TODO
    Node* aRoot;
    if(!data)
        return NULL;
    aRoot = malloc(sizeof(Node));
    if(!aRoot){
        printf("Unable to allocate memory for node.\n");
        return NULL;
    }
    aRoot->data = data;
    aRoot->left = NULL;
    aRoot->right = NULL;
    return aRoot;
}
    /* helper functions to print a bst; You just need to call displayTree when visualizing a bst */
void padding(char ch, int n)
{
    int i;
    for (i = 0; i < n; i++)
    printf("%c%c%c%c", ch, ch ,ch, ch);
}
void displayTree(Node* root, int depth){
    if (root == NULL) {
        padding (' ', depth);
        printf("-\n");
    }
    else {
        displayTree(root->right, depth+1);
        padding(' ', depth);
        printf ( "%d\n", root->data);
        displayTree(root->left, depth+1);
    }
}
main, createNode, displayTree, 并且padding没问题,我相信。这bstInsert就是我遇到麻烦的地方。我只是不确定如何订购东西以创建有效的树。
编辑:
我编辑了 bstInsert 并注入了更多逻辑。它应该在树上打印出更多的叶子,但是,它只打印出数字“30”。这是新功能。
Node* bstInsert(Node* root, int data) {
if(root == NULL){
    root = createNode(data);
    if(root != NULL){
        root= createNode(data);
    }
    else{
        printf("%d not inserted, no memory available.\n", data);
    }
}
else{
    if(data < root->data){
        bstInsert(root->left, data);
    }
    else if(data > root->data || data == root->data){
        bstInsert(root->right, data);
    }
        }
return root;
}