SlideShare a Scribd company logo
1 of 19
Lecture No.1
Data Structures
Mohammad Nazir
Trace of insert
17, 9, 14, 5
14
154
9
7
183
5
16 20
17
p
p->setRight( node );
node
Cost of Search
 Given that a binary tree is level d deep.
How long does it take to find out whether a
number is already present?
 Consider the insert(17) in the example
tree.
 Each time around the while loop, we did
one comparison.
 After the comparison, we moved a level
down.
Cost of Search
 With the binary tree in place, we can write
a routine find(x) that returns true if the
number x is present in the tree, false
otherwise.
 How many comparison are needed to find
out if x is present in the tree?
 We do one comparison at each level of the
tree until either x is found or q becomes
NULL.
Cost of Search
 If the binary tree is built out of n numbers,
how many comparisons are needed to find
out if a number x is in the tree?
 Recall that the depth of the complete
binary tree built using ‘n’ nodes will be
log2(n+1) – 1.
 For example, for n=100,000, log2(100001)
is less than 20; the tree would be 20 levels
deep.
Binary Search Tree
 A binary tree with the property that items in
the left subtree are smaller than the root
and items are larger or equal in the right
subtree is called a binary search tree
(BST).
 The tree we built for searching for
duplicate numbers was a binary search
tree.
 BST and its variations play an important
role in searching algorithms.
Traversing a Binary Tree
 Suppose we have a binary tree, ordered
(BST) or unordered.
 We want to print all the values stored in
the nodes of the tree.
 In what order should we print them?
Traversing a Binary Tree
 Ways to print a 3 node tree:
14
154
(4, 14, 15), (4,15,14)
(14,4,15), (14,15,4)
(15,4,14), (15,14,4)
Traversing a Binary Tree
 In case of the general binary tree:
node
(L,N,R), (L,R,N)
(N,L,R), (N,R,L)
(R,L,N), (R,N,L)
left
subtree
right
subtree
L
N
R
Traversing a Binary Tree
 Three common ways
node
Preorder: (N,L,R)
Inorder: (L,N,R)
Postorder: (L,R,N)
left
subtree
right
subtreeL
N
R
Traversing a Binary Tree
void preorder(TreeNode<int>* treeNode)
{
if( treeNode != NULL )
{
cout << *(treeNode->getInfo())<<" ";
preorder(treeNode->getLeft());
preorder(treeNode->getRight());
}
}
Traversing a Binary Tree
void inorder(TreeNode<int>* treeNode)
{
if( treeNode != NULL )
{
inorder(treeNode->getLeft());
cout << *(treeNode->getInfo())<<" ";
inorder(treeNode->getRight());
}
}
Traversing a Binary Tree
void postorder(TreeNode<int>* treeNode)
{
if( treeNode != NULL )
{
postorder(treeNode->getLeft());
postorder(treeNode->getRight());
cout << *(treeNode->getInfo())<<" ";
}
}
Traversing a Binary Tree
cout << "inorder: "; preorder( root);
cout << "inorder: "; inorder( root );
cout << "postorder: "; postorder( root );
Traversing a Binary Tree
Preorder: 14 4 3 9 7 5 15 18 16 17 20
14
154
9
7
183
5
16 20
17
Traversing a Binary Tree
Inorder: 3 4 5 7 9 14 15 16 17 18 20
14
154
9
7
183
5
16 20
17
Traversing a Binary Tree
Postorder: 3 5 7 9 4 17 16 20 18 15 14
14
154
9
7
183
5
16 20
17
Recursive Call
 Recall that a stack is used during function
calls.
 The caller function places the arguments
on the stack and passes control to the
called function.
 Local variables are allocated storage on
the call stack.
 Calling a function itself makes no
difference as far as the call stack is
concerned.
Stack Layout during a call
 Here is stack layout when function F calls
function F (recursively):
Parameters(F)
Local variables(F)
Return address(F)
Parameters(F)
Parameters(F)
Local variables(F)
Return address(F)
Parameters(F)
Local variables(F)
Return address(F)
Parameters(F)
Local variables(F)
Return address(F)
During execution of F After callAt point of call
sp
sp
sp

More Related Content

What's hot (20)

THREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREETHREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREE
 
