SlideShare a Scribd company logo
1 of 55
1.Tree
2.Graph
In computer science, a tree is a widely-used data
structure that emulates a hierarchical tree structure
with a set of linked nodes.
Nature View of a Tree
branche
s
leaves
root
Computer Scientist’s View
branches
leaves
root
nodes
What is a Tree
 A tree is a finite nonempty set
of elements.
 It is an abstract model of a
hierarchical structure.
 consists of nodes with a
parent-child relation.
 Applications:
 Organization charts
 File systems
 Programming environments
Computers”R”Us
Sales R&DManufacturing
Laptops DesktopsUS International
Europe Asia Canada
Tree Representation
Cont…
 Root-The top most Node.
 Children
 Parents
 Siblings- Have same parents.
 Leaf- Has no Child.
RELATION OF TREE
1
3
2
4
6
5
8
7
11109
8
Trees Data Structures
 Tree
 Nodes
 Each node can have 0 or more children
 A node can have at most one parent
 Binary tree
 Tree with 0–2 children per node
Tree Binary Tree 9
Trees
 Terminology
 Root  no parent
 Leaf  no child
 Interior  non-leaf
 Height  distance from root to leaf
Root node
Leaf nodes
Interior nodes Height
10
Example Tree
root
ActivitiesTeaching Research
Sami’s Home Page
Papers PresentationsCS211CS101
Tree Terminology
 Path: Traversal from node to node along the edges results
in a sequence called path.
 Root: Node at the top of the tree.
 Parent: Any node, except root has exactly one edge running
upward to another node. The node above it is called parent.
 Child: Any node may have one or more lines running
downward to other nodes. Nodes below are children.
 Leaf: A node that has no children.
 Subtree: Any node can be considered to be the root of a
subtree, which consists of its children and its children’s
children and so on.
subtree
Tree Terminology Root: node without parent (A)
 Siblings: nodes share the same parent
 Internal node: node with at least one
child (A, B, C, F)
 External node (leaf ): node without
children (E, I, J, K, G, H, D)
 Ancestors of a node: parent, grandparent,
grand-grandparent, etc.
 Descendant of a node: child, grandchild,
grand-grandchild, etc.
 Depth of a node: number of ancestors
 Height of a tree: maximum depth of any
node (3)
 Degree of a node: the number of its
children
 Degree of a tree: the maximum number of
its node.
A
B DC
G HE F
I J K
Subtree: tree consisting of a
node and its descendants
General Trees
 Trees store data in a hierarchical manner
 Root node is A
 A's children are B, C, and D
 E, F, and D are leaves
 Length of path from A to E is 2 edges
A
DB C
FE
Trees have layers of nodes,
where some are higher,
others are lower.
Logic of Tree
Used to represent hierarchical data.
 BS(CS) 1ST tree e.g.
BS(CS)1st
Section (A) Section (B)
CR 1 CR2
group 1 group 2 group 3 group 4 group 1 group 2 group 3 group 4
15
Logic of Tree
Used to represent hierarchical data.
 BS(CS) 1ST tree e.g.
BS(CS)1st
Section (A) Section (B)
CR 1 CR2
group 1 group 2 group 3 group 4 group 1 group 2 group 3 group 4
16
Cont…
 A Collection of entities called Nodes.
 Tree is a Non-Linear Data Structure.
 It’s a hierarchica Structure.
TREE
Nodes
17
Tree Traversal
 Unlike linear data structures (Array, Linked List,
Queues, Stacks, etc) which have only one logical way
to traverse them, trees can be traversed in different
ways.
 Traversal is a process to visit all the nodes of a tree and
may print their values too. Because, all nodes are
connected via edges (links) we always start from the
root (head) node.
Cont…
 That is, we cannot randomly access a node in a tree. There are
three ways which we use to traverse a tree
 (a) Inorder (Left, Root, Right) : 4 2 5 1 3
(b) Preorder (Root, Left, Right) : 1 2 4 5 3
(c) Postorder (Left, Right, Root) : 4 5 2 3 1

In-order Traversal
 In this traversal method, the left subtree is visited first,
