用C转换二叉树为Array(后来保存) [英] Converting a Binary Tree into an Array (and later save) in C

查看:91
本文介绍了用C转换二叉树为Array(后来保存)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我做这个客户的应用,您可以创建/修改/搜索/列表的客户。后来,这个扩展到连接客户的产品与订单等,但我的重点,现在仅仅是客户。我创建了一个二叉树,所有这些功能的工作,但是我需要一种方法来存储客户创造另一次。

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.

我想,在某种程度上我会所有的客户(每个节点中)转移到一个数组,然后FWRITE该数组到一个文件中customer.dat。花了很多的时间就可以了。这里是一些code片段,以帮助更好地了解我有什么功能和结构:

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;

以上是结构,节点包含和类型Cstmr项目挂钩左,右节点。树包含根节点和大小。

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);
}

和最后通过写执行

Traverse(tree,printItem); 

我试图改变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?

推荐答案

如果这不是重要的是你存放在数组中的数据,可以适应你的 printItem 函数直接写信给使用 文件fprintf中 而不是的printf 。您将需要也是一个文件句柄传递给你的函数。

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.

穿越也有点马车。行序(根 - &GT;右,pfun); ,你立即返回其上方从不执行。仅删除收益关键字。当您验证遍历工作正常,请尝试适当的修改,把它打印到文件(preferably使用函数指针像你<$办C $ C> printItem )。

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).

如果你其实想编写一个数组,你需要知道物品的总数你的树或使用动态容器(在C不可用,除非你自己写),但你可以做到你需要什么没有一个数组。

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.

这篇关于用C转换二叉树为Array(后来保存)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