SlideShare a Scribd company logo
THUSHAPAN
Data Structures and Algorithms
HND in Computing and Software
Engineering
Lecture - 10
What we have done previously ?
Summary of previous lectures.
● Introduction to Data Structure
● Abstract Data Types
● Why data structure
● Introduction to Algorithms
● Measurements of Algorithms
● Stacks & Queues
● Arrays & Linked Lists
● Stack , Queue With Linked List
● Algorithms
● Big O notation
● Recursion
● Sorting
● Searching
Binary Trees
Fundamental data structure
Combines the advantages of an ordered array and a linked list.
You can search an ordered array quickly [O(log N)].
You can insert and delete items quickly in a linked list [O(1)].
It would be nice, if there were a data structure with quick insertion/deletion
and quick search.
Trees provide both of these characteristics.
Trees
A tree consists of nodes connected by edges.
The nodes are represented as circles.
The edges are represented as lines.
In computer programs,
Nodes - people, car parts, airline reservations, and so on(objects).
Edges - relationship between two nodes (represented as a reference in a Java program).
Traversing
Only way to get from node to node is to follow a path along the lines.
Trees (Contd.)
Structure
There is one node (root) in the top row of a tree.
One or more nodes in the second row connecting to the root node.
Even more nodes in the third row and they are connected to nodes in the second row, and
so on.
Programs starts an operation at the top (root), and traverse from top to bottom.
Binary Tree
Each node in a binary tree has a maximum of two children.
More generally, a tree can have more than two children, and are called as multiway trees.
Tree Terminology
Tree Terminology (Contd.)
Path - Think of someone walking from node to node along the edges that
connect them. The resulting sequence of nodes is called a path.
Root - The node at the top of the tree.
- There is only one root in a tree.
- For a collection of nodes and edges to be a tree, there must be one and
only one path from the root to any other node.
Tree Terminology (Contd.)
Parent - Any node (except the root) has exactly one edge running upward to
another node. The node above it is called the parent of the node.
Child - Any node may have one or more lines running downward to other
nodes. These nodes below a given node are called its children.
Leaf - A node that has no children is called a leaf node or simply a leaf. There
can be only one root in a tree, but there can be many leaves.
Subtree - Any node may be considered to be the root of a subtree, which
consists of its children, and its children’s children, and so on. If you think in
terms of families, a node’s subtree contains all its descendants.
Tree Terminology (Contd.)
Visiting
A node is visited when the program control arrives at the node.
Carry out some operation on the node (E.g. display contents).
Passing the control from one node to another is not considered as visiting
the node.
Traversing
Visit all the nodes in some specified order (E.g. visiting all the nodes in
ascending order).
Tree Terminology (Contd.)
Level of a node
How many generations the node is from the root.
Root - level 0
Its children - level 1
Its grandchildren - level 2
Keys
One data field in an object is designated as a key value.
This value is used to search for the item or perform other operations on it.
Binary Trees
A tree that can have at most two children.
Simplest and most common in Data Structures.
Two children - left and right (corresponding to their position).
A node in a binary tree doesn’t need to have the maximum of two children.
It may have only a left child, or only a right child, or it can have no children at
all (leaf).
E.g. See slide 5.
Binary Search Trees
It’s a binary tree.
For each node,
Left child must have a key value less than the node’s key value.
Right child must have a key value greater than or equal to the node’s key
value.
Unbalanced Trees
Most of their nodes on one side of the root or the other.
Trees become unbalanced because of the order in which the data items are
inserted.
E.g. If the sequence of numbers 11, 18, 33, 42 and 65 is inserted into a binary
search tree, all the values will be right children and the tree will be
unbalanced.
The Node Class
The Tree Class
Finding a Node
Finding Node 57
Code
Big O ?
Inserting a Node
Think yourself ?
Inserting a Node
Traversing the Tree
Inorder Traversal (LNR)
Example
● We start by inOrder() with the root A as an argument (say inOrder(A)).
● inOrder(A) first calls inOrder() with its left child, B, as an argument
(inOrder(B)).
● inOrder(B) now calls inOrder() with its left child as an argument.
● However, it has no left child, so this argument is null (inOrder(null)).
● There are now three frames of inOrder() in the call stack (inOrder(A),
inOrder(B) and inOrder(null)).
● However, inOrder(null) returns immediately to its called method
(inOrder(B)) because its argument is null.
Now inOrder(B) goes on to visit B and display it.
Then inOrder(B) calls inOrder() again, with its right child (null) as an argument
(i.e. inOrder(null)).
Again it returns immediately to inOrder(B).
Now inOrder(B) completed its three steps, so the program control goes back to
inOrder(A). ? Now inOrder(A) visits A and display it.
Next inOrder(A) calls inOrder() with its right child, C, as an argument (inOrder(C)).
Like inOrder(B), inOrder(C) has no children, so step 1 returns with no action, step
2 visits C, and step 3 returns with no action.
Next, control goes back to inOrder(A), and inOrder(A) has completed its 3 steps,
and the entire traversal is complete.
Code: Inorder Traversal
Example - Inorder (LNR)
A
B C
D E F
G H I
B D A G E C H F I
Preorder and Postorder Traversals
Preorder Traversal (NLR)
1. Visit the node.
2. Call itself to traverse the nodes left subtree.
3. Call itself to traverse the nodes right subtree.
Postorder Traversal (LRN)
1. Call itself to traverse the nodes left subtree.
2. Call itself to traverse the nodes right subtree.
3. Visit the node.
Example - Preorder (NLR)
A
B C
D E F
G H I
A B D C E G F H I
Example - Postorder (LRN)
A
B C
D E F
G H I
D B G E H I E G A
Try this
Find Preorder Traversal , Inorder Traversal , Postorder Traversal
100 , 20 , 10 , 30 , 200 , 150 , 300
10 , 20 , 30 , 100 , 150 , 200 , 300
10 , 30 , 20 , 150 , 300 , 200 , 100
Deleting a Node
Deleting a Node - Leaf Node
Deleting a Node - One Child
Data Structures and Algorithms - Lecture 10 - Thushapan.pptx
Data Structures and Algorithms - Lecture 10 - Thushapan.pptx
Deleting a Node - Two Children
it's up to you
Data Structures and Algorithms - Lecture 10 - Thushapan.pptx
Big O of Binary tree ?
Do you have any idea ?
Let’s take N = No of nodes and L = No of levels
N = 2L
1
−
N + 1 = 2L
L = log2(N + 1)
Thus, the time needed to carry out the common tree operati
is proportional to the base 2 log of N.
In Big O notation we say such operations take O(logN) time.
Time for questions
Do you have any questions ?
Will meet again in the next session !
Thushapan

