1. CSC303: Data Structures and Algorithms
Audience: B. Tech. (CSE), Semester III
Instructor: Dr. Mamata Dalui
The study materials/presentations used in this course are solely meant for academic
purposes and collected from different available course materials, slides, books, etc.
Assistant Professor
Department of Computer Science and Engineering
National Institute of Technology Durgapur
5. Tree
• Definition (recursive): A tree is a finite set of one or more nodes
such that
• There is a specially designated node called root.
• The remaining nodes are partitioned into n>=0 disjoint set T1,…,Tn,
where each of these sets is a tree. T1,…,Tn are called the subtrees of the
root.
• Every node in the tree is the root of some subtree
r
T2
T1 Tn
Root
(designated node)
…
6. • The root of each subtree T1, T2 …,Tn is said to be a child of r and r is the parent.
• Trees are drawn like graphs, where there is an edge (sometimes directed) from
a node to each of its children.
Tree
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
r
T2
T1 Tn
Root
(designated node)
…
level 0
level 1
level 2
level 3
A
B C D
E F G H I
J K L M
Subtrees
Root
7. Some Terminologies
• Node: A node of the tree stores the item of
information plus the links to other nodes.
Example: A, B, C, …, M are the nodes of
this tree.
• Degree: The maximum number of subtrees
of a node is called the degree of the node.
Example: Degree of node A is 3, that of B is
2 and C is 1. Degrees of the nodes E, J, K, L,
G, M & I all are 0.
• Degree of a tree: The maximum of the
degree of the nodes in the tree is called the
degree of the tree.
Example: Here, degree of the tree is 3.
Tree
level 0
level 1
level 2
level 3
A
B C D
E F G H I
J K L M
Subtrees
Root
8. Some Terminologies (cont’d)
• Terminal nodes (or leaf): The nodes of the tree
that have degree zero, are called terminal or
leaf nodes.
Example: Here, E, J, K, L, G, M & I are the
leaf nodes.
• Nonterminal nodes: The nodes that don’t
belong to terminal nodes.
Example: A, B, C, D, F, H are the non-terminal
nodes.
• Children: The roots of the subtrees of a node
are the children of that node. The children of a
node X are the immediate successors of X.
Example: Here, B, C, D are the children of A.
Tree
level 0
level 1
level 2
level 3
A
B C D
E F G H I
J K L M
Subtrees
Root
9. Some Terminology (cont’d)
• Parent: The immediate predecessor of a node is the
parent of the node.
Example: A is the parent of B, C & D. Similarly, F
is the parent of J, K, L and C is the parent of G.
• Siblings: The children of the same parent are said to
be siblings of each other.
Example: B, C, & D are the siblings of each other.
• Ancestors of a node: All the nodes along the path
from the root to that node are the ancestors of that
node.
Example: Here, F, B, A are the ancestors of L.
• Descendants of a node: All the nodes along the path
from the node to any leaf node are the descendants
of that node.
Example: Here, H, M, I are the descendants of D.
Tree
level 0
level 1
level 2
level 3
A
B C D
E F G H I
J K L M
Subtrees
Root
10. Some Terminology (cont’d)
• The level of a node: The level is the rank in
the hierarchy defined by letting the root to be
at level zero. If a node is at level l, then it
children are at level l+1.
Example: Root A is at level 0, level of nodes
B, C & D is 1.
• Height (or depth): The maximum number of
nodes in a path from the root node to a leaf is
called the height of the tree. Thus h=lmax+1,
where h is the height of the tree and lmax is
the maximum level of the tree.
Example: Here, the height of this tree is 4.
Tree
level 0
level 1
level 2
level 3
A
B C D
E F G H I
J K L M
Subtrees
Root
11. Representation of Trees
• List Representation
• We can represent a tree as a list in which each of the subtrees is
also a list
• The root comes first, then followed by a list of sub-trees
( A ( B ( E, F ( J, K, L ) ), C ( G ), D ( H ( M ), I ) )
A
B C D
E F G H I
J K L M
Root
12. • Left Child-Right Sibling Representation
A
B C D
E F G H I
J K L M
A
B C D
E F G H I
J K L M
Representation of Trees
• In this representation, instead of having each node storing pointers to all of its
children, a node will store pointer to just one of its child, the left child.
• Apart from this the node will also store a pointer to its immediate right sibling.
Original tree Left child right sibling representation of the tree
13. • Binary trees are characterized by the fact that any node can have at
most two branches.
• Definition (recursive):
• A binary tree is a finite set of nodes that is either empty or consists of a
root and two disjoint binary trees called the left subtree and the right
subtree.
A
B C
D F
H I
E G
J K
Binary Tree
Binary tree
14. Varients of binary trees:
(a) strictly binary tree (b) complete binary tree (c) almost
complete binary tree (d) skewed tree
(a) Strictly binary tree – If every non-leaf node in a binary tree is having non-empty left and
right subtrees, the tree is strictly binary tree.
A
B C
D E
F G
Level 0
Level 1
Level 2
Level 3
Binary Tree
15. (b) Complete binary tree – A complete binary tree of height h is the strictly binary tree all
whose leaves are at level h.
A
B C
D F
H I
Level 0
Level 1
Level 2
Level 3
E G
J K L M N O
Binary Tree
16. (c) Almost complete binary tree – A binary tree of height h is an almost complete binary tree if:
1. Any node nh at level less than h-1 has two children
2. For any node nh in the tree with a right descendant at level h, nh must have a left child and every
left descendant of nh is either a leaf at level h or has two children
A
B C
D F
H I
Level 0
Level 1
Level 2
Level 3
E G
J
Binary Tree
17. Binary Tree
A
B C
D E
F G
(a)
A
B C
D F
H I
E G
J
A
B C
D F
H I
E G
J K
A
B C
D F
H I
E G
(a) Strictly binary tree,
but not an almost
complete binary tree,
since leaf nodes are at
levels 1, 2, 3 (Cond. 1
doesn’t satisfy)
(b)
(b) Strictly binary tree, but
not an almost complete
binary tree, since Cond 1
is true but Cond 2 fails as
node A has right
descendant at level 3 (J)
but also has a left
descendant at level 2 (E)
(c)
(c) Strictly binary tree,
satisfies Cond 1 and 2,
hence is an almost
complete binary tree
(d) Almost complete binary
tree, but not a strictly binary
tree, since node E has a left
child but not right one
(d)
18. (d) Skewed binary tree – A skewed binary tree is a type of binary tree in which all the nodes have only
either one child or no child.
A
B
C
D
Binary Tree
Two special types of Skewed Binary trees:
Left Skewed Binary Tree: Left skewed binary trees are those in which all the nodes are having either a left child
or no child at all. These are left side dominated trees wherein all the right children remain as null.
A
B
C
D
Right Skewed Binary Tree: Right skewed binary trees are those in which all the nodes are having either a right
child or no child at all. These are right side dominated trees wherein all the left children remain as null.
(a) Left-skewed binary tree (b) Right-skewed binary tree
19. CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
Thank You
Questions?