Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 560
  • Last Modified:

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

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..
  • 2
1 Solution

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

SOBKingAuthor Commented:
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

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

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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now