Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Assignment #4 Binary Trees in C++

Similar presentations


Presentation on theme: "Programming Assignment #4 Binary Trees in C++"— Presentation transcript:

1 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) CS-2303, A-Term 2012 Programming Assignment #4

2 Programming Assignment #4
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 CS-2303, A-Term 2012 Programming Assignment #4

3 Programming Assignment #4
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 CS-2303, A-Term 2012 Programming Assignment #4

4 Programming Assignment #4
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 CS-2303, A-Term 2012 Programming Assignment #4

5 Programming Assignment #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 Total number of different words CS-2303, A-Term 2012 Programming Assignment #4

6 Programming Assignment #4
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” CS-2303, A-Term 2012 Programming Assignment #4

7 Programming Assignment #4
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! CS-2303, A-Term 2012 Programming Assignment #4

8 Programming Assignment #4
Test Files Provided in project assignment CS-2303, A-Term 2012 Programming Assignment #4

9 Programming Assignment #4
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 CS-2303, A-Term 2012 Programming Assignment #4

10 Programming Assignment #4
What is a Binary Tree? CS-2303, A-Term 2012 Programming Assignment #4

11 Programming Assignment #4
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 CS-2303, A-Term 2012 Programming Assignment #4

12 Programming Assignment #4
Binary Tree A linked list but with two links per item struct treeItem { type payload; treeItem *left, *right; }; left right payload left right payload left right payload left right payload left right payload left right payload left right payload CS-2303, A-Term 2012 Programming Assignment #4

13 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 CS-2303, A-Term 2012 Programming Assignment #4

14 Definitions (continued)
See K & R, §6.5 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 CS-2303, A-Term 2012 Programming Assignment #4

15 Programming Assignment #4
Binary Tree A linked list but with two links per item struct treeItem { type payload; treeItem *left, *right; }; left right payload left right payload left right payload left right payload left right payload left right payload left right payload CS-2303, A-Term 2012 Programming Assignment #4

16 Programming Assignment #4
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? CS-2303, A-Term 2012 Programming Assignment #4

17 Searching and Adding to a Binary Tree
A linked list but with two links per item struct treeItem { type payload; treeItem *left, *right; }; 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 left right payload left right payload left right payload left right payload left right payload left right payload CS-2303, A-Term 2012 Programming Assignment #4

18 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); } CS-2303, A-Term 2012 Programming Assignment #4

19 Notes on implementation in C++
class TreeNode { public: TreeNode(const string &w); ~TreeNode(); private: const string word; int count; TreeNode *left, *right; }; // class TreeNode CS-2303, A-Term 2012 Programming Assignment #4

20 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(); TreeNode(const string &w); ~TreeNode(); private: const string word; int count; TreeNode *left, *right; }; // class TreeNode TreeNode.h CS-2303, A-Term 2012 Programming Assignment #4

21 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 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 CS-2303, A-Term 2012 Programming Assignment #4

22 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 CS-2303, A-Term 2012 Programming Assignment #4

23 Programming Assignment #4
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 CS-2303, A-Term 2012 Programming Assignment #4

24 Programming Assignment #4
Questions? CS-2303, A-Term 2012 Programming Assignment #4


Download ppt "Programming Assignment #4 Binary Trees in C++"

Similar presentations


Ads by Google