为什么树形图功能失败? [英] Why does the treeprint function fail?

查看:75
本文介绍了为什么树形图功能失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个尴尬的问题,但如果我不问我永远不会知道发生了什么,那么感谢您的帮助。我正在考虑在K& R(二叉树)中练习6-2。它要求我编写一个读取C程序的程序,并按字母顺序打印每组变量名称,这些变量名称在第一个X字符中相同,但之后在某处不同。我的方法是定义一个包含单词数组而不只是一个单词的结构(如本书中给出的示例),因此具有相同X字符的单词可以存储在同一节点中。所以我尝试在解决整个问题之前测试这个概念。但是当我尝试运行以下代码时,虽然addtree函数似乎有效(根据调试器),但树形图却没有。它会在崩溃之前打印具有相同前3个字符的变量(分段错误,因此它不会打印其他节点中的单词)。不确定发生了什么。谢谢!



顺便说一下,我发现二叉树是一个非常有趣的概念!



我尝试过:



 struct tnode * addtree(struct tnode * p,char * w){
int cond;

if(p == NULL){
p = talloc();
p-> np = p-> word;
*(p-> np)= strdup(w);
p-> count = 1;
p-> left = p-> right = NULL;
}
else if((cond = strncmp(w,*(p-> np),3))== 0){
(p-> np)++;
*(p-> np)= strdup(w);
p-> count ++;
}
else if(cond< 0)
p-> left = addtree(p-> left,w);
else
p-> right = addtree(p-> right,w);
返回p;
}

void treeprint(struct tnode * p){
if(p!= NULL){
treeprint(p-> left);
for(int i = 0;(p-> word [i])!=; i ++)
printf(%4d%s \ n,p-> count,对 - >字[I]);
treeprint(p-> right);
}
}

解决方案

当你不明白你的代码在做什么或为什么它做什么确实如此,答案是调试器

使用调试器查看代码正在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里显示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。如果代码没有达到预期的效果,那么你就接近了一个错误。


如果不知道 struct tnode,就无法详细解答这个问题。 code>和 talloc()功能。



一些可疑指示:

 if(p == NULL){
/ *什么是talloc()在做什么? * /
/ *确保它分配内存以保存struct tnode * /
p = talloc();刚刚分配了
/ * p。 * /
/ *如果没有由talloc()设置,那么p->字是未定义的。 * /
p-> np = p-> word;
*(p-> np)= strdup(w);
p-> count = 1;
p-> left = p-> right = NULL;如果没有由talloc()设置,
/ * p->字是未定义的。 * /
}
else if((cond = strncmp(w,*(p-> np),3))== 0){
/ *这点在分配的内存后面p-> count是> = * /
/ * p-> word数组的大小* /
(p-> np)++;
*(p-> np)= strdup(w);
p-> count ++;
}
/ * ... * /

/ *将字符串指针与指向空字符串的指针进行比较,该字符串为* /
/ *通常始终为false (指针永远不会有相同的值)。 * /
/ *检查与NULL比较的字符串列表的结尾。 * /
/ *但这需要使用NULL指针初始化列表。 * /
/ *我想使用p-> count是你想在这里使用的。 * /
for(int i = 0;(p-> word [i])!=; i ++)
printf(%4d%s \ n,p->算,对 - >字[I]);


This is an awkward question, but if I don't ask I will never know what's going on, so thanks for your help in advance. I'm attempting exercise 6-2 in K&R (binary tree). It requires me to write a program that reads a C program and prints in alphabetical order each group of variable names that are identical in the first 'X' characters, but different somewhere thereafter. My approach is to define a struct that holds an array of words rather than just one word (as the example given in the book), so words that have the same 'X' characters can be stored in the same node. So I tried testing this concept before address the entire. But when I try running the following code, while the addtree function seems to work (according to the debugger), the treeprint doesn't. It prints the variables that have the same first 3 characters before crashing (segmentation error, so it doesn't print the words in the other nodes). Not really sure what's going on. Thanks!

By the way, I find the binary tree a really intriguing concept!

What I have tried:

struct tnode *addtree(struct tnode *p, char *w){
	int cond;
	
	if(p==NULL){
		p=talloc();
		p->np=p->word;
		*(p->np)=strdup(w);
		p->count=1;
		p->left=p->right=NULL;
	}
	else if((cond=strncmp(w,*(p->np),3))==0){
		(p->np)++;
		*(p->np)=strdup(w);
	    p->count++;
	}
	else if(cond<0)
		p->left=addtree(p->left,w);
	else 
		p->right=addtree(p->right,w);
	return p;
}

void treeprint(struct tnode *p){
	if(p!=NULL){
		treeprint(p->left);
		for(int i=0;(p->word[i])!="";i++)
			printf("%4d %s\n",p->count,p->word[i]);
		treeprint(p->right);
	}
}

解决方案

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


This can't be answered in detail without knowing the struct tnode and the talloc() function.

Some suspicious instructions:

if(p==NULL){
    /* What is talloc() doing? */
    /* Ensure that it allocates memory to hold a struct tnode */
    p=talloc();
    /* p has just been allocated. */
    /* So p->word is undefined if not set by talloc(). */
    p->np=p->word;
    *(p->np)=strdup(w);
    p->count=1;
    p->left=p->right=NULL;
    /* p->word is undefined if not set by talloc(). */
}
else if((cond=strncmp(w,*(p->np),3))==0){
    /* This points behind allocated memory when p->count is >= */
    /*  the size of the p->word array */
    (p->np)++;
    *(p->np)=strdup(w);
    p->count++;
}
/* ... */

/* Compares a string pointer with a pointer to an empty string which is */
/*  usually always false (pointers have never the same value). */
/* To check for an end of a list of strings compare with NULL. */
/* But this requires that the list is initialised with NULL pointers. */
/* I guess that using p->count is what you want to use here. */
for(int i=0;(p->word[i])!="";i++)
    printf("%4d %s\n",p->count,p->word[i]);


这篇关于为什么树形图功能失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