More Related Content

PPTX
Saikat techhnology of techtechhnology of techGhorai.pptx
PPTX
Tree structure and its definitions with an example
PPT
Binary tree
PPTX
trees in data structure
PDF
Lecture notes data structures tree
PPTX
BST.pptx this isp used for learning binary search trees
PPTX
BST.pptx this is Good for data structure
PDF
481803111-Trees-Unit-Review-pdf - Copy.pdf
Saikat techhnology of techtechhnology of techGhorai.pptx
Tree structure and its definitions with an example
Binary tree
trees in data structure
Lecture notes data structures tree
BST.pptx this isp used for learning binary search trees
BST.pptx this is Good for data structure
481803111-Trees-Unit-Review-pdf - Copy.pdf

Similar to Data Structures and Algorithms - Lecture 10 - Thushapan.pptx (20)

PDF
Tree Data Structure by Daniyal Khan
PPT
Tree and Binary Search tree
PDF
Dsc++ unit 3 notes
DOCX
Biary search Tree.docx
PPTX
Basic Tree Data Structure BST Traversals .pptx
DOCX
data structures Unit 3 notes.docxdata structures Unit 3 notes.docx
PPT
9. TREE Data Structure Non Linear Data Structure
PPTX
TREE PRESENTATION COMPUTER SCIENCE/DATA STRUCTURE
PPTX
Introduction to Tree_Data Structure.pptx
PPT
Cinterviews Binarysearch Tree
PPTX
Tree.pptx
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PPTX
NON-LINEAR DATA STRUCTURE-TREES.pptx
PPTX
Binary trees1
PPTX
Tree all information about tree concept are available .
PPTX
Tree Data Structure Tree Data Structure Details
PPTX
Trees in data structure
PPT
BINARY SEARCH TREE
PPTX
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
PPT
Tree data structure
Tree Data Structure by Daniyal Khan
Tree and Binary Search tree
Dsc++ unit 3 notes
Biary search Tree.docx
Basic Tree Data Structure BST Traversals .pptx
data structures Unit 3 notes.docxdata structures Unit 3 notes.docx
9. TREE Data Structure Non Linear Data Structure
TREE PRESENTATION COMPUTER SCIENCE/DATA STRUCTURE
Introduction to Tree_Data Structure.pptx
Cinterviews Binarysearch Tree
Tree.pptx
UNIT III Non Linear Data Structures - Trees.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
Binary trees1
Tree all information about tree concept are available .
Tree Data Structure Tree Data Structure Details
Trees in data structure
BINARY SEARCH TREE
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
Tree data structure
Ad

