programming assignment #4, binary treesweb.cs.wpi.edu/.../assignment4--binarytrees_slides.pdf ·...

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

Upload: others

Post on 18-Aug-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 2: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 3: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 4: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 5: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 6: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 7: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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!

Page 8: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 9: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 10: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

Carnegie Mellon Worcester Polytechnic Institute

What is a Binary Tree?

Programming Assignment #4 CS-2303, A-Term 2012 10

Page 11: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 12: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 13: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 14: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 15: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 16: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 17: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 18: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 19: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 20: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 21: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 22: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 23: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

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

Page 24: Programming Assignment #4, Binary Treesweb.cs.wpi.edu/.../Assignment4--BinaryTrees_slides.pdf · Worcester Polytechnic InstituteCarnegie Mellon Programming Assignment #4 Binary Trees

Carnegie Mellon Worcester Polytechnic Institute

Questions?

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