CSCI-1200 Data Structures Fall 2016 Lecture 17 Associative Containers (Maps), Part 2

Similar documents
CSCI-1200 Data Structures Spring 2018 Lecture 15 Associative Containers (Maps), Part 2

CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

CSCI-1200 Data Structures Fall 2017 Lecture 17 Trees, Part I

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

CSCI-1200 Data Structures Spring 2018 Lecture 16 Trees, Part I

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation

CSCI-1200 Data Structures Fall 2014 Lecture 18 Trees, Part III

CSCI-1200 Data Structures Spring 2015 Lecture 18 Trees, Part I

CSCI-1200 Data Structures Fall 2018 Lecture 19 Trees, Part III

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

CSCI-1200 Data Structures Spring 2015 Lecture 20 Trees, Part III

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

Computer Science II CSci 1200 Lecture 18 Operators and Friends

CSCI-1200 Data Structures Fall 2017 Test 3 Solutions

Computer Science II Lecture 1 Introduction and Background

CPSC 427a: Object-Oriented Programming

Templates and Vectors

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Computer Science II Lecture 2 Strings, Vectors and Recursion

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Computer Science II CSci 1200 Lecture 8 Iterators; Programming Examples

CS 104 (Spring 2014) Final Exam 05/09/2014

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

CSCI-1200 Data Structures Spring 2018 Lecture 18 Trees, Part III

CSE100. Advanced Data Structures. Lecture 4. (Based on Paul Kube course materials)

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

Containers in C++ and Java

CSCI-1200 Data Structures Spring 2016 Lecture 22 Priority Queues, part II (& Functors)

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

CS197c: Programming in C++

Lecture 34. Wednesday, April 6 CS 215 Fundamentals of Programming II - Lecture 34 1

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Chapter 4 Computation

Priority Queues and Huffman Trees

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Computer Science II CSci 1200 Test 2 Overview and Practice

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

Chapter 5. The Standard Template Library.

CMSC 341 Lecture 10 Binary Search Trees

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

2

Binary Trees and Binary Search Trees

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Chapter 4. Computation. Bjarne Stroustrup.

Data Structures CSci 1200 Test 2 Questions

12 Abstract Data Types

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

CSCI-1200 Computer Science II Spring 2006 Test 3 Practice Problem Solutions

Review of Important Topics in CS1600. Functions Arrays C-strings

CHAPTER 3 BASIC INSTRUCTION OF C++

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Getting started with C++ (Part 2)

Scientific Computing

CS 216 Exam 1 Fall SOLUTION

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Data Structures Lecture 3 Order Notation and Recursion

UEE1302 (1102) F10: Introduction to Computers and Programming

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

Solution to CSE 250 Final Exam

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

CSCI Trees. Mark Redekopp David Kempe

CSCI-1200 Data Structures Fall 2017 Lecture 18 Trees, Part II

C++ For Science and Engineering Lecture 15

TREES. Trees - Introduction

CSI33 Data Structures

Maps; Binary Search Trees

The C++ Language. Arizona State University 1

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Topic Binary Trees (Non-Linear Data Structures)

CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

CSCI-1200 Data Structures Spring 2015 Lecture 2 STL Strings & Vectors

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

INF2220: algorithms and data structures Series 1

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

STL: C++ Standard Library

COMP322 - Introduction to C++ Lecture 01 - Introduction

Programming Abstractions

TREES Lecture 10 CS2110 Spring2014

CSCI-1200 Data Structures Test 3 Practice Problem Solutions

CSC212 Data Structure - Section FG

Computer Science II CSci 1200 Lecture 24 C++ Inheritance and Polymorphism

CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

4. Structure of a C++ program

CSE 12 Abstract Syntax Trees

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

Transcription:

