preserving the left and right child while printing python graphs using networkx
Question: I am trying to print the binary tree using networkx library in python.

But, I am unable to preserve the left and right childs. Is there a way to tell
the Graph to print left child first and then the right child?

    
    
    import networkx as nx
    G = nx.Graph()
    G.add_edges_from([(10,20), (11,20)])
    nx.draw_networkx(G)
    

[![enter image description
here](http://i.stack.imgur.com/KlNua.jpg)](http://i.stack.imgur.com/KlNua.jpg)
￼

EDIT 1: On using the pygraphwiz, it results in a directed graph atleast. So, I
have better picture of the root node.

Below is the code I am using:

    
    
    import pygraphviz as pgv
    G = pgv.AGraph()
    G.add_node('20')
    G.add_node('10')
    G.add_node('11')
    G.add_edge('20','10')
    G.add_edge('20','11')
    G.add_edge('10','7')
    G.add_edge('10','12')
    
    G.layout()
    G.draw('file1.png')
    from IPython.display import Image
    Image('file1.png')
    

But, this is still far from a structured format. I will post on what I find
out next. The new graph looks like below (atleast we know the root):

[![Root to Leaf Binary
tree](http://i.stack.imgur.com/czjFJ.png)](http://i.stack.imgur.com/czjFJ.png)

EDIT 2: For those who are facing issues with installation, please [refer to
this post.](http://stackoverflow.com/questions/22698227/python-installation-
issues-with-pygraphviz-and-graphviz) [The answer to
this](http://stackoverflow.com/a/28832992/4160889) \- its very helpful if you
want to install pygraphviz on windows 64 bit.


Answer: I believe Networkx is not suited to binary trees but you can set the node
positions yourself. I have wrote the following algorithm to setup node
positions but it works fine for full or complete binary trees where key nodes
are ordered [0,1,...].

    
    
    def full_tree_pos(G):
        n = G.number_of_nodes()
        if n == 0 : return {}
        # Set position of root
        pos = {0:(0.5,0.9)}
        if n == 1:
            return pos
        # Calculate height of tree
        i = 1
        while(True):
            if n >= 2**i and n<2**(i+1):
                height = i 
                break
            i+=1
        # compute positions for children in a breadth first manner
        p_key = 0
        p_y = 0.9
        p_x = 0.5
        l_child = True # To indicate the next child to be drawn is a left one, if false it is the right child
        for i in xrange(height):
            for j in xrange(2**(i+1)):
                if 2**(i+1)+j-1 < n:
                    print 2**(i+1)+j-1
                    if l_child == True:
                        pos[2**(i+1)+j-1] = (p_x - 0.2/(i*i+1) ,p_y - 0.1)
                        G.add_edge(2**(i+1)+j-1,p_key)
                        l_child = False
                    else:
                        pos[2**(i+1)+j-1] = (p_x + 0.2/(i*i+1) ,p_y - 0.1)
                        l_child = True
                        G.add_edge(2**(i+1)+j-1,p_key)
                        p_key += 1
                        (p_x,p_y) = pos[p_key]
    
        return pos
    
    G = nx.Graph()
    G.add_nodes_from(xrange(25))
    pos = full_tree_pos(G)
    nx.draw(G, pos=pos, with_labels=True)
    plt.show()
    

which gave the following graph. [![enter image description
here](http://i.stack.imgur.com/Kdysx.png)](http://i.stack.imgur.com/Kdysx.png)

