31
DATA STRUCTURES ACM EXECUTIVE BODY 2k11

DATA STRUCTURES ACM EXECUTIVE BODY 2k11. A series of elements of same type Placed in contiguous memory locations Can be individually referenced

Embed Size (px)

Citation preview

Page 1: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

DATA STRUCTURESACM EXECUTIVE BODY

2k11

Page 2: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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

Page 3: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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)

Page 4: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

Last In First Out (LIFO)

Operations◦ Push◦ Pop◦ Top◦ isEmpty (Underflow)◦ isFull (Overflow)

STACK

Data 4

Data 3

Data 2

Data 1

Page 5: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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

Page 6: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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

Page 7: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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

Page 8: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

Maximum Rectangle Area in a Histogram

Maximum Rectangular Area = 4x6 = 24

Problems

7

3

8

6

9

78

Page 9: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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

Page 10: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

First In First Out (FIFO)

Operations◦ Enqueue (Insert)◦ Dequeue (Remove)◦ Front◦ Rear / Back◦ isEmpty◦ isFull

Queue

Data 1 Data 2 Data 3 Data 4

Page 11: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

Operating System Job Scheduling

Breadth First Traversal of a Graph

Level Order Traversal of a Binary Tree

Applications

Page 12: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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

Page 13: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

04/19/23

Circular Queue Complete use of

space

Uses single, fixed size buffer, as if it were connected from end to beginning

Page 14: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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

Page 15: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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

Page 16: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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

Page 17: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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

Page 18: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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

Page 19: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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

Page 20: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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

Page 21: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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

Page 22: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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.

Page 23: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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();

Page 24: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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'

Page 25: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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

Page 26: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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

Page 27: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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

Page 28: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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();

Page 29: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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 );

Page 30: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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();

Page 31: DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced

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