4 Replies - 886 Views - Last Post: 11 October 2012 - 04:38 AM Rate Topic: -----

#1 ria22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-October 12

writing a binary search tree to a binary file

Posted 10 October 2012 - 05:16 AM

i have to write a binary search tree into a binary file and read it back from that file.I know the code of binary search tree but have no clue how to do the second part....pls help
Is This A Good Question/Topic? 0
  • +

Replies To: writing a binary search tree to a binary file

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,685
  • Joined: 16-October 07

Re: writing a binary search tree to a binary file

Posted 10 October 2012 - 06:27 AM

If your BST is in an array, just dump it, specifying size first. If it's a collection of nodes and pointers, write only the data, with size first, then rebuild the BST when you read it back.
Was This Post Helpful? 0
  • +
  • -

#3 ria22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-October 12

Re: writing a binary search tree to a binary file

Posted 11 October 2012 - 01:28 AM

View Postbaavgai, on 10 October 2012 - 06:27 AM, said:

If your BST is in an array, just dump it, specifying size first. If it's a collection of nodes and pointers, write only the data, with size first, then rebuild the BST when you read it back.


Can you please specify the code?I have never worked on files thats why its a bit difficult for me.How to write all the inputs to the file???and how to then get the inputs sequentially from that file??should i use a .dat file?what is its difference with .bin file?I am using a linked list for the bst and my code is in C
Was This Post Helpful? 0
  • +
  • -

#4 peace_fixation  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 198
  • Joined: 01-November 11

Re: writing a binary search tree to a binary file

Posted 11 October 2012 - 03:24 AM

A binary file means you write out your data in raw form, as opposed to an ASCII text (or other) representation. There's a good tutorial here.

You'll use fwrite() to write your data structure directly to the file in block sizes that you define using sizeof(myDataStructure). When you read it back, you read it back in blocks of the same size and re-populate your data structure.

It doesn't matter what extension (if any) you give your file, it makes no difference, although there are conventions you can follow and .dat is fine

Start with a small program and learn how to write one piece of data to the file and read it back, then work up to an array, and then a more complicated data structure like your linked list.

Relevant functions you need to know - read the man pages they are very informative, type 'man fopen' at your terminal or search engine (ie Google):

fopen (open the file)
fclose (close the file)
fwrite (write binary data to the file)
fread (read binary data from the the file)

and the sizeof operator.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,685
  • Joined: 16-October 07

Re: writing a binary search tree to a binary file

Posted 11 October 2012 - 04:38 AM

You shouldn't be using a linked list, you should be using a tree. ;)

e.g.
typedef struct NodeStruct {
   Data data;
   NodeStruct *left, *right;
} Node;

typedef struct {
   Node *root;
} Tree;



To save the thing, it needn't be a tree; you just need to save the data. If files are a mystery, read up on files.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1