Skip to content

Instantly share code, notes, and snippets.

@draganmarjanovic
Created October 30, 2016 07:52
Show Gist options
  • Save draganmarjanovic/4cad6b32b0ab5cbbcadd10d82dc45d6d to your computer and use it in GitHub Desktop.
Save draganmarjanovic/4cad6b32b0ab5cbbcadd10d82dc45d6d to your computer and use it in GitHub Desktop.
import graphviz as gv
tree = [54, 26, 15, 93, 8, 1, 23, 39];
def left_child(index, length):
child = (index * 2 + 1)
if (child <= length - 1):
return child
else:
return -1
def right_child(index, length):
child = (index * 2 + 2)
if (child <= length - 1):
return child
else:
return -1
def main():
g1 = gv.Graph(format='svg')
for index, value in enumerate(tree):
left_index = left_child(index, len(tree))
right_index = right_child(index, len(tree))
if (left_index != -1):
g1.edge(str(value), str(tree[left_index]))
if (right_index != -1):
g1.edge(str(value), str(tree[right_index]))
#Print the GraphViz code
print(g1.source)
# Render the graph
filename = g1.render(filename='img/graph')
print (filename)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment