Click here to Skip to main content
15,936,565 members
Articles / Programming Languages / Python

Visualize Binary Search Tree using Python, Tkinter and Graphviz

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
21 Feb 2024CPOL3 min read 3.9K   86   4  
Visualizing a Binary Search Tree using Python, Tkinter and Graphviz
With this article, I wish to demonstrate inserting and traversing nodes in a Binary Search Tree. I also wish to explain the process of visualizing graphically the relationships of nodes in a binary search tree.

Image 1


This article is a demonstration of adding nodes to a Binary Search Tree, traversing nodes and visualizing the tree in a GUI environment using Python with Tkinter.


A tree data structure is a non-linear data structure because it does not store data sequentially. It is called as a hierarchical structure as data is arranged in multiple levels in a tree. In a tree, the topmost node is known as the root node. Each node contains some data which can be of any type and address of other node called children.

Following are some terms related to trees:

  1. Root: It is the topmost node in a tree. It does not have any parent node.
  2. Child: It is the descendent of some node.
  3. Parent: It is a node having a sub node (child node).
  4. Sibling: They are children of a common (same) parent.
  5. Leaf Node: It is a node that does not have children. It is the bottom most node. It is also called as external node.
  6. Internal Node: It is a node which has at least one child node.
  7. Ancestor Node: It is a predecessor node on a path from the root till the current node.
  8. Descendant Node: It is the immediate successor of any node.

A Binary Search Tree is a special type of tree in which each node tree can have maximum two child nodes with the restriction that the value of each node in the left subtree must be less than the value of the root node and the value of each node in the right subtree must be more than the value of the root node.

A node in a binary search tree contains three fields:

  1. Data Field
  2. Left Child Address Field
  3. Right Child Address Field

Node Traversal

Nodes in a Binary Search Tree can be traversed in three ways:

(All traversal functions are recursive functions)

  1. inorder - left-root-right
    • Traverse the left subtree.
    • Visit the root.
    • Traverse the right subtree.
  2. preorder - root-left-right
    • Visit the root.
    • Traverse the left subtree.
    • Traverse the right subtree.
  3. postorder - left-right-root
    • Traverse the left subtree.
    • Traverse the right subtree.
    • Visit the root.

Using the Code

The application is a Tkinter based GUI application in which a user can enter data and visualize it in real time.

A node class can be used to describe a node of a Binary Search Tree as follows:

class Node:
    def __init__(self,data): = data
         self.left = None
         self.right = None

Insertion and Traversal operations are defined in the Tree class as follows:

class Tree:
    def __init__(self):
        self.root = None
        self.nodes = ""
    def addnode(self,data):
        currnode = Node(data)
        if self.root is None:
            self.root = currnode
            parent = None
            ptr = self.root
            while ptr is not None:
                parent = ptr
                if int( < int(
                    ptr = ptr.left
                    ptr = ptr.right
            if int( < int(
                parent.left = currnode
                parent.right = currnode
    def inorder(self,root):
            if root != None:
                self.nodes += + " "
    def preorder(self,root):
            if root != None:
                self.nodes += + " "
    def postorder(self,root):
            if root != None:
                self.nodes += + " "
    def visualizetree(self,root):
        dot = graphviz.Digraph()
    def addedge(self,node,dot):
        if node.left:
        if node.right:

In the above code, the addnode() function adds a node to the tree at the appropriate position based on its value. The inorder(), preorder() and postorder() recursive functions perform the respective traversals and store the traversed node values in the nodes variable. The visualizetree() method performs visualization using graphviz. The addedge() function recursively draws edges from a node to its children nodes. The tree is rendered and stored as a "png" image in the current folder.

The following code provides the GUI environment for the application:

def add():
    img = ImageTk.PhotoImage("tree.png"))
    lblimage.image = img

def inorder():
    tree.nodes = ""

def preorder():
    tree.nodes = ""

def postorder():
    tree.nodes = ""

def showimage(event):
    os.system("tree.png") if os.path.exists("tree.png") else None

if __name__ == "__main__":

    tree = Tree()
    root = Tk()
    root.title("Binary Search Tree")

    lblvalue = Label(root,text="Enter data: "),y=50,width=100)

    txtvalue = Entry(root),y=50,width=100)

    btnadd = Button(root,text="Add",command=add),y=100,width=100)

    btninorder = Button(root,text="Inorder",command=inorder),y=100,width=100)

    btnpreorder = Button(root,text="Preorder",command=preorder),y=150,width=100)

    btnpostorder = Button(root,text="Postorder",command=postorder),y=150,width=100)

    lblimage = Label(root)

    if os.path.exists("tree.png"):

The above code can be used to add nodes and visualize the tree interactively. The rendered tree is displayed in a label, clicking on which opens the tree image in the default image viewer app.

Points of Interest

I hope readers find this article and its concept useful.


  • 21st February, 2024: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Instructor / Trainer NIIT, India
India India
I am a trainer by profession. Currently I am working with iFuture Technologies(India) as a Senior Faculty. I enjoy programming as a hobby. During my career I have seen the growth and decline of many technologies, many of them being my favorites like Flash, WPF, Windows Mobile Development. Few of my current favorites are Android, Xamarin and Python, though I also like traditional and evergreen languages like PHP, C#, Visual Basic and Java.

Apart from computers, my favorite pastime is bicycling.

Comments and Discussions

-- There are no messages in this forum --