0

I want to create a Binary Search Tree(BST) from list of data with graphical manner in python.I used the following code as

import pydot

from binarytree import build

values = [3, 5, 2, 1, 4,14]

tree = build(values)

print(tree)

print(tree.values)

graph = pydot.Dot(graph_type='graph')

for i in tree.values:

edge = pydot.Edge(i, i+1)

graph.add_edge(edge)

graph.write_png('example1_graph.png')

I got the output in python environment as

enter image description here

and in png format as

enter image description here

But I want the output as in binary tree format with graphical manner as follows

enter image description here

How can I convert the tree(code variable) values in to a graphical BST format? (Your answer may be using graphviz,matplotlib package also) Thanks in advance.

user1999109
  • 421
  • 7
  • 19
  • 2
    Possible duplicate of [Tree plotting in Python](https://stackoverflow.com/questions/7670280/tree-plotting-in-python) – Nico Haase Nov 05 '18 at 07:21
  • Thanks.. The above mentioned link shows, how to draw the general trees.. but I want to draw the binary search tree in data structure subject with its rules(property).. if any packages is there in python means share with me.. thanks.. – user1999109 Nov 07 '18 at 02:04
  • Can you highlight the differences? You have shown three completely different trees. How are they generated? Why should something like building the proper tree structure, output it in DOT format, and plot it through GraphViz not work? – Nico Haase Nov 07 '18 at 07:42
  • Using the print(tree.values) code I got the first diagram output. But it is not in graphical manner.. So I used the graph.write_png('example1_graph.png') code to got second diagram. But it not follows the Binary tree rules(Actually I can't convert it). My expected output is the third diagram(green coloured) which I got it from online editor not in python code.. If I want the get the third diagram(green coloured), what python code am I want to use?(It should follows binary tree rules as well as in graphical manner). – user1999109 Nov 07 '18 at 14:31
  • Well, your code looks flawed: why do you build edges for the second graph between a value and the next larger integer, while your first graph has completely other edges? The third (green) graph has a third set of edges - which one is the proper set? – Nico Haase Nov 07 '18 at 15:06
  • Hi Nico Haase.. please forget my code.. I searched lot of codes, but I couldn't find the exact one.. So I used that code and I know it may not be correct.. If I give a list of input values like [3,2,5,1,4,14] I want to get the third diagram(green coloured) which follows binary tree rules and it is in the form of graphical manner.. I want to get the third diagram(green coloured) like output using python code.. Guide me.. Thanks.. – user1999109 Nov 07 '18 at 15:47
  • Please refer the link https://stackoverflow.com/questions/21586085/difference-between-binary-search-and-binary-search-tree for binary search tree rules.. Thanks.. – user1999109 Nov 07 '18 at 15:51
  • Please don't unneccessarily link to other questions. I think the main problem is that you should import `bst` rather than `build` - have you tried that? – Nico Haase Nov 07 '18 at 16:06
  • Thanks Nico Haase for your patience response yet now.. Now I got some basic idea based on your guideline.. I will update myself.. thanks.. – user1999109 Nov 08 '18 at 12:54

0 Answers0