如何从txt文件中读取多个数字并存储到多个BST中

时间:2014-04-18 17:11:10

标签: c linked-list binary-tree

对于这个程序,我必须读取大约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<sdtio.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 != 3){
            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;
    BST *root;




   while (fscanf(input, "%d", &value) != EOF){
            if(value != -1){
            insert_BST(root, value, treeNum);
            }
            else{
            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){


    if(listHead == NULL){


            rootList *temp = (rootList*)malloc(sizeof(rootList));
            temp->root = root;
            listHead = temp;
    }
    else{


            listHead->next = insert_rootList(listHead->next, root);
 }
 }

 /* 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){




 }



/* 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);
}


}

0 个答案:

没有答案