Binary tree traversal ppt
Binary tree traversal pptBinary tree traversal ppt
Binary tree traversal ppt
 
binary tree
binary treebinary tree
binary tree
 
CMSC 350 HOMEWORK 3
CMSC 350 HOMEWORK 3CMSC 350 HOMEWORK 3
CMSC 350 HOMEWORK 3
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Lecture7 data structure(tree)
Lecture7 data structure(tree)Lecture7 data structure(tree)
Lecture7 data structure(tree)
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 
Week 8 (trees)
Week 8 (trees)Week 8 (trees)
Week 8 (trees)
 
SPIRE2013-tabei20131009
SPIRE2013-tabei20131009SPIRE2013-tabei20131009
SPIRE2013-tabei20131009
 
Lec18
Lec18Lec18
Lec18
 
10 Red-Black Trees
10 Red-Black Trees10 Red-Black Trees
10 Red-Black Trees
 
Suffix Tree and Suffix Array
Suffix Tree and Suffix ArraySuffix Tree and Suffix Array
Suffix Tree and Suffix Array
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
 
Binary tree
Binary tree Binary tree
Binary tree
 
binary search tree
binary search treebinary search tree
binary search tree
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Review session2
Review session2Review session2
Review session2
 

Similar to Data structures final lecture 1

Similar to Data structures final lecture 1 (20)

Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
Trees
TreesTrees
Trees
 
Trees
TreesTrees
Trees
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
Balance tree. Short overview
Balance tree. Short overviewBalance tree. Short overview
Balance tree. Short overview
 
Chap 5 Tree.ppt
Chap 5 Tree.pptChap 5 Tree.ppt
Chap 5 Tree.ppt
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Q
QQ
Q
 
Paul presentation P2P Chord v1
Paul presentation P2P Chord v1Paul presentation P2P Chord v1
Paul presentation P2P Chord v1
 
Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103
 
lecture 9
lecture 9lecture 9
lecture 9
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptx
 
Red black tree
Red black treeRed black tree
Red black tree
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
Trees in data structrures
Trees in data structruresTrees in data structrures
Trees in data structrures
 
Tree distance algorithm
Tree distance algorithmTree distance algorithm
Tree distance algorithm
 
Unit8 C
Unit8 CUnit8 C
Unit8 C
 
Red Black Tree
Red Black TreeRed Black Tree
Red Black Tree
 
L&NDeltaTalk
L&NDeltaTalkL&NDeltaTalk
L&NDeltaTalk
 

More from Nazir Ahmed

More from Nazir Ahmed (20)

Data structures lecture 04
Data structures  lecture 04Data structures  lecture 04
Data structures lecture 04
 
Data structure
Data structureData structure
Data structure
 
Cover letter
Cover letterCover letter
Cover letter
 
Control unit design(1)
Control unit design(1)Control unit design(1)
Control unit design(1)
 
Computer architecture mcq (2)
Computer architecture mcq (2)Computer architecture mcq (2)
Computer architecture mcq (2)
 
Complete binary tree and heap
Complete binary tree and heapComplete binary tree and heap
Complete binary tree and heap
 
Clamper clipper(10)
Clamper clipper(10)Clamper clipper(10)
Clamper clipper(10)
 
Cisc mc68000
Cisc mc68000Cisc mc68000
Cisc mc68000
 
Chapter 08
Chapter 08Chapter 08
Chapter 08
 
Chapter 07 ddl_sql
Chapter 07 ddl_sqlChapter 07 ddl_sql
Chapter 07 ddl_sql
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
 
Chapter # 12 er modeling
Chapter # 12 er modelingChapter # 12 er modeling
Chapter # 12 er modeling
 
Chapeter 2
Chapeter 2Chapeter 2
Chapeter 2
 
Ch7 1 v1
Ch7 1 v1Ch7 1 v1
Ch7 1 v1
 
Ch07 deadlocks
Ch07 deadlocksCh07 deadlocks
Ch07 deadlocks
 
Ch05 cpu-scheduling
Ch05 cpu-schedulingCh05 cpu-scheduling
Ch05 cpu-scheduling
 
Ch04 threads
Ch04 threadsCh04 threads
Ch04 threads
 
