473,397 Members | 2,056 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,397 software developers and data experts.

How to save trees on files?

44
Hello
I am trying to use tree data structures in an address book. Would someone tell me how the nodes..that is.. a tree can be saved in a file.? A file, Or any other method through which the tree can saved onto the computer.
I had made the address book in C just using structures. And saved them onto files using fread, fwrite etc. I want to make modifications in it, to now use C++, and trees for it to be more efficient
Thank you
Hanaa
Nov 25 '07 #1
5 6417
weaknessforcats
9,208 Expert Mod 8TB
Just write the key and the value to the disc. You should have a sequential file of these key/value pairs.

To restore the tree, you create an empty one, read a key/value from the disc an insert it in the new tree.

Then just do the same for the other key/value pairs.

There is no need to preserve the actual tree structure in the disc file.
Nov 25 '07 #2
hanaa
44
I'm afraid I didnt completely understand. I tried to do it. I couldnt. Would you please show me a simple illustrative piece of code..(esp, on how to read the sequential file contents into the tree..).
thank you,
Hanaa
Dec 2 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Pseudo-code:
To store tree:
1) Create emtpy disc file or posittion to beginning if existing
2) Read Tree to get key/value
3) Write key/value to disc file
4) Repeat 2 and 3 until all keys in the Treee have been read.


To load from disc:
1) Create empty tree.
2) Read key/value from disc
3) Insert in tree
4) Repeat 2 and 3 until disc file EOF is reached.
Dec 2 '07 #4
Orensh
1
For an arbitrary tree (where there are no rules in the construction of the tree) you may need to store info about the parent of each node.
something like:
<id>, <parent>, <data>

for example:
<1>, <0 -- this means this is the root>, <"I am the root">
<2>, <1>, <"I am the first child">
<3>, <1>, <"I am the second child">
<4>, <2>, <"I am the grandchild">

hope this helps,
Oren
Dec 3 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
[quoteOrensch]
<1>, <0 -- this means this is the root>, <"I am the root">
<2>, <1>, <"I am the first child">
<3>, <1>, <"I am the second child">
<4>, <2>, <"I am the grandchild">
[/quote]

Generally you shouold not need to do this unless you desire to have the disc file be the actual tree. In which case the <1>, <4>, etc.. would be seek offsets from the beginning of the file.

I have seen flat disc files containing the key/value pairs that are processed by a utility to do just as you suggest with the result in a second file called the index file. Then you use the index file to get to offset of the record in the data file.

I just don't know if that is the case here.
Dec 3 '07 #6

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

Similar topics

6
by: Alex Le Dain | last post by:
Is there a generic "tree" module that can enable me to sort and use trees (and nodes). Basically having methods such as .AddNode(), ..GetAllChildren(), .FindNode() etc. Is this handled natively...
6
by: C++ Shark | last post by:
Hi, which stl class is good for creating search trees? I am looking for something flexible, that allows me to search for a required state (of matrices, graphs, etc.) quickly. thanks in...
5
by: bugbear | last post by:
I need to process some XML files that are rather large. However their structure may usefully be expressed as <ELEMENT FILE (RECORD)+> .. .. .. Each record is a few Kb. The files are many...
1
by: barnesc | last post by:
Hi again, Since my linear algebra library appears not to serve any practical need (I found cgkit, and that works better for me), I've gotten bored and went back to one of my other projects:...
3
by: ptrSriram | last post by:
Can someone help me with an algorithm to merge two binary search trees. One method I thought of was to flatten both the trees into sorted lists(inorder traversal),merge those two sorted lists,...
3
by: David Svoboda | last post by:
I have a server program that takes commands and acts on them. The server program can also take these commands from an input file or standard input (mainly for testing purposes). As such, I often...
2
by: herbasher | last post by:
I need a few pointers. It isn't really related to C but I'm not sure where to post, and thought this group probably has really smart programmers. I need to store and manipulate a big tree...
2
by: parasuram | last post by:
Hi friends ............. this is a question regarding the data structures trees Pleas post it if possible with in 2 days I will thankful if some body could help doing this. Operating...
6
by: rsprawls | last post by:
I found a disk for a b-tree algorithm that I purchased back in 93 or so. I'd hoped to find this, but now I'd like to know how worthwhile are b-trees in today's advancements? This is old C code...
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?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.