CSCI-1200 Data Structures Fall 2016 Lecture 17 Associative Containers (Maps), Part 2 Review of Lecture 16 Maps are associations between keys and values. Maps have fast insert, access and remove operations: O(log n), we ll learn why next week when we study the implementation! Maps store pairs; map iterators refer to these pairs. The primary map member functions we discussed are operator[], find, insert, and erase. The choice between maps, vectors and lists is based on naturalness, ease of programming, and efficiency of the resulting program. 16.12 Choices of Containers We can solve this word counting problem using several different approaches and different containers: a vector or list of strings a vector or list of pairs (string and int) a map? How do these approaches compare? Which is cleanest, easiest, and most efficient, etc.? Today s Class Maps, Part 2 Maps containing more complicated values. Example: index mapping words to the text line numbers on which they appear. Maps whose keys are class objects, example: maintaining student records. Lists vs. Graphs vs. Trees Intro to Binary Trees, Binary Search Trees, & Balanced Trees 17.1 More Complicated Values map<string, vector<int> > m Let s look at the example: first second map<string, vector<int> > m; map<string, vector<int> >::iterator p; q Note that the space between the > > is required (by many compiler parsers). Otherwise, >> is treated as an operator. p "hello" 15 5

Here s the syntax for entering the number 5 in the vector associated with the string "hello": m[string("hello")].push_back(5); Here s the syntax for accessing the size of the vector stored in the map pair referred to by map iterator p: p = m.find(string("hello")); p->second.size() Now, if you want to access (and change) the i th entry in this vector you can either using subscripting: (p->second)[i] = 15; (the parentheses are needed because of precedence) or you can use vector iterators: vector<int>::iterator q = p->second.begin() + i; *q = 15; Both of these, of course, assume that at least i+1 integers have been stored in the vector (either through the use of push back or through construction of the vector). We can figure out the correct syntax for all of these by drawing pictures to visualize the contents of the map and the pairs stored in the map. We will do this during lecture, and you should do so all the time in practice. 17.2 Exercise Write code to count the odd numbers stored in the map map<string, vector<int> > m; This will require testing all contents of each vector in the map. Try writing the code using subscripting on the vectors and then again using vector iterators. 17.3 A Word Index in a Text File // Given a text file, generate an alphabetical listing of the words in the file // and the file line numbers on which each word appears. If a word appears on // a line more than once, the line number is listed only once. #include <algorithm> #include <cctype> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; // implementation omitted, will be covered in a later lecture vector<string> breakup_line_into_strings(const string& line); int main() { map<string, vector<int> > words_to_lines; string line; int line_number = 0; while (getline(cin, line)) { line_number++; // Break the string up into words vector<string> words = breakup_line_into_strings(line); 2

// Find if each word is already in the map. for (vector<string>::iterator p = words.begin(); p!= words.end(); ++p) { // If not, create a new entry with an empty vector (default) and // add to index to the end of the vector map<string, vector<int> >::iterator map_itr = words_to_lines.find(*p); if (map_itr == words_to_lines.end()) words_to_lines[*p].push_back(line_number); // could use insert here // If it is, check the last entry to see if the line number is // already there. If not, add it to the back of the vector. else if (map_itr->second.back()!= line_number) map_itr->second.push_back(line_number); // Output each word on a single line, followed by the line numbers. map<string, vector<int> >::iterator map_itr; for (map_itr = words_to_lines.begin(); map_itr!= words_to_lines.end(); map_itr++) { cout << map_itr->first << ":\t"; for (unsigned int i = 0; i < map_itr->second.size(); ++i) cout << (map_itr->second)[ i ] << " "; cout << "\n"; return 0; 17.4 Our Own Class as the Map Key So far we have used string (mostly) and int (once) as the key in building a map. Intuitively, it would seem that string is used quite commonly. More generally, we can use any class we want as long as it has an operator< defined on it. Suppose we want to maintain data for students including name, address, courses, grades, and tuition fees and calculate things like GPAs, credits, and remaining required courses. We could do this by making a single Student class object that stores everything for a particular student and put that in a vector or list. Alternately, we could break the information into separate classes and use a map. First, let s look at a sketch of a few classes that can work together to store the data: class Name { public: Name(const string& first, const string& last) : m_first(first), m_last(last) { const string& first() const { return m_first; const string& last() const { return m_last; private: string m_first; string m_last; ; class CourseGrade { public: Course(const string &c_name, const string & grade) : course_name(c_name), final_grade(grade) { const string & get_course_name() const { return course_name; const string & get_final_grade() const { return final_grade; private: string course_name; string final_grade; ; 3

