SlideShare a Scribd company logo
Introduction to Tree
1
So far we have discussed many linear data structures
Lecture 15 – Tree Terminologies
Data Structure
Unit 3: Linked List
• A non-linear data structure - Tree
• Trees are mainly used to represent data containing a hierarchical
relationship between elements, for example, records, family trees and table
of contents.
Introduction to Tree continued..
Lecture 15 – Tree Terminologies
Introduction to Tree continued..
3
 Tree consists of a finite set of elements called nodes and a finite set of directed
lines, called edges that connect the nodes.
 A tree is an abstract model of a hierarchical structure that consists of nodes with a
parent-child relationship.
 There is a starting node known as a root node.
 There is one and only one path between every pair of nodes in a tree.
 A tree with n nodes has exactly (n-1) edges.
edge
root
node
Lecture 15 – Tree Terminologies
Tree Terminologies
4
root
A is parent of B and C
C is parent of D and E
D and E are children of C
Root:- The first node from where the tree originates is called as a root
node.
In any tree, there must be only one root node.
Parent node:- A node is parent node, if it has successor nodes
Child node:- A node with predecessor is called as a child node
Leaf:- A leaf node is any node with no children.
B, D and E are leaf nodes
Lecture 15 – Tree Terminologies
Terminologies
5
root
Leaf Node: D,I,J,F,K,H
Internal nodes:- The nodes that are not a root or a leaf are known as internal nodes.
Sibling:- two or more nodes with same parents are called as siblings.
D,E and F are Siblings
Internal Node: B,C,E,G
G and H are Siblings
B and C are Siblings
Lecture 15 – Tree Terminologies
Tree Terminologies contiuned..
6
e. g. A – B -– D
A – B – E – I
Path:- A path is a sequence of nodes in which each node is adjacent to the next one.
It is the sequence of consecutive edges from source node to destination node.
Lecture 15 – Tree Terminologies
Tree Terminologies contiuned..
7
Consider a path A- B- E- I
Here for node E , node B and A
are ancestors
Node E and I are
descendent of node B
Ancestor:- An ancestor is any node in the path from root to the node.
Descendent:- a descendent is any node in the path below the parent node. That
means all nodes in the paths from a given node to leaf are descendent of the node.
Lecture 15 – Tree Terminologies
Tree Terminologies contiuned..
8
Level of node A is 0
Level of node B and C is 1
Level:- The level of a node is its distance from the root node
i.e. root is at level 0, children of root at level 1 and so on.
Level of node D,E,F,G and H is 2
Level of node I , J and K is 3
Lecture 15 – Tree Terminologies
Tree Terminologies contiuned..
9
Degree of node A=2
Degree of node B=3
Degree: Degree of a node is equal to the number of children that a node has.
The degree of a leaf node is zero.
In-degree: In-degree of a node is the number of edges arriving at that node.
Out-degree: Out-degree of a node is the number of edges leaving that node.
Degree of node E=2
Degree of node J=0
Lecture 15 – Tree Terminologies
Tree Terminologies contiuned..
10
Height of node B is 2
Height of node E is 1
Height of a node: The height of a node is the max path length form that node to a leaf
node.
Height of a tree: The height of a tree is the height of the root
Height of tree = Height of
node A=3
Lecture 15 – Tree Terminologies
Tree Terminologies contiuned..
11
Depth of node B is 1
Depth of node E is 2
Total number of edges from root node to a particular node is called as depth of that
node.
Depth of a tree: Depth of a tree is the max level of any leaf in the tree
Depth of tree is 3
Lecture 15 – Tree Terminologies
Tree Terminologies contiuned..
12
Subtree : Any node of a tree, with all of its descendants is a subtree.
Lecture 15 – Tree Terminologies
Binary Tree
13
•In general, tree nodes can have any number of children.
• In a binary tree, each node can have at most two children. A binary tree is either
empty or consists of a node called the root together with two binary trees called
the left subtree and the right subtree.
•A tree with no nodes is called as a null tree.
.
Lecture 15 – Tree Terminologies
Examples of Trees
1 2
3
4
Examples of Binary Trees
Types of Binary Tree
16
1. Strictly Binary Tree
If every non-leaf node in a binary tree consists of non-empty left sub-tree and
right sub-tree then such a tree is called as strictly binary tree
Lecture 15 – Tree Terminologies
Types of Binary Tree
17
2. Complete Binary Tree
A binary tree with n nodes and of depth d is a strictly binary tree in which all leaf
nodes are at level d is called as complete binary tree.
Lecture 15 – Tree Terminologies
Array Representation
Binary trees can also be stored in arrays
In this arrangement, if a node has an index i, its children are found at
indices 2i+1 and 2i+2, while its parent (if any) is found at index floor((i-
1)/2) (assuming the root of the tree stored in the array at an index
zero).
more compact storage and better locality of reference
requires contiguous memory
expensive to grow and wastes space proportional
Binary Tree Representation
Lecture 15 – Tree Terminologies
18
Index element
Example 1 : Array Representation
Lecture 15 – Tree Terminologies
19
0 A
1 B
2
C
3 D
4 E
5
F
6 G
7
H
8
I
Children at indices 2i+1 and 2i+2, while its
parent (if any) is found at index floor((i-1)/2)
9
10
Index element
Example 2: Array Representation
Lecture 15 – Tree Terminologies
20
0 A
1 B
2
-
3 C
4 -
5 -
6 -
7 D
8
Disadvantages : (1) waste space
Children at indices 2i+1 and 2i+2, while its
parent (if any) is found at index floor((i-1)/2)
-
Array representation is good for complete binary tree, but it is wasteful for
many other binary trees.
The representation suffers from insertion and deletion of node from the
middle of the tree.
To overcome this difficulty we represent the binary tree in linked
representation.
Limitations: Array Representation
Lecture 15 – Tree Terminologies
21
In linked representation each node in a binary has three fields, the left
child field denoted as LeftChild, data field denoted as data and the right
child field denoted as RightChild.
If any sub-tree is empty then the corresponding pointer’s LeftChild and
RightChild will store a NULL value.
If the tree itself is empty the root pointer will store a NULL value.
Linked Representation
Lecture 15 – Tree Terminologies
22
LeftChild Data RightChild
Example
Lecture 15 – Tree Terminologies
23
1
2
NULL 3
NULL 5 NULL
NULL 4 NULL NULL 6 NULL
struct node{
int data
struct node *left
struct node *right
}
Node Representation
Lecture 15 – Tree Terminologies
24

