无法在二叉树中打印出数据

时间:2015-06-19 23:00:59

标签: c debugging binary-search-tree

实际上,我在这里请你帮忙调试。所以,如果你没有兴趣帮我调试,你可以在这里停下来。但是,我相信我的代码只包含一个小bug,因为我测试了它的大部分内容。

我自己做了一点测试。当我注释掉函数 pinrtOneNode()时,代码成功运行,这意味着我只将数据输入到该二叉树中。因此,唯一的错误可能是 printOnrNode()函数。

此外,编译时错误是

c:\users\...\Local\temp\ccwCUbY.oprompt
c:\users\...\.../bst.c:45printOneNode

collect2.exe:error:ld returned 1 exit status

顺便说一下,bst.c是我的源代码

所以这是代码:

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

typedef struct BSTnode{
    struct BSTnode* leftchild;
    struct BSTnode* rightchild;
    char data[20];
}BSTnode;

void prompt();        //this function always prompt the user for selection everytime 
                    //after he input data( inputData() ) or print data( printOneNode() )

void inputData();
void printOneNode();

BSTnode firstNode;
BSTnode* MyNode=&firstNode;   //I don't know if it's good to initialize it this way

int main()
{
   //MyNode=malloc(sizeof(BSTnode));
   MyNode->leftchild=NULL;
   MyNode->rightchild=NULL;
   strcpy(MyNode->data,"");
   while(1)
      prompt();        // I use a while loop to prompt user for selection continuously

  return 0;
}

/* this function is used to prompt user for selection input*/
void prompt(){
   int i=0;     
   printf("Please select:\n");
   printf("1.input a data\n2.print only one data\n3.Exit\n");

   scanf("%d",&i);
   switch(i){
     case 1:
         inputData();
         break;
     case 2:
         printOneNode();
         break;
     case 3:
         exit(0);
     default:
         printf("Please input a valid number!(1-3)");
   }
 }

/* this is used to input data into the binary tree*/
void inputData(){
   char data[20]={0};
   printf("Input your data here(character only / less than 19 characters!): ");
   scanf("%s",data);
   BSTnode** ptr=&MyNode;
   while(1){
      if(strcmp(data,(*ptr)->data)){
         if((*ptr)->rightchild!=NULL){
                ptr=&((*ptr)->rightchild);
                continue;
           }
         else{
               (*ptr)->rightchild=malloc(sizeof(BSTnode));
               ptr=&((*ptr)->rightchild);
               (*ptr)->rightchild=NULL;
               (*ptr)->leftchild=NULL;
               strcpy((*ptr)->data,data);
               break;
             }
         }

    else{
        if((*ptr)->leftchild!=NULL){
            ptr=&((*ptr)->leftchild);
            continue;
        }
        else{
            (*ptr)->leftchild=malloc(sizeof(BSTnode));
            ptr=&((*ptr)->leftchild);
            (*ptr)->leftchild=NULL;
            (*ptr)->rightchild=NULL;
            strcpy((*ptr)->data,data);
            break;
         }
     }
 }

 printf(" Your data have been input successfully!\n");
 return;
}

/* this is used to print data, only one at a time*/
void PrintOneNode(){
    BSTnode** ptr=&MyNode;
    printf("The first node is%s\n",(*ptr)->data);
    while(1){
        printf("Select which side of node do you want to print now(l/r)?(q for quit) ");
//I ask user whether he wanna print out the data in node in the right side
// of the current node or left side

        char a;
        scanf("%c",&a);
        switch(a){
            case 'l':
                if((*ptr)->leftchild!=NULL){
                    ptr=&((*ptr)->leftchild);
                    printf("\t%s\n",(*ptr)->data);
                    }
                else
                    printf("There is no more leftchild");
                break;

           case 'r':

                if((*ptr)->rightchild!=NULL){
                    ptr=&((*ptr)->rightchild);
                    printf("\t%s\n",(*ptr)->data);
                }
                else
                    printf("There is no more rightchild!");
                break;

           case 'q':
              return;
           default:
              return;
      }
   }
}

请帮忙。除了bug之外,对这段代码的任何改进都会受到很多赞赏。谢谢。

0 个答案:

没有答案