我有一个我无法在C

时间:2018-02-17 02:53:03

标签: c binary-search-tree

嘿,当我分配孩子时,我在某处出现了分段错误。在add

时,我的i==2方法中似乎特意在我的for循环中

编译代码时,您输入您的姓名,功能和"father('name of parent', 'name of child')"或母亲的相同格式。除了根孩子外,只有父母没有其他孩子。

更新:我将变量放在main中。

例如输入:

Brandon

add

father(a,Brandon)

预期结果:

father

a

Brandon

但它是:

father

a

代码:

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

//definition of whats in a node
typedef struct BST {
        char *data;
        int generation;
        struct BST *left;
        struct BST *right;
}node;

//starting nodes
node *root = NULL;

//method declarations
node *create(char[]);
node scan(node *, char[]);
void delete (char[]);
void preorder (node *);
void add (char[]);
void ridChild (node *);
void quit(void);

int main(){
        char name[12];
        char array[50];
        char option[12]; //options: add, remove or print
        char parent[12];

        node *temp;
        printf ("Please enter your name :");
        fgets(name, 12, stdin);
        name[strlen(name) -1] = '\0';
        temp = create(name); //create root
        root = temp;



        printf("Please specify whether to add or delete an entry, or print the tree\n");
        fgets(option, 12, stdin);
        option[strlen(option) -1] = '\0';

        if(strcmp(option,"quit")!=0) {
                do {
                        if(strcmp(option,"add")==0) {
                                add(name); //put the add method here
                        }
                        if(strcmp(option,"delete")==0) {
                                //put the delete method here
                                delete(array);
                        }
                        if(strcmp(option,"print")==0) {
                                preorder(root); //print method
                        }
                        printf("Please specify whether to add or delete an entry, or print the tree\n");
                        getchar();
                        fgets(option, 12, stdin);
                        option[strlen(option) -1] = '\0';
                } while (strcmp (option, "quit") != 0);
        }
        quit();
}

//methods
void quit(){
        exit(1);
}

node *create(char inputName[]){
        node *temp;
        temp = (node *) malloc (sizeof(node));
        temp->data = inputName;
        temp->left = temp->right = NULL;
        return temp;
}

void preorder (node * root){
        if (root != NULL) {
                printf ("%s\n", root->data);
                preorder (root->left);
                preorder (root->right);
        }
}

void add(char array[]){
        node *child = NULL, *parent = NULL;
        char *pch;
        printf ("Please specify a relation to add\n");
        fgets(array, 50, stdin); // store the command in array
        array[strlen(array) -1] = '\0';
        pch = strtok (array, "(,)");
        int parentdir = 2; //father = 0, mother = 1;
        for (int i = 0; pch != NULL && i < 3; i++) {
                if(i==0) {
                        if (strcmp (pch, "father") == 0) { //father
                                parentdir = 0;
                        }
                        if (strcmp (pch, "mother") == 0) { //mother
                                parentdir = 1;
                        }
                }
                else if(i==1) { //make parent
                        parent = create (pch);
                }
                else if (i==2) {
                        //where we assign
                        if (parentdir == 0) { //father
                                //find function to find the node and return
                                *child = scan(root, pch);
                                //assign left of the found node as the temp (father)
                                child->left = parent;
                        }
                        else if (parentdir == 1) { //mother
                                //find function to find the node and return
                                *child = scan(root, pch);
                                //assign left of the found node as the temp (mother)
                                child->right = parent;
                        }
                }
                printf ("%s\n", pch); //REFERENCE PRINTING
                pch = strtok (NULL, "(,)");
        }
}

void delete(char array[]){
        node *toDelete;
        //takes in name of node to be deleted
        //scan method to find the node to delete and deletes all of the children of the node first before deleting
        printf ("Please specify a name to delete\n");
        fgets(array, 50, stdin);
        array[strlen(array) -1] = '\0';
        *toDelete = scan(root, array); //return which node to delete
        //helper method here to go through and delete each children
        ridChild(toDelete);
}

void ridChild(node * trash){
        if(trash->left == NULL && trash->right == NULL) { //no parents
                free(trash);
        }
        else if(trash->left == NULL && trash->right != NULL) { //have mother
                ridChild(trash->right);
        }
        else if(trash->left != NULL && trash->right == NULL) { //have father
                ridChild(trash->left);
        }
        else if(trash->left != NULL && trash->right == NULL) { //have both
                ridChild(trash->left);
                ridChild(trash->right);
        }
}

node scan(node * temp, char inputName[]){
        //returns node that is searched for associated with one of parents
        if (temp != NULL) {
                if(strcmp(temp->data,inputName)==0) {
                        return *temp;
                } else {
                        scan (temp->left, inputName);
                        scan (temp->right, inputName);
                }
        }
        return *temp;
}

1 个答案:

答案 0 :(得分:0)

问题是扫描功能,我建议默认情况下应该返回一个空结构,为清楚起见。我在下面列了一些修改后的代码。

插入函数的开头:

node ret;

然后,不要总是在结尾返回* temp,而是使用以下内容来结束函数:

if (temp != NULL) {
    ret=*temp;
}

return ret;

此外,在add函数中还需要进行一些微小的更改,从现在定义的子项开始如下:

node child, *parent = NULL;

...

child = scan(root, pch);

child.left = parent;