Programming Assignment #4 Binary Trees in C++

Slides:



Advertisements
Similar presentations
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Advertisements

Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Binary TreesCS-2303, C-Term Binary Trees (and Big “O” notation) CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
Linked Lists in C and C++ By Ravi Prakash PGT(CS).
Strings and Dynamic Memory Allocation CS-2301, B-Term Programming Assignment #6 Strings and Dynamic Memory Allocation CS-2301, System Programming.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Lists and TreesCS-2301 D-term Data Structures — Lists and Trees CS-2301 System Programming D-term 2009 (Slides include materials from The C Programming.
Lists and Trees (continued) CS-2301, B-Term Lists and Trees (continued) CS-2301, System Programming for Non-Majors (Slides include materials from.
Programming Assignment #6 CS-2301 D-term Programming Assignment #6 Binary Trees CS-2301 System Programming D-term 2009 (Slides include materials.
Programming Assignment #4 Binary Trees
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Binary TreesCS-2301, B-Term Programming Assignment #5 Binary Trees CS-2301, System Programming for Non-Majors (Slides include materials from The.
Data Structures, Lists, and Trees CS-2301 B-term Data Structures — Lists and Trees CS-2301, System Programming for Non-majors (Slides include materials.
CS-2303 System Programming Concepts
Data Structures — Lists and Trees CS-2301, B-Term Data Structures — Lists and Trees CS-2301, System Programming for Non-Majors (Slides include materials.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
1 Data Structures Lists and Trees. 2 Real-Life Computational Problems All about organizing data! –What shape the data should have to solve your problem.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
CS 1031 Tree Traversal Techniques; Heaps Tree Traversal Concept Tree Traversal Techniques: Preorder, Inorder, Postorder Full Trees Almost Complete Trees.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
CS 1031 Trees A Quick Introduction to Graphs Definition of Trees Rooted Trees Binary Trees Binary Search Trees.
Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University.
CIS* Quiz 2. For a large array, and in the worst case, selection sort is faster than insertion sort. False Both selection sort and insertion sort.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Copy Constructor / Destructors Stacks and Queues
Data Structures and Design in Java © Rick Mercer
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Chapter 20: Binary Trees.
Chapter 16-2 Linked Structures
Chapter 21: Binary Trees.
Makefiles and Notes on Programming Assignment PA2
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
“Under the Hood” of Polymorphism
Classes, Constructors, etc., in C++
Containers and the Standard Template Library (STL)
Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Structures, Unions, and Typedefs
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Linked Lists in C and C++
Binary Trees (and Big “O” notation)
Bit Fields & Bitwise Operations
Programming Assignment #1 12-Month Calendar—
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Symbolic Constants in C
Recursion and Implementation of Functions
Programming Assignment #6
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Scope Rules and Storage Types
Programming Assignment #5
Operations on Binary Tree
Differences between Java and C
Your first C and C++ programs
Iterators Professor Hugh C. Lauer CS-2303, System Programming Concepts
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Introduction to C++ Linear Linked Lists
Pointers & Dynamic Data Structures
A Deeper Look at Classes
Introduction to Classes and Objects
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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