Learn practical skills, build real-world projects, and advance your career

Binary Trees

Table Of Contents

1). Insertion in Binary Trees

2). Tree Traversal Algorithms

Problem Statement: Binary Trees and Traversal Algorithms

First we will implement a Binary tree and then we will go through different traversal algorithms that traverses each node of the tree.

# Node class which will represent a single node in a Binary Tree
class Node:
    def __init__(self,val):
        self.val = val
        self.left = None
        self.right = None
        
# Forming a symmetric binary tree
root = Node(50)
root.left = Node(30)
root.right = Node(20)
root.left.left = Node(40)
root.left.right = Node(70)
root.right.left = Node(60)
root.right.right = Node(80)
print("Tree successfully created.")
Tree successfully created.
Pre order : 50,30,40,70,20,60,80

Inorder: 40,30,70,50,60,20,80

Postorder: 40,70,30,60,80,20,50