How To Form Tree Structure by using php Script .

For Example i signup in one site they give one referral code to me , with my referral code only two can join , more then two members cant join its condition . Then first signup person should come on left and second person on right , like this it should go chain process , please help me from this .

Poster :
Sapare Narasimha