我正在尝试从头开始构建一个BST来完成一项任务,而我所拥有的只有几个月的经验C.坚持添加新节点,显然Root每次都被替换。这里有几个片段。
结构:
typedef struct node
{
int Num;
struct node * left; /* Points to the left child*/
struct node * right; /*Points to the right child*/
struct node *parent;
}Node;
typedef struct tree
{
Node * root; /*Points to the root*/
int height;
}Tree;
功能:
void AddNode(int num, Tree * tree){
Node NewNode;
NewNode.Num=num;
NewNode.left=NULL;
NewNode.right=NULL;
NewNode.parent=NULL;
Compare(tree,&NewNode,tree->root);
}
void Compare(Tree * tree,Node * newNode,Node * oldNode)
{
if (oldNode == NULL)
{
oldNode=newNode;
printf("hi1");
}
else if (newNode->Num<oldNode->Num)
{
printf("hi2");
if (oldNode->left==NULL)
{
oldNode->left=newNode;
oldNode->left->parent=oldNode;
}
else
Compare(tree,newNode,oldNode->left);
}
else if (newNode->Num>oldNode->Num)
{
printf("hi3");
if (oldNode->right==NULL)
{
oldNode->right=newNode;
oldNode->right->parent=oldNode;
}
else
Compare(tree,newNode,oldNode->right);
}
}
我有一些带有一些数字的数组。一开始我初始化了树。 printfs用于调试。输出是:
Tree Initialized
hi1hi1hi1hi1
这必须意味着每次都要更换根。我试着带走
if (oldNode == NULL)
{
oldNode=newNode;
printf("hi1");
}
并在调用AddNode方法之前将根作为数组[0],但程序停止执行。有什么想法吗?
答案 0 :(得分:3)
主要问题在于AddNode()
:
void AddNode(int num, Tree * tree)
{
Node NewNode;
NewNode.Num=num;
NewNode.left=NULL;
NewNode.right=NULL;
NewNode.parent=NULL;
Compare(tree,&NewNode,tree->root);
}
从函数返回时,NewNode
的存储空间将在下次调用时重复使用。您需要动态分配新节点:
void AddNode(int num, Tree * tree)
{
Node *NewNode = malloc(sizeof(*NewNode));
if (NewNode == 0)
abort();
NewNode->Num=num;
NewNode->left=NULL;
NewNode->right=NULL;
NewNode->parent=NULL;
Compare(tree, NewNode, tree->root);
}
还有其他问题 - 仍然在努力解决这些问题。如果你展示代码(a)填充BST和(b)打印BST的内容和(c)释放BST使用的存储,将会有所帮助。如果您没有项目(b),则无法调试自己的结构。
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int Num;
struct node *left; /* Points to the left child */
struct node *right; /* Points to the right child */
struct node *parent;
} Node;
typedef struct tree
{
Node *root;
int height;
} Tree;
static void Compare(Node *newNode, Node **oldNode)
{
if (*oldNode == NULL)
{
*oldNode = newNode;
printf("hi1\n");
}
else if (newNode->Num < (*oldNode)->Num)
{
printf("hi2\n");
if ((*oldNode)->left==NULL)
{
(*oldNode)->left=newNode;
(*oldNode)->left->parent = *oldNode;
}
else
Compare(newNode, &(*oldNode)->left);
}
else if (newNode->Num > (*oldNode)->Num)
{
printf("hi3\n");
if ((*oldNode)->right==NULL)
{
(*oldNode)->right=newNode;
(*oldNode)->right->parent = *oldNode;
}
else
Compare(newNode, &(*oldNode)->right);
}
}
static void AddNode(int num, Tree *tree)
{
assert(tree != 0);
Node *NewNode = malloc(sizeof(*NewNode));
if (NewNode == 0)
abort();
NewNode->Num = num;
NewNode->left = NULL;
NewNode->right = NULL;
NewNode->parent = NULL;
Compare(NewNode, &tree->root);
}
static void DumpNode(const Node *node)
{
assert(node != 0);
printf("Number: %4d - Node: 0x%.9" PRIXPTR " (parent 0x%.9" PRIXPTR ", left 0x%.9" PRIXPTR ", right 0x%.9" PRIXPTR ")\n",
node->Num, (uintptr_t)node, (uintptr_t)node->parent, (uintptr_t)node->left, (uintptr_t)node->right);
if (node->left != 0)
DumpNode(node->left);
if (node->right != 0)
DumpNode(node->right);
}
static void DumpTree(const char *tag, const Tree *tree)
{
assert(tag != 0 && tree != 0);
printf("BST Dump: %s\n", tag);
printf("Tree: 0x%.9" PRIXPTR " (root 0x%.9" PRIXPTR ", height %d)\n",
(uintptr_t)tree, (uintptr_t)tree->root, tree->height);
if (tree->root != 0)
DumpNode(tree->root);
}
static void FreeNode(Node *node)
{
assert(node != 0);
if (node->left != 0)
{
FreeNode(node->left);
node->left = 0;
}
if (node->right != 0)
{
FreeNode(node->right);
node->right = 0;
}
node->parent = 0;
node->Num = 0;
free(node);
}
static void FreeTree(Tree *tree)
{
assert(tree != 0);
if (tree->root != 0)
FreeNode(tree->root);
}
int main(void)
{
char buffer[32];
int list[] = { 19, 7, 12, 15, 21, 8, 16, 1, 3, 5 };
enum { LIST_SIZE = sizeof(list) / sizeof(list[0]) };
Tree tree = { 0, 0 };
DumpTree(buffer, &tree);
for (int i = 0; i < LIST_SIZE; i++)
{
AddNode(list[i], &tree);
snprintf(buffer, sizeof(buffer), "After %2d: value %2d", i, list[i]);
DumpTree(buffer, &tree);
}
FreeTree(&tree);
return 0;
}
主要的变化是Compare()
函数不需要tree
参数,并且需要Node **oldNode
参数。 Compare()
的其余部分有相应的变化。 AddNode()
中的代码几乎与最初的建议相同;当然,差异在于对Compare()
的调用。
添加了DumpTree()
和FreeTree()
个功能。我的64位计算机上需要0x%.9X
格式,其中堆地址为0x100000000及以上。有了这些和简单的main()
,我使用valgrind
运行的输出是:
==48994== Memcheck, a memory error detector
==48994== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==48994== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==48994== Command: ./bst
==48994==
==48994== Conditional jump or move depends on uninitialised value(s)
==48994== at 0xE567: strlen (mc_replace_strmem.c:407)
==48994== by 0x1778C2: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==48994== by 0x17618D: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==48994== by 0x17F2CF: printf (in /usr/lib/system/libsystem_c.dylib)
==48994== by 0x100001AFE: main (bst.c:78)
==48994==
BST Dump:
Tree: 0x7FFF5FBFF410 (root 0x000000000, height 0)
hi1
BST Dump: After 0: value 19
Tree: 0x7FFF5FBFF410 (root 0x100009170, height 0)
Number: 19 - Node: 0x100009170 (parent 0x000000000, left 0x000000000, right 0x000000000)
hi2
BST Dump: After 1: value 7
Tree: 0x7FFF5FBFF410 (root 0x100009170, height 0)
Number: 19 - Node: 0x100009170 (parent 0x000000000, left 0x1000091D0, right 0x000000000)
Number: 7 - Node: 0x1000091D0 (parent 0x100009170, left 0x000000000, right 0x000000000)
hi2
hi3
BST Dump: After 2: value 12
Tree: 0x7FFF5FBFF410 (root 0x100009170, height 0)
Number: 19 - Node: 0x100009170 (parent 0x000000000, left 0x1000091D0, right 0x000000000)
Number: 7 - Node: 0x1000091D0 (parent 0x100009170, left 0x000000000, right 0x100009230)
Number: 12 - Node: 0x100009230 (parent 0x1000091D0, left 0x000000000, right 0x000000000)
hi2
hi3
hi3
BST Dump: After 3: value 15
Tree: 0x7FFF5FBFF410 (root 0x100009170, height 0)
Number: 19 - Node: 0x100009170 (parent 0x000000000, left 0x1000091D0, right 0x000000000)
Number: 7 - Node: 0x1000091D0 (parent 0x100009170, left 0x000000000, right 0x100009230)
Number: 12 - Node: 0x100009230 (parent 0x1000091D0, left 0x000000000, right 0x100009290)
Number: 15 - Node: 0x100009290 (parent 0x100009230, left 0x000000000, right 0x000000000)
hi3
BST Dump: After 4: value 21
Tree: 0x7FFF5FBFF410 (root 0x100009170, height 0)
Number: 19 - Node: 0x100009170 (parent 0x000000000, left 0x1000091D0, right 0x1000092F0)
Number: 7 - Node: 0x1000091D0 (parent 0x100009170, left 0x000000000, right 0x100009230)
Number: 12 - Node: 0x100009230 (parent 0x1000091D0, left 0x000000000, right 0x100009290)
Number: 15 - Node: 0x100009290 (parent 0x100009230, left 0x000000000, right 0x000000000)
Number: 21 - Node: 0x1000092F0 (parent 0x100009170, left 0x000000000, right 0x000000000)
hi2
hi3
hi2
BST Dump: After 5: value 8
Tree: 0x7FFF5FBFF410 (root 0x100009170, height 0)
Number: 19 - Node: 0x100009170 (parent 0x000000000, left 0x1000091D0, right 0x1000092F0)
Number: 7 - Node: 0x1000091D0 (parent 0x100009170, left 0x000000000, right 0x100009230)
Number: 12 - Node: 0x100009230 (parent 0x1000091D0, left 0x100009350, right 0x100009290)
Number: 8 - Node: 0x100009350 (parent 0x100009230, left 0x000000000, right 0x000000000)
Number: 15 - Node: 0x100009290 (parent 0x100009230, left 0x000000000, right 0x000000000)
Number: 21 - Node: 0x1000092F0 (parent 0x100009170, left 0x000000000, right 0x000000000)
hi2
hi3
hi3
hi3
BST Dump: After 6: value 16
Tree: 0x7FFF5FBFF410 (root 0x100009170, height 0)
Number: 19 - Node: 0x100009170 (parent 0x000000000, left 0x1000091D0, right 0x1000092F0)
Number: 7 - Node: 0x1000091D0 (parent 0x100009170, left 0x000000000, right 0x100009230)
Number: 12 - Node: 0x100009230 (parent 0x1000091D0, left 0x100009350, right 0x100009290)
Number: 8 - Node: 0x100009350 (parent 0x100009230, left 0x000000000, right 0x000000000)
Number: 15 - Node: 0x100009290 (parent 0x100009230, left 0x000000000, right 0x1000093B0)
Number: 16 - Node: 0x1000093B0 (parent 0x100009290, left 0x000000000, right 0x000000000)
Number: 21 - Node: 0x1000092F0 (parent 0x100009170, left 0x000000000, right 0x000000000)
hi2
hi2
BST Dump: After 7: value 1
Tree: 0x7FFF5FBFF410 (root 0x100009170, height 0)
Number: 19 - Node: 0x100009170 (parent 0x000000000, left 0x1000091D0, right 0x1000092F0)
Number: 7 - Node: 0x1000091D0 (parent 0x100009170, left 0x100009410, right 0x100009230)
Number: 1 - Node: 0x100009410 (parent 0x1000091D0, left 0x000000000, right 0x000000000)
Number: 12 - Node: 0x100009230 (parent 0x1000091D0, left 0x100009350, right 0x100009290)
Number: 8 - Node: 0x100009350 (parent 0x100009230, left 0x000000000, right 0x000000000)
Number: 15 - Node: 0x100009290 (parent 0x100009230, left 0x000000000, right 0x1000093B0)
Number: 16 - Node: 0x1000093B0 (parent 0x100009290, left 0x000000000, right 0x000000000)
Number: 21 - Node: 0x1000092F0 (parent 0x100009170, left 0x000000000, right 0x000000000)
hi2
hi2
hi3
BST Dump: After 8: value 3
Tree: 0x7FFF5FBFF410 (root 0x100009170, height 0)
Number: 19 - Node: 0x100009170 (parent 0x000000000, left 0x1000091D0, right 0x1000092F0)
Number: 7 - Node: 0x1000091D0 (parent 0x100009170, left 0x100009410, right 0x100009230)
Number: 1 - Node: 0x100009410 (parent 0x1000091D0, left 0x000000000, right 0x100009470)
Number: 3 - Node: 0x100009470 (parent 0x100009410, left 0x000000000, right 0x000000000)
Number: 12 - Node: 0x100009230 (parent 0x1000091D0, left 0x100009350, right 0x100009290)
Number: 8 - Node: 0x100009350 (parent 0x100009230, left 0x000000000, right 0x000000000)
Number: 15 - Node: 0x100009290 (parent 0x100009230, left 0x000000000, right 0x1000093B0)
Number: 16 - Node: 0x1000093B0 (parent 0x100009290, left 0x000000000, right 0x000000000)
Number: 21 - Node: 0x1000092F0 (parent 0x100009170, left 0x000000000, right 0x000000000)
hi2
hi2
hi3
hi3
BST Dump: After 9: value 5
Tree: 0x7FFF5FBFF410 (root 0x100009170, height 0)
Number: 19 - Node: 0x100009170 (parent 0x000000000, left 0x1000091D0, right 0x1000092F0)
Number: 7 - Node: 0x1000091D0 (parent 0x100009170, left 0x100009410, right 0x100009230)
Number: 1 - Node: 0x100009410 (parent 0x1000091D0, left 0x000000000, right 0x100009470)
Number: 3 - Node: 0x100009470 (parent 0x100009410, left 0x000000000, right 0x1000094D0)
Number: 5 - Node: 0x1000094D0 (parent 0x100009470, left 0x000000000, right 0x000000000)
Number: 12 - Node: 0x100009230 (parent 0x1000091D0, left 0x100009350, right 0x100009290)
Number: 8 - Node: 0x100009350 (parent 0x100009230, left 0x000000000, right 0x000000000)
Number: 15 - Node: 0x100009290 (parent 0x100009230, left 0x000000000, right 0x1000093B0)
Number: 16 - Node: 0x1000093B0 (parent 0x100009290, left 0x000000000, right 0x000000000)
Number: 21 - Node: 0x1000092F0 (parent 0x100009170, left 0x000000000, right 0x000000000)
==48994==
==48994== HEAP SUMMARY:
==48994== in use at exit: 18,500 bytes in 33 blocks
==48994== total heap usage: 43 allocs, 10 frees, 18,820 bytes allocated
==48994==
==48994== LEAK SUMMARY:
==48994== definitely lost: 0 bytes in 0 blocks
==48994== indirectly lost: 0 bytes in 0 blocks
==48994== possibly lost: 0 bytes in 0 blocks
==48994== still reachable: 18,500 bytes in 33 blocks
==48994== suppressed: 0 bytes in 0 blocks
==48994== Rerun with --leak-check=full to see details of leaked memory
==48994==
==48994== For counts of detected and suppressed errors, rerun with: -v
==48994== Use --track-origins=yes to see where uninitialised values come from
==48994== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
仍在使用的33个分配在这台机器上完全正常(Mac OS X 10.7.5,Valgrind 3.7.1)。它们出现在调用main()
之前调用的函数中。
除Compare()
中的系统更改和AddNode()
中的动态分配外,代码还可以使用。
答案 1 :(得分:0)
我能看到的一个问题是你的代码
void AddNode(int num, Tree * tree){
Node NewNode;
NewNode.Num=num;
NewNode.left=NULL;
NewNode.right=NULL;
NewNode.parent=NULL;
Compare(tree,&NewNode,tree->root);
}
您将newNode声明为简单的局部变量,只要您退出AddNode函数(调用compare之后),变量newNode就会超出范围而且它的值会丢失。
您可以尝试的是使用指针,使用malloc分配空间然后添加新的点头