More Related Content

PPTX
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
PPTX
Introduction to tree ds
PPTX
TERMINOLOGIES OF TREE, TYPES OF TREE.pptx
PDF
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure
PPTX
Data Structures -Non Linear DS-Basics ofTrees
PPTX
Data structure using c module 2
PPTX
Tree.pptx
PDF
unit-2-dsa-tree introduction of tree and terninology
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
Introduction to tree ds
TERMINOLOGIES OF TREE, TYPES OF TREE.pptx
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure
Data Structures -Non Linear DS-Basics ofTrees
Data structure using c module 2
Tree.pptx
unit-2-dsa-tree introduction of tree and terninology

Similar to Binary Trees - Tree Terminologies and representation (20)

DOCX
data structures Unit 3 notes.docxdata structures Unit 3 notes.docx
PPTX
TreesTreesTreesTreesTreesTreesTrees.pptx
PPTX
week-Lec 10Trees.pptx for dsa students in detail
PPTX
Tree Data Structure Tree Data Structure Details
PPT
UNIT-4 TREES.ppt
PPTX
Data Structure of computer science and technology
PPTX
Basic terminology Unit in data structures
PDF
07 trees
PPTX
TREES 1.pptx
PPTX
TREES basics simple introduction .pptx
PPTX
Lecture 2-Trees in Data Structure Complete Lecture Slide
PPT
Tree 11.ppt
PPTX
lecture_13 tree in mmmmmmmm mmmmmfftro.pptx
PPT
Final tree.ppt tells about tree presentation
PPTX
Basic Tree Data Structure BST Traversals .pptx
PPTX
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
PPTX
TREE PRESENTATION COMPUTER SCIENCE/DATA STRUCTURE
PPTX
Introduction to Tree .pptx
PPTX
Lecture 21_Trees - I.pptx
data structures Unit 3 notes.docxdata structures Unit 3 notes.docx
TreesTreesTreesTreesTreesTreesTrees.pptx
week-Lec 10Trees.pptx for dsa students in detail
Tree Data Structure Tree Data Structure Details
UNIT-4 TREES.ppt
Data Structure of computer science and technology
Basic terminology Unit in data structures
07 trees
TREES 1.pptx
TREES basics simple introduction .pptx
Lecture 2-Trees in Data Structure Complete Lecture Slide
Tree 11.ppt
lecture_13 tree in mmmmmmmm mmmmmfftro.pptx
Final tree.ppt tells about tree presentation
Basic Tree Data Structure BST Traversals .pptx
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
TREE PRESENTATION COMPUTER SCIENCE/DATA STRUCTURE
Introduction to Tree .pptx
Lecture 21_Trees - I.pptx
Ad

