rdr-py / code / newick_format.py
newick_format.py
Raw
"""
*******************************************************
Owner: Ashutosh Jha
For: APPCAIR BITS Pilani Goa Campus, Reflexis Systems
Function Name: newick_format()
Takes root of tree, an empty list newick, right bool 
value as input. Returns newick as a newick tree 
representation by traversing the tree in PreOrder Way.
*******************************************************
"""

def newick_format(root,newick,right=False,leftAlsoNone=False):
    
    

    if root:

        if((right==True) and (newick[-1] != ")")):
            newick.append(",")

        newick.append(root.name)

        if((root.left != None) or (root.right != None)):
            newick.append(")")

        #if((right==True) and (root.left == None) and (root.right == None)):
         #   newick.append("(")
          

        newick_format(root.left,newick)
    
        newick_format(root.right,newick,True)
        
    else:
        if(right==True):
            newick.append("(")