Recent

Author Topic: How to save tree?  (Read 4978 times)

kernel

  • Newbie
  • Posts: 3
How to save tree?
« on: November 26, 2009, 04:17:13 pm »
Hi :)
I thought it would be like code below, but it doesn't work. What's wrong? Thanks for help.

var
  Tree1 : Tree
  f : File of Tree;
begin
  assignFile(f, 'file.dat');
  rewrite(f);
  Seek(f,0);
  write(f, Tree1);
  closeFile(f);
end;

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1891
Re: How to save tree?
« Reply #1 on: November 26, 2009, 05:58:10 pm »
Whrere / how is "Tree" defined?

Blaazen

  • Hero Member
  • *****
  • Posts: 2912
  • POKE 54296,15
    • Eye-Candy Controls
Re: How to save tree?
« Reply #2 on: November 26, 2009, 06:11:32 pm »
Hi,
I don't know what is exactly your type Tree, but probably it is some tree structure with your data (like AVL tree). It means if you try to save Tree to file this way you only save some pointers which are used for realization of Tree and some pointers to your data, but not data itself.
I think solution is go step by step through Tree and save all data to file (probably it is records).
When file is opened next time, you create new empty Tree and you add new elements and make them points to your data step by step until you reach end of file.
I think it is similar to all dynamic structures, it is simplier for lists (one or bi-directional) and more difficult for Trees (because they are usually sorted).
Lazarus 2.1.0 r63881 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: How to save tree?
« Reply #3 on: November 26, 2009, 07:29:29 pm »
If Tree is a pointer to a record then try write(f, Tree1^). But if Tree is a data structure that contains pointers then, like Blaazen said, in order to save the data to a file you need to traverse the structure.
« Last Edit: November 27, 2009, 12:13:57 am by Troodon »
Lazarus/FPC on Linux

 

TinyPortal © 2005-2018