📖 Scenario: Imagine you are building a simple family tree app. Each person can have up to two children: a left child and a right child. To represent this, you will create a binary tree node structure.
🎯 Goal: Create a binary tree node class in Python that can hold a person's name and links to their left and right children. Then, create a small tree and print the root node's name.
📋 What You'll Learn
Create a class called TreeNode with an __init__ method
The __init__ method must accept name, left, and right parameters
The left and right parameters should default to None
Create an instance of TreeNode called root with the name 'Grandparent'
Print the name attribute of the root node
💡 Why This Matters
🌍 Real World
Binary trees are used in many applications like family trees, decision trees, and organizing data for quick search.
💼 Career
Understanding how to create and link nodes in a binary tree is a foundational skill for software developers working with data structures.
Progress0 / 4 steps
1
Create the TreeNode class
Create a class called TreeNode with an __init__ method that takes name, left, and right parameters. Set left and right to None by default. Inside __init__, assign these parameters to instance variables self.name, self.left, and self.right.
DSA Python
Hint
Remember to use def __init__(self, ...) to create the constructor and assign parameters to self variables.
2
Create the root node
Create an instance of TreeNode called root with the name set to 'Grandparent'. Do not set left or right yet.
DSA Python
Hint
Use root = TreeNode('Grandparent') to create the root node.
3
Add children to the root node
Create two more TreeNode instances called left_child with name 'Parent1' and right_child with name 'Parent2'. Then, assign left_child to root.left and right_child to root.right.
DSA Python
Hint
Create child nodes and assign them to root.left and root.right.
4
Print the root node's name
Print the name attribute of the root node using print(root.name).
DSA Python
Hint
Use print(root.name) to show the root node's name.
Practice
(1/5)
1. What does a typical binary tree node contain in its structure?
easy
A. A value and a reference to the parent node only
B. Only a value and a reference to the next node
C. A value, a reference to the left child, and a reference to the right child
D. A value and a list of all children nodes
Solution
Step 1: Understand binary tree node components
A binary tree node typically stores a value and has two child references: left and right.
Step 2: Compare options with definition
A value, a reference to the left child, and a reference to the right child matches this structure exactly, while others describe different data structures.
Final Answer:
A value, a reference to the left child, and a reference to the right child -> Option C
Quick Check:
Binary tree node = value + left + right [OK]
Hint: Binary tree nodes always have left and right child pointers [OK]
Common Mistakes:
Confusing binary tree node with linked list node
Thinking nodes have parent pointers by default
Assuming children are stored in a list
2. Which of the following is the correct Python syntax to define a binary tree node class with attributes val, left, and right?
easy
A. class Node:
def __init__(self, val):
val = self
left = None
right = None
B. class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
C. class Node:
def __init__(self):
self.val = None
self.left = None
self.right = None
D. class Node:
def __init__(self, val, left, right):
self.val = val
self.left = left
self.right = right
Solution
Step 1: Check constructor parameters and attribute assignments
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None correctly initializes val and sets left and right to None by default.
Step 2: Validate syntax correctness
class Node:
def __init__(self, val):
val = self
left = None
right = None incorrectly assigns variables; class Node:
def __init__(self):
self.val = None
self.left = None
self.right = None misses val parameter; class Node:
def __init__(self, val, left, right):
self.val = val
self.left = left
self.right = right requires left and right on init which is less common.
Final Answer:
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None -> Option B
Quick Check:
Constructor sets val and initializes children to None [OK]
Hint: Init val, set left and right to None inside __init__ [OK]
Common Mistakes:
Not using self to assign attributes
Forgetting to initialize left and right
Incorrect parameter order or missing parameters
3. What will be the output of the following code snippet?
Root node has value 10; left child is assigned a node with value 5; right child assigned node with value 15.
Step 2: Print left and right child values
Printing root.left.val and root.right.val outputs 5 and 15 respectively.
Final Answer:
5 15 -> Option D
Quick Check:
Left child val = 5, Right child val = 15 [OK]
Hint: Print left.val and right.val after assignment to get child values [OK]
Common Mistakes:
Printing root.val instead of children
Confusing left and right child values
Assuming children are None without assignment
4. Identify the error in the following binary tree node class definition:
class Node:
def __init__(self, val):
val = self.val
left = None
right = None
medium
A. Attributes are assigned incorrectly; should use self.val, self.left, self.right
B. Missing return statement in __init__ method
C. Class name should be lowercase
D. No error; code is correct
Solution
Step 1: Review attribute assignments in __init__
Attributes must be assigned to self to belong to the instance, e.g., self.val = val.
Step 2: Identify incorrect assignments
Code assigns val = self.val which reverses assignment and does not use self for left and right.
Final Answer:
Attributes must be assigned using self, not local variables -> Option A
Quick Check:
Use self.attr = value to set instance variables [OK]
Hint: Always assign attributes with self.attribute = value [OK]
Common Mistakes:
Assigning variables without self prefix
Reversing assignment order
Expecting return in __init__
5. Given the following binary tree node class, how would you create a tree with root value 1, left child 2, and right child 3, then print the root and its children values?
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None