then the root and later the right sub-tree. We should
always remember that every node may represent a
subtree itself.
Inorder (Left, Root, Right)
Inorder (Left, Root, Right)
 We start from A, and following in-order traversal, we
move to its left subtree B. B is also traversed in-order.
The process goes on until all the nodes are visited. The
output of inorder traversal of this tree will be −
 D → B → E → A → F → C → G
Inorder Algorithm
 Until all nodes are traversed −
 Step 1 − Recursively traverse left subtree.
 Step 2 − Visit root node.
 Step 3 − Recursively traverse right subtree.
Pre-order Traversal
 In this traversal method, the root node is visited first,
then the left subtree and finally the right subtree.
Preorder (Root, Left, Right)
Preorder (Root, Left, Right)
 We start from A, and following pre-order traversal, we first visit A itself and
then move to its left subtree B. B is also traversed pre-order. The process goes
on until all the nodes are visited. The output of pre-order traversal of this tree
will be.
 A → B → D → E → C → F → G
Algorithm
 Until all nodes are traversed −
 Step 1 − Visit root node.
 Step 2 − Recursively traverse left subtree.
 Step 3 − Recursively traverse right subtree.
Post-order Traversal
 In this traversal method, the root node is visited last,
hence the name. First we traverse the left subtree, then
the right subtree and finally the root node.
Postorder (Left, Right, Root)
Postorder (Left, Right, Root)
 We start from A, and following pre-order traversal, we first visit the left
subtree B. B is also traversed post-order. The process goes on until all
the nodes are visited. The output of post-order traversal of this tree will
be.
 D → E → B → F → G → C → A
Algorithm
 Until all nodes are traversed
 Step 1 − Recursively traverse left subtree.
 Step 2 − Recursively traverse right subtree.
 Step 3 − Visit root node.
1.Insertion
2.Deletion
3.Searching
• If root is NULL
• then create root node
• return
• If root exists then
• compare the data with node.data
• while until insertion position is located
• If data is greater than node.data
• goto right subtree
• else
• goto left subtree
• endwhile
• insert data
• end If
If root.data is equal to search.data
return root
else
while data not found
If data is greater than node.data
goto right subtree
else
goto left subtree
If data found
return node
endwhile
return data not found
end if
Deletion algorithm ????
There are a large number of tree types, however three types
specifically are used the majority of the time (both in real world
development and in Computer Science courses). The types are:
 Binary trees
 B-Trees
 Heaps
Binary trees
 Binary tree follows the rule that each parent node has
no more than 2 child nodes. This means that a binary
tree can look like this:
 OR
Strict Binary Tree
 Strict Binary Tree is a Binary tree in which root can
have exactly two children or no children at all. That
means, there can be 0 or 2 children of any node.
Complete Binary Tree
 Complete Binary Tree is a Strict Binary tree in which
every leaf node is at same level. That means, there are
equal number of children in right and left subtree for
every node.
Extended Binary Tree
 An extended binary tree is a transformation of any
binary tree into a complete binary tree.
 This transformation consists of replacing every null
subtree of the original tree with “special nodes.”
 The nodes from the original tree are then called as
internal nodes, while the “special nodes” are called as
external nodes.
Extended Binary Tree
A binary tree with special nodes replacing every null subtree. Every regular node
has two children, and every special node has no children.
Binary Search Tree
 The value at any node, • Greater than every value in left subtree. Smaller than
every value in right subtree – Example
 Y > X
 Y < Z
 Values in left sub tree less than parent
 Values in right sub tree greater than parent
 Fast searches in a Binary Search tree, maximum of log n comparisons.
Binary Search Trees
 Examples
 Binary search tree
B - Trees
 B – Tree is another type of search tree called B-Tree in
which a node can store more than one value (key) and
it can have more than two children.
 B-Tree can be defined as follows…
 B-Tree is a self-balanced search tree with multiple keys in every node and more than two
children for every node.
Operations on a B-Tree
 The following operations are performed on a B-Tree...
 Search
 Insertion
 Deletion
Heap Tree
 Heap is a special case of balanced binary tree data
