Materialized Path, tree normalization

Hi everybody!

I have a problem with Materialized path, so I thought may be here somebody can help me. I wrote a class which provides operations with Materialized path such as add, edit, delete and move. It’s pretty easy, but I have a problem with tree normalization.

Imagine we have the following tree:

1.1

1.2

1.4

1.4.1

1.6

1.8.2.1

and we need to change it to:

1.1

1.2

1.3

1.3.1

1.4

1.5

How can we do it? I mean can somebody give an algorithm?

Well, you could simply rebuild the tree. The indices should re-arrange that way.

May be I didn’t understand you… How the index can help me?

Just iterate over your tree with array_values(). Take the new array keys and separate them with a dot => all done.

If it were so simple, I wouldn’t have created the topic. I understand the need to go through the array and rebuild it, but you need to take into account the nesting level…

Pardon me. I forgot the word “recursive”. So, walklt over your tree recursively :P

You know what algorithm? What I read above, it seems to me to banter…