Download presentation
Presentation is loading. Please wait.
Published byYulia Budiono Modified over 5 years ago
1
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) CS-2303, A-Term 2012 Iterators
2
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 CS-2303, A-Term 2012 Iterators
3
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(); TreeNode(const string &w); ~TreeNode(); private: const string word; int count; TreeNode *left, *right; }; // class TreeNode TreeNode.h CS-2303, A-Term 2012 Iterators
4
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 BinaryTree.h CS-2303, A-Term 2012 Iterators
5
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 There is something very unsatisfying about this implementation. What is it? BinaryTree.h CS-2303, A-Term 2012 Iterators
6
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 These two functions are problem specific — not about binary trees Output format is built into Binary Tree class! CS-2303, A-Term 2012 Iterators
7
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 CS-2303, A-Term 2012 Iterators
8
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 CS-2303, A-Term 2012 Iterators
9
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 CS-2303, A-Term 2012 Iterators
10
Binary tree iterators Pre-order Post-order In-order
CS-2303, A-Term 2012 Iterators
11
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 CS-2303, A-Term 2012 Iterators
12
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; } CS-2303, A-Term 2012 Iterators
13
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 CS-2303, A-Term 2012 Iterators
14
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 For these methods to work, class iterator must be a friend of class TreeNode Private members of TreeNode must become protected CS-2303, A-Term 2012 Iterators
15
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!= CS-2303, A-Term 2012 Iterators
16
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. CS-2303, A-Term 2012 Iterators
17
Questions? CS-2303, A-Term 2012 Iterators
18
Reading Absolute C++, §17.3 & 19.1 Iterators also exist in Java
Similar in concept Different in detail CS-2303, A-Term 2012 Iterators
19
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 != CS-2303, A-Term 2012 Iterators
20
end() method points to one after last element!
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 = ...) begin() method set “pointer” to first element Iterator acts like a pointer end() method points to one after last element! CS-2303, A-Term 2012 Iterators
21
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 “[ ]" CS-2303, A-Term 2012 Iterators
22
Uses of Iterators To help you sequence through objects of a container — e.g., Arrays, vectors Lists (singly- doubly-linked) Trees Input Output … CS-2303, A-Term 2012 Iterators
23
Iterator Example (again)
In this case, it appears that iterator is a member class or struct of class list<class T> 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 = ...) CS-2303, A-Term 2012 Iterators
24
The following is legal in C++
class C { class D { … } // 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) You can also declare classes inside of functions! CS-2303, A-Term 2012 Iterators
25
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 What other iterator is suggested by this problem? CS-2303, A-Term 2012 Iterators
26
Questions? CS-2303, A-Term 2012 Iterators
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.