structure where the root-node key is compared with its
children and arranged accordingly. If α has child
node β then
 key(α) ≥ key(β)
 A heap can be of two types
 Min-Heap
 Max-Heap
Max-Heap
 Max-Heap − Where the value of the root node is
greater than or equal to either of its children.
Max-Heap insertion
Max-Heap deletion
Max Heap Construction Algorithm
 Step 1 − Create a new node.
 Step 2 − Assign new value to the node.
 Step 3 − Compare the value of this child node with its
parent.
 Step 4 − If value of parent is less than child, then swap
them.
 Step 5 − Repeat step 3 & 4 until Heap property holds.
Max Heap Deletion Algorithm
 Step 1 − Remove root node.
 Step 2 − Move the last element of last level to root.
 Step 3 − Compare the value of this child node with its
parent.
 Step 4 − If value of parent is less than child, then swap
them.
 Step 5 − Repeat step 3 & 4 until Heap property holds.
Min-Heap
 Min-Heap − Where the value of the root node is less
than or equal to either of its children.
Min-Heap insertion
Min-Heap deletion

More Related Content

What's hot (20)

Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Tree-In Data Structure
Tree-In Data StructureTree-In Data Structure
Tree-In Data Structure
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Binary tree
Binary treeBinary tree
Binary tree
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
Linked list
Linked listLinked list
Linked list
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFS
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
 
Heaps
HeapsHeaps
Heaps
 
Avl trees
Avl treesAvl trees
Avl trees
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 

Similar to trees in data structure

Similar to trees in data structure (20)

Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
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
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Tree
TreeTree
Tree
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
Tree
TreeTree
Tree
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructure
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
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
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
 
Unit iv data structure-converted
Unit  iv data structure-convertedUnit  iv data structure-converted
Unit iv data structure-converted
 

Recently uploaded

Philippine History cavite Mutiny Report.ppt
Philippine History cavite Mutiny Report.pptPhilippine History cavite Mutiny Report.ppt
Philippine History cavite Mutiny Report.pptssuser319dad
 
SBFT Tool Competition 2024 -- Python Test Case Generation Track
SBFT Tool Competition 2024 -- Python Test Case Generation TrackSBFT Tool Competition 2024 -- Python Test Case Generation Track
SBFT Tool Competition 2024 -- Python Test Case Generation TrackSebastiano Panichella
 
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...NETWAYS
 
Gaps, Issues and Challenges in the Implementation of Mother Tongue Based-Mult...
Gaps, Issues and Challenges in the Implementation of Mother Tongue Based-Mult...Gaps, Issues and Challenges in the Implementation of Mother Tongue Based-Mult...
Gaps, Issues and Challenges in the Implementation of Mother Tongue Based-Mult...marjmae69
 
call girls in delhi malviya nagar @9811711561@
call girls in delhi malviya nagar @9811711561@call girls in delhi malviya nagar @9811711561@
call girls in delhi malviya nagar @9811711561@vikas rana
 
OSCamp Kubernetes 2024 | A Tester's Guide to CI_CD as an Automated Quality Co...
OSCamp Kubernetes 2024 | A Tester's Guide to CI_CD as an Automated Quality Co...OSCamp Kubernetes 2024 | A Tester's Guide to CI_CD as an Automated Quality Co...
OSCamp Kubernetes 2024 | A Tester's Guide to CI_CD as an Automated Quality Co...NETWAYS
 
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...henrik385807
 
Simulation-based Testing of Unmanned Aerial Vehicles with Aerialist
Simulation-based Testing of Unmanned Aerial Vehicles with AerialistSimulation-based Testing of Unmanned Aerial Vehicles with Aerialist
Simulation-based Testing of Unmanned Aerial Vehicles with AerialistSebastiano Panichella
 
Genesis part 2 Isaiah Scudder 04-24-2024.pptx
Genesis part 2 Isaiah Scudder 04-24-2024.pptxGenesis part 2 Isaiah Scudder 04-24-2024.pptx
Genesis part 2 Isaiah Scudder 04-24-2024.pptxFamilyWorshipCenterD
 
