From 6040db9cb15caeca34ef621dcd4b2f8faea30b18 Mon Sep 17 00:00:00 2001 From: Gautam Akiwate Date: Mon, 7 Apr 2014 22:59:09 -0700 Subject: [PATCH] PA1: Starter Code --- BST.hpp | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ BSTIterator.hpp | 68 ++++++++++++++++++++++++++++++++++++++++ BSTNode.hpp | 58 ++++++++++++++++++++++++++++++++++ Makefile | 23 ++++++++++++++ test_BST.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 327 insertions(+) create mode 100644 BST.hpp create mode 100644 BSTIterator.hpp create mode 100644 BSTNode.hpp create mode 100644 Makefile create mode 100644 test_BST.cpp diff --git a/BST.hpp b/BST.hpp new file mode 100644 index 0000000..6c26b06 --- /dev/null +++ b/BST.hpp @@ -0,0 +1,97 @@ +#ifndef BST_HPP +#define BST_HPP +#include "BSTNode.hpp" +#include "BSTIterator.hpp" +#include // for std::pair + +template +class BST { + +protected: + + /** Pointer to the root of this BST, or nullptr if the BST is empty */ + BSTNode* root; + + /** Number of Data items stored in this BST. */ + unsigned int isize; + + +public: + + /** iterator is an aliased typename for BSTIterator. */ + typedef BSTIterator iterator; + + /** Default constructor. + Initialize an empty BST. + */ + BST() : root(nullptr), isize(0) { + } + + + /** Default destructor. + * It is virtual, to allow appropriate destruction of subclass objects. + * Delete every node in this BST. + */ // TODO + virtual ~BST() { + + } + + /** Insert a Data item in the BST. + * Return a pair, with the pair's first member set to an + * iterator pointing to either the newly inserted element + * or to the equivalent element already in the set. + * The pair's second element is set to true + * if a new element was inserted or false if an + * equivalent element already existed. + */ // TODO + virtual std::pair insert(const Data& item) { + + } + + + /** Find a Data item in the BST. + * Return an iterator pointing to the item, or the end + * iterator if the item is not in the BST. + */ // TODO + iterator find(const Data& item) const { + + } + + + /** Return the number of items currently in the BST. + */ // TODO + unsigned int size() const { + + } + + /** Remove all elements from this BST, and destroy them, + * leaving this BST with a size of 0. + */ // TODO + void clear() { + + } + + /** Return true if the BST is empty, else false. + */ // TODO + bool empty() const { + + } + + /** Return an iterator pointing to the first item in the BST. + */ // TODO + iterator begin() const { + + } + + /** Return an iterator pointing past the last item in the BST. + */ + iterator end() const { + return typename BST::iterator(nullptr); + } + + + + }; + + +#endif //BST_HPP diff --git a/BSTIterator.hpp b/BSTIterator.hpp new file mode 100644 index 0000000..43f0fd3 --- /dev/null +++ b/BSTIterator.hpp @@ -0,0 +1,68 @@ +#ifndef BSTITERATOR_HPP +#define BSTITERATOR_HPP +#include "BSTNode.hpp" +#include +#include + +// declare BST here, so friend declaration below will work. +template class BST; + +template +class BSTIterator : public std::iterator { + +private: + + BSTNode* curr; // pointer to this BSTIterator's current BSTNode + + /** Constructor. Use the argument to initialize a given BSTNode + * in this BSTIterator. + * Note: this constructor is private; but friend classes can access. + */ // TODO + BSTIterator(BSTNode* curr) { + } + + +public: + // make BST a friend class, so BST can create a BSTIterator when needed. + friend class BST; + // make BSTNode a friend class, so BSTNode can access curr member variable. + friend class BSTNode; + + + /** Dereference operator. + * Return a copy of the Data item in the current BSTNode. + */ + Data operator*() const { + return curr->data; + } + + /** Pre-increment operator. + * Update this BSTIterator to point to the inorder successor of the current + * BSTNode, and return a reference to the updated BSTIterator. + */ + BSTIterator& operator++() { + curr = curr->successor(); + return *this; + } + + /** Post-increment operator. + * Update this BSTIterator to point to the inorder successor of the current + * BSTNode, and return a copy of the old, non-updated BSTIterator. + */ + BSTIterator operator++(int) { + BSTIterator before = BSTIterator(curr); + ++(*this); + return before; + } + + /** Equality test operator. */ // TODO + bool operator==(BSTIterator const & other) const { + } + + /** Inequality test operator. */ // TODO + bool operator!=(BSTIterator const & other) const { + } + +}; + +#endif //BSTITERATOR_HPP diff --git a/BSTNode.hpp b/BSTNode.hpp new file mode 100644 index 0000000..244d591 --- /dev/null +++ b/BSTNode.hpp @@ -0,0 +1,58 @@ +#ifndef BSTNODE_HPP +#define BSTNODE_HPP +#include +#include + + +/** This class template defines a node type for the BST container. + * Note that all members are public. So, the BST implementation should + * take care not to expose any BSTNode objects. + */ +template +class BSTNode { + +public: + + /** Member variables. */ + BSTNode* parent; + BSTNode* left; + BSTNode* right; + const Data data; // the const Data in this node + int info; // variable used in advanced algorithms + + /** Constructor. Initialize a BSTNode with the given Data item, + * no parent, and no children. + */ + BSTNode(const Data & d): data(d) { + left = right = parent = nullptr; + } + + + /** Return the inorder successor of this BSTNode in a BST, + * or nullptr if none. + * PRECONDITION: this BSTNode is a node in a BST. + * POSTCONDITION: the BST is unchanged. + * RETURNS: the BSTNode that is the inorder successor of this BSTNode, + * or nullptr if there is none. + */ // TODO + BSTNode* successor() { + + } + +}; + +/** Overload operator<< to insert a BSTNode's fields in an ostream. */ +template +std::ostream & operator<<(std::ostream& stm, const BSTNode & n) { + stm << '['; + stm << std::setw(10) << &n; // address of the BSTNode + stm << "; p:" << std::setw(10) << n.parent; // address of its parent + stm << "; l:" << std::setw(10) << n.left; // address of its left child + stm << "; r:" << std::setw(10) << n.right; // address of its right child + stm << "; i:" << std::setw(10) << n.info; // its info field + stm << "; d:" << n.data; // its data field + stm << ']'; + return stm; +} + +#endif // BSTNODE_HPP diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f554541 --- /dev/null +++ b/Makefile @@ -0,0 +1,23 @@ +# A simple makefile for CSE 100 P1 + +#use g++ for everything +CC= g++ + +# include debugging symbols in object files, +# and enable all warnings +CXXFLAGS= -std=c++11 -O2 -g -Wall + +#include debugging symbols in executable +LDFLAGS= -g + +bst: test_BST.o + g++ -o bst test_BST.o + +test_BST.o: BST.hpp BSTNode.hpp BSTIterator.hpp + + + +.PHONY: clean +clean: + $(RM) *.o bst ; + $(RM) core; diff --git a/test_BST.cpp b/test_BST.cpp new file mode 100644 index 0000000..a797c46 --- /dev/null +++ b/test_BST.cpp @@ -0,0 +1,81 @@ +#include "BST.hpp" +#include +#include +#include + +using namespace std; + +/** + * A simple test driver for the BST class template. + * P1 CSE 100 2013 + * Author: P. Kube (c) 2013 + */ +int main() { + + /* Create an STL vector of some ints */ + vector v; + v.push_back(3); + v.push_back(4); + v.push_back(1); + v.push_back(100); + v.push_back(-33); + + /* Create an instance of BST holding int */ + BST b; + + /* Insert a few data items. */ + vector::iterator vit = v.begin(); + vector::iterator ven = v.end(); + for(; vit != ven; ++vit) { + // all these inserts are unique, so should return a std::pair + // with second part true + std::pair::iterator,bool> pr = b.insert(*vit); + if(! pr.second ) { + cout << "Incorrect bool return value when inserting " << *vit << endl; + return -1; + } + if(*(pr.first) != *vit) { + cout << "Incorrect iterator return value when inserting " << *vit << endl; + return -1; + } + } + + /* Test size. */ + cout << "Size is: " << b.size() << endl; + if(b.size() != v.size()) { + cout << "... which is incorrect." << endl; + return -1; + } + + /* Test find return value. */ + vit = v.begin(); + for(; vit != ven; ++vit) { + if(*(b.find(*vit)) != *vit) { + cout << "Incorrect return value when finding " << *vit << endl; + return -1; + } + } + + /* Sort the vector, to compare with inorder iteration on the BST */ + sort(v.begin(),v.end()); + + /* Test BST iterator; should iterate inorder */ + cout << "traversal using iterator:" << endl; + vit = v.begin(); + BST::iterator en = b.end(); + BST::iterator it = b.begin(); + for(; vit != ven; ++vit) { + if(! (it != en) ) { + cout << *it << "," << *vit << ": Early termination of BST iteration." << endl; + return -1; + } + cout << *it << endl; + if(*it != *vit) { + cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl; + return -1; + } + ++it; + } + cout << "OK." << endl; + +} -- 1.9.1