Recently uploaded (20)

PPT
Teaching material agriculture food technology
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Approach and Philosophy of On baking technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Cloud computing and distributed systems.
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Encapsulation_ Review paper, used for researhc scholars
Teaching material agriculture food technology
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Network Security Unit 5.pdf for BCA BBA.
Programs and apps: productivity, graphics, security and other tools
Approach and Philosophy of On baking technology
“AI and Expert System Decision Support & Business Intelligence Systems”
Unlocking AI with Model Context Protocol (MCP)
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Understanding_Digital_Forensics_Presentation.pptx
Machine learning based COVID-19 study performance prediction
Building Integrated photovoltaic BIPV_UPV.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Big Data Technologies - Introduction.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Cloud computing and distributed systems.
NewMind AI Weekly Chronicles - August'25 Week I
Spectral efficient network and resource selection model in 5G networks
Encapsulation_ Review paper, used for researhc scholars
Ad

Data Structures and Algorithms - Lecture 10 - Thushapan.pptx

  • 1. THUSHAPAN Data Structures and Algorithms HND in Computing and Software Engineering Lecture - 10
  • 2. What we have done previously ? Summary of previous lectures. ● Introduction to Data Structure ● Abstract Data Types ● Why data structure ● Introduction to Algorithms ● Measurements of Algorithms ● Stacks & Queues ● Arrays & Linked Lists ● Stack , Queue With Linked List ● Algorithms ● Big O notation ● Recursion ● Sorting ● Searching
  • 3. Binary Trees Fundamental data structure Combines the advantages of an ordered array and a linked list. You can search an ordered array quickly [O(log N)]. You can insert and delete items quickly in a linked list [O(1)]. It would be nice, if there were a data structure with quick insertion/deletion and quick search. Trees provide both of these characteristics.
  • 4. Trees A tree consists of nodes connected by edges. The nodes are represented as circles. The edges are represented as lines. In computer programs, Nodes - people, car parts, airline reservations, and so on(objects). Edges - relationship between two nodes (represented as a reference in a Java program). Traversing Only way to get from node to node is to follow a path along the lines.
  • 5. Trees (Contd.) Structure There is one node (root) in the top row of a tree. One or more nodes in the second row connecting to the root node. Even more nodes in the third row and they are connected to nodes in the second row, and so on. Programs starts an operation at the top (root), and traverse from top to bottom. Binary Tree Each node in a binary tree has a maximum of two children. More generally, a tree can have more than two children, and are called as multiway trees.
  • 7. Tree Terminology (Contd.) Path - Think of someone walking from node to node along the edges that connect them. The resulting sequence of nodes is called a path. Root - The node at the top of the tree. - There is only one root in a tree. - For a collection of nodes and edges to be a tree, there must be one and only one path from the root to any other node.
  • 8. Tree Terminology (Contd.) Parent - Any node (except the root) has exactly one edge running upward to another node. The node above it is called the parent of the node. Child - Any node may have one or more lines running downward to other nodes. These nodes below a given node are called its children. Leaf - A node that has no children is called a leaf node or simply a leaf. There can be only one root in a tree, but there can be many leaves. Subtree - Any node may be considered to be the root of a subtree, which consists of its children, and its children’s children, and so on. If you think in terms of families, a node’s subtree contains all its descendants.
  • 9. Tree Terminology (Contd.) Visiting A node is visited when the program control arrives at the node. Carry out some operation on the node (E.g. display contents). Passing the control from one node to another is not considered as visiting the node. Traversing Visit all the nodes in some specified order (E.g. visiting all the nodes in ascending order).
  • 10. Tree Terminology (Contd.) Level of a node How many generations the node is from the root. Root - level 0 Its children - level 1 Its grandchildren - level 2 Keys One data field in an object is designated as a key value. This value is used to search for the item or perform other operations on it.
  • 11. Binary Trees A tree that can have at most two children. Simplest and most common in Data Structures. Two children - left and right (corresponding to their position). A node in a binary tree doesn’t need to have the maximum of two children. It may have only a left child, or only a right child, or it can have no children at all (leaf). E.g. See slide 5.
  • 12. Binary Search Trees It’s a binary tree. For each node, Left child must have a key value less than the node’s key value. Right child must have a key value greater than or equal to the node’s key value.
  • 13. Unbalanced Trees Most of their nodes on one side of the root or the other. Trees become unbalanced because of the order in which the data items are inserted. E.g. If the sequence of numbers 11, 18, 33, 42 and 65 is inserted into a binary search tree, all the values will be right children and the tree will be unbalanced.
  • 18. Code
  • 25. ● We start by inOrder() with the root A as an argument (say inOrder(A)). ● inOrder(A) first calls inOrder() with its left child, B, as an argument (inOrder(B)). ● inOrder(B) now calls inOrder() with its left child as an argument. ● However, it has no left child, so this argument is null (inOrder(null)). ● There are now three frames of inOrder() in the call stack (inOrder(A), inOrder(B) and inOrder(null)). ● However, inOrder(null) returns immediately to its called method (inOrder(B)) because its argument is null.
  • 26. Now inOrder(B) goes on to visit B and display it. Then inOrder(B) calls inOrder() again, with its right child (null) as an argument (i.e. inOrder(null)). Again it returns immediately to inOrder(B). Now inOrder(B) completed its three steps, so the program control goes back to inOrder(A). ? Now inOrder(A) visits A and display it. Next inOrder(A) calls inOrder() with its right child, C, as an argument (inOrder(C)). Like inOrder(B), inOrder(C) has no children, so step 1 returns with no action, step 2 visits C, and step 3 returns with no action. Next, control goes back to inOrder(A), and inOrder(A) has completed its 3 steps, and the entire traversal is complete.
  • 28. Example - Inorder (LNR) A B C D E F G H I B D A G E C H F I
  • 29. Preorder and Postorder Traversals Preorder Traversal (NLR) 1. Visit the node. 2. Call itself to traverse the nodes left subtree. 3. Call itself to traverse the nodes right subtree. Postorder Traversal (LRN) 1. Call itself to traverse the nodes left subtree. 2. Call itself to traverse the nodes right subtree. 3. Visit the node.
  • 30. Example - Preorder (NLR) A B C D E F G H I A B D C E G F H I
  • 31. Example - Postorder (LRN) A B C D E F G H I D B G E H I E G A
  • 32. Try this Find Preorder Traversal , Inorder Traversal , Postorder Traversal 100 , 20 , 10 , 30 , 200 , 150 , 300 10 , 20 , 30 , 100 , 150 , 200 , 300 10 , 30 , 20 , 150 , 300 , 200 , 100
  • 34. Deleting a Node - Leaf Node
  • 35. Deleting a Node - One Child
  • 38. Deleting a Node - Two Children it's up to you
  • 40. Big O of Binary tree ? Do you have any idea ? Let’s take N = No of nodes and L = No of levels N = 2L 1 − N + 1 = 2L L = log2(N + 1) Thus, the time needed to carry out the common tree operati is proportional to the base 2 log of N. In Big O notation we say such operations take O(logN) time.
  • 41. Time for questions Do you have any questions ?
  • 42. Will meet again in the next session ! Thushapan