Python Trees


Tree is a non-linear data structure used to represent hierarchical relationships. It starts with a root node and branches out to child nodes.

In Python, trees are commonly implemented using classes.


🔹 Basic Tree Terminology

Term Meaning
Root Topmost node
Child Node directly connected to another node
Parent Node that has branches to children
Leaf Node with no children
Subtree A tree within another tree
Binary Tree Tree where each node has up to 2 children

🔹 Creating a Tree Node in Python

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

🔹 Creating a Binary Tree

root = Node(10)
root.left = Node(5)
root.right = Node(15)

# Tree structure:
#      10
#     /  \
#    5    15

🔹 Tree Traversal Methods

🔸 Inorder (Left, Root, Right)
def inorder(node):
    if node:
        inorder(node.left)
        print(node.data, end=' ')
        inorder(node.right)

inorder(root)  # Output: 5 10 15

🔸 Preorder (Root, Left, Right)
def preorder(node):
    if node:
        print(node.data, end=' ')
        preorder(node.left)
        preorder(node.right)

🔸 Postorder (Left, Right, Root)
def postorder(node):
    if node:
        postorder(node.left)
        postorder(node.right)
        print(node.data, end=' ')

🔸 Level Order (Breadth-First)
from collections import deque

def level_order(root):
    if root is None:
        return
    queue = deque([root])
    while queue:
        current = queue.popleft()
        print(current.data, end=' ')
        if current.left:
            queue.append(current.left)
        if current.right:
            queue.append(current.right)

🔹 Use Cases of Trees

  • File systems

  • HTML/XML DOM

  • Databases (B-trees, AVL trees)

  • Routing tables

  • Decision-making algorithms


Practice Questions

Q1. Write a Python program to create a binary tree with root = 1 and two children 2 (left) and 3 (right).

Q2. Write a Python program to print all elements of the binary tree using Inorder traversal.

Q3. Write a Python program to count the total number of nodes in the binary tree.

Q4. Write a Python program to find the maximum value stored in the binary tree.

Q5. Write a Python program to add a new node to the left of a given node.

Q6. Write a Python program to traverse the binary tree using Postorder traversal.

Q7. Write a Python program to check if a binary tree is empty.

Q8. Write a Python program to find and print all leaf nodes of the binary tree.

Q9. Write a Python program to search for a specific value in the binary tree.

Q10. Write a Python program to convert a list of values into a binary tree structure (level-wise).


Python Trees Quiz

Q1: What is the top node of a tree called?

A. Head
B. Root
C. Parent
D. Leaf

Q2: In a binary tree, how many children can a node have?

A. 1
B. 2
C. 3
D. Unlimited

Q3: Which traversal visits root before children?

A. Inorder
B. Postorder
C. Preorder
D. Level order

Q4: What does Inorder traversal of a binary tree return?

A. Root only
B. Left → Root → Right
C. Root → Left → Right
D. Right → Root → Left

Q5: What type of traversal is used in BFS?

A. Inorder
B. Preorder
C. Level Order
D. Postorder

Q6: Which is a node with no children?

A. Root
B. Internal
C. Leaf
D. Branch

Q7: What data structure is used in level-order traversal?

A. Stack
B. Queue
C. List
D. Set

Q8: Which traversal is Left → Right → Root?

A. Preorder
B. Inorder
C. Postorder
D. Level order

Q9: Which tree type keeps data sorted?

A. Random Tree
B. Binary Search Tree
C. AVL Tree
D. Trie

Q10: Which Python module provides queues for BFS?

A. os
B. array
C. collections
D. random

Go Back Top