SlideShare a Scribd company logo
Data Structure and
Algorithms
Lecture # 9
Ehtisham Rasheed
Department of Computer Science
University of Gurjat, Gujrat
2
Tree
• Nature view of tree
branche
s
leave
s
root
Tree
• Computer scientist’s view of tree
branche
s
leave
s
root
nodes
Tree (Definition)
• Tree represents the nodes connected by edges
Tree (Representation)
Important Terms
• Path – Path refers to the sequence of nodes along the edges of a tree
• Root – The node at the top of the tree is called root.
• Parent – Any node except the root node has one edge upward to a
node is called parent
• Child – The node below a given node connected by its edge downward
is called child node
• Leaf – The node which doesn’t have any child node is called the leaf
node. Also called External (all other nodes are internal)
• Siblings – A group of nodes with the same parent
• Descendant – A node reachable by repeated proceedings from parent
to child
Important Terms
• Ancestor – A node reachable by repeated proceedings from child to parent
• Degree – The number of sub trees of a node
• Edge – The connection between one node and another
• Traversing – Passing through nodes in a specific order
• Depth – Number of ancestors
• Height – Height of a node is the length of the longest path from that node to
any leaf
• Binary Tree – A binary tree is a tree such that
• Every node has at most two children
• Each node is either a left child or a right child
Tree Applications
• Table of contents of a book
• Organization hierarchy
• Unix file system
• Representing sorted lists of data
• Routing algorithms
Linked Structure for Binary Trees
• A node is represented by an object storing
• Element
• Parent node
• Left child node
• Right child node
Linked Structure for Binary Trees
An Array-Based Representation
• Nodes are stored in an array A
• Node v is stored at A [ rank ( v ) ]
• Rank ( root ) = 1
• Left in even: if node is the left child of parent ( node ),
Rank ( node ) = 2 * rank ( parent ( node ) )
• Right in odd: if node is the right child of parent ( node ),
Rank ( node ) = 2 * rank ( parent ( node ) ) + 1
• A [ 0 ] is always empty
• A [ i ] is empty if there is no node in the ith position
• The array size N is 2 (h+1)
An Array-Based Representation
Tree Traversal
• Traversal is a process to visit all the nodes of a tree and
print their values. We can’t access a random node in a
tree instead we start from root node. We use the
following three ways to traverse tree nodes:
• In-Order Traversal
• Pre-Order Traversal
• post-Order Traversal
In-Order Traversal
• In this method, the left subtree is visited first, then the
root and later the right subtree
• The output of the in-order traversal of this tree will be
Pre-Order Traversal
• In this method, the root node is visited first, then the left
subtree and finally the right subtree
• The output of the pre-order traversal of this tree will be
Post-Order Traversal
• In this method, the left subtree is traversed first, then
the right subtree, and finally the root node
• The output of the pre-order traversal of this tree will be
Expression Tree
• Expression tree is a special kind of binary tree used to represent
expressions
• Each internal node correspond to operator and each external
node corresponds to operand. Example: ( 2 x ( a – 1 ) +
( 3 x b ) )
Easy to Generate Infix, Prefix Postfix Expressions
• Infix: ( ( 8 – 5 ) * ( ( 4 + 2 ) / 3 )
• Prefix: * - 8 5 / + 4 2 3
• Postfix: 8 5 – 4 2 + 3 / *

More Related Content

PPT
Tree 11.ppt
PPTX
TREE PRESENTATION COMPUTER SCIENCE/DATA STRUCTURE
PPT
UNIT-4 TREES.ppt
PPTX
Trees, Basic Terminology and Binary Trees
PPTX
Lecture 2-Trees in Data Structure Complete Lecture Slide
PPT
9. TREE Data Structure Non Linear Data Structure
PPTX
Tree.pptx
PPT
Tree and Binary Search tree
Tree 11.ppt
TREE PRESENTATION COMPUTER SCIENCE/DATA STRUCTURE
UNIT-4 TREES.ppt
Trees, Basic Terminology and Binary Trees
Lecture 2-Trees in Data Structure Complete Lecture Slide
9. TREE Data Structure Non Linear Data Structure
Tree.pptx
Tree and Binary Search tree

Similar to Lecture 9 (DS) - Tree, Tree Traversal.pptx (20)

PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PPTX
Basic Tree Data Structure BST Traversals .pptx
PDF
8.haftajava notlarıiçeriyordökumanlar.pdf
DOCX
data structures Unit 3 notes.docxdata structures Unit 3 notes.docx
PPTX
unit-2-data structure and algorithms-tree-2024-1.pptx
PPTX
tree Data Structures in python Traversals.pptx
PPTX
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PDF
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure
PPTX
trees in data structure
PPTX
Tree structure and its definitions with an example
PPTX
Data Structures and Algorithms - Lecture 10 - Thushapan.pptx
PPTX
Data Structures -Non Linear DS-Basics ofTrees
PPT
Final tree.ppt tells about tree presentation
PPTX
Why Tree is considered a non-linear data structure?
PPTX
Data structure using c module 2
PPT
PPTX
TreesTreesTreesTreesTreesTreesTrees.pptx
PPTX
07-Lecture.pptxlkjslkjdfkjskljdflksj;fdkj
UNIT III Non Linear Data Structures - Trees.pptx
Basic Tree Data Structure BST Traversals .pptx
8.haftajava notlarıiçeriyordökumanlar.pdf
data structures Unit 3 notes.docxdata structures Unit 3 notes.docx
unit-2-data structure and algorithms-tree-2024-1.pptx
tree Data Structures in python Traversals.pptx
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
UNIT III Non Linear Data Structures - Trees.pptx
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure
trees in data structure
Tree structure and its definitions with an example
Data Structures and Algorithms - Lecture 10 - Thushapan.pptx
Data Structures -Non Linear DS-Basics ofTrees
Final tree.ppt tells about tree presentation
Why Tree is considered a non-linear data structure?
Data structure using c module 2
TreesTreesTreesTreesTreesTreesTrees.pptx
07-Lecture.pptxlkjslkjdfkjskljdflksj;fdkj
Ad

More from itxdevilmehar (11)

PPT
Lecture No-19.ppt lecture number 19 ppt .
PPTX
COAL-8.pptx lecture number 19 Ppt number
PPTX
Research-Ethics-ppt.pptx research ethics
PPTX
COAL-8.pptx coal assignment ppt number 2
PPTX
Procedure.lecture number pptx slide form
PPT
Management lecture number 14-3.ppt slide
PPTX
Lecture No-14.pptx Lecture No-14.pptx xx
PPTX
Conditional Flow Control Directive.pptxx
PPTX
Database ppt[}}.pptx database system and
PPTX
transaction management.pptx isolationand
PPT
Multimedia and ict projecg and assignmen
Lecture No-19.ppt lecture number 19 ppt .
COAL-8.pptx lecture number 19 Ppt number
Research-Ethics-ppt.pptx research ethics
COAL-8.pptx coal assignment ppt number 2
Procedure.lecture number pptx slide form
Management lecture number 14-3.ppt slide
Lecture No-14.pptx Lecture No-14.pptx xx
Conditional Flow Control Directive.pptxx
Database ppt[}}.pptx database system and
transaction management.pptx isolationand
Multimedia and ict projecg and assignmen
Ad

