473,387 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

writiing/reading a tree in a file

9
how can i read/write a binary tree sturcture from/in a txt file?
Sep 27 '07 #1
15 6610
Ganon11
3,652 Expert 2GB
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.
Sep 27 '07 #2
jgd
9
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?
Sep 28 '07 #3
Ganon11
3,652 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. name
  2. my LEFT
  3. is RIGHT
  4. name LEFT LEFT
  5. Ganon11 RIGHT RIGHT
This would be the tree:

Expand|Select|Wrap|Line Numbers
  1.     name
  2.     /  \
  3.   my    is
  4.   /      \
  5. hello   Ganon11
So each line is a different node, the first element is the content, and the remaining elements describe the path to that node. However, this might have problems. For instance, what if a file to be read contains:

Expand|Select|Wrap|Line Numbers
  1. name
  2. hello LEFT RIGHT
Wait, name's left child doesn't exist, so how can you have a right child?

You may want to consider an alternate way of implementing this...look into a Binary Search Tree, perhaps? Or, depending on your project, other data structures entirely.
Sep 28 '07 #4
jgd
9
actually, my code will be such that like it creates a node first and then inserts that in a particular position. So there wont be a node of invalid path. The file is kind of a database to search for leaf node with a particular path. I can also use any other data structure. But the requirements of the tree construction requires me to stick to this plan only. There should be some other way out of that also. But, I need to stick to this way only. how can I implement that?



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:

Expand|Select|Wrap|Line Numbers
  1. name
  2. my LEFT
  3. is RIGHT
  4. name LEFT LEFT
  5. Ganon11 RIGHT RIGHT
This would be the tree:

Expand|Select|Wrap|Line Numbers
  1.     name
  2.     /  \
  3.   my    is
  4.   /      \
  5. hello   Ganon11
So each line is a different node, the first element is the content, and the remaining elements describe the path to that node. However, this might have problems. For instance, what if a file to be read contains:

Expand|Select|Wrap|Line Numbers
  1. name
  2. hello LEFT RIGHT
Wait, name's left child doesn't exist, so how can you have a right child?

You may want to consider an alternate way of implementing this...look into a Binary Search Tree, perhaps? Or, depending on your project, other data structures entirely.
Sep 28 '07 #5
Ganon11
3,652 Expert 2GB
Hmmm...I don't know your project, so I can't tell if there's a better alternative available. This just seems inherently messy to me.
Sep 28 '07 #6
jgd
9
ok! so can u plz tell me how do u write read tree in a file by usign any of your methods. plz describe any of the storing method that u r using.
Sep 30 '07 #7
Ganon11
3,652 Expert 2GB
Are you focusing on reading a file to retrieve a tree, or on taking an existing tree and saving it in a file?
Sep 30 '07 #8
jgd
9
my program needs to have two parts.

first to ask user to enter data's values and "yes" and "no" values to store in a particular format.

then in second part, I need to search a particular data value, by asking user the "yes-no" option path. . .


can u plz help me with that. . . ???
Oct 6 '07 #9
Ganon11
3,652 Expert 2GB
OK, this sounds a LOT more do-able. I would just use a basic Binary Tree structure, except that instead of a node having left, right, and element, make it yes, no, and element. The element I presume is a string? This won't be too hard, and I'm sure there's simple Binary Tree code somewhere on the internet.
Oct 6 '07 #10
jgd
9
can't you help me in that code. can u plz gimme the code for the logic with which you are asking me to code. that will be a great help.



OK, this sounds a LOT more do-able. I would just use a basic Binary Tree structure, except that instead of a node having left, right, and element, make it yes, no, and element. The element I presume is a string? This won't be too hard, and I'm sure there's simple Binary Tree code somewhere on the internet.
Oct 7 '07 #11
jgd
9
actually you get it right this time. This is what I actually want to code. Can u help me in coding???


can't you help me in that code. can u plz gimme the code for the logic with which you are asking me to code. that will be a great help.
Oct 7 '07 #12
Ganon11
3,652 Expert 2GB
Sure, what code do you have so far?
Oct 7 '07 #13
jgd
9
actually i hav made a tree by user input.
Each node is a having an element of string & two pointers Y and N.

I want to store this tree in a file such that in future when required, this file can be read and same tree can be constructed once again.

I can have any format for storing that. But, I am out of ideas.

Can you help in format idea and coding?
Oct 11 '07 #14
jgd
9
i have stored my tree in a vector of strings in preorder format. how can i build a tree from that vector of strings??? gimme code if possible

plz help
thanx in advance
Oct 14 '07 #15
Ganon11
3,652 Expert 2GB
If you have the strings in pre-order format, then let's say you have this vector:

[1][2][3][4][5][6][7]

which represents this tree:

Expand|Select|Wrap|Line Numbers
  1.             1
  2.          /     \
  3.        2        3
  4.       / \      / \
  5.      4  5     6  7
Then, in the vector, the children of vector[0] are vector[1] and vector[2], the children of vector[1] are vector[3] and vector[4], the children of vector[2] are vector[6] and vector[7]. In general, this means that the children of vector[n] are vector[2n+1] and vector[2n+2].

You'll have to put in empty spots, though, if your tree is not complete (i.e. a node has 1 child, instead of the example, where there are only full nodes or leaf nodes).
Oct 14 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
4
by: Jason Kumpf | last post by:
OK I've been staring at this code all day and still with everything I have tried I cannot figure out two problems I am having. Once is why the space limit for the directory I create in the code...
4
by: Ganesh Muthuvelu | last post by:
Hi STAN, Stan: Thanks for your response to my previous post on reading a XSD file using your article in "https://blogs.msdn.com/stan_kitsis/archive/2005/08/06/448572.aspx". it works quite well...
1
by: mtanq | last post by:
My project is an ASP-based web page, with C# as the language of choice. I have written a simple tree view that displays the structure of a file system mounted as 'MVFS' (On my system, it's W:/)....
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
14
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, In my windows applicationm, i need to excute a batch file. this batch file throws some text and questions to the screen, i need to catch the standard Output, check if it's a question, in...
2
by: Derik | last post by:
I've got a XML file I read using a file_get_contents and turn into a simpleXML node every time index.php loads. I suspect this is causing a noticeable lag in my page-execution time. (Or the...
1
by: happy.john1234 | last post by:
Hi, i have got large xml file which i have to show in tree view windows form.I tried with loading all those data at once to tree view but it took a long time to render that tree view.Now i am...
8
by: slizorn | last post by:
Hi guys, I have this coding that I wanted to try out. Basically this is meant to be done in Java as practice for the Topic trees in data structures and algorithms. I have recently learned C++ on...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.