SlideShare uma empresa Scribd logo
1 de 14
Baixar para ler offline
CS221A – Data Structures &
Algorithms
Trees
Agenda
 Trees Concepts and Preliminaries
 Tree Implementation
 TreeTraversal
Trees
 An ADT that can store data hierarchically.With a parent
child relationship.
 Widely-used data structure that emulates a hierarchical
tree structure with a set of linked nodes.
 It is a connected graph where each node has a set of zero
or more children nodes, and at most one parent node.
Trees
 Node is a structure which may contain a value, a
condition, or represent a separate data structure (which
could be a tree of its own).
 Each node in a tree has zero or more child nodes, which are
below it in the tree (by convention, trees grow down, not up as
they do in nature).
 A node that has a child is called the child's parent node (or
ancestor node, or superior).
 A node has at most one parent.
 Nodes that do not have any children are called leaf nodes.
They are also referred to as terminal nodes.
Trees
 A simple unordered tree;
 the node labeled 7 has
two children, labeled 2
and 6, one parent, labeled
2.The root node, at the
top, has no parent.
Trees
 The topmost node in a
tree is called the root
node.
 The root node will not have
parents.
 It is the node at which
operations on the tree
commonly begin (although
some algorithms begin with
the leaf nodes and work up
ending at the root).
 All other nodes can be
reached from it by following
links or edges.
Root NodeLinks or edges
Trees
 The height of a node is
the length of the longest
downward path to a leaf
from that node.
 The height of the root is
the height of the tree.
0
1
2
Height of the
Tree : 3
00
1
2
0
Trees
 The depth of a node is
the length of the path to
its root (i.e., its root path).
 An internal node or
inner node is any node of
a tree that has child nodes
and is thus not a leaf node.
 Stepping through the items
of a tree, by means of the
connections between
parents and children, is
called walking the tree
or TreeTraversal.
3
1
2
33
1
2
0
2
Inner Node
Trees
 Tree is a collection of N
nodes, one of which is the
root and N - 1 edges.
 N = 9
 E = 8
Trees Implementation
Tree Implementation
Trees Implementation
 Each Node Maintains Data and a pointer to each of its
child nodes.
 As Number of Children per Node is an unknown , we
keep the children of each node in a linked list of tree
nodes.
Tree Implementation
 typedef struct TreeNode *PtrToNode;
 struct TreeNode
 {
 ElementType Element;
 PtrToNode FirstChild;
 PtrToNode NextSibling;
 }
Tree Implementation
 voidTraverse(Tree t)
 {
 printf (tElement);
 if (tFirstChild != Null)
 Traverse(tFirstChild);
 if (tNextSibling !=Null)
 Traverse(tNextSibling);
 }

Mais conteúdo relacionado

Mais procurados

DISE - Database Concepts
DISE - Database ConceptsDISE - Database Concepts
DISE - Database Concepts
Rasan Samarasinghe
 

Mais procurados (20)

Data structure tree - intermediate
Data structure tree - intermediateData structure tree - intermediate
Data structure tree - intermediate
 
Introduction to data structures (ss)
Introduction to data structures (ss)Introduction to data structures (ss)
Introduction to data structures (ss)
 
Tree in Graph Theory in Discrete structure
Tree in Graph Theory in Discrete structure  Tree in Graph Theory in Discrete structure
Tree in Graph Theory in Discrete structure
 
DATA BASE MODEL Rohini
DATA BASE MODEL RohiniDATA BASE MODEL Rohini
DATA BASE MODEL Rohini
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 
Data structure tree - beginner
Data structure tree - beginnerData structure tree - beginner
Data structure tree - beginner
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Data structure tree- advance
Data structure tree- advanceData structure tree- advance
Data structure tree- advance
 
SQL
SQL SQL
SQL
 
Learn Database Design with MySQL - Chapter 1 - What is a database
Learn Database Design with MySQL - Chapter 1 -   What is a databaseLearn Database Design with MySQL - Chapter 1 -   What is a database
Learn Database Design with MySQL - Chapter 1 - What is a database
 