LANDMARKS AND MONUMENTS IN NIGERIA.pptx
LANDMARKS  AND MONUMENTS IN NIGERIA.pptxLANDMARKS  AND MONUMENTS IN NIGERIA.pptx
LANDMARKS AND MONUMENTS IN NIGERIA.pptxBasil Achie
 
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...Salam Al-Karadaghi
 
Open Source Camp Kubernetes 2024 | Monitoring Kubernetes With Icinga by Eric ...
Open Source Camp Kubernetes 2024 | Monitoring Kubernetes With Icinga by Eric ...Open Source Camp Kubernetes 2024 | Monitoring Kubernetes With Icinga by Eric ...
Open Source Camp Kubernetes 2024 | Monitoring Kubernetes With Icinga by Eric ...NETWAYS
 
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...Pooja Nehwal
 
The 3rd Intl. Workshop on NL-based Software Engineering
The 3rd Intl. Workshop on NL-based Software EngineeringThe 3rd Intl. Workshop on NL-based Software Engineering
The 3rd Intl. Workshop on NL-based Software EngineeringSebastiano Panichella
 
Event 4 Introduction to Open Source.pptx
Event 4 Introduction to Open Source.pptxEvent 4 Introduction to Open Source.pptx
Event 4 Introduction to Open Source.pptxaryanv1753
 
Genshin Impact PPT Template by EaTemp.pptx
Genshin Impact PPT Template by EaTemp.pptxGenshin Impact PPT Template by EaTemp.pptx
Genshin Impact PPT Template by EaTemp.pptxJohnree4
 
Work Remotely with Confluence ACE 2.pptx
Work Remotely with Confluence ACE 2.pptxWork Remotely with Confluence ACE 2.pptx
Work Remotely with Confluence ACE 2.pptxmavinoikein
 
OSCamp Kubernetes 2024 | Zero-Touch OS-Infrastruktur für Container und Kubern...
OSCamp Kubernetes 2024 | Zero-Touch OS-Infrastruktur für Container und Kubern...OSCamp Kubernetes 2024 | Zero-Touch OS-Infrastruktur für Container und Kubern...
OSCamp Kubernetes 2024 | Zero-Touch OS-Infrastruktur für Container und Kubern...NETWAYS
 
CTAC 2024 Valencia - Henrik Hanke - Reduce to the max - slideshare.pdf
CTAC 2024 Valencia - Henrik Hanke - Reduce to the max - slideshare.pdfCTAC 2024 Valencia - Henrik Hanke - Reduce to the max - slideshare.pdf
CTAC 2024 Valencia - Henrik Hanke - Reduce to the max - slideshare.pdfhenrik385807
 
Presentation for the Strategic Dialogue on the Future of Agriculture, Brussel...
Presentation for the Strategic Dialogue on the Future of Agriculture, Brussel...Presentation for the Strategic Dialogue on the Future of Agriculture, Brussel...
Presentation for the Strategic Dialogue on the Future of Agriculture, Brussel...Krijn Poppe
 

Recently uploaded (20)

Philippine History cavite Mutiny Report.ppt
Philippine History cavite Mutiny Report.pptPhilippine History cavite Mutiny Report.ppt
Philippine History cavite Mutiny Report.ppt
 
SBFT Tool Competition 2024 -- Python Test Case Generation Track
SBFT Tool Competition 2024 -- Python Test Case Generation TrackSBFT Tool Competition 2024 -- Python Test Case Generation Track
SBFT Tool Competition 2024 -- Python Test Case Generation Track
 
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...
 
Gaps, Issues and Challenges in the Implementation of Mother Tongue Based-Mult...
Gaps, Issues and Challenges in the Implementation of Mother Tongue Based-Mult...Gaps, Issues and Challenges in the Implementation of Mother Tongue Based-Mult...
Gaps, Issues and Challenges in the Implementation of Mother Tongue Based-Mult...
 
call girls in delhi malviya nagar @9811711561@
call girls in delhi malviya nagar @9811711561@call girls in delhi malviya nagar @9811711561@
call girls in delhi malviya nagar @9811711561@
 
