In Preorder traversal, we first visit the root node of the tree and then left subtree and then right subtree of the root node.

Properties of Preorder traversal

  • Visit the root node
  • Traverse the left subtree of the root node in preorder
  • Traverse the right subtree of the root node in preorder

let's say we have a binary tree as you see in the given below image.

preorder traversal of binary tree

to find the preorder of this tree first we need to find the subtree of the given tree as you see in the image.

as you know in the preorder first we visit the root node and then left and right subtree. so the preorder of node P is

P B C

but B is a subtree so we also need to find the preorder of subtree B and for subtree B the preorder is A D E

so the preorder of the tree is

P A D E C

but D is also a subtree so the preorder of subtree D is S T Q

so the preorder of the tree is

P A S T Q E C

E is also a subtree and the preorder of E is E D

so the preorder of the tree is

P A S T Q E D C

now the C is also a subtree and the preorder of subtree C is X F G

so the preorder of the tree is

P A S T Q E D X F G

now the F is also a subtree and the preorder of subtree F is M

so the preorder of the tree is

P A S T Q E D X M G

G is also a subtree so the preorder of subtree G is R C

so the complete preorder of the binary tree is

P A S T Q E D X M R C

Program to find the preorder traversal of binary tree using python programming.

from collections import deque


class Node:
    def __init__(self, value):
        self.info = value
        self.lchild = None        self.rchild = None

class BinaryTree:
    def __init__(self):
        self.root = None
    def is_empty(self):
        return self.root is None
    def display(self):
        self._display(self.root, 0)
        print()

    def _display(self,p,level):
        if p is None:
            return        self._display(p.rchild, level+1)
        print()

        for i in range(level):
            print(" ", end='')
        print(p.info)
        self._display(p.lchild, level+1)

    def preorder(self):
        self._preorder(self.root)
        print()

    def _preorder(self,p):
        if p is None:
            return        print(p.info, " ", end='')
        self._preorder(p.lchild)
        self._preorder(p.rchild)

    def inorder(self):
        self._inorder(self.root)
        print()

    def _inorder(self,p):
        if p is None:
            return        self._inorder(p.lchild)
        print(p.info," ", end='')
        self._inorder(p.rchild)

    def postorder(self):
        self._postorder(self.root)
        print()

    def _postorder(self,p):
        if p is None:
            return        self._postorder(p.lchild)
        self._postorder(p.rchild)
        print(p.info," ",end='')

    def level_order(self):
        if self.root is None:
            print("Tree is empty")
            return
        qu = deque()
        qu.append(self.root)

        while len(qu) != 0:
            p = qu.popleft()
            print(p.info + " ", end='')
            if p.lchild is not None:
                qu.append(p.lchild)
            if p.rchild is not None:
                qu.append(p.rchild)

    def height(self):
        return self._height(self.root)

    def _height(self,p):
        if p is None:
            return 0
        hL = self._height(p.lchild)
        hR = self._height(p.rchild)

        if hL > hR:
            return 1 + hL
        else:
            return 1 + hR

    def create_tree(self):
        self.root = Node('p')
        self.root.lchild = Node('Q')
        self.root.rchild = Node('R')
        self.root.lchild.lchild = Node('A')
        self.root.lchild.rchild = Node('B')
        self.root.rchild.lchild = Node('X')


##########################
bt = BinaryTree()

bt.create_tree()

bt.display()
print()

print("Preorder : ")
bt.preorder()
print("")

print("Inorder : ")
bt.inorder()
print()

print("Postorder : ")
bt.postorder()
print()

print("Level order : ")
bt.level_order()
print()

print("Height of tree is ", bt.height())