- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试自己从头开始构建 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);
}
}
Tree Initialized
hi1hi1hi1hi1
if (oldNode == NULL)
{
oldNode=newNode;
printf("hi1");
}
最佳答案
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);
}
#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)
main()
之前调用的函数中。叫做。
Compare()
的系统性变化以及
AddNode()
中的动态分配,代码很好。
关于c - 尝试在 C 中从头开始构建 BST 树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15801229/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!