Programming Assignment #4 Binary Trees in C++

Similar documents
Iterators. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Classes, Constructors, etc., in C++

Binary Trees (and Big O notation)

Containers and the Standard Template Library (STL)

Linked Lists in C and C++

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Your first C and C++ programs

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Derived Classes in C++

Arrays and Pointers in C & C++

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Structures, Unions, and Typedefs

A Deeper Look at Classes

lecture14: Tree Traversals

CS 241 Data Organization Binary Trees

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts

CS24 Week 3 Lecture 1

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Strings in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering. Program 8 EECE.3220 Data Structures Fall 2017

Dynamic Data Structures

Algorithms and Data Structures CS-CO-412

An Introduction to Trees

Short Notes of CS201

CS 215 Fundamentals of Programming II Fall 2017 Project 7. Morse Code. 30 points. Out: November 20, 2017 Due: December 4, 2017 (Monday) a n m

Out: April 19, 2017 Due: April 26, 2017 (Wednesday, Reading/Study Day, no late work accepted after Friday)

CS201 - Introduction to Programming Glossary By

Exception Handling in C++

CSCI-1200 Data Structures Fall 2017 Lecture 17 Trees, Part I

ECE 244 Programming Fundamentals Fall Lab Assignment #5: Binary Search Trees

And Even More and More C++ Fundamentals of Computer Science

Financial computing with C++

Chapter 17: Linked Lists

CSCI-1200 Data Structures Spring 2018 Lecture 16 Trees, Part I

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering. Program 10 EECE.3220 Data Structures Fall 2017.

CMSC 341 Lecture 10 Binary Search Trees

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CSCI-1200 Data Structures Spring 2015 Lecture 20 Trees, Part III

CSCI-1200 Data Structures Fall 2018 Lecture 19 Trees, Part III

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

CSCI-1200 Data Structures Spring 2015 Lecture 18 Trees, Part I

Chapter 20: Binary Trees

CSE100. Advanced Data Structures. Lecture 12. (Based on Paul Kube course materials)

Chapter 17: Linked Lists

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CSCE 110 PROGRAMMING FUNDAMENTALS

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Absolute C++ Walter Savitch

CSCE121: Introduction to Program Design and Concepts Practice Questions for Final

CSCI-1200 Data Structures Spring 2018 Lecture 18 Trees, Part III

Trees, Part 1: Unbalanced Trees

CS 171: Introduction to Computer Science II. Linked List. Li Xiong

Search Trees. Data and File Structures Laboratory. DFS Lab (ISI) Search Trees 1 / 17

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

EL2310 Scientific Programming

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

TREES Lecture 12 CS2110 Spring 2018

III. Classes (Chap. 3)

CSC 1052 Algorithms & Data Structures II: Linked Lists Revisited

CS201 Some Important Definitions

selectors, methodsinsert() andto_string() the depth of a tree and a membership function

Assignment of Structs

CS

Final Exam Data Structure course. No. of Branches (5)

CS 215 Fundamentals of Programming II Spring 2018 Project 5

TREES Lecture 10 CS2110 Spring2014

CSCI-1200 Data Structures Fall 2014 Lecture 18 Trees, Part III

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Final Examination 8:30 AM, Wednesday, April 18, 2012

COMP251: Algorithms and Data Structures. Jérôme Waldispühl School of Computer Science McGill University

PESIT-BSC Department of Science & Humanities

CS250 Final Review Questions

COMP322 - Introduction to C++ Lecture 10 - Overloading Operators and Exceptions

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Expression Trees and the Heap

CS3157: Advanced Programming. Outline

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Vector and Free Store (Pointers and Memory Allocation)

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

Structuur van Computerprogramma s 2

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

If you took your exam home last time, I will still regrade it if you want.

CSCI-1200 Data Structures Fall 2017 Lecture 18 Trees, Part II

Common Misunderstandings from Exam 1 Material

Computer Science II CSci 1200 Lecture 18 Operators and Friends

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA === Homework submission instructions ===

Programming Assignment 2

Programming, numerics and optimization

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

Lecture No.07. // print the final avaerage wait time.

THINK LIKE CREATIVE PROBLEM SOLVING V. ANTON SPRAUL

Tutorial AVL TREES. arra[5] = {1,2,3,4,5} arrb[8] = {20,30,80,40,10,60,50,70} FIGURE 1 Equivalent Binary Search and AVL Trees. arra = {1, 2, 3, 4, 5}

Data Structure (CS301)

EL2310 Scientific Programming

Transcription:

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