OSCamp Kubernetes 2024 | A Tester's Guide to CI_CD as an Automated Quality Co...
OSCamp Kubernetes 2024 | A Tester's Guide to CI_CD as an Automated Quality Co...OSCamp Kubernetes 2024 | A Tester's Guide to CI_CD as an Automated Quality Co...
OSCamp Kubernetes 2024 | A Tester's Guide to CI_CD as an Automated Quality Co...
 
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
 
Simulation-based Testing of Unmanned Aerial Vehicles with Aerialist
Simulation-based Testing of Unmanned Aerial Vehicles with AerialistSimulation-based Testing of Unmanned Aerial Vehicles with Aerialist
Simulation-based Testing of Unmanned Aerial Vehicles with Aerialist
 
Genesis part 2 Isaiah Scudder 04-24-2024.pptx
Genesis part 2 Isaiah Scudder 04-24-2024.pptxGenesis part 2 Isaiah Scudder 04-24-2024.pptx
Genesis part 2 Isaiah Scudder 04-24-2024.pptx
 
LANDMARKS AND MONUMENTS IN NIGERIA.pptx
LANDMARKS  AND MONUMENTS IN NIGERIA.pptxLANDMARKS  AND MONUMENTS IN NIGERIA.pptx
LANDMARKS AND MONUMENTS IN NIGERIA.pptx
 
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...
 
Open Source Camp Kubernetes 2024 | Monitoring Kubernetes With Icinga by Eric ...
Open Source Camp Kubernetes 2024 | Monitoring Kubernetes With Icinga by Eric ...Open Source Camp Kubernetes 2024 | Monitoring Kubernetes With Icinga by Eric ...
Open Source Camp Kubernetes 2024 | Monitoring Kubernetes With Icinga by Eric ...
 
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
 
The 3rd Intl. Workshop on NL-based Software Engineering
The 3rd Intl. Workshop on NL-based Software EngineeringThe 3rd Intl. Workshop on NL-based Software Engineering
The 3rd Intl. Workshop on NL-based Software Engineering
 
Event 4 Introduction to Open Source.pptx
Event 4 Introduction to Open Source.pptxEvent 4 Introduction to Open Source.pptx
Event 4 Introduction to Open Source.pptx
 
Genshin Impact PPT Template by EaTemp.pptx
Genshin Impact PPT Template by EaTemp.pptxGenshin Impact PPT Template by EaTemp.pptx
Genshin Impact PPT Template by EaTemp.pptx
 
Work Remotely with Confluence ACE 2.pptx
Work Remotely with Confluence ACE 2.pptxWork Remotely with Confluence ACE 2.pptx
Work Remotely with Confluence ACE 2.pptx
 
OSCamp Kubernetes 2024 | Zero-Touch OS-Infrastruktur für Container und Kubern...
OSCamp Kubernetes 2024 | Zero-Touch OS-Infrastruktur für Container und Kubern...OSCamp Kubernetes 2024 | Zero-Touch OS-Infrastruktur für Container und Kubern...
OSCamp Kubernetes 2024 | Zero-Touch OS-Infrastruktur für Container und Kubern...
 
CTAC 2024 Valencia - Henrik Hanke - Reduce to the max - slideshare.pdf
CTAC 2024 Valencia - Henrik Hanke - Reduce to the max - slideshare.pdfCTAC 2024 Valencia - Henrik Hanke - Reduce to the max - slideshare.pdf
CTAC 2024 Valencia - Henrik Hanke - Reduce to the max - slideshare.pdf
 
Presentation for the Strategic Dialogue on the Future of Agriculture, Brussel...
Presentation for the Strategic Dialogue on the Future of Agriculture, Brussel...Presentation for the Strategic Dialogue on the Future of Agriculture, Brussel...
Presentation for the Strategic Dialogue on the Future of Agriculture, Brussel...
 

