Binary Tree with Nodes

2,064 views
Skip to first unread message

Trentonknight

unread,
Dec 4, 2009, 8:55:50 PM12/4/09
to networkx-discuss
Does anyone have the best way to approach creating a binary tree that
will allow for the root node to remain on top above the children nodes
while the child nodes can be placed smartly on the left and right. It
seem strange to me that there are layouts for a massive array of
intricate graphs and I cannot even make a proper binary tree. I have
played a little with using the pos[0]=[0,0] but I cannot seem to see
any reaction from my graphs. Any advice would be greatly appreciated.
Any tricks for sage of python graphing at all will also be greatly
appreciated. I'm ready to give up and start looking at Boost. Thank
You.

V/r
TrentonKnight

Aric Hagberg

unread,
Dec 4, 2009, 9:22:01 PM12/4/09
to networkx...@googlegroups.com
I think you are saying that you want to draw binary tree. NetworkX
isn't primarily a graph drawing package but you can make some simple
drawings (see
http://networkx.lanl.gov/gallery.html
)

In particular if you might use the Graphviz "dot" layout algorithm to
draw a binary tree. You'll need Graphviz + PyGraphviz, or Pydot
to do that. Here is a slightly modified version of
http://networkx.lanl.gov/examples/drawing/circular_tree.html
that draws a binary tree:

-----

import networkx as nx
import matplotlib.pyplot as plt

try:
from networkx import graphviz_layout
except ImportError:
raise ImportError("This example needs Graphviz and either
PyGraphviz or Pydot")


G=nx.balanced_tree(2,4)
pos=nx.graphviz_layout(G,prog='dot',args='')
plt.figure(figsize=(8,8))
nx.draw(G,pos,node_size=20,alpha=0.5,node_color="blue", with_labels=False)
plt.axis('equal')
plt.savefig('circular_tree.png')
plt.show()

---
Aric

Trentonknight

unread,
Dec 5, 2009, 2:10:22 PM12/5/09
to networkx-discuss
Ok, thank you for your help. I will look at Graphviz. I did take a
peak at the circular binary tree earlier at it will be to confusing
for people to understand simple graphs illustrating huffman codes,
etc.

On Dec 4, 9:22 pm, Aric Hagberg <ahagb...@gmail.com> wrote:
> On Fri, Dec 4, 2009 at 6:55 PM, Trentonknight <trentonkni...@gmail.com> wrote:
> > Does anyone have the best way to approach creating a binary tree that
> > will allow for the root node to remain on top above the children nodes
> > while the child nodes can be placed smartly on the left and right. It
> > seem strange to me that there are layouts for a massive array of
> > intricate graphs and I cannot even make a proper binary tree. I have
> > played a little with using the pos[0]=[0,0] but I cannot seem to see
> > any reaction from my graphs. Any advice would be greatly appreciated.
> > Any tricks for sage of python graphing at all will also be greatly
> > appreciated. I'm ready to give up and start looking at Boost. Thank
>
> I think you are saying that you want to draw  binary tree.  NetworkX
> isn't primarily a graph drawing package but you can make some simple
> drawings (seehttp://networkx.lanl.gov/gallery.html
> )
>
> In particular if you might use the Graphviz "dot" layout algorithm to
> draw a binary tree.  You'll need Graphviz + PyGraphviz, or Pydot
> to do that.    Here is a slightly modified version ofhttp://networkx.lanl.gov/examples/drawing/circular_tree.html

Trentonknight

unread,
Dec 5, 2009, 2:44:14 PM12/5/09
to networkx-discuss
Wow, I feel really lame right now. I was able to do with sage in 2
minuets what took me all day yesterday. Sage has some clear additions
to their graphing library that I complete overlooked. Here is a simple
binary tree with sage. That was too easy.

BinaryTree={0:[1,2,3,4],1:[],2:[],3:[],4:[]}
T=Graph(BinaryTree)
TREE=T.plot(graph_border=True,vertex_size=120, layout="tree",
tree_orientation="down",tree_root=0,vertex_colors="green",edge_colors="red")
TREE.show()



On Dec 4, 9:22 pm, Aric Hagberg <ahagb...@gmail.com> wrote:
> On Fri, Dec 4, 2009 at 6:55 PM, Trentonknight <trentonkni...@gmail.com> wrote:
> > Does anyone have the best way to approach creating a binary tree that
> > will allow for the root node to remain on top above the children nodes
> > while the child nodes can be placed smartly on the left and right. It
> > seem strange to me that there are layouts for a massive array of
> > intricate graphs and I cannot even make a proper binary tree. I have
> > played a little with using the pos[0]=[0,0] but I cannot seem to see
> > any reaction from my graphs. Any advice would be greatly appreciated.
> > Any tricks for sage of python graphing at all will also be greatly
> > appreciated. I'm ready to give up and start looking at Boost. Thank
>
> I think you are saying that you want to draw  binary tree.  NetworkX
> isn't primarily a graph drawing package but you can make some simple
> drawings (seehttp://networkx.lanl.gov/gallery.html
> )
>
> In particular if you might use the Graphviz "dot" layout algorithm to
> draw a binary tree.  You'll need Graphviz + PyGraphviz, or Pydot
> to do that.    Here is a slightly modified version ofhttp://networkx.lanl.gov/examples/drawing/circular_tree.html

Aric Hagberg

unread,
Dec 5, 2009, 6:48:02 PM12/5/09
to networkx...@googlegroups.com
On Sat, Dec 5, 2009 at 12:44 PM, Trentonknight <trento...@gmail.com> wrote:
> Wow, I feel really lame right now. I was able to do with sage in 2
> minuets what took me all day yesterday. Sage has some clear additions
> to their graphing library that I complete overlooked. Here is a simple
> binary tree with sage. That was too easy.
>

I'm glad you figured it out. In NetworkX you can of course do

In [9]: BinaryTree={0:[1,2,3,4],1:[],2:[],3:[],4:[]}
In [10]: T=Graph(BinaryTree)
In [11]: draw(T)

but you won't get a downward tree like Sage. I'm glad they have
add nice tree drawings to Sage!

Aric

Nathan Cobb

unread,
Dec 8, 2009, 10:37:38 AM12/8/09
to networkx...@googlegroups.com, networkx...@googlegroups.com
Oiopk
P po

Nathan K. Cobb

Sent from my mobile, please excuse typos.
> --
>
> You received this message because you are subscribed to the Google
> Groups "networkx-discuss" group.
> To post to this group, send email to networkx-
> dis...@googlegroups.com.
> To unsubscribe from this group, send email to networkx-discu...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/networkx-discuss?hl=en
> .
>
>
Reply all
Reply to author
Forward
0 new messages