class StudentRecord { public: const string& getaddress() const { return address; const string& getgradeincourse(const string &course_name) const; /* implementation omitted */ bool hascompletedcourse(const string &course_name) const; /* implementation omitted */ float getgpa() const { return GPA; /* additional member functions omitted */ private: string address; vector<coursegrade> completed_coursework; float GPA; /* etc. */ ; Now if we want to create a map of student names and associated student records, we need to add an operator< for Name objects. This is simple: bool operator< (const Name& left, const Name& right) { return left.last() < right.last() (left.last() == right.last() && left.first() < right.first()); Now we can define a map: map<name, StudentRecord> students; 17.5 Exercises First let s draw a picture of this map data structure populated with interesting data: So what are the advantages of organizing this data using a map in this way? Let s assume there are s students, c different classes offered at the school, each student takes up to k classes before graduation, and at most p students take a particular course. Write a fragment of code to access student X s grade in course Y. What is the order notation of this operation? Write a fragment of code to make a list of all students who have taken course Y. What is the order notation of this operation? 4

17.6 Typedefs One of the painful aspects of using maps is the syntax. For example, consider a constant iterator in a map associating strings and vectors of ints: map < string, vector<int> > :: const_iterator p; Typedefs are a syntactic means of shortening this. For example, if you place the line: typedef map < string, vector<int> > map_vect; before your main function (and any function prototypes), then anywhere you want the map you can just use the identifier map_vect: map_vect :: const_iterator p; The compiler makes the substitution for you. 17.7 When to Use Maps, Reprise Maps are an association between two types, one of which (the key) must have a operator< ordering on it. The association may be immediate: Words and their counts. Words and the lines on which they appear Or, the association may be created by splitting a type: Splitting off the name (or student id) from rest of student record. 17.8 Overview: Lists vs. Trees vs. Graphs Trees create a hierarchical organization of data, rather than the linear organization in linked lists (and arrays and vectors). Binary search trees are the mechanism underlying maps & sets (and multimaps & multisets). Mathematically speaking: A graph is a set of vertices connected by edges. And a tree is a special graph that has no cycles. The edges that connect nodes in trees and graphs may be directed or undirected. 17.9 Definition: Binary Trees A binary tree (strictly speaking, a rooted binary tree ) is either empty or is a node that has pointers to two binary trees. 77 Here s a picture of a binary tree storing integer values. In this figure, each large box indicates a tree node, with the top rectangle representing the value stored and the two lower boxes representing pointers. Pointers that are null are shown with a slash through the box. 99 14 The topmost node in the tree is called the root. 198 52 33 The pointers from each node are called left and right. The nodes they point to are referred to as that node s (left and right) children. The (sub)trees pointed to by the left and right pointers at any node are called the left subtree and right subtree of that node. A node where both children pointers are null is called a leaf node. 98 235 12 53 A node s parent is the unique node that points to it. Only the root has no parent. 5

17.10 Definition: Binary Search Trees mouse A binary search tree is a binary tree where at each node of the tree, the value stored at the node is cat mule greater than or equal to all values stored in the left subtree, and less than or equal to all values stored in the right subtree. ant goat zebra Here is a picture of a binary search tree storing string values. dog lion tiger 17.11 Definition: Balanced Trees The number of nodes on each subtree of each node in a balanced tree is approximately the same. In order to be an exactly balanced binary tree, what must be true about the number of nodes in the tree? horse In order to claim the performance advantages of trees, we must assume and ensure that our data structure remains approximately balanced. (You ll see much more of this in Intro to Algorithms!) 17.12 Exercise Consider the following values: 4.5, 9.8, 3.5, 13.6, 19.2, 7.4, 11.7 1. Draw a binary tree with these values that is NOT a binary search tree. 2. Draw two different binary search trees with these values. Important note: This shows that the binary search tree structure for a given set of values is not unique! 3. How many exactly balanced binary search trees exist with these numbers? How many exactly balanced binary trees exist with these numbers? 6