分段错误 - 显示树
调用 viewTree(root); 时出现段错误
struct treeElement {
unsigned long weight;
unsigned short id;
char chr;
struct treeElement *lchild, *rchild, *parent;
};
typedef struct treeElement node;
node *root;
//INITIALIZE TREE
void initTree() {
root = malloc(sizeof(node));
currentNYT = root;
} //initTree
//VIEW TREE
void viewTree(node *tree) {
printf("%5d%5d%5d%5d%5c%lu", tree->id, tree->parent->id, tree->lchild->id, tree->rchild->id, tree->chr, tree->weight);
viewTree(tree->lchild);
viewTree(tree->rchild);
}
//ADD NODE
void addNode(char newNodeChr) {
node *newNYT, *newExternal;
newNYT = malloc(sizeof(node));
newNYT->id=maxNodes-idCount; idCount++;
newNYT->chr='\0';
newNYT->weight=0;
newNYT->parent=currentNYT;
newNYT->lchild=newNYT->rchild=NULL;
newExternal = malloc(sizeof(node));
newExternal->id=maxNodes-idCount;
newExternal->chr=newNodeChr;
newExternal->weight=1;
newExternal->parent=currentNYT;
newExternal->lchild=newExternal->rchild=NULL;
currentNYT->lchild = newNYT;
currentNYT->rchild = newExternal;
currentNYT=newNYT;
} //addNode
int main()
{
initTree();
addNode('a');
addNode('b');
viewTree(root);
getchar();
return 0;
}
I get a segfault when calling viewTree(root);
struct treeElement {
unsigned long weight;
unsigned short id;
char chr;
struct treeElement *lchild, *rchild, *parent;
};
typedef struct treeElement node;
node *root;
//INITIALIZE TREE
void initTree() {
root = malloc(sizeof(node));
currentNYT = root;
} //initTree
//VIEW TREE
void viewTree(node *tree) {
printf("%5d%5d%5d%5d%5c%lu", tree->id, tree->parent->id, tree->lchild->id, tree->rchild->id, tree->chr, tree->weight);
viewTree(tree->lchild);
viewTree(tree->rchild);
}
//ADD NODE
void addNode(char newNodeChr) {
node *newNYT, *newExternal;
newNYT = malloc(sizeof(node));
newNYT->id=maxNodes-idCount; idCount++;
newNYT->chr='\0';
newNYT->weight=0;
newNYT->parent=currentNYT;
newNYT->lchild=newNYT->rchild=NULL;
newExternal = malloc(sizeof(node));
newExternal->id=maxNodes-idCount;
newExternal->chr=newNodeChr;
newExternal->weight=1;
newExternal->parent=currentNYT;
newExternal->lchild=newExternal->rchild=NULL;
currentNYT->lchild = newNYT;
currentNYT->rchild = newExternal;
currentNYT=newNYT;
} //addNode
int main()
{
initTree();
addNode('a');
addNode('b');
viewTree(root);
getchar();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根节点有父节点吗?子叶节点是否有左子节点和右子节点?
我认为您的大部分问题在于您的
printf
语句 - 在尝试打印它们的id
s。在其中添加一些if
语句,看看是否有帮助。Does the root node have a parent? Do the child leaf nodes have left and right children?
I think most of your problem lies in your
printf
statement - you don't check whether or not any of the objects you're accessing actually exist before you try to print theirid
s. Add someif
statements in there and see if it helps.在您的
viewTree(node *tree)
中,您没有检查tree
是否为null
。当您在tree
为null
时尝试访问tree->id
时,就会出现段错误。最终将在递归调用中为子树传递
null
。编辑:通常,每次需要访问对象的成员时,您都会检查
null
。因此,在读取tree->id
之前使用tree != null
,在读取tree- 之前使用
。tree->lchild != null
。必须确保>lchild->idIn your
viewTree(node *tree)
you are not checking iftree
isnull
or not. Definite recipe for segfault when you try to accesstree->id
whentree
isnull
.null
will be passed for a subtree in a recursive call eventually.EDIT: In general you have check for
null
every time you need to access a member of an object. So,tree != null
before readingtree->id
andtree->lchild != null
before readingtree->lchild->id
must be ensured.不要只分配根节点,还要初始化它,特别是指向兄弟节点和父节点的指针(将它们设置为 NULL)。添加节点时使用未初始化的指针。
Don't just allocate the root node, but initialize it, especially the pointers to siblings and to parent (set them to NULL). You are using the uninitialized pointers when adding nodes.