iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../lectures_a12/week6_iterators.pdf · so program can...
TRANSCRIPT
Carnegie Mellon Worcester Polytechnic Institute
Iterators
Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel)
Iterators CS-2303, A-Term 2012 1
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Recall Programming Assignment #4
Read words from input
Store in binary tree
Print out words in alphabetical order, along with number of occurrences of each word
Iterators CS-2303, A-Term 2012 2
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Recommended implementation in C++ class TreeNode { public: int incr(); int getCount(); string getWord(); int compare(const string &w2); TreeNode *setleft(TreeNode *t); TreeNode *setright(TreeNode *t); TreeNode *getleft(); TreeNode *getright();
Iterators CS-2303, A-Term 2012 3
TreeNode(const string &w); ~TreeNode(); private: const string word; int count; TreeNode *left, *right; }; // class TreeNode
TreeNode.h
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Recommended implementation (continued)
class BinaryTree { public: TreeNode *AddNode(const string &word); int getTotalNodes(); void PrintTree(ostream &output); BinaryTree(); //Default constructor ~BinaryTree(); //Destructor private: static int totalNodes; void PrintTree(TreeNode *subtree, ostream &output); TreeNode *AddNode(TreeNode *subtree, const string &word); TreeNode *root; }; // class BinaryTree
Iterators CS-2303, A-Term 2012 4
BinaryTree.h
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Recommended implementation (continued)
class BinaryTree { public: TreeNode *AddNode(const string &word); int getTotalNodes(); void PrintTree(ostream &output); BinaryTree(); //Default constructor ~BinaryTree(); //Destructor private: static int totalNodes; void PrintTree(TreeNode *subtree, ostream &output); TreeNode *AddNode(TreeNode *subtree, const string &word); TreeNode *root; }; // class BinaryTree
Iterators CS-2303, A-Term 2012 5
BinaryTree.h
There is something very unsatisfying about this implementation.
What is it?
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Recommended implementation (continued)
class BinaryTree { public: TreeNode *AddNode(const string &word); int getTotalNodes(); void PrintTree(ostream &output); BinaryTree(); //Default constructor ~BinaryTree(); //Destructor private: static int totalNodes; void PrintTree(TreeNode *subtree, ostream &output); TreeNode *AddNode(TreeNode *subtree, const string &word); TreeNode *root; }; // class BinaryTree
Iterators CS-2303, A-Term 2012 7
These two functions are problem specific — not about binary trees
Output format is built into Binary Tree class!
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Preference
Would like to make tree traversal part of Binary Tree class …
… while printing details (and other problem-specific issues) are handled outside of Binary Tree class
Iterators CS-2303, A-Term 2012 8
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Iterator
A construct that allows cycling through objects of a data structure in some useful order…
… so program can operate on those objects in sequence
Iterators CS-2303, A-Term 2012 9
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Characteristics of an Iterator Has state So it can keep track of where you are in the data structure
Has begin(), end(), ==, and != operations So program can start iterator, compare iterator objects, and know
when it has finished iterating
Has ++ operator (and possibly -- operator) So program can step to next object (and possibly previous object)
May have [], *, -> operators So program can access specific objects from iterator
Iterators CS-2303, A-Term 2012 10
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Binary tree iterators
Pre-order
Post-order
In-order
Iterators CS-2303, A-Term 2012 11
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Binary tree in-order iterator
/* This iterator uses a stack object from Standard Template Library*/
#include <stack> class iterator { TreeNode *node; // pointer to current iteration
stack<TreeNode *> stack; // record of how we got there
public: iterator(); //constructor TreeNode *begin(); TreeNode *end(); TreeNode *operator++(); bool operator==(iterator &); bool operator!=(iterator &); } //class iterator
Iterators CS-2303, A-Term 2012 12
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Binary tree in-order iterator (continued)
iterator::iterator() { stack.push(NULL); // initialize stack
node = root; if (node != NULL) while (node->left) { stack.push(node); node = node->left; }
return; }
iterator::operator++(){ current = node; if (node -> right) { node = node -> right; while (node -> left) { stack.push(node); node = node-> left; } } else node = stack.pop();
return current; }
Iterators CS-2303, A-Term 2012 13
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Binary tree in-order iterator (continued)
/* This iterator uses a stack object from Standard Template Library*/
#include <stack> class iterator { TreeNode *node; // pointer to current iteration
stack <TreeNode *> stack; // record of how we got there
public: iterator(); //constructor TreeNode *begin(); TreeNode *end(); TreeNode *operator++(); bool operator==(iterator &); bool operator!=(iterator &);
} //class iterator
Iterators CS-2303, A-Term 2012 14
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Binary tree in-order iterator (continued)
/* This iterator uses a stack object from Standard Template Library*/
#include <stack> class iterator { TreeNode *node; // pointer to current iteration
stack <TreeNode *> stack; // record of how we got there
public: iterator(); //constructor TreeNode *begin(); TreeNode *end(); TreeNode *operator++(); bool operator==(iterator &); bool operator!=(iterator &);
} //class iterator
Iterators CS-2303, A-Term 2012 15
For these methods to work, class iterator must be a friend of class TreeNode
Private members of TreeNode must become protected
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Binary tree in-order iterator (continued)
iterator::TreeNode *begin(){ while (!stack.empty()) stack.pop(); stack.push(NULL); // re-initialize stack node = root; if (node != NULL) while (node->left) { stack.push(node); node = node->left; } return node; }
iterator::TreeNode *end(){ return NULL } //iterator::TreeNode *end bool iterator::operator==(iterator
it &){ return node == it.node; } //iterator::TreeNode *end bool iterator::operator!=(iterator
it &){ return node != it.node; } //bool iterator::operator!=
Iterators CS-2303, A-Term 2012 16
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Binary tree in-order iterator (concluded)
An iterator that steps through the binary tree in the appropriate order
Needs access to protected members of the TreeNode class
Pre-order and post-order traversal are easy and obvious extensions
Allows the design of a Binary Tree independent of what we want to do with it.
Iterators CS-2303, A-Term 2012 17
Carnegie Mellon Worcester Polytechnic Institute
Questions?
Iterators CS-2303, A-Term 2012 18
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Reading
Absolute C++, §17.3 & 19.1
Iterators also exist in Java Similar in concept Different in detail
Iterators CS-2303, A-Term 2012 19
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Iterator – An Abstraction
Generalization of pointer into an array
Key concepts Access to current element:– * and −>
(Sometimes also random access:– [ ] ) Next element:– ++
(Sometimes also previous element:– -- ) Equality:– == and !=
Iterators CS-2303, A-Term 2012 20
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Iterator Example list<Event> EventQueue; list<Event>::iterator it; for (it=EventQueue.begin(); it != EventQueue.end(); it++) { if (it −> time > newEvent.time) EventQueue.insert(it, newEvent); break; } // for (it = ...)
Iterators CS-2303, A-Term 2012 21
Iterator acts like a pointer begin() method set “pointer” to first element
end() method points to one after last element!
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
More on Iterators
Can build own iterators for own container classes
Different iterators provide different operations E.g., linked list does not offer "--" or “[ ]" string and vector containers do offer "--" or “[ ]"
Iterators CS-2303, A-Term 2012 22
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Uses of Iterators
To help you sequence through objects of a container — e.g.,
Arrays, vectors Lists (singly- doubly-linked) Trees Input Output …
Iterators CS-2303, A-Term 2012 23
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Iterator Example (again)
list<Event> EventQueue; list<Event>::iterator it; for (it=EventQueue.begin(); it != EventQueue.end(); it++) { if (it −> time > newEvent.time) EventQueue.insert(it, newEvent); break; } // for (it = ...)
Iterators CS-2303, A-Term 2012 24
In this case, it appears that iterator is a member class or
struct of class list<class T>
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
The following is legal in C++
class C { class D { … } // class C } // class C
Class D is a nested class in Class C aka “local class”
Member functions of class D can access members of class C
Class D may be public, protected, private
See Absolute C++ §7.2 (end)
Iterators CS-2303, A-Term 2012 25
You can also declare classes inside of functions!
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Recall Programming Assignment #4
Read words from input
Store in binary tree
Print out words in alphabetical order, along with number of occurrences of each word
Iterators CS-2303, A-Term 2012 26
What other iterator is suggested by this problem?
Carnegie Mellon Worcester Polytechnic Institute
CS-2303, A-Term 2012 Iterators 27
Questions?