Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在二元搜索树中打印字符串的有序3字谜_C_String_Binary Search Tree - Fatal编程技术网

C 在二元搜索树中打印字符串的有序3字谜

C 在二元搜索树中打印字符串的有序3字谜,c,string,binary-search-tree,C,String,Binary Search Tree,C语言中的这段代码输入一个整数和一个字符串,并应按字典顺序输出字符串中3个连续字母的所有字谜 EXAMPLE: Input 3 mississippi Output ipp iss iss mis ppi sip sis ssi ssi 它没有,相反,它只给出最后3克重复n次,n=可能的3克数 我用了一个字符串的二叉搜索树来记忆每个字谜,我不能用一个简单的字符串数组,因为这个练习需要这样做。要按顺序打印字符串,请进行simmetric访问(从左到右) 我做了一些调试测试,我认为问题在于插入函数

C语言中的这段代码输入一个整数和一个字符串,并应按字典顺序输出字符串中3个连续字母的所有字谜

EXAMPLE:
Input
3
mississippi
Output
ipp
iss
iss
mis
ppi
sip
sis
ssi
ssi
它没有,相反,它只给出最后3克重复n次,n=可能的3克数

我用了一个字符串的二叉搜索树来记忆每个字谜,我不能用一个简单的字符串数组,因为这个练习需要这样做。要按顺序打印字符串,请进行simmetric访问(从左到右)

我做了一些调试测试,我认为问题在于插入函数,可能是我弄乱了穿孔器

如果有人想看一看,我将不胜感激

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

typedef struct nodo
{
  char * key;
  int dim;
  struct nodo * left;
  struct nodo * right;
} Nodo;
typedef Nodo* albero;


/*int dimensions (albero t)
{
  int d;
  if (t==NULL) return 0;
  else d=(1+dimensions(t->left)+dimensions(t->right));
  t->dim=d;
  return d;
}*/

simmetrica(albero t,int z)
{
  if (t!=NULL)
  {
  simmetrica(t->left,z);
  /*if ((t->dim)>=z)*/ printf("%s\n",t->key);
  simmetrica(t->right,z);
  }
}

Nodo* insert(albero t, char* key)
{
  Nodo* new = malloc(sizeof(Nodo));
  new->key=key;
  new->dim=0;
  new->right=NULL;
  new->left=NULL;
  if(t==NULL)
    {
      return new;
    }
  Nodo* parent;
  Nodo* current=t;
  while(current!=NULL)
    {
      parent=current;
      if/*(current->key<key)*/(strcmp(current->key,key)<0)
        {
          current=current->right;
        }
      else
        {
          current=current->left;
        }
    }
  if/*(parent->key<key)*/(strcmp(parent->key,key)<0)
    {
      parent->right=new;
    }
  else parent->left=new;
  return t;
}




int main()
{
  int z;
  char s[501];
  char trip[4];
  albero t=malloc(sizeof(Nodo));
  t=NULL;
  scanf("%d", &z);
  scanf("%s", s);
  int o=0;
  while (s[o+2] !='\0')
  { //printf("debug %d\n",o);
    trip[0]=s[o];
    trip[1]=s[o+1];
    trip[2]=s[o+2];
    trip[3]='\0';
    //printf("trip %s\n",trip);
    t=insert(t, trip);
    o++;
  }
  //dimensions(t);
  simmetrica(t,z);
  return 0;
}
#包括
#包括
#包括
#包括
类型定义结构节点
{
字符*键;
int-dim;
结构nodo*左;
结构nodo*右;
}野藤;
类型def Nodo*albero;
/*内部尺寸(阿尔贝罗t)
{
int d;
如果(t==NULL)返回0;
否则d=(1+维度(t->左)+维度(t->右));
t->dim=d;
返回d;
}*/
simmetrica(阿尔贝罗t,内特z)
{
如果(t!=NULL)
{
simmetrica(t->左,z);
/*如果((t->dim)>=z)*/printf(“%s\n”,t->key);
simmetrica(t->右,z);
}
}
Nodo*插入(albero t,char*键)
{
Nodo*new=malloc(sizeof(Nodo));
新建->键=键;
新建->尺寸=0;
新建->右=空;
新建->左=空;
如果(t==NULL)
{
归还新的;
}
Nodo*父母;
Nodo*电流=t;
while(当前!=NULL)
{
父项=当前;
如果/*(当前->键,键)正确;
}
其他的
{
当前=当前->左侧;
}
}
如果/*(父->键,键)右=新;
}
else parent->left=新建;
返回t;
}
int main()
{
intz;
chars[501];
炭跳闸[4];
阿尔贝罗t=malloc(sizeof(Nodo));
t=零;
scanf(“%d”、&z);
scanf(“%s”,s);
INTO=0;
而(s[o+2]!='\0')
{//printf(“调试%d\n”,o);
跳闸[0]=s[o];
跳闸[1]=s[o+1];
跳闸[2]=s[o+2];
跳闸[3]='\0';
//printf(“行程%s\n”,行程);
t=插入(t,跳闸);
o++;
}
//尺寸(t);
simmetrica(t,z);
返回0;
}

这一切都与存储有关<代码>行程已分配存储空间。它存在于某个地址,让我们假装它的
0x401fc
。执行插入时,将地址
0x401fc
传递到
insert
。Insert创建类型为
struct nodo
的新记录,并将地址
0x401fc
复制到字段
key

然后继续并更改
trip
中的值,该值更改
0x401fc
处的值,现在所有
struct nodo
都指向新值


执行
new->key=strdup(key)
或使用
struct nodo
使用
char键[4]并执行
strcpy()
。这两种解决方案都会创建新的存储。第一个在短期内是一个更简单的修复,但需要处理更多的工作(因为它与结构分开存储)。第二种形式需要更多的初始更改,但会使取消分配内存变得更容易。

太好了!我刚刚更改了
new->key=key带有
新建->键=strdup(键)现在它似乎工作得很好,正如我所期望的,我只是有noob穿刺器技能,谢谢!嗯,是的,我不想让这听起来像是一个借口,但你可能已经理解了,我不是以英语为母语的。