16_trees_I - CSCI-1200 Data Structures - Fall 2010 Lecture...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CSCI-1200 Data Structures — Fall 2010 Lecture 16 – Trees, Part I Review from Lectures 15 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 Today’s Lecture STL set container class (like STL map , but without the pairs!) Implementation of ds_set class using binary search trees Note: Lots more tree stuff in CSCI 2300 Introduction to Algorithms! 16.1 Standard Library Sets STL sets are ordered containers storing unique “keys”. An ordering relation on the keys, which defaults to operator < , is necessary. Because STL sets are ordered, they are technically not traditional mathematical sets. Sets are like maps except they have only keys, there are no associated values. Like maps, the keys are constant . This means you can’t change a key while it is in the set. You must remove it, change it, and then reinsert it. Access to items in sets is extremely fast! O (log n ), just like maps. Like other containers, sets have the usual constructors as well as the size member function. 16.2 Set iterators Set iterators, similar to map iterators, are bidirectional: they allow you to step forward ( ++ ) and backward ( -- ) through the set. Sets provide begin() and end() iterators to delimit the bounds of the set. Set iterators refer to const keys (as opposed to the pairs referred to by map iterators). For example, the following code outputs all strings in the set words : for (set<string>::iterator p = words.begin(); p!= words.end(); ++p) cout << *p << endl; 16.3 Set insert , erase , and find There are two different versions of the insert member function. The first version inserts the entry into the set and returns a pair. The first component of the returned pair refers to the location in the set containing the entry. The second component is true if the entry wasn’t already in the set and therefore was inserted. It is false otherwise. The second version also inserts the key if it is not already there. The iterator
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/21/2012 for the course CSCI 1200 taught by Professor Cutler during the Fall '08 term at Rensselaer Polytechnic Institute.

Page1 / 4

16_trees_I - CSCI-1200 Data Structures - Fall 2010 Lecture...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online