Ch03 processes
Ch03 processesCh03 processes
Ch03 processes
 
Ch1 v1
Ch1 v1Ch1 v1
Ch1 v1
 

Recently uploaded

internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 

Recently uploaded (20)

internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 

Data structures final lecture 1

  • 2. Trace of insert 17, 9, 14, 5 14 154 9 7 183 5 16 20 17 p p->setRight( node ); node
  • 3. Cost of Search  Given that a binary tree is level d deep. How long does it take to find out whether a number is already present?  Consider the insert(17) in the example tree.  Each time around the while loop, we did one comparison.  After the comparison, we moved a level down.
  • 4. Cost of Search  With the binary tree in place, we can write a routine find(x) that returns true if the number x is present in the tree, false otherwise.  How many comparison are needed to find out if x is present in the tree?  We do one comparison at each level of the tree until either x is found or q becomes NULL.
  • 5. Cost of Search  If the binary tree is built out of n numbers, how many comparisons are needed to find out if a number x is in the tree?  Recall that the depth of the complete binary tree built using ‘n’ nodes will be log2(n+1) – 1.  For example, for n=100,000, log2(100001) is less than 20; the tree would be 20 levels deep.
  • 6. Binary Search Tree  A binary tree with the property that items in the left subtree are smaller than the root and items are larger or equal in the right subtree is called a binary search tree (BST).  The tree we built for searching for duplicate numbers was a binary search tree.  BST and its variations play an important role in searching algorithms.
  • 7. Traversing a Binary Tree  Suppose we have a binary tree, ordered (BST) or unordered.  We want to print all the values stored in the nodes of the tree.  In what order should we print them?
  • 8. Traversing a Binary Tree  Ways to print a 3 node tree: 14 154 (4, 14, 15), (4,15,14) (14,4,15), (14,15,4) (15,4,14), (15,14,4)
  • 9. Traversing a Binary Tree  In case of the general binary tree: node (L,N,R), (L,R,N) (N,L,R), (N,R,L) (R,L,N), (R,N,L) left subtree right subtree L N R
  • 10. Traversing a Binary Tree  Three common ways node Preorder: (N,L,R) Inorder: (L,N,R) Postorder: (L,R,N) left subtree right subtreeL N R
  • 11. Traversing a Binary Tree void preorder(TreeNode<int>* treeNode) { if( treeNode != NULL ) { cout << *(treeNode->getInfo())<<" "; preorder(treeNode->getLeft()); preorder(treeNode->getRight()); } }
  • 12. Traversing a Binary Tree void inorder(TreeNode<int>* treeNode) { if( treeNode != NULL ) { inorder(treeNode->getLeft()); cout << *(treeNode->getInfo())<<" "; inorder(treeNode->getRight()); } }
  • 13. Traversing a Binary Tree void postorder(TreeNode<int>* treeNode) { if( treeNode != NULL ) { postorder(treeNode->getLeft()); postorder(treeNode->getRight()); cout << *(treeNode->getInfo())<<" "; } }
  • 14. Traversing a Binary Tree cout << "inorder: "; preorder( root); cout << "inorder: "; inorder( root ); cout << "postorder: "; postorder( root );
  • 15. Traversing a Binary Tree Preorder: 14 4 3 9 7 5 15 18 16 17 20 14 154 9 7 183 5 16 20 17
  • 16. Traversing a Binary Tree Inorder: 3 4 5 7 9 14 15 16 17 18 20 14 154 9 7 183 5 16 20 17
  • 17. Traversing a Binary Tree Postorder: 3 5 7 9 4 17 16 20 18 15 14 14 154 9 7 183 5 16 20 17
  • 18. Recursive Call  Recall that a stack is used during function calls.  The caller function places the arguments on the stack and passes control to the called function.  Local variables are allocated storage on the call stack.  Calling a function itself makes no difference as far as the call stack is concerned.
  • 19. Stack Layout during a call  Here is stack layout when function F calls function F (recursively): Parameters(F) Local variables(F) Return address(F) Parameters(F) Parameters(F) Local variables(F) Return address(F) Parameters(F) Local variables(F) Return address(F) Parameters(F) Local variables(F) Return address(F) During execution of F After callAt point of call sp sp sp