Lecture 9 Week 11 Intro Trees 26112024 121749am
Lecture 9 Week 11 Intro Trees 26112024 121749am
Nodes directly accessible (using one arc) from a node are called
the children of that node, which is called the parent of these
children; these nodes are siblings of each other.
BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Tree
1 root
2 3
8 9
leaves
BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Tree Terminologies A
B C
Root
• It is the key node of a tree structure that does not have parent. It is the
first node in hierarchical arrangement.
Node
• The node of a tree stores the data and its role is the same as in the
linked list. Nodes are connected by the means of links with other nodes.
Parent
• It is the immediate predecessor of a node. In the figure A is the parent
of B and C.
B C
Child
• All successor nodes are called child nodes. In the figure B and C are the child
nodes of A
Sibling
• The child node of same parent are called sibling.
Link / Edge
• An edge connects the two nodes. The line drawn from one node to other
node is called edge / link. Link is nothing but a pointer to node in a tree
structure.
Leaf / Terminal Node
• This node is located at the end of the tree. It does not have any child hence it
is called leaf node.
BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Tree Terminologies
Level
• Level is the rank of tree hierarchy. The
whole tree structure is leveled. The level of
the root node is always at 0. The immediate
children of root are at level 1 and their Root node
children are at level 2 and so on.
Height
Depth Interior nodes
• The depth of a node n is the length of the
Leaf nodes
path from the root to the node.
• The depth (or height) of a tree is the length
of the path from the root to the deepest
node in the tree.
BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Levels and Depth
Root
Level 0 Node
Level 1
Arc
Level 2
Level 3
Leaf
BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Levels and Depth
root
O
M T
left subtree right subtree
of node “O” of node “O”
C E • P U
•
•
76 99
26 85 26
21 50 80 99 21 76
26
50 85
76
80
85
99
BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Binary Trees
If A is the root of a binary tree and B is the root of its left or right subtree,
then A is said to be father of B, and B is said to be the left or right son of A.
D E • F G
•
•
left son of B right son of B
B C
D E F G
leaves
A
A is ancestor of D
B C H is descendent of B
D E F G G is ancestor of J
F is ___ of H ???
H I J
Two nodes are brothers if they are left and right sons of the same
father.
A
G is right descendent of A.
B C
E is left descendent of A.
H is right descendent of B.
D E F G
I is right descendent of C.
H I J
H is left descendent of E.
B C
D E F
Left Subtree of A
G H I
BS (CS) – Data Structures & Algorithms Right Subtree of A Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Examples – familiarization with notation
A
An Edge
B C
D E F
G H I
Child nodes of A? A
B C
D E F
G H I
B C
D E F
G H I
B C
D E F
G H I
Ancestors of Node D?
A
Ancestors of Node H?
B C
D E F
G H I
Path: B C
If n1, n2,…nk is a sequence of nodes such
that ni is the parent ( child) of ni+1, then F
that sequence is a path. D E
G H I
J
BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Examples – familiarization with notation
Leaf node: A
Any node that has empty children
B C
D E F
G H I
Internal node: A
Any node that has at least one
non-emptyChild B C
D E F
G H I
Three nodes