Recently uploaded (20)

PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
DOCX
573137875-Attendance-Management-System-original
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Geodesy 1.pptx...............................................
PDF
PPT on Performance Review to get promotions
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
web development for engineering and engineering
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Operating System & Kernel Study Guide-1 - converted.pdf
573137875-Attendance-Management-System-original
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
R24 SURVEYING LAB MANUAL for civil enggi
UNIT-1 - COAL BASED THERMAL POWER PLANTS
bas. eng. economics group 4 presentation 1.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Geodesy 1.pptx...............................................
PPT on Performance Review to get promotions
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
UNIT 4 Total Quality Management .pptx
web development for engineering and engineering
Lecture Notes Electrical Wiring System Components
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
CYBER-CRIMES AND SECURITY A guide to understanding
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Ad

Binary Trees - Tree Terminologies and representation

  • 1. Introduction to Tree 1 So far we have discussed many linear data structures Lecture 15 – Tree Terminologies
  • 2. Data Structure Unit 3: Linked List • A non-linear data structure - Tree • Trees are mainly used to represent data containing a hierarchical relationship between elements, for example, records, family trees and table of contents. Introduction to Tree continued.. Lecture 15 – Tree Terminologies
  • 3. Introduction to Tree continued.. 3  Tree consists of a finite set of elements called nodes and a finite set of directed lines, called edges that connect the nodes.  A tree is an abstract model of a hierarchical structure that consists of nodes with a parent-child relationship.  There is a starting node known as a root node.  There is one and only one path between every pair of nodes in a tree.  A tree with n nodes has exactly (n-1) edges. edge root node Lecture 15 – Tree Terminologies
  • 4. Tree Terminologies 4 root A is parent of B and C C is parent of D and E D and E are children of C Root:- The first node from where the tree originates is called as a root node. In any tree, there must be only one root node. Parent node:- A node is parent node, if it has successor nodes Child node:- A node with predecessor is called as a child node Leaf:- A leaf node is any node with no children. B, D and E are leaf nodes Lecture 15 – Tree Terminologies
  • 5. Terminologies 5 root Leaf Node: D,I,J,F,K,H Internal nodes:- The nodes that are not a root or a leaf are known as internal nodes. Sibling:- two or more nodes with same parents are called as siblings. D,E and F are Siblings Internal Node: B,C,E,G G and H are Siblings B and C are Siblings Lecture 15 – Tree Terminologies
  • 6. Tree Terminologies contiuned.. 6 e. g. A – B -– D A – B – E – I Path:- A path is a sequence of nodes in which each node is adjacent to the next one. It is the sequence of consecutive edges from source node to destination node. Lecture 15 – Tree Terminologies
  • 7. Tree Terminologies contiuned.. 7 Consider a path A- B- E- I Here for node E , node B and A are ancestors Node E and I are descendent of node B Ancestor:- An ancestor is any node in the path from root to the node. Descendent:- a descendent is any node in the path below the parent node. That means all nodes in the paths from a given node to leaf are descendent of the node. Lecture 15 – Tree Terminologies
  • 8. Tree Terminologies contiuned.. 8 Level of node A is 0 Level of node B and C is 1 Level:- The level of a node is its distance from the root node i.e. root is at level 0, children of root at level 1 and so on. Level of node D,E,F,G and H is 2 Level of node I , J and K is 3 Lecture 15 – Tree Terminologies
  • 9. Tree Terminologies contiuned.. 9 Degree of node A=2 Degree of node B=3 Degree: Degree of a node is equal to the number of children that a node has. The degree of a leaf node is zero. In-degree: In-degree of a node is the number of edges arriving at that node. Out-degree: Out-degree of a node is the number of edges leaving that node. Degree of node E=2 Degree of node J=0 Lecture 15 – Tree Terminologies
  • 10. Tree Terminologies contiuned.. 10 Height of node B is 2 Height of node E is 1 Height of a node: The height of a node is the max path length form that node to a leaf node. Height of a tree: The height of a tree is the height of the root Height of tree = Height of node A=3 Lecture 15 – Tree Terminologies
  • 11. Tree Terminologies contiuned.. 11 Depth of node B is 1 Depth of node E is 2 Total number of edges from root node to a particular node is called as depth of that node. Depth of a tree: Depth of a tree is the max level of any leaf in the tree Depth of tree is 3 Lecture 15 – Tree Terminologies
  • 12. Tree Terminologies contiuned.. 12 Subtree : Any node of a tree, with all of its descendants is a subtree. Lecture 15 – Tree Terminologies
  • 13. Binary Tree 13 •In general, tree nodes can have any number of children. • In a binary tree, each node can have at most two children. A binary tree is either empty or consists of a node called the root together with two binary trees called the left subtree and the right subtree. •A tree with no nodes is called as a null tree. . Lecture 15 – Tree Terminologies
  • 16. Types of Binary Tree 16 1. Strictly Binary Tree If every non-leaf node in a binary tree consists of non-empty left sub-tree and right sub-tree then such a tree is called as strictly binary tree Lecture 15 – Tree Terminologies
  • 17. Types of Binary Tree 17 2. Complete Binary Tree A binary tree with n nodes and of depth d is a strictly binary tree in which all leaf nodes are at level d is called as complete binary tree. Lecture 15 – Tree Terminologies
  • 18. Array Representation Binary trees can also be stored in arrays In this arrangement, if a node has an index i, its children are found at indices 2i+1 and 2i+2, while its parent (if any) is found at index floor((i- 1)/2) (assuming the root of the tree stored in the array at an index zero). more compact storage and better locality of reference requires contiguous memory expensive to grow and wastes space proportional Binary Tree Representation Lecture 15 – Tree Terminologies 18
  • 19. Index element Example 1 : Array Representation Lecture 15 – Tree Terminologies 19 0 A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I Children at indices 2i+1 and 2i+2, while its parent (if any) is found at index floor((i-1)/2) 9 10
  • 20. Index element Example 2: Array Representation Lecture 15 – Tree Terminologies 20 0 A 1 B 2 - 3 C 4 - 5 - 6 - 7 D 8 Disadvantages : (1) waste space Children at indices 2i+1 and 2i+2, while its parent (if any) is found at index floor((i-1)/2) -
  • 21. Array representation is good for complete binary tree, but it is wasteful for many other binary trees. The representation suffers from insertion and deletion of node from the middle of the tree. To overcome this difficulty we represent the binary tree in linked representation. Limitations: Array Representation Lecture 15 – Tree Terminologies 21
  • 22. In linked representation each node in a binary has three fields, the left child field denoted as LeftChild, data field denoted as data and the right child field denoted as RightChild. If any sub-tree is empty then the corresponding pointer’s LeftChild and RightChild will store a NULL value. If the tree itself is empty the root pointer will store a NULL value. Linked Representation Lecture 15 – Tree Terminologies 22 LeftChild Data RightChild
  • 23. Example Lecture 15 – Tree Terminologies 23 1 2 NULL 3 NULL 5 NULL NULL 4 NULL NULL 6 NULL
  • 24. struct node{ int data struct node *left struct node *right } Node Representation Lecture 15 – Tree Terminologies 24