trees in data structure

  • 2.
  • 3. In computer science, a tree is a widely-used data structure that emulates a hierarchical tree structure with a set of linked nodes.
  • 4. Nature View of a Tree branche s leaves root
  • 6. What is a Tree  A tree is a finite nonempty set of elements.  It is an abstract model of a hierarchical structure.  consists of nodes with a parent-child relation.  Applications:  Organization charts  File systems  Programming environments Computers”R”Us Sales R&DManufacturing Laptops DesktopsUS International Europe Asia Canada
  • 8. Cont…  Root-The top most Node.  Children  Parents  Siblings- Have same parents.  Leaf- Has no Child. RELATION OF TREE 1 3 2 4 6 5 8 7 11109 8
  • 9. Trees Data Structures  Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent  Binary tree  Tree with 0–2 children per node Tree Binary Tree 9
  • 10. Trees  Terminology  Root  no parent  Leaf  no child  Interior  non-leaf  Height  distance from root to leaf Root node Leaf nodes Interior nodes Height 10
  • 11. Example Tree root ActivitiesTeaching Research Sami’s Home Page Papers PresentationsCS211CS101
  • 12. Tree Terminology  Path: Traversal from node to node along the edges results in a sequence called path.  Root: Node at the top of the tree.  Parent: Any node, except root has exactly one edge running upward to another node. The node above it is called parent.  Child: Any node may have one or more lines running downward to other nodes. Nodes below are children.  Leaf: A node that has no children.  Subtree: Any node can be considered to be the root of a subtree, which consists of its children and its children’s children and so on.
  • 13. subtree Tree Terminology Root: node without parent (A)  Siblings: nodes share the same parent  Internal node: node with at least one child (A, B, C, F)  External node (leaf ): node without children (E, I, J, K, G, H, D)  Ancestors of a node: parent, grandparent, grand-grandparent, etc.  Descendant of a node: child, grandchild, grand-grandchild, etc.  Depth of a node: number of ancestors  Height of a tree: maximum depth of any node (3)  Degree of a node: the number of its children  Degree of a tree: the maximum number of its node. A B DC G HE F I J K Subtree: tree consisting of a node and its descendants
  • 14. General Trees  Trees store data in a hierarchical manner  Root node is A  A's children are B, C, and D  E, F, and D are leaves  Length of path from A to E is 2 edges A DB C FE Trees have layers of nodes, where some are higher, others are lower.
  • 15. Logic of Tree Used to represent hierarchical data.  BS(CS) 1ST tree e.g. BS(CS)1st Section (A) Section (B) CR 1 CR2 group 1 group 2 group 3 group 4 group 1 group 2 group 3 group 4 15
  • 16. Logic of Tree Used to represent hierarchical data.  BS(CS) 1ST tree e.g. BS(CS)1st Section (A) Section (B) CR 1 CR2 group 1 group 2 group 3 group 4 group 1 group 2 group 3 group 4 16
  • 17. Cont…  A Collection of entities called Nodes.  Tree is a Non-Linear Data Structure.  It’s a hierarchica Structure. TREE Nodes 17
  • 18. Tree Traversal  Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways.  Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all nodes are connected via edges (links) we always start from the root (head) node.
  • 19. Cont…  That is, we cannot randomly access a node in a tree. There are three ways which we use to traverse a tree  (a) Inorder (Left, Root, Right) : 4 2 5 1 3 (b) Preorder (Root, Left, Right) : 1 2 4 5 3 (c) Postorder (Left, Right, Root) : 4 5 2 3 1 
  • 20. In-order Traversal  In this traversal method, the left subtree is visited first, then the root and later the right sub-tree. We should always remember that every node may represent a subtree itself.
  • 22. Inorder (Left, Root, Right)  We start from A, and following in-order traversal, we move to its left subtree B. B is also traversed in-order. The process goes on until all the nodes are visited. The output of inorder traversal of this tree will be −  D → B → E → A → F → C → G
  • 23. Inorder Algorithm  Until all nodes are traversed −  Step 1 − Recursively traverse left subtree.  Step 2 − Visit root node.  Step 3 − Recursively traverse right subtree.
  • 24. Pre-order Traversal  In this traversal method, the root node is visited first, then the left subtree and finally the right subtree.
  • 26. Preorder (Root, Left, Right)  We start from A, and following pre-order traversal, we first visit A itself and then move to its left subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited. The output of pre-order traversal of this tree will be.  A → B → D → E → C → F → G
  • 27. Algorithm  Until all nodes are traversed −  Step 1 − Visit root node.  Step 2 − Recursively traverse left subtree.  Step 3 − Recursively traverse right subtree.
  • 28. Post-order Traversal  In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree, then the right subtree and finally the root node.
  • 30. Postorder (Left, Right, Root)  We start from A, and following pre-order traversal, we first visit the left subtree B. B is also traversed post-order. The process goes on until all the nodes are visited. The output of post-order traversal of this tree will be.  D → E → B → F → G → C → A
  • 31. Algorithm  Until all nodes are traversed  Step 1 − Recursively traverse left subtree.  Step 2 − Recursively traverse right subtree.  Step 3 − Visit root node.
  • 33. • If root is NULL • then create root node • return • If root exists then • compare the data with node.data • while until insertion position is located • If data is greater than node.data • goto right subtree • else • goto left subtree • endwhile • insert data • end If
  • 34. If root.data is equal to search.data return root else while data not found If data is greater than node.data goto right subtree else goto left subtree If data found return node endwhile return data not found end if
  • 36.
  • 37. There are a large number of tree types, however three types specifically are used the majority of the time (both in real world development and in Computer Science courses). The types are:  Binary trees  B-Trees  Heaps
  • 38. Binary trees  Binary tree follows the rule that each parent node has no more than 2 child nodes. This means that a binary tree can look like this:  OR
  • 39. Strict Binary Tree  Strict Binary Tree is a Binary tree in which root can have exactly two children or no children at all. That means, there can be 0 or 2 children of any node.
  • 40. Complete Binary Tree  Complete Binary Tree is a Strict Binary tree in which every leaf node is at same level. That means, there are equal number of children in right and left subtree for every node.
  • 41. Extended Binary Tree  An extended binary tree is a transformation of any binary tree into a complete binary tree.  This transformation consists of replacing every null subtree of the original tree with “special nodes.”  The nodes from the original tree are then called as internal nodes, while the “special nodes” are called as external nodes.
  • 42. Extended Binary Tree A binary tree with special nodes replacing every null subtree. Every regular node has two children, and every special node has no children.
  • 43. Binary Search Tree  The value at any node, • Greater than every value in left subtree. Smaller than every value in right subtree – Example  Y > X  Y < Z  Values in left sub tree less than parent  Values in right sub tree greater than parent  Fast searches in a Binary Search tree, maximum of log n comparisons.
  • 44. Binary Search Trees  Examples  Binary search tree
  • 45. B - Trees  B – Tree is another type of search tree called B-Tree in which a node can store more than one value (key) and it can have more than two children.  B-Tree can be defined as follows…  B-Tree is a self-balanced search tree with multiple keys in every node and more than two children for every node.
  • 46. Operations on a B-Tree  The following operations are performed on a B-Tree...  Search  Insertion  Deletion
  • 47. Heap Tree  Heap is a special case of balanced binary tree data structure where the root-node key is compared with its children and arranged accordingly. If α has child node β then  key(α) ≥ key(β)  A heap can be of two types  Min-Heap  Max-Heap
  • 48. Max-Heap  Max-Heap − Where the value of the root node is greater than or equal to either of its children.
  • 51. Max Heap Construction Algorithm  Step 1 − Create a new node.  Step 2 − Assign new value to the node.  Step 3 − Compare the value of this child node with its parent.  Step 4 − If value of parent is less than child, then swap them.  Step 5 − Repeat step 3 & 4 until Heap property holds.
  • 52. Max Heap Deletion Algorithm  Step 1 − Remove root node.  Step 2 − Move the last element of last level to root.  Step 3 − Compare the value of this child node with its parent.  Step 4 − If value of parent is less than child, then swap them.  Step 5 − Repeat step 3 & 4 until Heap property holds.
  • 53. Min-Heap  Min-Heap − Where the value of the root node is less than or equal to either of its children.