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

#include "BST_ADT.h"


// Structure
typedef struct
    {
        char* name;
        char* market;
        char* initial;
        float stock;
    }COMPANY;

// Prototype Delarations
void addComp    (BST_TREE* list);
void deleteComp (BST_TREE* list);
void findComp   (BST_TREE* list);
void printList (BST_TREE* list);
int  compareComp (void* Comp1, void* Comp2);
void processComp (void* dataPtr);


int main (void)
{
// Local Definitions
   BST_TREE* list;

// Statements
   list = BST_Create(compareComp);
   addComp(list);
   deleteComp(list);
   findComp (list);
   printList(list);

    return 0;
}


/*===================== addComp =========================*/

void addComp    (BST_TREE* list)
{
// Local Declarations
   COMPANY* CompPtr;
   FILE* fp;
   char fileName[25];
   char buffer [100];
   char* tempString;
// Statements

   CompPtr = (COMPANY*)malloc (sizeof (COMPANY));
   CompPtr->name = (char*) malloc(128 * sizeof(char));
   CompPtr->market = (char*) malloc(128 * sizeof(char));
   CompPtr->initial = (char*) malloc(128 * sizeof(char));
   tempString = (char*) malloc(128 * sizeof(char));

   printf("Enter the file name: ");
           gets(fileName);
           fp = fopen(fileName, "r");
           if(fp == NULL)
           {
              printf("Error cannot open the file!\n");
              exit(101);
           }
   while(fgets(buffer, 100, fp) != NULL)
        {
            if (!CompPtr)
                printf("MEmory overflow!\n"), exit(101);
            strcpy(tempString, strchr(buffer, ';') + 2);
            CompPtr->name = strtok(buffer, ";");
            sscanf(tempString, "%15s   %15s    %f ", CompPtr->market, CompPtr->initial, &(CompPtr->stock));
            BST_Insert(list, CompPtr);
        } // end while

} //addComp

/*===================== deleteComp =========================*/

void deleteComp (BST_TREE* list)
{
// local definitions
   char name[100];
   char* namePtr = (char*) malloc(128 * sizeof(char));
   namePtr = name;
// statements
   printf("Enter Company name: ");
   scanf ("%39s", namePtr);
   if (!BST_Delete (list, namePtr))
       printf("ERROR: No Company: %0\n", *namePtr);
   else
       BST_Delete(list, namePtr);
} // deleteComp


/*===================== findComp =========================*/

void findComp   (BST_TREE* list)
{
// Local Definitions
   char initial[15];
   COMPANY* CompPtr;

// Statements
   printf("Enter Company initial: ");
   scanf("%s", initial);
   CompPtr = (COMPANY*)BST_Retrieve (list, &initial);
   if (CompPtr)
      {
        printf("Company name:  %s", initial);
        printf("market name:  %s", *CompPtr->market);
        printf("company's initial:  %s", *CompPtr->initial);
        printf("stock price is:  %f", CompPtr->stock);
      } // if
   else
       printf("Company %s not in the file\n", initial);



}

/*===================== printList =========================*/

void printList (BST_TREE* list)
{
// Statements
   printf("\nCOMPANY list:\n");
   BST_Traverse(list, processComp);
   printf("end of COMPANY list\n");
   return;
} // printList


/*===================== compareComp =========================*/

int  compareComp (void* Comp1, void* Comp2)
{
// Local definitions
   COMPANY s1;
   COMPANY s2;

// Statements
   s1 = *(COMPANY*)Comp1;
   s2 = *(COMPANY*)Comp2;

   if (s1.initial < s2.initial)
       return -1;
   if (s1.initial == s2.initial)
       return 0;
   return +1;
} // compareComp

/*===================== processComp =========================*/

void processComp (void* CompPtr)
{
// Local Definitions
    COMPANY aComp;

// Statements
    aComp = *(COMPANY*) CompPtr;
    printf("%20s  %20s  %20s  %4.1f\n", *aComp.name, aComp.market, aComp.initial, aComp.stock);

    return;
} // processComp

在这段代码中,我试图将文本文件中的数据插入二叉树。我能够将数据读入 CompPtr ,但是当我将它传递给 BST_Insert 函数时,没有任何内容被读入 BST 树。为什么数据不添加到 BST 树中?提前感谢您的帮助。

这是我的 BST_Insert ADT 函数

/*===================== BST_Insert =========================*/

bool BST_Insert (BST_TREE* tree, void* DATA)
{
NODE* newPtr;

newPtr = (NODE*)malloc(sizeof(NODE));
if (!newPtr)
return false;

newPtr->right = NULL;
newPtr->left = NULL;
newPtr->dataPtr = DATA;

if (tree->count == 0)
tree->root = newPtr;
else
_insert(tree, tree->root, newPtr);

(tree->count)++;
return true;
}

/*===================== _insert =========================*/

NODE* _insert (BST_TREE* tree, NODE* root, NODE* newPtr)
{

if (!root)
return newPtr;
if (tree->compare(newPtr->dataPtr,
root->dataPtr) < 0)
{
root->left = _insert(tree, root->left, newPtr);
return root;
}
else
{
root->right = _insert(tree, root->right, newPtr);
return root;
}
return root;
} 

经过一段时间的调试,我意识到每次比较函数运行,它返回0,由于两个参数相等,因此_insert函数只将数据添加到树的右侧而不是左侧。我想什么比较函数正在做的是比较输入的相同值,我想要的是比较函数比较根值和我要添加的值,以确定数据应该添加到哪一侧。我应该如何解决这个问题?对不起,如果这段代码太长,我也把它发布在代码审查上,但由于代码不完整,在这里发布似乎更合适。

4

0 回答 0