Lecture 1
Lecture 1
SCHOOL OF TECHNOLOGY
Computer Science : Year 2
Page 1 of 20
Trees
Introduction to Trees
In computer science, a tree is a non-linear data structure used to represent hierarchical
relationships. Unlike arrays, stacks, or queues (which are linear), trees branch out in
multiple directions.Imagine a tree data structure like a family tree or an organization
chart: at the top, there's a single "root" node representing the starting point or main idea.
From that root, branches extend downwards to child nodes, which are like family
members or departments, each connecting to their own smaller branches. These
connections form a clear hierarchy, with every node accessible through its parent, making
it easy to trace pathways from the top to any specific leaf — or endpoint — in the
structure. Think of it as a family tree showing how everything is connected in a neat,
branching way!
Below is a taxonomy tree starting from the kingdom Animalia, classifying living organisms
into more specific groups:
Page 2 of 20
Trees
🔹 Node
Definition: A data element in the tree.
Example: Mammal , Felidae , Homo , and Sapiens are all nodes.
Page 3 of 20
Trees
🔹 Edge
Definition: The connection between a parent and child.
Example: The line from Mammal to Primate represents an edge.
🔹 Siblings
Definition: Nodes that share the same parent.
Examples:
Domestica and Leo are siblings.
🔹 Leaf
Definition: A node with no children.
Examples:
Sapiens (Human), Troglodytes (Chimpanzee), Domestica (House
Cat), Leo (Lion), and Domestica (Housefly) are all leaf nodes.
🔹 Subtree
Definition: A smaller tree within a bigger one, starting from any node.
Example: The entire branch starting from Primate is a subtree.
Page 4 of 20
Trees
🔹 Depth
Definition: The number of edges from the root to the node.
Node Depth
Animalia 0
Chordate 1
Mammal 2
Primate 3
Hominidae 4
Homo 5
Sapiens 6
🔹 Height
Definition: The number of edges on the longest path from a node to a leaf node.
Node Height
Animalia 6
Chordate 5
Mammal 4
Primate 3
Hominidae 2
Homo 1
Sapiens 0
🔹 Level
Definition: The group of nodes at the same depth.
Page 5 of 20
Trees
Level Nodes
0 Animalia
1 Chordate, Arthropoda
2 Mammal, Insect
✍️ Practice Questions
1. What is the depth of Musca ?
Page 6 of 20
Trees
🔖 Labeled Trees
✅ What Is It?
A labeled tree is a tree where each node has a unique name or value. These labels help us
distinguish one node from another.
💡 Real-Life Example:
A family tree is labeled — each person has a name (like "Grandma", "Dad", "You").
text
Grandma
└── Dad
├── You
└── Sister
n−2
Total labeled trees = n
🧮 Example:
If you have 4 labeled nodes (say, A, B, C, D), the total number of labeled trees is:
4−2 2
4 = 4 = 16
So, you can make 16 different trees using 4 uniquely labeled nodes.
🚫 Unlabeled Trees
✅ What Is It?
An unlabeled tree is a tree where the nodes are not uniquely named. You only care about
the shape or structure of the tree — not the identity of nodes.
Page 7 of 20
Trees
💡 Real-Life Example:
Imagine an organization chart where we only care about the roles (e.g., Manager →
Employee), but we don't know who exactly the people are.
text
•
└── •
├── •
└── •
Each dot ( • ) is a node, but we don’t know or care which is which — we only care about
how they connect.
(2n)!
Cn =
(n + 1)! ⋅ n!
🧮 Example:
If you have 3 nodes, the number of possible unlabeled binary trees is:
(2×3)! 6! 720
C3 = = = = 5
(3 + 1)!×3! 4!×3! 24×6
So, you can make 5 different binary tree structures with 3 anonymous nodes.
Formula n
n−2
(Cayley) Catalan Numbers
Page 8 of 20
Trees
🏫 Classwork Activities
✍️ Exercise 2: Apply Cayley’s Formula
Question: How many labeled trees can you create with:
3 nodes
6 nodes
(2n)!
Cn =
(n + 1)! ⋅ n!
Questions:
1. How many unlabeled binary trees can be formed with:
2 nodes?
4 nodes?
🌳
Types of Trees in Data Structures
Understanding tree types helps us solve problems faster — just like knowing the types of
buildings helps you find a hospital vs. a school. Let’s look at each tree type, with simple
definitions and examples:
🌲 General Tree
Definition: A tree where each node can have any number of children.
🔹 Real-Life Analogy:
Think of a family: A parent can have 1 child, 2 children, or 10!
Page 9 of 20
Trees
✅ Key Point:
No rules on how many children a node must have.
🖼️ Example:
text
A
/ | \
B C D
/ \
E F
🌳 Binary Tree
Definition: Each node can have at most 2 children — usually called left and
right.
🔹 Real-Life Analogy:
Think of a yes/no question flowchart: every step leads to 2 choices only — Yes or No.
✅ Key Point:
Each node has 0, 1, or 2 children.
Helps organize data in a structured way.
🖼️ Example:
text
A
/ \
B C
/ \
D E
Page 10 of 20
Trees
🔹 Real-Life Analogy:
Think of a dictionary:
✅ Key Point:
Helps in fast searching (like binary search).
Always sorted automatically.
🖼️ Example:
text
8
/ \
4 10
/ \ \
2 6 12
⚖️ Balanced Trees
Definition: Trees that keep their height small to make searching faster.
🔹 Real-Life Analogy:
Imagine searching books on shelves. If the shelf is tall and thin, you climb more. If it's
short and wide, you find books faster.
Page 11 of 20
Trees
✅ Key Point:
Designed to stay “not too tall”.
Examples:
AVL Tree
Red-Black Tree
🖼️ Unbalanced vs Balanced:
text
Unbalanced BST: Balanced Tree:
1 4
\ / \
2 2 6
\ / \ / \
3 1 3 5 7
✅ Summary Table
Tree Type Children per Special Rule Use Case
Node
General Tree Any number None Family tree, HTML
DOM
Great! Here are 5 multiple-choice questions (MCQs) based on the Types of Trees — clear,
beginner-friendly, and perfect for classroom or homework use.
Page 12 of 20
Trees
Question 2:
In a Binary Search Tree (BST), where is a node with a larger value than the parent usually
placed?
A. To the left
B. To the right
C. Anywhere
D. At the root
Question 3:
Which of these trees is designed to maintain a low height for efficient searching?
A. General Tree
B. Binary Tree
C. Balanced Tree
D. Family Tree
Question 4:
Which type of tree allows each node to have any number of children?
A. Binary Tree
B. Binary Search Tree
Page 13 of 20
Trees
C. Balanced Tree
D. General Tree
Question 5:
What is a real-world example of a General Tree?
📌 A. Pre-order Traversal
Visit Order: Root → Left Subtree → Right Subtree
📌 B. In-order Traversal
Visit Order: Left Subtree → Root → Right Subtree
⚠️ Note: In-order traversal is most useful for Binary Search Trees because it
returns nodes in sorted order. Our tree isn’t binary, so in-order isn't as
meaningful here, but here’s the logic:
✔ In our case, in-order would only apply meaningfully if each node had exactly 2
children, which it doesn’t. So it’s not commonly used for general trees like the book
structure.
📌 C. Post-order Traversal
Visit Order: Left Subtree → Right Subtree → Root
Page 15 of 20
Trees
🔑 Summary Table:
Traversal Type Visit Order Good For
Pre-order (DFS) Root → Left → Right Copying trees, prefix
expression
Page 16 of 20
Trees
Example
3. Right Subtree of A: F → G → H, I
4. Leaves: D, E, H, I (nodes with no children).
Page 17 of 20
Trees
Page 18 of 20
Trees
2. Visit A (root).
3. Explore the right subtree (F → G → H, I).
Final In-Order:
D→C→E→B→A→H→G→I→F
Visit D → E → C → B.
Visit H → I → G → F.
Final Post-Order:
D→E→C→B→H→I→G→F→A
Pre-Order A→B→C→D→E→F→G→H→I
In-Order D→C→E→B→A→H→G→I→F
Page 19 of 20
Trees
classwork
Page 20 of 20