Unit iv data structure-converted
Unit  iv data structure-convertedUnit  iv data structure-converted
Unit iv data structure-converted
 
RDBMS concepts
RDBMS conceptsRDBMS concepts
RDBMS concepts
 
Data structures
Data structuresData structures
Data structures
 
Rdbms
RdbmsRdbms
Rdbms
 
Week 4 The Relational Data Model & The Entity Relationship Data Model
Week 4 The Relational Data Model & The Entity Relationship Data ModelWeek 4 The Relational Data Model & The Entity Relationship Data Model
Week 4 The Relational Data Model & The Entity Relationship Data Model
 
Bca examination 2015 dbms
Bca examination 2015 dbmsBca examination 2015 dbms
Bca examination 2015 dbms
 
Database management system chapter1
Database management system chapter1Database management system chapter1
Database management system chapter1
 
DISE - Database Concepts
DISE - Database ConceptsDISE - Database Concepts
DISE - Database Concepts
 
Rdbms
RdbmsRdbms
Rdbms
 

Destaque

Datastructure notes
Datastructure notesDatastructure notes
Datastructure notes
Srikanth
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 

Destaque (20)

EEE 3rd year oops cat 3 ans
EEE 3rd year  oops cat 3  ansEEE 3rd year  oops cat 3  ans
EEE 3rd year oops cat 3 ans
 
Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)
 
EEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answerEEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answer
 
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014
 
Viva questions
Viva questions Viva questions
Viva questions
 
inline function
inline function inline function
inline function
 
M.Tech : Advanced DBMS Assignment I
M.Tech : Advanced DBMS Assignment IM.Tech : Advanced DBMS Assignment I
M.Tech : Advanced DBMS Assignment I
 
Software Engineering tools
Software Engineering tools Software Engineering tools
Software Engineering tools
 
6th Semester CS / IS (2013-June) Question Papers
6th Semester CS / IS (2013-June) Question Papers6th Semester CS / IS (2013-June) Question Papers
6th Semester CS / IS (2013-June) Question Papers
 
05211201 Advanced Data Structures And Algorithms
05211201 Advanced Data Structures  And  Algorithms05211201 Advanced Data Structures  And  Algorithms
05211201 Advanced Data Structures And Algorithms
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Dacj 1-1 b
Dacj 1-1 bDacj 1-1 b
Dacj 1-1 b
 
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMSM.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notes
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
 
Best Engineering Colleges of Computer science – GNIOT
Best Engineering Colleges of Computer science – GNIOTBest Engineering Colleges of Computer science – GNIOT
Best Engineering Colleges of Computer science – GNIOT
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
Inline function
Inline functionInline function
Inline function
 

Semelhante a Trees

Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
nakulvarshney371
 
Describe the tree data structure- What is a root node- What is a child.docx
Describe the tree data structure- What is a root node- What is a child.docxDescribe the tree data structure- What is a root node- What is a child.docx
Describe the tree data structure- What is a root node- What is a child.docx
andyb37
 
Tree data structure in java
Tree data structure in javaTree data structure in java
Tree data structure in java
Irfan CH
 
Basic Terminologies of Tree and Tree Traversal methods.pptx
Basic Terminologies of Tree and Tree Traversal methods.pptxBasic Terminologies of Tree and Tree Traversal methods.pptx
Basic Terminologies of Tree and Tree Traversal methods.pptx
22001003058
 
Trees - Data structures in C/Java
Trees - Data structures in C/JavaTrees - Data structures in C/Java
Trees - Data structures in C/Java
geeksrik
 

Semelhante a Trees (20)

tree traversals.pdf
tree traversals.pdftree traversals.pdf
tree traversals.pdf
 
Trees in data structures
Trees in data structuresTrees in data structures
Trees in data structures
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
 
Lecture 5 trees
Lecture 5 treesLecture 5 trees
Lecture 5 trees
 
Trees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and AlgorithmsTrees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and Algorithms
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
Describe the tree data structure- What is a root node- What is a child.docx
Describe the tree data structure- What is a root node- What is a child.docxDescribe the tree data structure- What is a root node- What is a child.docx
Describe the tree data structure- What is a root node- What is a child.docx
 
