SlideShare a Scribd company logo
DATA STRUCTURES
FOREST
By,
M.SHARMILA DEVI M.Sc(IT)
FORESTS
⮚ Definition: A forest is a set of n>=0 disjoint trees.
⮚ The concept of a forest is very close to that of a
tree because if we remove the root of a tree, we
obtain a forest.
⮚ For example, removing the root of any binary tree
produces a forest of two trees.
THREE-TREE FOREST
TRANSFORMING A FOREST INTO A
BINARY TREE
⮚ To transform a forest into a single binary tree, we
first obtain the binary tree representation of each
of the trees in the forest and then link these binary
trees together through the rightChild field of the
root nodes.
BINARY TREE REPRESENTATION OF
FOREST
We can define this transformation in a formal
way as follows:
⮚ Definition: If T1 , . . . , Tn is a forest of trees, then the
binary tree corresponding to this forest, denoted by B(T1
, . . . , Tn),
⮚ 1. is empty if n=0
⮚ 2, has root equal to root (T1); has left subtree equal to
B(T11 , T12, . . . , T1m), where T11, . . . , T1m are the
subtrees of root(T1); and has right subtree B(T2 , . . . ,
Tn).
FOREST TRAVERSALS
⮚ Preorder and inorder traversals of the
corresponding binary tree T of a forest F have a
natural correspondence to traversals on F.
⮚ Preorder Traversal
⮚ Preorder traversal of T is equivalent to visiting
the nodes of F in forest preorder, which is defined
as follows:
1. If F is empty then return.
2. Visit the root of the first tree of F.
3. Traverse the subtrees of the first tree in forest
preorder
4. Traverse the remaining trees of F in forest
preorder.
InOrder Traversal
InOrder Traversal of T is equivalent to visiting the
nodes of F in forest inorder, which is defined as
follows:
1. If F is empty then return
2. Traverse the subtrees of the first tree in forest
inorder
3. Visit the root of the first tree
4. Traverse the remaining trees in forest inorder
Postorder Traversal
We can define the postorder traversal of a forest as
follows:
1. If F is empty then return
2. Traverse the subtrees of the first tree of F in
forest postorder.
3. Traverse the remaining trees of F in forest
postorder.
4. Visit the root of the first tree of F.

More Related Content

What's hot

Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
ghhgj jhgh
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
topu93
 

What's hot (20)

Red black tree
Red black treeRed black tree
Red black tree
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 
Binary tree
Binary treeBinary tree
Binary tree
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
 
Arrays
ArraysArrays
Arrays
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Red black trees
Red black treesRed black trees
Red black trees
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Instruction format
Instruction formatInstruction format
Instruction format
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 

Recently uploaded

AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
Alluxio, Inc.
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 

Recently uploaded (20)

Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysis
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
 

FORESTS

  • 2. FORESTS ⮚ Definition: A forest is a set of n>=0 disjoint trees. ⮚ The concept of a forest is very close to that of a tree because if we remove the root of a tree, we obtain a forest. ⮚ For example, removing the root of any binary tree produces a forest of two trees.
  • 4. TRANSFORMING A FOREST INTO A BINARY TREE ⮚ To transform a forest into a single binary tree, we first obtain the binary tree representation of each of the trees in the forest and then link these binary trees together through the rightChild field of the root nodes.
  • 6. We can define this transformation in a formal way as follows: ⮚ Definition: If T1 , . . . , Tn is a forest of trees, then the binary tree corresponding to this forest, denoted by B(T1 , . . . , Tn), ⮚ 1. is empty if n=0 ⮚ 2, has root equal to root (T1); has left subtree equal to B(T11 , T12, . . . , T1m), where T11, . . . , T1m are the subtrees of root(T1); and has right subtree B(T2 , . . . , Tn).
  • 7. FOREST TRAVERSALS ⮚ Preorder and inorder traversals of the corresponding binary tree T of a forest F have a natural correspondence to traversals on F.
  • 8. ⮚ Preorder Traversal ⮚ Preorder traversal of T is equivalent to visiting the nodes of F in forest preorder, which is defined as follows: 1. If F is empty then return. 2. Visit the root of the first tree of F. 3. Traverse the subtrees of the first tree in forest preorder 4. Traverse the remaining trees of F in forest preorder.
  • 9. InOrder Traversal InOrder Traversal of T is equivalent to visiting the nodes of F in forest inorder, which is defined as follows: 1. If F is empty then return 2. Traverse the subtrees of the first tree in forest inorder 3. Visit the root of the first tree 4. Traverse the remaining trees in forest inorder
  • 10. Postorder Traversal We can define the postorder traversal of a forest as follows: 1. If F is empty then return 2. Traverse the subtrees of the first tree of F in forest postorder. 3. Traverse the remaining trees of F in forest postorder. 4. Visit the root of the first tree of F.