standard template library stacks · stacks – container class available in the c++ stl •...
TRANSCRIPT
![Page 1: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/1.jpg)
STANDARD TEMPLATE LIBRARY STACKS
Problem Solving with Computers-II
Freq AC
![Page 2: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/2.jpg)
Announcements• Midterm Review session 5p - 7p Monday( today) in HFH 1132
!2
Cumulative TopicBig four BST
Big ohexoz webpage 624 website
![Page 3: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/3.jpg)
Height of a completely filled treeLevel 0
Level 1
Level 2
……Finally, what is the height (exactly) of the tree in terms of N?
!3
Almost all the operationsof aBST a OCHMax t.tl a N l
bestcase H OClosN
He 0C dog N
H dogCN l I
![Page 4: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/4.jpg)
Balanced trees• Balanced trees by definition have a height of O(log N) • A completely filled tree is one example of a balanced tree • Other Balanced BSTs include AVL trees, red black trees and so on • Visualize operations on an AVL tree: https://visualgo.net/bn/bst
![Page 5: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/5.jpg)
C++STL• The C++ Standard Template Library is a very handy set of three built-in components:
• Containers: Data structures • Iterators: Standard way to search containers • Algorithms: These are what we ultimately use to solve problems
!5
generic datastructuresGeneric wayof parsing1
iteratingthroughdatastructures
![Page 6: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/6.jpg)
C++ STL container classes
!6
array vector
forward_list list set
stack queue
priority_queue multiset (non unique keys)
deque unordered_set
map unordered_map
multimap bitset
fixed lengtharraydynamicsingle linked
listdouble linkedlistbalanced BSTtoday
7032andonwards
![Page 7: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/7.jpg)
Stacks – container class available in the C++ STL• Container class that uses the Last In First Out (LIFO) principle • Methods i. push() ii. pop() iii. top() iv. empty()
!7
Demo reversing a string
insert to the top of stack
delete'pishcuo the datasmuchue stackgettop push IOthetopC 1110 is difgereat from
the runtime stackitemptypope even though
both
10 follow the Ufo
140J principle
![Page 8: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/8.jpg)
Lab05 – part 1: Evaluate a fully parenthesized infix expression
!8
I 4 As 5 not balanced
![Page 9: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/9.jpg)
( ( 2 * 2 ) + ( 8 + 4 ) )
!9
stack char sD g push t
a
![Page 10: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/10.jpg)
( ( 2 * 2 ) + ( 8 + 4 ) )
!10
What should be the next step after the first right parenthesis is encountered? A. Push the right parenthesis
onto the stack B. If the stack is not empty pop
the next item on the top of the stack
C. Ignore the right parenthesis and continue checking the next character
D.None of the above
an
OO
iffarrciifc s emptyS popCelse return false
![Page 11: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/11.jpg)
( ( 2 * 2 ) + ( 8 + 4 ) )
!11S i
1 la
![Page 12: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/12.jpg)
Evaluating a fully parenthesized infix expression
!12
Student's solutiondemoed in class9
b
z I.is u.e.hfiraw
![Page 13: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/13.jpg)
Evaluating a fully parenthesized infix expression
!13
1
We can evaluate a
fully paranthesized
expression usingtwo shades
![Page 14: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/14.jpg)
Evaluating a fully parenthesized infix expression
!14
![Page 15: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/15.jpg)
Evaluating a fully parenthesized infix expression
!15
![Page 16: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/16.jpg)
Notations for evaluating expression• Infix number operator number • (Polish) Prefix operators precede the operands • (Reverse Polish) Postfix operators come after the operands
• 3 * 5 • 4 / 2 • 7 + ( 3 * 5) • ( 7 + ( 3 * 5) ) – ( 4 / 2 )
!16
Prefix Polish postfix Reverse
35T 35 polis
4 2 4 21
D5 735
t
7 35 42
735 At 42
![Page 17: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/17.jpg)
Lab 05, part2 : Evaluating post fix expressions using a single stackPostfix: 7 3 5 * + 4 2 / - Infix: ( 7 + ( 3 * 5) ) – ( 4 / 2 )
!173
20
iii
![Page 18: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/18.jpg)
Small group exerciseWrite a ADT called in minStack that provides the following methods • push() // inserts an element to the “top” of the minStack • pop() // removes the last element that was pushed on the stack • top () // returns the last element that was pushed on the stack • min() // returns the minimum value of the elements stored so far
!18
![Page 19: STANDARD TEMPLATE LIBRARY STACKS · Stacks – container class available in the C++ STL • Container class that uses the Last In First Out (LIFO) principle • Methods i. push()](https://reader034.vdocuments.net/reader034/viewer/2022051915/60067b14efc67410ed0d6506/html5/thumbnails/19.jpg)
Summary of operationsOperation Sorted Array Binary Search Tree Linked List
MinMaxMedianSuccessorPredecessorSearchInsertDelete