SlideShare a Scribd company logo
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
Trees (Part I)
• Faster than linear lists
• More natural fit for some kinds of data
Why a tree?
Example Tree
root
Activities
Teaching Research
Faculty Home Page
Projects Publications
CSE2003
CSC303
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)
…
• 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
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
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
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
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
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
• 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
• 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
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
(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
(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
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)
(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
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
Thank You
Questions?

More Related Content

PPTX
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
PPTX
Data structure using c module 2
PPTX
Introduction to tree ds
PDF
Tree terminology and introduction to binary tree
PPT
data_structures_and_applications_-_module-4.ppt
PPT
PPTX
Lecture 21_Trees - I.pptx
PPTX
Tree Data Structure Tree Data Structure Details
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
Data structure using c module 2
Introduction to tree ds
Tree terminology and introduction to binary tree
data_structures_and_applications_-_module-4.ppt
Lecture 21_Trees - I.pptx
Tree Data Structure Tree Data Structure Details

Similar to tree basics to advanced all concepts part 1 (20)

PPTX
TERMINOLOGIES OF TREE, TYPES OF TREE.pptx
PPTX
non linear data structure -introduction of tree
PPT
Chapter 8 ds
PPTX
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
PPTX
binary tree.pptx
PPTX
Data Structures -Non Linear DS-Basics ofTrees
PPT
Tree 11.ppt
PPT
Introduction To Binary Search Trees .ppt
PPTX
BCS304 Module 3 Slide 29-61.pptx DSA notes 3rd sem
PPTX
Lecture 2-Trees in Data Structure Complete Lecture Slide
PPTX
Binary Trees - Tree Terminologies and representation
PPTX
Data Structure of computer science and technology
PPTX
TreesTreesTreesTreesTreesTreesTrees.pptx
PPTX
Tree.pptx
PPTX
PPT
Lecture 5 tree.pptx
PPTX
Module 4.pptx
PPTX
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
PPT
Unit iii(dsc++)
PDF
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure
TERMINOLOGIES OF TREE, TYPES OF TREE.pptx
non linear data structure -introduction of tree
Chapter 8 ds
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
binary tree.pptx
Data Structures -Non Linear DS-Basics ofTrees
Tree 11.ppt
Introduction To Binary Search Trees .ppt
BCS304 Module 3 Slide 29-61.pptx DSA notes 3rd sem
Lecture 2-Trees in Data Structure Complete Lecture Slide
Binary Trees - Tree Terminologies and representation
Data Structure of computer science and technology
TreesTreesTreesTreesTreesTreesTrees.pptx
Tree.pptx
Lecture 5 tree.pptx
Module 4.pptx
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
Unit iii(dsc++)
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure
Ad

Recently uploaded (20)

PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPT
Occupational Health and Safety Management System
PPTX
communication and presentation skills 01
PPTX
Nature of X-rays, X- Ray Equipment, Fluoroscopy
PPTX
Current and future trends in Computer Vision.pptx
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
Abrasive, erosive and cavitation wear.pdf
PDF
86236642-Electric-Loco-Shed.pdf jfkduklg
PDF
Integrating Fractal Dimension and Time Series Analysis for Optimized Hyperspe...
PPTX
Fundamentals of Mechanical Engineering.pptx
PPTX
Information Storage and Retrieval Techniques Unit III
PDF
737-MAX_SRG.pdf student reference guides
PPT
introduction to datamining and warehousing
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Occupational Health and Safety Management System
communication and presentation skills 01
Nature of X-rays, X- Ray Equipment, Fluoroscopy
Current and future trends in Computer Vision.pptx
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Abrasive, erosive and cavitation wear.pdf
86236642-Electric-Loco-Shed.pdf jfkduklg
Integrating Fractal Dimension and Time Series Analysis for Optimized Hyperspe...
Fundamentals of Mechanical Engineering.pptx
Information Storage and Retrieval Techniques Unit III
737-MAX_SRG.pdf student reference guides
introduction to datamining and warehousing
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Safety Seminar civil to be ensured for safe working.
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Ad

tree basics to advanced all concepts part 1

  • 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
  • 3. • Faster than linear lists • More natural fit for some kinds of data Why a tree?
  • 4. Example Tree root Activities Teaching Research Faculty Home Page Projects Publications CSE2003 CSC303
  • 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?