views:

469

answers:

1

This is a follow-up to Find first null in binary tree with limited memory.

Wikipedia says that iterative-deepening depth first search will find the shortest path. I would like an implementation that is limited in memory to k nodes and accesses the tree the least number of times.

For instance, if my binary tree is:

           0
    1             2
 3    4        5      6
7 8  9 10    11 12  13 14

And I'm limited to 5 nodes of memory than my search order is:

mem[0] = read node 0
mem[1] = read node 1
mem[2] = read node 2
mem[3] = read node 3
mem[4] = read node 4 //Now my memory is full.  I continue...
mem[3] = read node 5 //overwrite where I stored node 3
mem[4] = read node 6 //overwrite where I stored node 4

Now if my next read is to 7, I need to re-read 3. But if I make my next read to 14, then I don't need to re-read 3 just yet. If the solution is at 14, this will make my algorithm a bit faster!

I'm looking for a general solution; something that will work for any size memory and number of branches per node.

A: 

If your nodes link to their parents, and the children of a node will always be enumerated in the same order, you can trace your steps without having to save them.

On Freund