c - 如何比较二叉搜索树的两个字符串

标签 c binary-tree binary-search-tree

我试图比较两个字符串,看看它作为根节点的叶节点应该去哪里,所以第四个。我一直尝试使用字符串比较,但出现错误。我对 c 中的二叉树非常不熟悉,需要插入节点的帮助。

这是我迄今为止遇到的错误代码:

#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 15

typedef struct treeNode{
  char string[MAXLEN+1];
  struct treeNode *left;
  struct treNode *right;
}treeNode;

treeNode * insert(treeNode *node, char s[MAXLEN+1]){
  if(node == NULL){
    treeNode *temp;
    temp = (treeNode *)malloc(sizeof(treeNode));
    strncpy(temp -> string, s, sizeof(treeNode));
    temp -> left = NULL;
    temp -> right = NULL;
    return temp;
  }

  else-if(strcmp(node->string,char s)>0){

  }
}

最佳答案

您的程序目前存在三个问题:

  1. “else-if”应为“else if”

  2. strcmp(node->string,char s) 应该是 strcmp(node->string, s)

  3. 没有主方法

关于c - 如何比较二叉搜索树的两个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28511976/

相关文章:

c - 将 C 字符串传递给 linux 命令行

c - recv() 总是返回 1

c - 内存竞技场和内存池有什么区别?

c++ - 二叉树包含其他二叉树

c - 无法从文件中打印字符

matlab - 如何在matlab中实现二叉树

data-structures - 二叉搜索树的定义中是否允许重复键?

algorithm - 将 BST 重建为 AVL

c - 如何编写一个递归函数来返回 BST 中的最小值?

c++ - 双线程二叉搜索树——保存父节点和祖父节点以便设置线程