161 Lab 5 exercises

See the main lab page for submission deadlines and late penalties.

It is assumed you are keeping up to date with the lectures and labs.

The focus for this lab is on the implementation of a variety of class features (inheritance, copy and move constructors, operator overloading) using a binary search tree class derived from a basic tree class.

The sequence for obtaining the lab will be much the same as previous labs, and the commands are briefly summaried below (see the lab1 discussion for explanations and any troubleshooting needed):
ssh -x csci fork csci161/lab5 csci161/$USER/lab5
cd csci161
git clone csci:csci161/$USER/lab5
cd lab5

Repository contents

The repository contains the usual makefile (this time to build executable lab5x) and readme, plus a collection of three pairs of .h/.cpp files:

The base tree class

The basic tree class provides: The base tree class is already complete and should not be altered.

The main routine

The lab5x main routine is set up to exercise the various binary search tree methods we are implementing.

The data we are storing/accessing is a collection of key value pairs, where both the keys and values are strings. The idea is that a user can store a variety of keys, each having a unique value associated with it (e.g. a user might be storing colours as the keys and their RGB values as the associated values, or storing usernames as keys and the users' passwords as the associated values).

Currently the main routine declres a variety of bstree variables, uses the supplied fillTree function to fill one of them with user data, then tests out the various bstree methods (the copy and move constructors, the overloaded = operator, the search method, the print method, etc).

The main routine and supporting functions are already complete and should not be altered.

The bstree definition

The bstree.h provides the definition for a bstree class derived from the basic tree class, with a variety of public and private methods.

The bstree is for a binary search tree of key,value pairs (both strings) where the tree is organized based on the keys following our usual binary search tree rules: smaller values go in the left subtree, larger values go in the right subtree.

(Note: the < == and > operators have been overloaded for the string class, so you can use them normally to compare keys.)

In this tree we won't allow duplicate keys: if someone tries to insert a new key,value pair and that key is already in the tree then we'll treat it as if they want to update the value for that key, not generate a new additional pair.

#pragma once

#include "tree.h"

// tree represents a binary search tree of key-value pairs
//    keys and values are both strings)
class bstree: public tree {
   public:
       bstree(); // constructor for empty tree
      ~bstree(); // destructor (delete all tree nodes)
      string lookup(string k);  // searches tree for the specified key k,
                                // returns the associated string, returns "NOT FOUND"
                                // if no such key is found
      void insert(string k, string v); // if a node with key k already exists then insert
                                       //    replaces its associated value with v,
                                       // otherwise inserts a new node with the given key/value
      bstree(const bstree& orig); // copy constructor
      bstree(bstree&& orig); // move constructor
      bstree& operator=(const bstree& rhs); // overload the assignment operator (makes deep copy)

   private:

      tnode* lookup(string k, tnode* t);  // searches subtree t for a node whose key matches k
                                          // and returns a pointer to the node (if found)
                                          // or nullptr (if no such node is found)
      void insert(string k, string v, tnode* &t); // performs insert within subtree t

      void copy(tnode* &t, tnode* origt); // make t a deep copy of subtree origt

};

The bstree.h file is already complete and should not be altered.

The bstree method implementations (your task)

The bstree.cpp file is intended to include all the bstree method implementations and needs to be completed by you.
(Initially it is nearly empty, consisting of just the #include.)

Details and recommended approaches for the various methods are discussed below:

Sample run
A sample run showing what main does and the output produced, with some notes added explaining what's happening where.
(User input has also been highlighted in bold italics for clarity.)



user enters some initial key/value pairs for tree T1, DONE when done
Enter a key (single word) or the word DONE (all caps) if you're finished me Enter a value (single word) to go with key me mnop Inserted new node me:mnop Enter a key (single word) or the word DONE (all caps) if you're finished whoever Enter a value (single word) to go with key whoever bbb Inserted new node whoever:bbb Enter a key (single word) or the word DONE (all caps) if you're finished anyone Enter a value (single word) to go with key anyone blahblah Inserted new node anyone:blahblah Enter a key (single word) or the word DONE (all caps) if you're finished someone Enter a value (single word) to go with key someone xyz Inserted new node someone:xyz Enter a key (single word) or the word DONE (all caps) if you're finished who Enter a value (single word) to go with key who abc Inserted new node who:abc Enter a key (single word) or the word DONE (all caps) if you're finished DONE
program tries the move constructor to move T1 content into T2 then prints both
Running move constructor The original tree content (should be empty) is: The tree is currently empty The content of the tree we've moved it into is: The current tree contents are: anyone:blahblah me:mnop someone:xyz who:abc whoever:bbb
program tries the copy constructor to copy T2 to T3 and prints T3
Running copy constructor The content of a copy of the second tree is: The current tree contents are: anyone:blahblah me:mnop someone:xyz who:abc whoever:bbb
user enters additional key/value pairs to add to T3
Enter a key (single word) or the word DONE (all caps) if you're finished anyone Enter a value (single word) to go with key anyone notblah Updated node anyone's value to notblah Enter a key (single word) or the word DONE (all caps) if you're finished foo Enter a value (single word) to go with key foo moreblah Inserted new node foo:moreblah Enter a key (single word) or the word DONE (all caps) if you're finished DONE
program shows contents of T3 and T2
The content of the modified copy is now: The current tree contents are: anyone:notblah foo:moreblah me:mnop someone:xyz who:abc whoever:bbb The content of the second tree (should be unchanged by the modifications to the copy) is: The current tree contents are: anyone:blahblah me:mnop someone:xyz who:abc whoever:bbb
program tries T4 = T3 then prints T4 to check overloaded = operator
Running copy constructor The content of the copy made using = is: The current tree contents are: anyone:notblah foo:moreblah me:mnop someone:xyz who:abc whoever:bbb
program gets user to search for a key in T4
Enter the key you would like to search for someone The search result for key someone is: xyz T3 and T4 are identical in content and structure T2 and T3 differ
program ends, all the destructors run Deallocating tree Deleting node foo:moreblah Deleting node anyone:notblah Deleting node who:abc Deleting node someone:xyz Deleting node whoever:bbb Deleting node me:mnop Deallocation complete Deallocating tree Deleting node foo:moreblah Deleting node anyone:notblah Deleting node who:abc Deleting node someone:xyz Deleting node whoever:bbb Deleting node me:mnop Deallocation complete Deallocating tree Deleting node anyone:blahblah Deleting node who:abc Deleting node someone:xyz Deleting node whoever:bbb Deleting node me:mnop Deallocation complete Deallocating tree Deallocation complete