在C中将二进制树转换为数组(以后再保存)(Converting a Binary Tree into an Array (and later save) in C)

编程入门 行业动态 更新时间:2024-10-26 06:28:06
在C中将二进制树转换为数组(以后再保存)(Converting a Binary Tree into an Array (and later save) in C)

所以,我正在做这个客户应用程序,您可以在其中创建/修改/搜索/列出客户。 后来,这扩展到将客户与订单等产品联系起来,但我现在关注的只是客户。 我已经创建了一个二叉树,所有这些功能都可以工作,但是我需要一种方法来存储已创建的客户。

我想在某种程度上我必须将所有客户(在每个节点中找到)转移到一个数组中,然后将该数组转换为文件“customer.dat”。 花了很多时间。 这里有一些代码片段,以帮助更好地理解我的功能和结构:

typedef struct customer { char Name[MAXNAME]; char Surname[MAXNAME]; char ID[MAXID]; char Address[MAXADDRESS]; } Cstmr; typedef struct node { Cstmr item; struct node * left; struct node * right; } Node; typedef struct tree { Node * root; int size; } Tree;

以上是结构,Node包含Cstmr类型的项目以及链接的左右节点。 树包含根节点和大小。

void Traverse (const Tree * ptree, void (* pfun)(Cstmr item)) { if (ptree != NULL) InOrder(ptree->root,pfun); } static void InOrder(const Node * root, void(* pfun)(Cstmr item)) { if (root != NULL) { InOrder(root->left, pfun); (*pfun)(root->item); InOrder(root->right, pfun); } }

这些函数用于通过添加函数来列出Customers

void printItem(Cstmr C) { printf("%-10s %-10s %-8s\n", C.Name, C.Surname, C.ID); }

最后通过写作执行

Traverse(tree,printItem);

我试图将printItem改为另一个函数,以便添加到一个数组(输出到文件而不是屏幕),但现在事情变得太复杂了! 有什么建议么?

So, I'm doing this customer application where you can create/Modify/Search/List Customers. Later on this expands to linking customers to products with an order and so on, but my focus right now is just on customers. I have created a binary tree and all these functions work, however I need a way to store created customers for another time.

I figured that in some way I would have to transfer all the customers (found in each node) into an array, and then fwrite that array into a file "customer.dat". have spent a lot of hours on it. here is some code snippets to help better understand what function and structs I have:

typedef struct customer { char Name[MAXNAME]; char Surname[MAXNAME]; char ID[MAXID]; char Address[MAXADDRESS]; } Cstmr; typedef struct node { Cstmr item; struct node * left; struct node * right; } Node; typedef struct tree { Node * root; int size; } Tree;

the above are structs, Node contains item of type Cstmr and linked left and right nodes. Tree contains a root node and size.

void Traverse (const Tree * ptree, void (* pfun)(Cstmr item)) { if (ptree != NULL) InOrder(ptree->root,pfun); } static void InOrder(const Node * root, void(* pfun)(Cstmr item)) { if (root != NULL) { InOrder(root->left, pfun); (*pfun)(root->item); InOrder(root->right, pfun); } }

these functions where used for listing Customers with the addition of the function

void printItem(Cstmr C) { printf("%-10s %-10s %-8s\n", C.Name, C.Surname, C.ID); }

and finally executed by writing

Traverse(tree,printItem);

I tried to alter printItem into another function in order to add to an array (outputting to a file instead of the screen), but now things just got way too complicated! any suggestions?

最满意答案

如果将数据存储在数组中并不重要,则可以使用fprintf而不是printf来调整printItem函数以直接写入文件。 您还需要将文件句柄传递给您的函数。

你的InOrder遍历也有点儿InOrder 。 行InOrder(root->right, pfun); 当你在它上方返回时,它永远不会执行。 仅删除return关键字。 一旦验证了InOrder遍历是否正常工作,请尝试进行适当的修改以将其打印到文件中(最好使用类似于printItem的函数指针)。

如果你真的想写一个数组,你需要知道你树中的项目总数或使用动态容器(除非你自己编写,否则不在C中提供),尽管你可以完成你需要的东西没有数组。

If it's not essential that you store your data in an array, you can adapt your printItem function to write directly to a file using fprintf instead of printf. You will need to also pass a file handle to your function.

Your InOrder traversal is also a bit buggy. The line InOrder(root->right, pfun); is never executed as you return immediately above it. Remove only the return keyword. Once you verify your InOrder traversal is properly working, try making the proper modifications to have it print to a file (preferably using a function pointer like you do with printItem).

If you actually do want to write to an array, you'll need to know the total number of items in your tree or use a dynamic container (not available in C, unless you write your own), though you can accomplish what you need without an array.

更多推荐

本文发布于:2023-07-22 20:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1223317.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   转换为   中将   Converting   save

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!