Basic Data Structures.

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
The Design and Analysis of Algorithms
Important Problem Types and Fundamental Data Structures
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Data Structures Using C++ 2E
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Review for Final Andy Wang Data Structures, Algorithms, and Generic Programming.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Main Index Contents 11 Main Index Contents Sets Defined by a key along with other data Sets Defined by a key along with other data Key-Value Data Key-Value.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
Final Exam Review COP4530.
Final Exam Review CS 3358.
Lecture 10 Collections Richard Gesick.
Cpt S 122 – Data Structures Abstract Data Types
Data Structure By Amee Trivedi.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
Stacks and Queues Chapter 4.
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
The Design and Analysis of Algorithms
Data Structure Interview Question and Answers
12 C Data Structures.
Chapter 15 Lists Objectives
Priority Queues and Heaps
Standard Template Library (STL)
Csc 2720 Instructor: Zhuojun Duan
Stacks and Queues.
Stack and Queue APURBO DATTA.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Compsci 201 Priority Queues & Autocomplete
CMSC 341 Lecture 13 Leftist Heaps
Searching.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Basic Data Structures.
Chapter 1.
CS313D: Advanced Programming Language
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8 – Binary Search Tree
structures and their relationships." - Linus Torvalds
Associative Structures
Graphs Representation, BFS, DFS
Lesson Objectives Aims
Final Exam Review COP4530.
Introduction to Data Structures
A Robust Data Structure
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Pointers & Dynamic Data Structures
Priority Queues & Heaps
Introduction to Data Structure
Fundaments of Game Design
EE 312 Final Exam Review.
Important Problem Types and Fundamental Data Structures
OPIM 915 Fall 2010 Data Structures 23-38,
structures and their relationships." - Linus Torvalds
10.3 Bubble Sort Chapter 10 - Sorting.
A dictionary lookup mechanism
Presentation transcript:

Basic Data Structures

Basic Data Structures A key part of most problems is being able to identify a basic structure that can be used for the problem Sometimes, the key to solving a problem is just knowing the right structure to use! These are some basic data structures – ones you should already be familiar with. We will, later, talk about other structures that you might be less familiar with. We will skip more general graph structures for now

Basic Data TYPES bool char short (short int) int long (long int) long long (long long int) float double long double string (provided via library)

Data structures (and the C++ implementation) Static arrays Queue int a[10]; queue<int> Dynamic arrays Priority Queue vector<int> priority_queue<int> Linked list Set list<int> set<int> Stack Map stack<int> map<int, int>

Array vs. Vector vs. List Many (most?) problems require storing data in such a format Array is generally faster, quicker to write, if you know size List does not support sort like array/vector does List is able to insert and erase more efficiently than vector and especially array Matters if doing a lot of insertion/deletion

Arrays: sort and search Sort: don’t bother writing your own sort in most cases Unless the sort compariso is tricky or the sort needs to be optimized somehow Sort (in C++): for a vector: sort(a.begin(), a.end()) for a static array of size n: sort(a, a+n) Search (in C++) for an UNSORTED array: gives pointer to the array element, not the index for a vector: find(a.begin(), a.end(), x) for a static array: find(a, a+n, x) Search (in C++) for a SORTED array (implements binary search): for a vector: lower_bound(a.begin(), a.end(), x) for a static array: lower_bound(a, a+n, x) Note: lower_bound gives iterator for first element not less than x, so can also check it to see if an element exists. Note: binary_search just returns true/false, not an index

Stacks / Queues LIFO / FIFO Stacks: Queues: Simulate recursion/function calls Good for matching: match parentheses, for instance Good for processing in reverse order when you don’t have a simple loop Queues: Good for processing things in order when you don’t have a simple loop When we get to graphs: Stacks needed for DFS, Queues for BFS

Priority Queue Implemented using a heap, underneath When want priority without having to enter/sort Good when new stuff will come in over time, rather than all read at beginning Many greedy algorithms can use this To prioritize the next thing to do Graphs: use for shortest path, MST Basic operations: empty, push, pop (or top)

Sets Keep track of distinct elements Set type in C++ will support, but can implement other ways Default way is as a binary search tree C++ algorithms: set_union set_intersection set_difference set_symmetric_difference Any sorted ranges can use these algorithms, not just sets!

A simple set representation For small sets of items (n<=30), labeled with index 0-n-1. Represent as an integer, where bit indicates in set or not To set bit: (1 << n) where n is how many spaces to shift over (i.e. the element number) Combine (or union) with bitwise or: | Set of {5, 17, 8} : int x = (1 << 5) | (1 << 17) | (1 << 8) Intersection with bitwise and (&) All elements: (1<<n)-1 Complement: ~x & ((1<<n)-1) Element i in set? : x & (1<<i)

Maps Key-value pairs Useful in some dynamic programming Implemented as binary search tree (sorted on key) Can use bracket operator with key side effect: if the key does not exist, it is created; use find if you want to avoid

Defining a comparison operator class mycomp() { public: bool operator() (const TYPE& lhs, const TYPE& rhs) { //return true if lhs is “less than” rhs, false otherwise } }; Can be especially useful for priority queues, or places where different comparisons are needed Can specify this when defining priority queue, sort, lower_bound, set, map, etc.

Augmenting Data Structures Can often get more functionality by augmenting a data structure and writing your own code. Example: augment a binary search tree so each element includes the size of the subtree increase size on path when adding, decrease size on path when deleting. Can quickly count how many items are less than a given element Add up opposite subtrees on path to element (plus sometimes the node itself) Can quickly find the i-th element in the tree Follow path by comparing sizes of child trees