gpt4 book ai didi

c - 尝试在 C 中从头开始构建 BST 树

转载 作者:行者123 更新时间:2023-12-04 05:03:42 27 4
gpt4 key购买 nike

我正在尝试自己从头开始构建 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 方法之前将根设为 array[0],但是程序停止执行。有任何想法吗?

最佳答案

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) 项,则无法调试自己的结构。

一个SSCCE
#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()添加了功能。 0x%.9X在我的 64 位机器上,堆地址在 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() 中的动态分配,代码很好。

关于c - 尝试在 C 中从头开始构建 BST 树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15801229/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com