Ch12 Tree
Ch12 TreeCh12 Tree
Ch12 Tree
 
Tree
TreeTree
Tree
 
Tree data structure in java
Tree data structure in javaTree data structure in java
Tree data structure in java
 
Unit 5 Tree.pptx
Unit 5 Tree.pptxUnit 5 Tree.pptx
Unit 5 Tree.pptx
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Binary tree in data structures
Binary tree in  data structuresBinary tree in  data structures
Binary tree in data structures
 
DATA STUCTURES-TREES.pptx
DATA STUCTURES-TREES.pptxDATA STUCTURES-TREES.pptx
DATA STUCTURES-TREES.pptx
 
Basic Terminologies of Tree and Tree Traversal methods.pptx
Basic Terminologies of Tree and Tree Traversal methods.pptxBasic Terminologies of Tree and Tree Traversal methods.pptx
Basic Terminologies of Tree and Tree Traversal methods.pptx
 
Module 8.1 Trees.pdf
Module 8.1 Trees.pdfModule 8.1 Trees.pdf
Module 8.1 Trees.pdf
 
Trees - Data structures in C/Java
Trees - Data structures in C/JavaTrees - Data structures in C/Java
Trees - Data structures in C/Java
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Discrete Mathematics Tree
Discrete Mathematics  TreeDiscrete Mathematics  Tree
Discrete Mathematics Tree
 

Último

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Último (20)

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 

Trees

  • 1. CS221A – Data Structures & Algorithms Trees
  • 2. Agenda  Trees Concepts and Preliminaries  Tree Implementation  TreeTraversal
  • 3. Trees  An ADT that can store data hierarchically.With a parent child relationship.  Widely-used data structure that emulates a hierarchical tree structure with a set of linked nodes.  It is a connected graph where each node has a set of zero or more children nodes, and at most one parent node.
  • 4. Trees  Node is a structure which may contain a value, a condition, or represent a separate data structure (which could be a tree of its own).  Each node in a tree has zero or more child nodes, which are below it in the tree (by convention, trees grow down, not up as they do in nature).  A node that has a child is called the child's parent node (or ancestor node, or superior).  A node has at most one parent.  Nodes that do not have any children are called leaf nodes. They are also referred to as terminal nodes.
  • 5. Trees  A simple unordered tree;  the node labeled 7 has two children, labeled 2 and 6, one parent, labeled 2.The root node, at the top, has no parent.
  • 6. Trees  The topmost node in a tree is called the root node.  The root node will not have parents.  It is the node at which operations on the tree commonly begin (although some algorithms begin with the leaf nodes and work up ending at the root).  All other nodes can be reached from it by following links or edges. Root NodeLinks or edges
  • 7. Trees  The height of a node is the length of the longest downward path to a leaf from that node.  The height of the root is the height of the tree. 0 1 2 Height of the Tree : 3 00 1 2 0
  • 8. Trees  The depth of a node is the length of the path to its root (i.e., its root path).  An internal node or inner node is any node of a tree that has child nodes and is thus not a leaf node.  Stepping through the items of a tree, by means of the connections between parents and children, is called walking the tree or TreeTraversal. 3 1 2 33 1 2 0 2 Inner Node
  • 9. Trees  Tree is a collection of N nodes, one of which is the root and N - 1 edges.  N = 9  E = 8
  • 12. Trees Implementation  Each Node Maintains Data and a pointer to each of its child nodes.  As Number of Children per Node is an unknown , we keep the children of each node in a linked list of tree nodes.
  • 13. Tree Implementation  typedef struct TreeNode *PtrToNode;  struct TreeNode  {  ElementType Element;  PtrToNode FirstChild;  PtrToNode NextSibling;  }
  • 14. Tree Implementation  voidTraverse(Tree t)  {  printf (tElement);  if (tFirstChild != Null)  Traverse(tFirstChild);  if (tNextSibling !=Null)  Traverse(tNextSibling);  }