Programming Assignment #4 Binary Trees in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd 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, 5 th and 6 th editions, by Deitel and Deitel) 1
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 2
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 3
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 4
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 5
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 6
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 Don t use templates from Standard Template Library! You must build your own classes! 7
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 8
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 9
What is a Binary Tree? 10
Definitions Linked List Tree 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 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 CS-2303, A-Term 2012 Programming Assignment #4 11
Binary Tree A linked list but with two links per item struct treeitem { type ; treeitem *, *; }; 12
Binary Tree (continued) Binary tree needs a root struct treeitem { type ; treeitem *, *; }; 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 13
Definitions (continued) See K & R, 6.5 Binary Tree Subtree A tree in which each element has two potential successors 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 14
Binary Tree A linked list but with two links per item struct treeitem { type ; treeitem *, *; }; 15
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 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? 16
Searching and Adding to a Binary Tree A linked list but with two links per item struct treeitem { type ; Look recursively down sequence treeitem 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 17
Example Searching a Tree in C typedef struct _treeitem { char *word; int count; _treeitem *, *; } treeitem; // part of // part of 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->, w); else return finditem(p->, w); 18
Notes on implementation in C++ class TreeNode { public: TreeNode(const string &w); ~TreeNode(); private: const string word; int count; TreeNode *, *; }; // class TreeNode 19
Notes on implementation in C++ (continued) class TreeNode { public: int incr(); int getcount(); string getword(); int compare(const string &w2); TreeNode(const string &w); ~TreeNode(); private: const string word; int count; TreeNode *, *; TreeNode *set(treenode *t); TreeNode *set(treenode *t); TreeNode *get(); TreeNode *get(); }; // class TreeNode TreeNode.h 20
Notes on implementation in C++ (continued) int TreeNode::incr() { TreeNode::~TreeNode() { return ++count; if () { } // incr delete ; int TreeNode::getCount() { = NULL; return count; } } // getcount if () { delete ; TreeNode *TreeNode::setLeft(TreeNode *newitem){ = NULL; if (!) } return = newitem; } // TreeNode destructor else { cerr << Error message" << endl; return NULL; TreeNode::TreeNode (const string &w){ // constructor deferred till } // next time } // setleft } TreeNode.cpp 21
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 BinaryTree.h 22
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 23
Questions? 24