【问题标题】:How to read in text file and store values into multiple binary trees [duplicate]如何读取文本文件并将值存储到多个二叉树中[重复]
【发布时间】:2014-04-18 20:26:14
【问题描述】:

对于这个程序,我必须读取大约 50 个数字的文本文件,然后我必须将它们分解(BST 的结尾由 -1 表示)。所以我很确定我对实际的树号使用链表,然后对实际的树使用 BST。不过,我的 while 循环遇到了一些问题,当数字为 -1 时,要破坏树木。示例输出如下。

Tree 1: 5 8 14 15 17 19 20 30

Tree 2: 28 60 65 80 83 86 90

Tree 3: 33 40 41 42 43 45 49

Tree 4: 1 4 13 21 47 72

Tree 5: 16 55 69 77 111

文本文件是....

15 

8 

14 

5 

19 

20 

17 

30 

-1 

80 

65 

60 

86 

83 

90 

28 

-1 

42 

45 

49 

40 

43 

33 

41 

-1 

13 

47 

21 

72 

1 

4 

-1 

55 

16 

69 

77 

111 

-1


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

#define MAX_LINE_SIZE 20

typedef struct BST {
    int value;
    int treeNum;
    struct BST* left;
    struct BST* right;
}BST;

typedef struct rootList {
struct BST* root;
struct rootList* next;
}rootList;

void insert_BST(BST** root, int insertValue, int treeNum);
BST* createTreeNode(int nodeValue, int treeNum);
void printTrees(rootList* listHead);
void free_BSTs(BST* root);
void insert_rootList(rootList** listHead, BST* root);
void print_BST_inorder(BST* root);

int main (int argc, char* argv[]){

    if (argc != 2){
            printf("Incorrect number of command args\n");
            return 1;
    }

    FILE *input = fopen(argv[1], "r");
    while(input == NULL){
        char file[100];
        printf("Unable to open file, enter a new name: ");
        scanf("%99s", file);
        input = fopen(file, "r");
    }

    int value;
    int treeNum = 1;
    BST **root;
    rootList **listHead;

    while (fscanf(input, "%d", &value) != EOF){
        while(value != -1){
            insert_BST(root, value, treeNum);
        }

        treeNum++;
        insert_rootList(listHead,(*root));
    }
    return 0;
}

/*createTreeNode
 *  *  parameters: int value to be inserted in the binary search tree
 *   *   returns: pointer to a newly created binary search tree node (BST*)
 *    *    -> createTreeNode simply creates a new tree node using the value passed as    the 
 *     *        parameter.*/

BST* createTreeNode(int nodeValue, int treeNum){

    BST *new_bst = (BST*)malloc(sizeof(BST));
    new_bst->value = nodeValue;
    new_bst->treeNum = treeNum;
    new_bst->right = new_bst->left = 0;

    return new_bst;
}

 /* insert_BST 
 *  *  parameters: the reference of the root (BST**) and the int value to be inserted.
 *   *   returns: void
 *    *     
 *     *      -> This function recursively finds the right position in the binary search tree 
 *      *          for the new value and inserts the node containing the new value in that position.
 *       *          */

void insert_BST(BST** root, int insertValue, int treeNum){
    *root = createTreeNode(insertValue, treeNum);

    if(root != 0){
        if(insertValue < (*root)->value)
            insert_BST((*root)->right, insertValue, treeNum);
        else if (insertValue > (*root)->value)
            insert_BST((*root)->left, insertValue, treeNum);
        return;
    }
}

/*insert_rootList
 *  *  parameters: the reference of the head pointer to the list (BST**) and pointer to the root of the new binary search tree
 *   *  returns: void
 *    *    
 *     *     -> This function inserts the new binary search tree at the BACK of the linked 
 *      *         list containing pointers to the roots of the binary search trees.
 *       *         */

void insert_rootList(rootList** listHead, BST* new_root){

    rootList *temp = (rootList*)malloc(sizeof(rootList));

    temp->root = new_root;
    temp->next = NULL;

    if((*listHead)->next == NULL){
        (*listHead)->next = temp;
    }
    else
    {
        rootList *current = *listHead;
        while((*listHead)->next != NULL){
            if(current->next == NULL)
            {
                current->next = temp;
                break;
            }
            current = current->next;
        }
    }
    return;
}

/* printTrees 
 *  *  parameters: pointer to the head of the linked list
 *   *   returns: void
 *    *     
 *     *      -> This Function prints all the binary search trees in the linked list
 *      *      */

void printTrees(rootList* listHead){
    while(listHead != NULL){
        print_BST_inorder(*listHead);
    }
}

/* print_BST_inorder 
*  *  parameters: pointer to the root of the tree
*   *   returns: void
*    *     
*     *      -> This Function prints the binary search tree using inorder traversal
*      *       */

void print_BST_inorder(BST* root){


    print_BST_inorder(root->left);
    printf("%d", root->value);
    print_BST_inorder(root->right);
}

这些是我在编译时遇到的错误

homework3.c: In function ‘insert_BST’:
homework3.c:101: warning: passing argument 1 of ‘insert_BST’ from incompatible pointer type
homework3.c:95: note: expected ‘struct BST **’ but argument is of type ‘struct BST *’
homework3.c:103: warning: passing argument 1 of ‘insert_BST’ from incompatible pointer type
homework3.c:95: note: expected ‘struct BST **’ but argument is of type ‘struct BST *’
homework3.c: In function ‘printTrees’:
homework3.c:157: warning: passing argument 1 of ‘print_BST_inorder’ from incompatible pointer type
homework3.c:28: note: expected ‘struct BST *’ but argument is of type ‘struct rootList *’

【问题讨论】:

  • 这些错误不是很明显吗?它告诉你它的期望和你给它的,并说那些不兼容。
  • @Dukeling 是的,但我不明白如何修复它们,当我将它们设为相同的指针类型时它不起作用,我收到另一个错误,告诉我左右不是联合或结构的一部分

标签: c data-structures linked-list binary-tree binary-search-tree


【解决方案1】:

将您的 insert_BST 函数替换为:

insert_BST(&(*root)->right, insertValue, treeNum);

insert_BST(&(*root)->left, insertValue, treeNum);

print_BST_inorder的调用改为:

print_BST_inorder(listHead->root);

说明:

(*root)-&gt;right(*root)-&gt;left 的类型为 struct BST*,但 insert_BST 函数采用 struct BST**,因此您只需取消对它们的引用即可。

listHeadrootList* 类型,因此 *listHeadrootList,而 print_BST_inorder 函数需要 BST*,它是该结构的成员 root

【讨论】:

  • 谢谢。不过现在我遇到了段错误。
  • 最好在单独的问题中处理编译器错误和运行时问题。
猜你喜欢
  • 1970-01-01
  • 2014-09-13
  • 2012-05-29
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-05
相关资源
最近更新 更多