0

I am having a list of values as lst=[5,7,3,1]. I want to create a graph with each node must have max of 2 edges. One left node should be in left side to root which has less than to root node value as well as one right node should be in right side to root which has higher value to root node value. I used the following code as

import pydot
lst= [5,7,3,1]
lst_len=len(lst)
graph = pydot.Dot(graph_type='graph')

for i in lst:
  start=lst.index(i)
  if start < lst_len-1:
    end=start+1
  edge = pydot.Edge(lst[start], lst[end])
  graph.add_edge(edge)
graph.write_png('bst.png')

I got the output as

enter image description here

But I want the output as

enter image description here

How can I modify my code? Guide me.. thanks..

user1999109
  • 421
  • 7
  • 19
  • What is the condition? why do you need the output like that. Is there any relation between 5 , 3, 7, Why is 5 parent of them? – Vineeth Sai Nov 09 '18 at 10:14
  • Binary tree (or) binary search tree is one of the concept in data structure. The mentioned rules in my question is the rules for that tree. I am having code for normal display in python environment. For graphical understanding I want to convert normal display to graph manner. So only I am asking. I know, in pydot edge connection plays main role to create the graph/tree. But I don't know how to apply this rules.. Is it possible to apply those rules in pydot graph? if means please let me know.. thanks.. – user1999109 Nov 09 '18 at 13:12

0 Answers0