Recently uploaded (20)

PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PPTX
Fundamentals of Mechanical Engineering.pptx
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
737-MAX_SRG.pdf student reference guides
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PPT
Occupational Health and Safety Management System
PDF
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Information Storage and Retrieval Techniques Unit III
PPTX
Feature types and data preprocessing steps
PPTX
communication and presentation skills 01
PPTX
Software Engineering and software moduleing
PPTX
Current and future trends in Computer Vision.pptx
PPTX
Artificial Intelligence
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PPTX
Management Information system : MIS-e-Business Systems.pptx
Exploratory_Data_Analysis_Fundamentals.pdf
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Fundamentals of Mechanical Engineering.pptx
"Array and Linked List in Data Structures with Types, Operations, Implementat...
R24 SURVEYING LAB MANUAL for civil enggi
737-MAX_SRG.pdf student reference guides
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
Occupational Health and Safety Management System
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Automation-in-Manufacturing-Chapter-Introduction.pdf
Information Storage and Retrieval Techniques Unit III
Feature types and data preprocessing steps
communication and presentation skills 01
Software Engineering and software moduleing
Current and future trends in Computer Vision.pptx
Artificial Intelligence
distributed database system" (DDBS) is often used to refer to both the distri...
Management Information system : MIS-e-Business Systems.pptx

