954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

binary tree

I''ve got a struct like this:

struct Node
{ int n;
Node *left;
Node *right;
}

I build up a binary tree from this struct. Now, how can i save the binary tree onto the hard disk, and how can i rebuild it from the hard disk ?

shimooli
Newbie Poster
3 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

You would use the output of your tree traverse function and construct the tree next time with your corresponding constructor from same traversal order. Also you could name the nodes and list the children of each node and identify the top node (say the first one in the list). This is generally considered more practical way for trees than actual concreate linked structure (in addition of the adjacency matrix, which you also can use to save, but it is highly redundant and sparse).

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 
Now, how can i save the binary tree onto the hard disk, and how can i rebuild it from the hard disk ?


Well, obviously you need to devise some kind of format, but the easiest method is with record ordering. You can treat serialization as an extension of copying, where a pre-order traversal is the way to go:

void serialize_tree(FILE *out, node *root)
{
    if (root != NULL) {
        fprintf(out, "%s\n", to_string(root->data));
        serialize_tree(out, root->left);
        serialize_tree(out, root->right);
    }
}

When reading back from the file, the records are already in an order conducive for reproducing the original tree:

void deserialize_tree(FILE *in, node *root)
{
    char buf[BUFSIZ];

    while (fgets(buf, sizeof buf, in) != NULL)
        root = insert(root, from_string(buf));
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

so tnx :)
but how can i saving binary tree onto the hard with fseek functions ??

shimooli
Newbie Poster
3 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 
how can i saving binary tree onto the hard with fseek functions ??


Why would you want to do that when it's completely unnecessary?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I want to learn it :) can u help me?
plzzzzzzzzzzzzzzzzzz

shimooli
Newbie Poster
3 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

Post full code, use [code] tags.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

@Narue

How does the method described by you handle the case when the binary tree is not complete ie. Some of the intermediate nodes could have 0 (or 1) children

abhimanipal
Master Poster
742 posts since Dec 2009
Reputation Points: 114
Solved Threads: 104
 
How does the method described by you handle the case when the binary tree is not complete


As long as the original tree is a valid binary search tree, the restored tree will be an exact copy. The down side of this method is reconstructing the tree is O(N log N) because it reuses the insert operation. There are ways around that, but it's not as concise.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: