Source tree algorithm for Dynamically creating Tree