programming assignment #4, binary treesweb.cs.wpi.edu/.../assignment4--binarytrees_slides.pdf ·...
TRANSCRIPT
Carnegie Mellon Worcester Polytechnic Institute
Programming Assignment #4 Binary Trees in C++
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)
Programming Assignment #4 CS-2303, A-Term 2012 1
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Assignment
Write a C++ program to Read one or more text files Build up a binary tree of words in those text files Print to an output file an alphabetical list of words and
number of occurrences of each word Also total number of different words
Programming Assignment #4 CS-2303, A-Term 2012 2
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Reading Absolute C++
Chapter 6 — structs and classes §7.1 — constructors §17.3 — Trees
Review Binary Trees C lecture notes on Binary Trees K&R §6.5
Programming Assignment #4 CS-2303, A-Term 2012 3
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
This Assignment
Command line program ./PA4 outputFile inputFile1 inputFile2 ...
Open and read each input file in turn Scan for words Store each new word in binary tree Increment count of words previously entered
Open and write to output List of words Count for each word Total number of distinct words
Programming Assignment #4 CS-2303, A-Term 2012 4
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Example Output 166 a
25 and 11 as 3 command 15 each 2 file 4 files 109 in 4 input 98 it 1 it’s 99 of 3 open 6 program 18 read 152 the 41 this 3 under 30 would ------------- 19 Total number of different words
Programming Assignment #4 CS-2303, A-Term 2012 5
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Notes Upper and lower case words are the same “This” and “this”
Related words that are spelled differently are different “Car” vs. “cars”
Recognize apostrophes & hyphens as parts of words “Bob’s” “double-ended”
Programming Assignment #4 CS-2303, A-Term 2012 6
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Guidance Think through this problem in Java
Use as inspiration and guideline for C++ implementation One or more class interface files (.h)
A corresponding class implementation file (.cpp) for each class interface
One or more .cpp files for main() and other non-class functions
Programming Assignment #4 CS-2303, A-Term 2012 7
Don’t use templates from Standard Template Library!
You must build your own classes!
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Test Files Provided in project assignment
http://www.cs.wpi.edu/~cs2303/a12/Common/Kennedy.txt
http://www.cs.wpi.edu/~cs2303/a12/Common/Obama.txt http://www.cs.wpi.edu/~cs2303/a12/Common/MartinLutherKing.txt
http://www.cs.wpi.edu/~cs2303/a12/Common/Macbeth.txt http://www.cs.wpi.edu/~cs2303/a12/Common/MuchAdoAboutNothing.txt
http://www.cs.wpi.edu/~cs2303/a12/Common/TamingOfTheShrew.txt
Programming Assignment #4 CS-2303, A-Term 2012 8
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Submission Build in Eclipse
Make clean before submitting Export to a zip file so grader can unzip and build on Linux
Project name:– PA4
Submit zip file, README, output from test cases
Programming Assignment #4 CS-2303, A-Term 2012 9
Carnegie Mellon Worcester Polytechnic Institute
What is a Binary Tree?
Programming Assignment #4 CS-2303, A-Term 2012 10
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Programming Assignment #4 11 CS-2303, A-Term 2012
Definitions
Linked List A data structure in which each element is dynamically allocated
and in which elements point to each other to define a linear relationship
Singly- or doubly-linked Stack, queue, circular list
Tree A data structure in which each element is dynamically allocated
and in which each element has more than one potential successor
Defines a partial order
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Binary Tree A linked list but with two links per
item struct treeItem {
type payload; treeItem *left, *right;
};
Programming Assignment #4 CS-2303, A-Term 2012 12
left right
payload
left right
payload left right
payload
left right
payload left right
payload left right
payload
left right
payload
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Binary Tree (continued)
Binary tree needs a root struct treeItem {
type payload; treeItem *left, *right;
}; struct treeItem *root;
Binary trees often drawn with root at top! Unlike ordinary trees in the forest More like the root systems of a tree
Programming Assignment #4 CS-2303, A-Term 2012 13
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Definitions (continued)
Binary Tree A tree in which each element has two potential
successors
Subtree A node plus the set of all of its successors, either
directly or indirectly
Root of a tree The node of the tree that is not the successor to any
other node, all other nodes are (directly or indirectly) successors to it
Programming Assignment #4 CS-2303, A-Term 2012 14
See K & R, §6.5
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Binary Tree A linked list but with two links per
item struct treeItem {
type payload; treeItem *left, *right;
};
Programming Assignment #4 CS-2303, A-Term 2012 15
left right
payload
left right
payload left right
payload
left right
payload left right
payload left right
payload
left right
payload
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Purpose of a Tree (Potentially) a very large data structure
Capable of storing very many items In an orderly way
Need to find items by value I.e., need to search through the data structure to see if it
contains an item with the payload value we want
Need to add new items If value is not already in the tree, add a new item … …so that it can be easily found in future
Why not use a linked list?
Programming Assignment #4 CS-2303, A-Term 2012 16
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Searching and Adding to a Binary Tree A linked list but with two links per
item struct treeItem {
type payload; treeItem *left, *right;
};
Programming Assignment #4 CS-2303, A-Term 2012 17
left right
payload
left right
payload left right
payload
left right
payload left right
payload left right
payload
left right
payload
Look recursively down sequence of branches until either Desired node is found; or Null branch is encountered
Replace with pointer to new item
Decide which branch to follow based on payload
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Example — Searching a Tree in C typedef struct _treeItem {
char *word; // part of payload int count; // part of payload _treeItem *left, *right; } treeItem;
treeItem *findItem(treeItem *p, char *w) {
if (p == NULL) return NULL; // item not found int c = strcmp(w, p->word); if (c == 0) return p; else if (c < 0) return findItem(p->left, w); else return findItem(p->right, w);
}
Programming Assignment #4 CS-2303, A-Term 2012 18
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Notes on implementation in C++ class TreeNode { public: TreeNode(const string &w); ~TreeNode(); private: const string word; int count; TreeNode *left, *right; }; // class TreeNode
Programming Assignment #4 CS-2303, A-Term 2012 19
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Notes on implementation in C++ (continued)
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();
Programming Assignment #4 CS-2303, A-Term 2012 20
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
Notes on implementation in C++ (continued)
int TreeNode::incr() { return ++count; } // incr int TreeNode::getCount() { return count; } // getCount TreeNode *TreeNode::setLeft(TreeNode *newItem){ if (!left) return left = newItem; else { cerr << “Error message" << endl; return NULL; } } // setLeft
Programming Assignment #4 CS-2303, A-Term 2012 21
TreeNode::~TreeNode() { if (left) { delete left; left = NULL; } if (right) { delete right; right = NULL; } } // TreeNode destructor TreeNode::TreeNode (const string &w){ // constructor deferred till // next time }
TreeNode.cpp
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Notes on implementation in C++ (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
Programming Assignment #4 CS-2303, A-Term 2012 22
BinaryTree.h
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Additional Notes See notes at the end of Programming Assignment
document Command line arguments (same as in C)
Input and Output streams Absolute C++ Chapter 1 intro Absolute C++ Chapter 12 in depth
Formatted output
Programming Assignment #4 CS-2303, A-Term 2012 23
Carnegie Mellon Worcester Polytechnic Institute
Questions?
Programming Assignment #4 CS-2303, A-Term 2012 24