在文件中写/读一棵树 [英] writiing/reading a tree in a file

查看:73
本文介绍了在文件中写/读一棵树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在txt文件中读取/写入二进制树结构?

how can i read/write a binary tree sturcture from/in a txt file?

推荐答案

对于读取,它取决于数据的存储方式。


写作时,这取决于你想如何写数据。


两者都是,文本文件中的数据是排序?它首先是根值,然后是根's的孩子等等?指定这些东西,向我们展示你到目前为止的想法,我们会帮助你。
For reading, it depends on how the data is stored.

For writing, it depends on how you want to write the data.

For both, is the data in the text file going to be sorted? Is it going to be the root value first, then the root''s children, etc.? Specify these things, show us what you''ve thought of so far, and we''ll help you out.


实际上我想通过要求用户输入值来构建树节点并询问他是否要将其存储到当前节点的左侧或右侧。

每个叶子节点的
将有一条来自root的路径(由左/右方向组成)到那个节点。这些信息将存储在一个文件中。稍后将读取此文件。


路径可以是以下形式: -


" hello"," RIGHT"," LEFT"。

here,value"" HELLO"存储在root'右子'的左侧位置。


我想将每个节点的路径存储在一个字符串向量中。向量的第一个元素将存储该叶节点的值,而其他元素将值存储为左。或正确的或方向的词。


将该值存储在文件中。下一个节点路径的值可以存储在另一个向量中。所以,我有2创建一个动态增加的向量数组。



在这种情况下如何将字符串存储在flie上?
actually i want to build a tree by asking user to enter the value of node and asking him whether it is to be stored to left or right of current node.

for each leaf node there will be a path (consisting of left/right directions ) from root to that node. such information is to be stored in a file. and later this file is to be read.

path can be of the form:-

"hello", "RIGHT", "LEFT".
here, value ""HELLO" is stored in root''s right child''s left postion.


i m thinking of storing each node''s path in a vector of strings. first element of the vector will store value of that leaf node and other elements will store the values as "left" or "right" word for directions.

storing that value in a file. the value of next node path can be stored in another vector. so, i hav 2 create a dynamically increasing array of vectors.


in this case how can i store the strings on a flie?


所以基本上你会有一个向量字符串向量?这看起来不必要地复杂......


至于将字符串存储在文件中......您的文件可能如下所示:

So basically you''ll have a vector of vectors of strings? This seems unnecessarily complicated...

As far as storing the strings in the file...your file for reading may look like this:

展开 | 选择 | Wrap | 行号


这篇关于在文件中写/读一棵树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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