URGENT!!!! C++ Builder Question About TreeViews

Posted on 2003-03-20
Medium Priority
Last Modified: 2008-02-01
my question is for C++Builder

i have a TreeView with multiple nodes.. each node has the .data property set to point to some structure with additional info about that node

now i need to know how to save that TTreeView to a file of some kind and then load it again...

and i need the .data to be saved as well so when im done loading it.. it will be there too!

please dont tell me to use the .SaveToFile property as that wont save the .data part  (unless im doing something wrong)

just incase no one know what im talking about the .data thing i will give some code

here is the code i use to add an item to the tree

//The Structure
struct TreeLeaf {
   char Name[40];
   char Description[100];
   char Pin[2];
   char Light[8];

//The Add Code
   TreeLeaf *data;
   TTreeNode *node;

   data = (TreeLeaf *)malloc(sizeof(TreeLeaf));

   strcpy(data->Pin, "00");
   strcpy(data->Light, "00000000");
   strcpy(data->Description, "New Item");

   node = spSkinTreeView1->Items->Add(NULL,"New Item");
   node->Data = data;
   node->Selected = true;

   Current = node;

so as you can see each node has some additional data attached to it.. which is critical to be saved along with the tree..
Question by:SOBKing
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 11

Expert Comment

ID: 8179746

you can use the falowing method to save your tree:

- save the data associated with the tree nodes in file as a sequence (given by a tree traversal -- Breadth-first for example) let's call it NS (Nodes Sequence)

- for reconstructing the tree you will need to save some aditional data namely a sequence of integers (of a lenght equal to that of the sequence obtained as a result of the traversal procedure) let's call it FS (Father Sequence).
each position in FS is associated to the node on the same position in NS and contains  the position where you have the node's parent in NS.

- you will also have to save the number of nodes in your file

1.you will need a travesal procedure which will insert a TreeNode in NS only after inserting the node's parent.

I hope this idea will help you


Author Comment

ID: 8179828
ok cool.. that makes sence...

but i dont know hot to traverse a complex tree...

i can do a binary search tree with recursion :)

i understand how u would do the complicated one.. but just dont know the code
LVL 11

Accepted Solution

Agarici earned 1460 total points
ID: 8180033

as I said before I recomend that you use a breadth-first traversal

the algorithm is folowing

in this algorithm:
Q is a queue ( you will probably choose a TreeLeaf* queue )
root is the root of the tree
NS, FS as before

Q <- empty queue
i <- 0
push root in Q

while Q is not empty
  CrtNode <- pop form Q

  for each   child of CrtNode   do
     push child in Q
  NS[i] <- CrtNode
  if CrtNode = root
     FS[i] <-   -1
     FS[i] <-   find CrtNode position in NS

n <- Nomber of elems in NS
write to file n

for i <- 0 to n - 1
   write to file FS[i]
for i <- 0 to n - 1
   write to file the content of NS[i]

hope that helps


Expert Comment

ID: 9517240
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: SOBKing

Please leave any comments here within the next seven days.


EE Cleanup Volunteer


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question