Lab+9+and+10

**Lab 9 ** **//"Abstract Data Types: Lists, Stacks, Queues" //**
 * CS 215 Summer 2012 **

Due Lab Day (**Monday 07/23)** submitted before midnight Electronic submission of answers and code to the CS portal: http://www.cs.uky.edu/csportal


 * Goals and Objectives **

The Abstract Data Type (ADT) is helpful as an abstraction for problem solving. C++ contains built-in (pre-defined) ADT structures in the Standard Template Library (STL). These structures are ready-made to perform algorithmic functions and implement various behaviors for structured data. This lab will make you familiar with a few of the options, also covered in Ch. 13 of the text.


 * 1. Lists **

Modify  the code provided for this lab in order to use the C++ standard ** list ** template. Use the ** list ** template to store every word that is read from a file in order to answer the query about how many occurrences of any given word are in the file. The code should do the following:

use ** sort ** and measure the time cost for sorting the list into alphabetical order
 * Declare a variable of the ** list ** template that can store data of type ** string **<span style="font-family: 'Times New Roman','serif'; font-size: 16px;">.
 * <span style="font-family: 'Times New Roman','serif'; font-size: 16px;">Build the list from the words in the file using the ** push_back **<span style="font-family: 'Times New Roman','serif'; font-size: 16px;"> method.
 * <span style="font-family: 'Times New Roman','serif'; font-size: 16px;">Measure the time it takes to create the complete list for the given example file " mystery4.txt <span style="font-family: 'Times New Roman','serif'; font-size: 16px;">".
 * <span style="font-family: 'Times New Roman','serif'; font-size: 16px;">Measure the time it takes to count occurrences for a random word given by the user
 * <span style="font-family: 'Times New Roman','serif'; font-size: 16px;">Using guidance from []

<span style="font-family: 'Times New Roman','serif'; font-size: 16px;">A stack is an ADT consisting of a collection of objects and a push and pop method. C++ includes a stack template in the standard template library. You can 'push' (add) new items onto the top of a stack or 'pop' (remove) items off of the top of it. The flow of this structure is known as FILO (First In, Last Out) or LIFO (Last In, First Out). This is a very common data type in computer science that is modeled after a common structure found in real life. //<span style="font-family: 'Times New Roman','serif'; font-size: 16px;">Name 3 things in real life that operate like a stack. // //<span style="font-family: 'Times New Roman','serif'; font-size: 16px;">Suppose you were to modify the code above to use a stack instead of a list. Discuss what would have to change and what the limitations would be. Would the stack-based program take longer to run? //
 * <span style="color: black; font-family: 'Times New Roman','serif'; font-size: 16px;">2. Stacks **

<span style="font-family: 'Times New Roman','serif'; font-size: 16px;">Queues are similar data structures to stacks, except that it is a FIFO (First In, First Out) structure. Accordingly, with a queue you no longer “pop” nodes from the top of the list, but instead you remove them from the opposite end of the list from where you insert them. C++ also includes a queue template in the standard template library. //<span style="font-family: 'Times New Roman','serif'; font-size: 16px;">Name 3 things in real life that operate like a queue. // //<span style="font-family: 'Times New Roman','serif'; font-size: 16px;">Suppose you were to modify the code above to use a queue instead of a stack or a list. Discuss what would have to change and what the limitations would be. Would the queue-based program take longer to run? //
 * <span style="font-family: 'Times New Roman','serif'; font-size: 16px;">3. Queues **

<span style="color: black; font-family: 'Times New Roman','serif'; font-size: 16px;">Turn in your modified code, your observations and answers to questions, and any other helper code you wrote as a single zip file.

**<span style="font-family: 'Times New Roman','serif'; font-size: 27px;">CS 215 Summer 2012 **

**<span style="font-family: 'Times New Roman','serif'; font-size: 27px;">Lab 10 **

**//<span style="font-family: 'Times New Roman','serif'; font-size: 27px;">"Binary Search Trees" //**

<span style="font-family: 'Times','serif'; font-size: 16px;">Due Lab Day (**Mon 07/23)** submitted before midnight

<span style="font-family: 'Times','serif'; font-size: 16px;">Electronic submission of answers and code to the CS portal:

http://www.cs.uky.edu/csportal


 * <span style="font-family: 'Times','serif'; font-size: 21px;">Goals and Objectives **

Chapter 14 introduces an important ADT: the Binary Search Tree (BST). Tree data structures are very commonly found in applications that require the ability to find a specific data point quickly. This lab provides code which implements a basic Binary Search Tree, will examine tree structures.


 * Tasks **


 * 1. //Insertion//**

A binary search tree (BST) is a data structure where each node has a value and a pointer to a child to the left of it and a child to the right of it. The left child and all of its children (left subtree) are always less than the current node, and the right child and its children (right subtree) are always greater than the current node.

When inserting nodes into a BST, you simply need to start at the root of the tree, and check to see if the new value is less than or greater than the current node. If the value is less than the root value, follow the left pointer down and set it as your current node. If it is greater than the root value, follow the right pointer and set it as your current node. Then simply repeat this process until you find pointer that is not pointing to anything yet, and put the new value there.

//Write a driver to create a BST that stores strings using the BinarySearchTree code provided for this lab. Declare the tree and insert some nodes into it in this order://

//"I", "K", "B", "A", "E", "O", "L", "J", G"//

//Draw a picture of what the tree should look like after all of these nodes have been inserted.//


 * 1) **//Search//**

The beauty of a BST is the ease of being able to quickly and simply search the tree for a value. The process of this search is essentially the same as that of insertion. You start with the root node, check to see if the value is bigger or smaller, and follow the left or right subtree down until you have successfully found the search value or a NULL where the value should be.

Using this structure, modify the code from last week's lab (the list STL) so that the driver reads words from mystery4.txt and builds both a BinarySearchTree and an STL list. Now do the following things to analyze this situation:

1. Measure and compare the time it takes to build these two structures.

2. Measure and compare the average time it takes to search for words in these two structures.

3. Is it necessary to provide a sort method for the BinarySearchTree object? Why or why not?

<span style="color: black; font-family: 'Times New Roman','serif'; font-size: 16px;">Turn in your modified code, your observations and answers to questions, and any other helper code you wrote as a single zip file.