Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a tree in computing?
A tree is a way to organize data where items are connected like branches. It starts with one main item called the root, and from there, it splits into smaller parts called nodes, like branches on a real tree.
Click to reveal answer
beginner
What is a root node?
The root node is the very first node in a tree. It is the starting point from which all other nodes branch out, just like the trunk of a real tree.
Click to reveal answer
beginner
Explain parent and child nodes in a tree.
In a tree, a parent node is a node that has one or more nodes connected below it. These connected nodes are called child nodes. Think of a parent as a boss and children as workers reporting to that boss.
Click to reveal answer
beginner
What is a leaf node?
A leaf node is a node that does not have any children. It is like the tip of a branch on a tree with no smaller branches coming out from it.
Click to reveal answer
intermediate
How is hierarchical data represented using trees?
Hierarchical data is shown as a tree where each level represents a rank or category. The top level is the root, and each lower level shows more detailed parts, like a family tree showing grandparents, parents, and children.
Click to reveal answer
What is the top node of a tree called?
ARoot node
BLeaf node
CChild node
DBranch node
✗ Incorrect
The root node is the top node from which all other nodes branch out.
Which node has no children?
ARoot node
BLeaf node
CParent node
DSibling node
✗ Incorrect
Leaf nodes are nodes without any children, like the ends of branches.
In a tree, what do we call nodes directly connected below a parent node?
AChildren
BSiblings
CRoots
DLeaves
✗ Incorrect
Child nodes are directly connected below a parent node.
Which of the following best describes hierarchical data?
AData arranged randomly
BData arranged in a flat list
CData arranged in levels like a tree
DData arranged in a circle
✗ Incorrect
Hierarchical data is arranged in levels, similar to a tree structure.
What is the relationship between parent and child nodes?
AThey are not connected
BThey are the same node
CChild node is above parent node
DParent node is above child node in the tree
✗ Incorrect
Parent nodes are above child nodes in the tree structure.
Describe the main parts of a tree data structure and how they relate to each other.
Think about how a family tree is organized.
You got /5 concepts.
Explain how hierarchical data can be visualized using a tree and give a real-life example.
Imagine organizing files on your computer or a family tree.
You got /4 concepts.
Practice
(1/5)
1. Which of the following best describes a root node in a tree structure?
easy
A. Any node that has siblings
B. A node with no children
C. A node that connects two branches
D. The top node with no parent
Solution
Step 1: Understand the root node concept
The root node is the starting point of a tree and has no parent node above it.
Step 2: Differentiate root from other nodes
Leaves have no children, siblings share the same parent, and connecting nodes are internal nodes, not necessarily root.
Final Answer:
The top node with no parent -> Option D
Quick Check:
Root = top node with no parent [OK]
Hint: Root node always has no parent node [OK]
Common Mistakes:
Confusing root with leaf nodes
Thinking root has siblings
Assuming root connects branches only
2. Which of the following is the correct way to represent a simple tree node in Python using a class?
easy
A. class Node:
def __init__(self, value):
self.value = value
self.children = []
B. class Node:
def __init__(self, value):
self.value = value
self.parent = None
self.children = None
C. class Node:
def __init__(self, value):
self.value = value
self.children = None
D. class Node:
def __init__(self, value):
self.value = value
self.children = 0
Solution
Step 1: Identify proper children initialization
Children should be a list to hold multiple child nodes, so initializing with an empty list is correct.
Step 2: Check other attributes
class Node:
def __init__(self, value):
self.value = value
self.children = [] correctly sets value and children as a list; other options set children to None or 0, which is incorrect for multiple children.
Final Answer:
class Node:
def __init__(self, value):
self.value = value
self.children = [] -> Option A
Quick Check:
Children list initialized as [] for multiple children [OK]
Hint: Children must be a list to hold multiple nodes [OK]
Common Mistakes:
Setting children to None or 0 instead of a list
Forgetting to initialize children
Confusing parent and children attributes
3. Given the tree structure below, what is the output of a preorder traversal?
A. ["Root", "Child1", "Grandchild1", "Grandchild2", "Child2"]
B. ["Root", "Child2", "Child1", "Grandchild1", "Grandchild2"]
C. ["Grandchild1", "Grandchild2", "Child1", "Child2", "Root"]
D. ["Child1", "Grandchild1", "Grandchild2", "Child2", "Root"]
Solution
Step 1: Understand preorder traversal order
Preorder visits the root first, then recursively visits each child from left to right.
Step 2: Apply preorder to given tree
Visit Root, then Child1, then Grandchild1, Grandchild2, and finally Child2.
Final Answer:
["Root", "Child1", "Grandchild1", "Grandchild2", "Child2"] -> Option A
Quick Check:
Preorder = root, left to right children [OK]
Hint: Preorder = root first, then children left to right [OK]
Common Mistakes:
Mixing preorder with postorder or inorder
Visiting children in wrong order
Starting traversal from a child node
4. Consider this Python code snippet for adding a child node to a tree node:
class Node:
def __init__(self, value):
self.value = value
self.children = []
root = Node('root')
child = Node('child')
root.children.append(child.value)
What is the problem with this code?
medium
A. The children list is not initialized properly
B. It appends the child's value instead of the child node itself
C. The root node is missing a parent attribute
D. The child node is not created correctly
Solution
Step 1: Analyze the append operation
The code appends child.value (a string) instead of the child node object itself.
Step 2: Understand why this is a problem
Appending the value loses the child node's structure and children; the tree should store node objects, not just values.
Final Answer:
It appends the child's value instead of the child node itself -> Option B
Quick Check:
Append node objects, not just values [OK]
Hint: Append node objects, not just their values [OK]
Common Mistakes:
Appending values instead of nodes
Confusing node attributes with node objects
Ignoring tree structure integrity
5. You have a company hierarchy tree where each node stores an employee's name and their direct reports as children. How would you write a function to find all employees under a given manager (including indirect reports)?
hard
A. Use a function that returns only the manager's name
B. Use a loop that only collects direct children once
C. Use a recursive function that collects children and their descendants
D. Use a function that counts the number of children without listing them
Solution
Step 1: Understand the problem of indirect reports
Indirect reports are children of children, so a simple loop over direct children is not enough.
Step 2: Use recursion to collect all descendants
A recursive function visits each child, then calls itself on that child to collect deeper descendants, gathering all employees under the manager.
Final Answer:
Use a recursive function that collects children and their descendants -> Option C
Quick Check:
Recursion collects all descendants in a tree [OK]
Hint: Recursion gathers all levels of children in a tree [OK]