binary search tree in a file

hello everyone,

I need help in writing a binary search tree into a text file and

later, search the nodes in the file by using search function

in the program.

I have a binary search tree i.e.,

7

/\

59

/\/\

36 810

to be changed into ....

file.txt

=====

7

5

9

3

6

8

10

To search the node 3 in the file, I have to start from the root 7 and then to its left node 5 and then to its left node 3.

Could anyone help me with an indexing approach to keep track of the nodes in the file.

Thanks in advance

[634 byte] By [qwedwea] at [2007-10-3 6:17:51]
# 1

> Could anyone help me with an indexing approach to keep track of the

> nodes in the file. Thanks in advance

You could index on the lines in the file and store the nodes as follows:

if a node is stored at line i, its children are stored at lines 2i+1 and 2i+2.

The first line in the file is line 0 where the root of the tree is stored.

If all lines have equal lengths you can even drop the index.

kind regards,

Jos

JosAHa at 2007-7-15 1:02:36 > top of Java-index,Other Topics,Algorithms...