读取stdin字符串并将其添加到C中的BST中

时间:2017-03-06 01:19:30

标签: c binary-search-tree nodes

我正在尝试将stdin字符串插入二叉搜索树,然后输出inorder和postorder遍历。我的问题是,似乎旧的节点被最近的覆盖,但我无法弄清楚为什么。所以我的inorder / postorder方法返回最后插入的字符串树中有多少个节点。

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <ctype.h>
#include "bstsort.h"

/* Case Sensitive String Comparison */
int strcmpCase(char* string1, char* string2) {
    while(*string1 && (*string1==*string2)) {
        string1++;
        string2++;
    }
    return *string1 - *string2;
}

/* Case Insensitive String Comparison */
int strcmpNoCase(char* string1, char* string2) {
    int i;
    char a[100];
    char b[100];

    strcpy(a, string1);
    strcpy(b, string2);

    for (i = 0; a[i]; ++i) {
        a[i] = tolower(a[i]);
    }
    for (i = 0; b[i]; ++i) {
        b[i] = tolower(b[i]);
    }
    return strcmpCase(a, b);
}

/* Inserts a Node into the Binary Search Tree */
void insert(struct Node **node, char *keyStr, int cflag) {
    // Creates new node
    if (*node == NULL) {
        *node = (struct Node*) malloc (100);
        strcpy((*node)->key, keyStr);
        (*node)->left = NULL;
        (*node)->right = NULL;
        (*node)->count = 1;
        return;
    }

    // Compares Strings
    int compareResult;
    if (cflag == 1){
        // Case sensitive 
        compareResult = strcmpCase(keyStr, (*node)->key);
    } else {
        // Case insensitive
        compareResult = strcmpNoCase(keyStr, (*node)->key);
    }

    // Moves down branches of BST to insert node in correct order
    if (compareResult < 0) {
        insert(&((*node)->left), keyStr, cflag);
    } else if (compareResult > 0) {
        insert(&((*node)->right), keyStr, cflag);
    }

    (*node)->count++;

    return;
}

/* Outputs in-order traversal or BST */
void inorder(Node* root) {
    if (root != NULL) {             // if current node is not null
        inorder(root->left);        // travel down left child, recursively
        printf("%s", root->key);    // prints key of current node, the root
        inorder(root->right);       // travel down right child after root printed, recursively
    }
}

void postorder(Node* root) {
    printf("Postorder: \n");
    if (root != NULL) {             // if current node is not null
        postorder(root->left);      // travel down left child, recursively
        postorder(root->right);     // travel down right child, recursively
        printf("%s", root->key);    // prints key of current node, the root
    }
}

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

    extern char *optarg;
    extern int optind;
    int c, err = 0, i = 0, numRead, isfirst = 1; 
    int cflag = 0, oflag = 0;   
    char *inName = NULL;            // Input filename
    char *outName = NULL;           // Output filename
    static char usage[] = "Usage: bstsort [-c] [-o output_file_name] [input_file_name]\n";
    FILE* inFile = NULL;        
    FILE* outFile = NULL;
    char *line;
    char tmp[100] = "";
    struct Node *root = NULL;

    while ((c = getopt(argc, argv, "co:")) != -1)
        switch (c) {
        case 'c':
            cflag = 1;
            break;
        case 'o':
            oflag = 1;
            outName = optarg;
            break;
        case '?':
            err = 1;
            break;
        }
    if (err) {
        fprintf(stderr, usage, argv[0]);
        exit(1);
    }
    /* see what we have */
    printf("cflag: %d\n", cflag);
    printf("oflag: %d\n", oflag);
    printf("Output Filename: \"%s\"\n", outName);
    /* these are the arguments after the command-line options */
    if (optind < argc) {
        for (; optind < argc; optind++) {
            inName = argv[optind];
            printf("Input Filename: \"%s\"\n", inName);
        }
    } else {
        printf("No input filename provided.\n");
    }

    /* Reads stdin one line at a time when input filename not provided*/
    line = (char*) malloc (100);
    if (inName == NULL) {
        printf("\nEnter one line at a time:\n");
        fflush(stdout);
        fgets(line, 100, stdin);
        insert(&root, line, cflag);

        while (strcmp(line, "\n") != 0) {
            fflush(stdout);
            fgets(line, 100, stdin);
            if (strcmp(line, "\n") != 0) {
                insert(&root, line, cflag);
            }
        }
    }
    inorder(root);
    postorder(root)


    free(line);
    free(root);
    fclose(inFile);
    exit(0);
}

这是我的Node结构

#ifndef BSTSORT_H
#define BSTSORT_H

/* Binary Search Tree Node Struct */
typedef struct Node {
    char *key;
    int count;
    struct Node *left;
    struct Node *right;
} Node;

void insert(Node **node, char *keyStr, int cflag);
void inorder(Node* root);
void postorder(Node* root);
void display_tree(Node* nd);

#endif

任何提示都会很棒。

0 个答案:

没有答案