简体   繁体   中英

Creating a simple Binary Search Tree

I've been trying to create a binary search tree that sorts in alphabetical order according to their "company" name out of these structs with recursion for quite a while now. The movement through the structs to get the pointers to point to the right spots is confusing me a lot.

I'm getting these errors:

gcc -c tree.c

tree.c: In function 'treeInsert':

tree.c:34:11: error: request for member 'entryPtr' in something not a structure or union

tree.c:36:55: error: request for member 'entryPtr' in something not a structure or union

tree.c:38:12: error: request for member 'right' in something not a structure or union

tree.c:39:35: error: request for member 'right' in something not a structure or union

tree.c:43:12: error: request for member 'left' in something not a structure or union

tree.c:44:35: error: request for member 'left' in something not a structure or union

tree.c:47:3: warning: passing argument 1 of 'printTree' from incompatible pointer type [enabled by default]

In file included from tree.c:19:0: tree.h:36:6: note: expected 'struct treeNode *' but argument is of type 'struct treeNode ' make: * [tree.o] Error 1

Here are the structs:

typedef struct companyEntryTag{
char * companyName;
char * companyDescription;
char * website;
char * streetAddr;
char * suiteNumber;
char * city;
char * state;
int zip;
double latitude;
double longitude;
} companyEntry;

typedef struct treeNodeTag{
companyEntry * entryPtr;
struct treeNodeTag * left;
struct treeNodeTag * right;
} treeNode;

typedef struct listNodeTag{
companyEntry * entryPtr;
struct listNodeTag * next;
} listNode;

I've tried a bunch of different solutions, but here is my current function I'm using to try to do this, that got the above error:

int treeInsert(listNode * list, treeNode ** rootPtr)
{
  if(list == NULL){return -1;}

  //Make the root next point to what list is
  *rootPtr->entryPtr = list->entryPtr;

  if(strcmp(list->next->entryPtr->companyName, rootPtr->entryPtr->companyName)==1)
  {
    rootPtr->right->entryPtr = list->next;
    treeInsert(list->next, rootPtr->right->entryPtr);
  } 
  else
  {
    rootPtr->left->entryPtr = list->next;
    treeInsert(list->next, rootPtr->left->entryPtr);
  }

  printTree(rootPtr);

  return 0;
}

I'm not perfectly sure of what the errors are telling me now that I have tried all these different ways of implementing the function. I'm all jumbled up and would love some help!

It should be (*rootPtr)->entryPtr , not *rootPtr->entryPtr or rootPtr->entryPtr .

Due to operator precedence, *rootPtr->entryPtr is indeed *(rootPtr->entryPtr) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM