Commit 6040db9cb15caeca34ef621dcd4b2f8faea30b18
1 parent
237b3fbe11
Exists in
master
PA1: Starter Code
Showing 5 changed files with 327 additions and 0 deletions Side-by-side Diff
BST.hpp
View file @
6040db9
1 | +#ifndef BST_HPP | |
2 | +#define BST_HPP | |
3 | +#include "BSTNode.hpp" | |
4 | +#include "BSTIterator.hpp" | |
5 | +#include <utility> // for std::pair | |
6 | + | |
7 | +template<typename Data> | |
8 | +class BST { | |
9 | + | |
10 | +protected: | |
11 | + | |
12 | + /** Pointer to the root of this BST, or nullptr if the BST is empty */ | |
13 | + BSTNode<Data>* root; | |
14 | + | |
15 | + /** Number of Data items stored in this BST. */ | |
16 | + unsigned int isize; | |
17 | + | |
18 | + | |
19 | +public: | |
20 | + | |
21 | + /** iterator is an aliased typename for BSTIterator<Data>. */ | |
22 | + typedef BSTIterator<Data> iterator; | |
23 | + | |
24 | + /** Default constructor. | |
25 | + Initialize an empty BST. | |
26 | + */ | |
27 | + BST() : root(nullptr), isize(0) { | |
28 | + } | |
29 | + | |
30 | + | |
31 | + /** Default destructor. | |
32 | + * It is virtual, to allow appropriate destruction of subclass objects. | |
33 | + * Delete every node in this BST. | |
34 | + */ // TODO | |
35 | + virtual ~BST() { | |
36 | + | |
37 | + } | |
38 | + | |
39 | + /** Insert a Data item in the BST. | |
40 | + * Return a pair, with the pair's first member set to an | |
41 | + * iterator pointing to either the newly inserted element | |
42 | + * or to the equivalent element already in the set. | |
43 | + * The pair's second element is set to true | |
44 | + * if a new element was inserted or false if an | |
45 | + * equivalent element already existed. | |
46 | + */ // TODO | |
47 | + virtual std::pair<iterator,bool> insert(const Data& item) { | |
48 | + | |
49 | + } | |
50 | + | |
51 | + | |
52 | + /** Find a Data item in the BST. | |
53 | + * Return an iterator pointing to the item, or the end | |
54 | + * iterator if the item is not in the BST. | |
55 | + */ // TODO | |
56 | + iterator find(const Data& item) const { | |
57 | + | |
58 | + } | |
59 | + | |
60 | + | |
61 | + /** Return the number of items currently in the BST. | |
62 | + */ // TODO | |
63 | + unsigned int size() const { | |
64 | + | |
65 | + } | |
66 | + | |
67 | + /** Remove all elements from this BST, and destroy them, | |
68 | + * leaving this BST with a size of 0. | |
69 | + */ // TODO | |
70 | + void clear() { | |
71 | + | |
72 | + } | |
73 | + | |
74 | + /** Return true if the BST is empty, else false. | |
75 | + */ // TODO | |
76 | + bool empty() const { | |
77 | + | |
78 | + } | |
79 | + | |
80 | + /** Return an iterator pointing to the first item in the BST. | |
81 | + */ // TODO | |
82 | + iterator begin() const { | |
83 | + | |
84 | + } | |
85 | + | |
86 | + /** Return an iterator pointing past the last item in the BST. | |
87 | + */ | |
88 | + iterator end() const { | |
89 | + return typename BST<Data>::iterator(nullptr); | |
90 | + } | |
91 | + | |
92 | + | |
93 | + | |
94 | + }; | |
95 | + | |
96 | + | |
97 | +#endif //BST_HPP |
BSTIterator.hpp
View file @
6040db9
1 | +#ifndef BSTITERATOR_HPP | |
2 | +#define BSTITERATOR_HPP | |
3 | +#include "BSTNode.hpp" | |
4 | +#include <list> | |
5 | +#include <iterator> | |
6 | + | |
7 | +// declare BST here, so friend declaration below will work. | |
8 | +template<typename X> class BST; | |
9 | + | |
10 | +template<typename Data> | |
11 | +class BSTIterator : public std::iterator<std::input_iterator_tag,Data> { | |
12 | + | |
13 | +private: | |
14 | + | |
15 | + BSTNode<Data>* curr; // pointer to this BSTIterator's current BSTNode | |
16 | + | |
17 | + /** Constructor. Use the argument to initialize a given BSTNode | |
18 | + * in this BSTIterator. | |
19 | + * Note: this constructor is private; but friend classes can access. | |
20 | + */ // TODO | |
21 | + BSTIterator(BSTNode<Data>* curr) { | |
22 | + } | |
23 | + | |
24 | + | |
25 | +public: | |
26 | + // make BST a friend class, so BST can create a BSTIterator when needed. | |
27 | + friend class BST<Data>; | |
28 | + // make BSTNode a friend class, so BSTNode can access curr member variable. | |
29 | + friend class BSTNode<Data>; | |
30 | + | |
31 | + | |
32 | + /** Dereference operator. | |
33 | + * Return a copy of the Data item in the current BSTNode. | |
34 | + */ | |
35 | + Data operator*() const { | |
36 | + return curr->data; | |
37 | + } | |
38 | + | |
39 | + /** Pre-increment operator. | |
40 | + * Update this BSTIterator to point to the inorder successor of the current | |
41 | + * BSTNode, and return a reference to the updated BSTIterator. | |
42 | + */ | |
43 | + BSTIterator<Data>& operator++() { | |
44 | + curr = curr->successor(); | |
45 | + return *this; | |
46 | + } | |
47 | + | |
48 | + /** Post-increment operator. | |
49 | + * Update this BSTIterator to point to the inorder successor of the current | |
50 | + * BSTNode, and return a copy of the old, non-updated BSTIterator. | |
51 | + */ | |
52 | + BSTIterator<Data> operator++(int) { | |
53 | + BSTIterator before = BSTIterator(curr); | |
54 | + ++(*this); | |
55 | + return before; | |
56 | + } | |
57 | + | |
58 | + /** Equality test operator. */ // TODO | |
59 | + bool operator==(BSTIterator<Data> const & other) const { | |
60 | + } | |
61 | + | |
62 | + /** Inequality test operator. */ // TODO | |
63 | + bool operator!=(BSTIterator<Data> const & other) const { | |
64 | + } | |
65 | + | |
66 | +}; | |
67 | + | |
68 | +#endif //BSTITERATOR_HPP |
BSTNode.hpp
View file @
6040db9
1 | +#ifndef BSTNODE_HPP | |
2 | +#define BSTNODE_HPP | |
3 | +#include <iostream> | |
4 | +#include <iomanip> | |
5 | + | |
6 | + | |
7 | +/** This class template defines a node type for the BST container. | |
8 | + * Note that all members are public. So, the BST implementation should | |
9 | + * take care not to expose any BSTNode objects. | |
10 | + */ | |
11 | +template<typename Data> | |
12 | +class BSTNode { | |
13 | + | |
14 | +public: | |
15 | + | |
16 | + /** Member variables. */ | |
17 | + BSTNode<Data>* parent; | |
18 | + BSTNode<Data>* left; | |
19 | + BSTNode<Data>* right; | |
20 | + const Data data; // the const Data in this node | |
21 | + int info; // variable used in advanced algorithms | |
22 | + | |
23 | + /** Constructor. Initialize a BSTNode with the given Data item, | |
24 | + * no parent, and no children. | |
25 | + */ | |
26 | + BSTNode(const Data & d): data(d) { | |
27 | + left = right = parent = nullptr; | |
28 | + } | |
29 | + | |
30 | + | |
31 | + /** Return the inorder successor of this BSTNode in a BST, | |
32 | + * or nullptr if none. | |
33 | + * PRECONDITION: this BSTNode is a node in a BST. | |
34 | + * POSTCONDITION: the BST is unchanged. | |
35 | + * RETURNS: the BSTNode that is the inorder successor of this BSTNode, | |
36 | + * or nullptr if there is none. | |
37 | + */ // TODO | |
38 | + BSTNode<Data>* successor() { | |
39 | + | |
40 | + } | |
41 | + | |
42 | +}; | |
43 | + | |
44 | +/** Overload operator<< to insert a BSTNode's fields in an ostream. */ | |
45 | +template <typename Data> | |
46 | +std::ostream & operator<<(std::ostream& stm, const BSTNode<Data> & n) { | |
47 | + stm << '['; | |
48 | + stm << std::setw(10) << &n; // address of the BSTNode | |
49 | + stm << "; p:" << std::setw(10) << n.parent; // address of its parent | |
50 | + stm << "; l:" << std::setw(10) << n.left; // address of its left child | |
51 | + stm << "; r:" << std::setw(10) << n.right; // address of its right child | |
52 | + stm << "; i:" << std::setw(10) << n.info; // its info field | |
53 | + stm << "; d:" << n.data; // its data field | |
54 | + stm << ']'; | |
55 | + return stm; | |
56 | +} | |
57 | + | |
58 | +#endif // BSTNODE_HPP |
Makefile
View file @
6040db9
1 | +# A simple makefile for CSE 100 P1 | |
2 | + | |
3 | +#use g++ for everything | |
4 | +CC= g++ | |
5 | + | |
6 | +# include debugging symbols in object files, | |
7 | +# and enable all warnings | |
8 | +CXXFLAGS= -std=c++11 -O2 -g -Wall | |
9 | + | |
10 | +#include debugging symbols in executable | |
11 | +LDFLAGS= -g | |
12 | + | |
13 | +bst: test_BST.o | |
14 | + g++ -o bst test_BST.o | |
15 | + | |
16 | +test_BST.o: BST.hpp BSTNode.hpp BSTIterator.hpp | |
17 | + | |
18 | + | |
19 | + | |
20 | +.PHONY: clean | |
21 | +clean: | |
22 | + $(RM) *.o bst ; | |
23 | + $(RM) core; |
test_BST.cpp
View file @
6040db9
1 | +#include "BST.hpp" | |
2 | +#include <iostream> | |
3 | +#include <algorithm> | |
4 | +#include <vector> | |
5 | + | |
6 | +using namespace std; | |
7 | + | |
8 | +/** | |
9 | + * A simple test driver for the BST class template. | |
10 | + * P1 CSE 100 2013 | |
11 | + * Author: P. Kube (c) 2013 | |
12 | + */ | |
13 | +int main() { | |
14 | + | |
15 | + /* Create an STL vector of some ints */ | |
16 | + vector<int> v; | |
17 | + v.push_back(3); | |
18 | + v.push_back(4); | |
19 | + v.push_back(1); | |
20 | + v.push_back(100); | |
21 | + v.push_back(-33); | |
22 | + | |
23 | + /* Create an instance of BST holding int */ | |
24 | + BST<int> b; | |
25 | + | |
26 | + /* Insert a few data items. */ | |
27 | + vector<int>::iterator vit = v.begin(); | |
28 | + vector<int>::iterator ven = v.end(); | |
29 | + for(; vit != ven; ++vit) { | |
30 | + // all these inserts are unique, so should return a std::pair | |
31 | + // with second part true | |
32 | + std::pair<BST<int>::iterator,bool> pr = b.insert(*vit); | |
33 | + if(! pr.second ) { | |
34 | + cout << "Incorrect bool return value when inserting " << *vit << endl; | |
35 | + return -1; | |
36 | + } | |
37 | + if(*(pr.first) != *vit) { | |
38 | + cout << "Incorrect iterator return value when inserting " << *vit << endl; | |
39 | + return -1; | |
40 | + } | |
41 | + } | |
42 | + | |
43 | + /* Test size. */ | |
44 | + cout << "Size is: " << b.size() << endl; | |
45 | + if(b.size() != v.size()) { | |
46 | + cout << "... which is incorrect." << endl; | |
47 | + return -1; | |
48 | + } | |
49 | + | |
50 | + /* Test find return value. */ | |
51 | + vit = v.begin(); | |
52 | + for(; vit != ven; ++vit) { | |
53 | + if(*(b.find(*vit)) != *vit) { | |
54 | + cout << "Incorrect return value when finding " << *vit << endl; | |
55 | + return -1; | |
56 | + } | |
57 | + } | |
58 | + | |
59 | + /* Sort the vector, to compare with inorder iteration on the BST */ | |
60 | + sort(v.begin(),v.end()); | |
61 | + | |
62 | + /* Test BST iterator; should iterate inorder */ | |
63 | + cout << "traversal using iterator:" << endl; | |
64 | + vit = v.begin(); | |
65 | + BST<int>::iterator en = b.end(); | |
66 | + BST<int>::iterator it = b.begin(); | |
67 | + for(; vit != ven; ++vit) { | |
68 | + if(! (it != en) ) { | |
69 | + cout << *it << "," << *vit << ": Early termination of BST iteration." << endl; | |
70 | + return -1; | |
71 | + } | |
72 | + cout << *it << endl; | |
73 | + if(*it != *vit) { | |
74 | + cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl; | |
75 | + return -1; | |
76 | + } | |
77 | + ++it; | |
78 | + } | |
79 | + cout << "OK." << endl; | |
80 | + | |
81 | +} |