Upload
shon-mcdowell
View
217
Download
3
Tags:
Embed Size (px)
Citation preview
DATA STRUCTURESACM EXECUTIVE BODY
2k11
A series of elements of same type
Placed in contiguous memory locations
Can be individually referenced by adding an index to a unique identifier
Example: Sort 10^7 numbers ranging from 100 to
200.
ARRAY
Abstract Data Types (ADTs) or data structures or collections store data and allow various operations on the data to access and change it
Why Abstract?◦ Specify the operations of the data structure and
leave implementation details to later
Every collection ADT provides a way to : insert, remove, find and access a data item
Abstract Data Type (ADT)
Last In First Out (LIFO)
Operations◦ Push◦ Pop◦ Top◦ isEmpty (Underflow)◦ isFull (Overflow)
STACK
Data 4
Data 3
Data 2
Data 1
Infix to Postfix Conversion
Matching tags in XML/HTML
Undo sequence in a text editor
Implementing functional calls (Recursion)
History of Web Browsers
Applications of Stack
Balancing Of Parenthesis◦ Given a sequence of ‘(‘ and ‘)’◦ Validate the sequence◦ A sequence is valid if for every ‘(‘, there is a
counter-balancing ‘)’ which occurs only later in the sequence
◦ Examples (()()()) : Valid (()(() : Invalid )()()( : Invalid
Problems
Nearest Smaller Element◦ Given a sequence of integers◦ For every integer, output the nearest integers to
the left and to the right which are smaller to it.◦ If there is no such number, output -1.
◦ Example 3 1 5 4 6 (-1,1) (-1,-1) (1,4) (1,-1) (4,-1)
Problems
Maximum Rectangle Area in a Histogram
Maximum Rectangular Area = 4x6 = 24
Problems
7
3
8
6
9
78
Water Harvesting◦ Find the maximum volume of water that can be collected in
between the adjacent buildings
◦ Maximum Volume Of Water Collected = 1 + 4 + 4 = 9
Problems
7
3
8
67 7
84
1
4
First In First Out (FIFO)
Operations◦ Enqueue (Insert)◦ Dequeue (Remove)◦ Front◦ Rear / Back◦ isEmpty◦ isFull
Queue
Data 1 Data 2 Data 3 Data 4
Operating System Job Scheduling
Breadth First Traversal of a Graph
Level Order Traversal of a Binary Tree
Applications
Print first 100 numbers comprising only of 1s, 3s and 5s.
The first few numbers in the sequence are 1,3,5, 11,13,15,31,33,35,51,53,55, 111,113,115,131,133,135,151,153,155,311,313...
and so on.
Problem
04/19/23
Circular Queue Complete use of
space
Uses single, fixed size buffer, as if it were connected from end to beginning
An generalized queue, for which elements can be added to or removed from either the front or the rear.
Operations:◦ PushBack◦ PushFront◦ PopBack◦ PopFront◦ Front◦ Back
Deque
Given an array and an integer k, find the maximum element for each and every sub-array of size k
Example:
◦ a = [ 8 , 5, 10, 7, 9, 4, 15, 12, 19, 13 ]◦ k = 4
◦ Output : [ 10, 10, 10, 15, 15, 19, 19 ]
Problems on Deque
Input Restricted Deque◦ Deletion can be made from both ends◦ Insertion can be made at one end only
Output Restricted Deque◦ Insertion can be made at both ends◦ Deletion can be made at one end only
Variations of Deque
Standard Template Library
Part of ISO standard C++ library
Why Use STL◦ Reduces Development time
◦ Highly Reliable Code ( no or small amount of debugging effort required )
◦ Highly Robust ( Container size grows automatically )
◦ Portable and easily maintainable
STL
Templates allow the usage of generic types◦ Advantage: no need of writing the same code all over again
for a different data type
Example:◦ template <class X> // X can represent any data type that support the operations
class ABC { ABC() { }
public: getMax(X a, X b)
{ return (a > b ? a: b); } };
ABC <double> a; max_val = a.getMax(15.30, 53.12);
Templates
Including the library◦ #include<stack>
Declaration◦ stack <int> s;
Push Operation◦ s.push(5);
Pop Operation◦ s.pop();
Access the Top Element◦ int x = s.top();
Check if Stack is Empty◦ if ( s.empty() ) cout<< “ Stack is Empty ” << endl;
STL - Stack
Including the library◦ #include<queue>
Declaration◦ queue <int> q;
Push Operation◦ q.push(5);
Pop Operation◦ q.pop();
Access the Top Element◦ int x = q.front();
Check if Stack is Empty◦ if ( q.empty() ) cout<< “ Queue is Empty ” << endl;
STL - Queue
Including the library◦ #include<deque>
Declaration◦ deque <int> dq;
Push Operation◦ dq.push_back(5);◦ dq.push_front(5);
Pop Operation◦ dq.pop_back();◦ dq.pop_front();
Access the front and back elements◦ int x = dq.front(); int y = dq.back();
Check if Stack is Empty◦ if ( dq.empty() ) cout<< “ Deque is Empty ” << endl;
STL - Deque
04/19/23
STL-Map Introduction Associative data structure
◦ a value is associated with the key
Stores the values in an order, sorted by the key values.
Internally implemented as a balanced BST
Multimap is used if multiple associated entries are allowed for same key values.
04/19/23
STL- Map Including the library
◦ #include<map>
Declaring a map◦ map<string, int>m;
Inserting a value◦ m.insert( pair<string, int > ("abc", 2);◦ m["abc"]=2;
Finding a value◦ if( m.find("abc") != m.end() ) cout << "Not found" << endl;
Clear all entries◦ m.clear();
04/19/23
STL- Pair and Vector Vector
◦ Dynamic array of variables, struct or objects of same type
◦ Resizes itself when inserting or erasing an element
Pair◦ A simple associative container consisting of a 2-
tuple of data elements or objects, denoted by ‘first' and ‘second'
04/19/23
STL- Vector Including the library
◦ #include<vector>
Declaration◦ vector<int>v; //1-D◦ vector<int>v[10000] ; //2-D
Pushing a value◦ v.push_back(5); //1-D◦ v[i].push_back(5); //2-D
Removing a value◦ v.pop_back(); //1-D
Accessing an element at position i◦ int x= v[i]; //1-D
Clear all entries◦ v.clear(); //1-D
04/19/23
STL- Pair Including the library
◦ #include<utility>
Declaration◦ pair<string, int> p;
Definition◦ p = make_pair("abc",10);
Accessing the elements◦ p.first ◦ p.second
04/19/23
STL-Set Used to store unique elements in a sorted
order
Elements in a set can't be modified once they has been inserted. But they can be inserted or removed from the container.
Internally implemented as balanced BST
Multiset can be used to allow duplicate entries
04/19/23
STL-Set Library to be included
◦ #include<set>
Declaration◦ set<int>s;
Inserting an element◦ s.insert(5);
Removing an element◦ s.erase(5);
Get the size of the set◦ int x= s.size();
04/19/23
Useful functions int a[]={1,2,2,3,5}; vector<int> v ( a, a+5 ) ;
◦ sort ( a, a+5 ); // sort an array◦ sort ( v.begin(), v.end() ); // sort a vector
◦ next_permutation( a, a+5 ); find next permutation of elements of array
◦ prev_permutation( a, a+5 );
◦ lower_bound( v.begin(), v.end(), 2); find the first occurence of a value in a sorted container
◦ upper_bound( v.begin(), v.end(), 2 );
04/19/23
Using Iterators Used with map, vector and set
Iterator: vector<int>::iterator it; //declaration it = v.begin(); // points to start of the container it = v.end(); //points to end of the container
◦ Other operations: it++ ; it-- ; it != m.end() ;
Reverse Iterator: iterates in a reverse fashion from end to beginning vector<int>::reverse_iterator it; it = v.rbegin(); it = v.rend();
If you have any problem solving a question or understanding some concept , feel free to bug us, we will be happy to debug you…
ACM Executive body 2014-15
o Nimesh Ghelani: [email protected] Prem Kamal: [email protected] Utkarsh Raj: [email protected] Rishikesh Jha: [email protected] Vivek Verma: [email protected] Nischal Kumar: [email protected] Chandan Agarwal: [email protected] o Sanket Singhal: [email protected] Aparajita Choudhary: [email protected] Bhavini Mishra: [email protected]
Happy Coding…
04/19/23