What you do is:
Get the id of the top-record in the branch that you want to move.
Then create a function that will fetch the ids of all the records that have the top record as a parent (the children).
That part is easy, now comes the tricky bit:
Everytime that function finds a child record, it should call itself with the id of the record it found.
That way, the function will start a new copy of itself, but now it will look for all records that have the newfly found child as a parent.
And if it finds children of that child, it will stat another copy of itself to look for it's childeren, etc etc untill there are no more children found.
For example:
id|parent|name
1|0|great grandma
2|1|grandma
3|1|grand_pa
4|3|father
5|4|son
I weant to find all the id's in the branch starting at grand_pa.
I lookup the id of grand_pa, and get 3.
I call the recursive function, and tell it to look for chilren of record 3.
That will find record 4, and when it find it ti will call itself to look for children of record 4, and it will find 5, and start another copy if itself looking for children of record 5, and it will find none.
That is the basic idea.
You have to make sure of two things:
One: the function must have a check builtin to make sure it will never try to find children of a record if that record is a child of itself.
id|parent|name
1|0|great grandma
...
6|6|village_idiot
The villagea_idiot is a child of itself, and will cause an infinate loop.
and two: you need to pass an array along from function to function in order to store all the IDs you find. You'll have to do this 'by reference'