SlideShare a Scribd company logo
1 of 14
Download to read 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);
 }

More Related Content

What's hot

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

What's hot (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
 

Viewers also liked

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

Viewers also liked (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
 

Similar to 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
 

Similar to 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
 

Recently uploaded

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
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Recently uploaded (20)

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
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
 
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
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
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
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 

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);  }