how can i read/write a binary tree sturcture from/in a txt file?
15 6610
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.
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: - name
-
my LEFT
-
is RIGHT
-
name LEFT LEFT
-
Ganon11 RIGHT RIGHT
This would be the tree: - name
-
/ \
-
my is
-
/ \
-
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:
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.
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: - name
-
my LEFT
-
is RIGHT
-
name LEFT LEFT
-
Ganon11 RIGHT RIGHT
This would be the tree: - name
-
/ \
-
my is
-
/ \
-
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:
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.
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.
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.
Are you focusing on reading a file to retrieve a tree, or on taking an existing tree and saving it in a file?
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. . . ???
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.
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.
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.
Sure, what code do you have so far?
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?
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
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: - 1
-
/ \
-
2 3
-
/ \ / \
-
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).
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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:/)....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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$) {
}
...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
|
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...
| |