Lecture 9 (DS) - Tree, Tree Traversal.pptx

  • 1. Data Structure and Algorithms Lecture # 9 Ehtisham Rasheed Department of Computer Science University of Gurjat, Gujrat
  • 2. 2 Tree • Nature view of tree branche s leave s root
  • 3. Tree • Computer scientist’s view of tree branche s leave s root nodes
  • 4. Tree (Definition) • Tree represents the nodes connected by edges
  • 6. Important Terms • Path – Path refers to the sequence of nodes along the edges of a tree • Root – The node at the top of the tree is called root. • Parent – Any node except the root node has one edge upward to a node is called parent • Child – The node below a given node connected by its edge downward is called child node • Leaf – The node which doesn’t have any child node is called the leaf node. Also called External (all other nodes are internal) • Siblings – A group of nodes with the same parent • Descendant – A node reachable by repeated proceedings from parent to child
  • 7. Important Terms • Ancestor – A node reachable by repeated proceedings from child to parent • Degree – The number of sub trees of a node • Edge – The connection between one node and another • Traversing – Passing through nodes in a specific order • Depth – Number of ancestors • Height – Height of a node is the length of the longest path from that node to any leaf • Binary Tree – A binary tree is a tree such that • Every node has at most two children • Each node is either a left child or a right child
  • 8. Tree Applications • Table of contents of a book • Organization hierarchy • Unix file system • Representing sorted lists of data • Routing algorithms
  • 9. Linked Structure for Binary Trees • A node is represented by an object storing • Element • Parent node • Left child node • Right child node
  • 10. Linked Structure for Binary Trees
  • 11. An Array-Based Representation • Nodes are stored in an array A • Node v is stored at A [ rank ( v ) ] • Rank ( root ) = 1 • Left in even: if node is the left child of parent ( node ), Rank ( node ) = 2 * rank ( parent ( node ) ) • Right in odd: if node is the right child of parent ( node ), Rank ( node ) = 2 * rank ( parent ( node ) ) + 1 • A [ 0 ] is always empty • A [ i ] is empty if there is no node in the ith position • The array size N is 2 (h+1)
  • 13. Tree Traversal • Traversal is a process to visit all the nodes of a tree and print their values. We can’t access a random node in a tree instead we start from root node. We use the following three ways to traverse tree nodes: • In-Order Traversal • Pre-Order Traversal • post-Order Traversal
  • 14. In-Order Traversal • In this method, the left subtree is visited first, then the root and later the right subtree • The output of the in-order traversal of this tree will be
  • 15. Pre-Order Traversal • In this method, the root node is visited first, then the left subtree and finally the right subtree • The output of the pre-order traversal of this tree will be
  • 16. Post-Order Traversal • In this method, the left subtree is traversed first, then the right subtree, and finally the root node • The output of the pre-order traversal of this tree will be
  • 17. Expression Tree • Expression tree is a special kind of binary tree used to represent expressions • Each internal node correspond to operator and each external node corresponds to operand. Example: ( 2 x ( a – 1 ) + ( 3 x b ) )
  • 18. Easy to Generate Infix, Prefix Postfix Expressions • Infix: ( ( 8 – 5 ) * ( ( 4 + 2 ) / 3 ) • Prefix: * - 8 5 / + 4 2 3 • Postfix: 8 5 – 4 2 + 3 / *