简体   繁体   English

创建一个简单的二进制搜索树

[英]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 gcc -c tree.c

tree.c: In function 'treeInsert': tree.c:在函数“ treeInsert”中:

tree.c:34:11: error: request for member 'entryPtr' in something not a structure or union tree.c:34:11:错误:请求成员“ entryPtr”的原因不是结构或联合

tree.c:36:55: error: request for member 'entryPtr' in something not a structure or union tree.c:36:55:错误:在非结构或联合中请求成员“ entryPtr”

tree.c:38:12: error: request for member 'right' in something not a structure or union tree.c:38:12:错误:请求成员“正确”的东西不是结构或联合

tree.c:39:35: error: request for member 'right' in something not a structure or union tree.c:39:35:错误:在非结构或联合中请求成员“正确”

tree.c:43:12: error: request for member 'left' in something not a structure or union tree.c:43:12:错误:请求成员“ left”的内容不是结构或联合

tree.c:44:35: error: request for member 'left' in something not a structure or union tree.c:44:35:错误:请求成员“ left”的内容不是结构或联合

tree.c:47:3: warning: passing argument 1 of 'printTree' from incompatible pointer type [enabled by default] tree.c:47:3:警告:从不兼容的指针类型传递'printTree'的参数1 [默认启用]

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 在tree.c:19:0:tree.h:36:6包含的文件中:注意:预期为'struct treeNode *',但参数的类型为'struct treeNode'make :* [tree.o]错误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 . 它应该是(*rootPtr)->entryPtr ,而不是*rootPtr->entryPtrrootPtr->entryPtr

Due to operator precedence, *rootPtr->entryPtr is indeed *(rootPtr->entryPtr) . 由于运算符的优先级, *rootPtr->entryPtr实际上是*(rootPtr->entryPtr)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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