📖 Scenario: You are building a simple binary tree to represent a family tree. Each node will hold a person's name. You will create the tree manually by linking nodes together.
🎯 Goal: Build a binary tree manually by creating nodes and linking them as left and right children. Then print the tree nodes in a simple format.
📋 What You'll Learn
Create a class called Node with attributes name, left, and right
Create nodes for "Grandparent", "Parent1", "Parent2", "Child1", and "Child2"
Link the nodes to form a binary tree with Grandparent as root
Print the names of the nodes in the tree in preorder traversal (root, left, right)
💡 Why This Matters
🌍 Real World
Binary trees are used in many areas like organizing family trees, file systems, and decision-making processes.
💼 Career
Understanding how to create and traverse trees is fundamental for software developers working with data structures, databases, and algorithms.
Progress0 / 4 steps
1
Create the Node class
Create a class called Node with an __init__ method that takes name as a parameter and sets self.name, self.left, and self.right to None.
DSA Python
Hint
Think of Node as a box that holds a name and has two empty slots for children.
2
Create nodes for family members
Create five variables: grandparent, parent1, parent2, child1, and child2. Assign each a Node object with names "Grandparent", "Parent1", "Parent2", "Child1", and "Child2" respectively.
DSA Python
Hint
Use the Node class to create each family member as a node.
3
Link the nodes to form the binary tree
Link the nodes by setting grandparent.left to parent1 and grandparent.right to parent2. Then set parent1.left to child1 and parent1.right to child2.
DSA Python
Hint
Think of left and right as branches connecting family members.
4
Print the tree nodes in preorder traversal
Define a function called preorder that takes a node parameter. It should print node.name, then recursively call itself on node.left and node.right if they exist. Then call preorder(grandparent) to print the tree.
DSA Python
Hint
Preorder means visit the current node first, then left child, then right child.
Practice
(1/5)
1. What is the correct way to create a root node manually in a binary tree using a simple class with attributes data, left, and right?
easy
A. root = Node(10) # creates root with data 10 and no children
B. root = Node() # creates root with no data
C. root = Node.left(10) # creates left child with data 10
D. root = Node.right(10) # creates right child with data 10
Solution
Step 1: Understand Node class constructor
The Node class constructor takes data as an argument and initializes left and right children as None.
Step 2: Create root node with data
Using root = Node(10) creates a root node with data 10 and no children.
Final Answer:
root = Node(10) # creates root with data 10 and no children -> Option A
Quick Check:
Create root node with data = root = Node(10) [OK]
Hint: Use Node(data) to create a node with data and empty children [OK]
Common Mistakes:
Trying to create children directly without root
Calling left or right as methods
Creating node without data when constructor requires it
2. Which of the following code snippets correctly assigns a left child node with data 5 to an existing root node named root?
easy
A. root.left = 5
B. root.left(5)
C. root.left.data = 5
D. root.left = Node(5)
Solution
Step 1: Assign a new Node object to root.left
To add a left child, assign a new Node instance with data 5 to root.left.
Step 2: Check syntax correctness
root.left = Node(5) correctly creates and assigns the left child node.
Final Answer:
root.left = Node(5) -> Option D
Quick Check:
Assign left child node = root.left = Node(5) [OK]
Hint: Assign Node(data) to left or right attribute to add child [OK]
Common Mistakes:
Calling left as a function instead of assigning
Assigning data directly without creating Node
Assigning integer directly to left attribute
3. Given the following code, what is the printed output representing the tree structure?