Permalink
Browse files

fixing shit

  • Loading branch information...
Yasmine A Nassar
Yasmine A Nassar committed Oct 12, 2017
1 parent 6095c84 commit e02c12b38a855d756aed7e1d8b93f1e9b1177f27
View
@@ -3,10 +3,12 @@
#include "BSTNode.hpp"
#include "BSTIterator.hpp"
#include <iostream>
#include <typeinfo>
template<typename Data>
class BST {
public:
/** define iterator as an aliased typename for BSTIterator<Data>. */
@@ -109,30 +111,34 @@ BST<Data>::~BST() {
* Note: This function should use only the '<' operator when comparing
* Data items. (should not use ==, >, <=, >=)
*/
using namespace std;
template <typename Data>
std::pair<BSTIterator<Data>, bool> BST<Data>::insert(const Data& item) {
//check if the tree is empty without using ==
if (empty()){
//add item as a new node to the tree
BSTNode<Data> first(item);
BSTNode<Data> * ptr = &first;
BSTNode<Data> * ptr = new BSTNode<Data>(item);
//set the root to the node
this->root = ptr;
root = ptr;
cout << "ROOT IN INSERT: " << *(root) << endl;
BSTIterator<Data> itr (ptr);
isize++;
cout << "beginning" << endl;
return std::pair<BSTIterator<Data>,bool>(itr, true);
}
//if the item already exists, we just need an iterator pointing to it
BSTIterator<Data> itemItr= find(item);
if(itemItr!=end()){
cout << "why" << endl;
return std::pair<BSTIterator<Data>,bool>(itemItr, false);
}
//the item does not exist in the tree so we need to put it in and return true
else{
BSTNode<Data> insert_node(item);
BSTNode<Data> * insert_node_ptr = &insert_node;
//BSTNode<Data> insert_node = new BSTNode<Data>(item);
BSTNode<Data> * insert_node_ptr = new BSTNode<Data>(item);
BSTNode<Data> * node = root;
//BSTIterator <Data> new_itr (nullptr);
while (node != NULL){
@@ -143,6 +149,8 @@ std::pair<BSTIterator<Data>, bool> BST<Data>::insert(const Data& item) {
//insert it if the child is not there
else{
node->left = insert_node_ptr;
(node->left)->parent = node;
cout << "ARE YOU HERE left" << endl;
BSTIterator <Data> new_itr(node->left);
isize++;
return std::pair<BSTIterator<Data>, bool>(new_itr, true);
@@ -155,6 +163,8 @@ std::pair<BSTIterator<Data>, bool> BST<Data>::insert(const Data& item) {
}
else{
node->right = insert_node_ptr;
(node->right)->parent = node;
cout << "ARE YOU HERE right" << endl;
BSTIterator <Data> new_itr(node->right);
isize++;
return std::pair<BSTIterator<Data>, bool>(new_itr, true);
@@ -176,19 +186,28 @@ std::pair<BSTIterator<Data>, bool> BST<Data>::insert(const Data& item) {
* Data items. (should not use ==, >, <=, >=). For the reasoning
* behind this, see the assignment writeup.
*/
using namespace std;
template <typename Data>
BSTIterator<Data> BST<Data>::find(const Data& item) const
{
BSTNode<Data> * node = root;
cout << "root: " << *(root) << endl;
cout << "item: " << item << endl;
cout << "node->data: " << node->data << endl;
//cout << "node: " << *node << endl;
while(node != NULL){
if(item < node->data){
cout << "less than" << endl;
node = node->left;
}
else if (node->data < item){
cout << "more than" << endl;
node = node->right;
}
//here the data and node are equal-- we found it!
else{
cout << "node was found" << endl;
BSTIterator<Data> iter(node);
return iter;
}
@@ -214,12 +233,8 @@ template <typename Data>
unsigned int BST<Data>::height() const
{
int height;
//if the tree is empty, the height is -1
if(empty()){
height = -1;
}
//if the tree has only one node (a root), then the height is 0
else if(size()== 1){
//if the tree has only one node (a root) or none , then the height is 0
if(empty() || size()== 1){
height = 0;
}
//else, find the height of the tree
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
@@ -19,28 +19,24 @@ int main() {
/* Create an STL vector of some ints */
/*test*/
/* vector<int> v;
vector<int> v;
v.push_back(3);
v.push_back(4);
v.push_back(1);
v.push_back(100);
v.push_back(-33);
BSTNode<int> * node(3);
node->left = BSTNode<int> * left(1);
node->right = BSTNode<int> * right(4);
BSTNode<int> * result = node.successor();
cout << result->data << endl;
*/
/* UNCOMMENT THE LINES BELOW TO TEST THE TEMPLATE-BASED ITERATOR */
/*
// Test the template version of the BST with ints
BST<int> btemp;
for (int item : v) {
// The auto type here is the pair of BSTIterator<int>, bool
cout << "Inserting " << item << " into the int template-based BST...";
auto p = btemp.insert(item);
cout << "did you reach here" << endl;
if (*(p.first) != item) {
cout << "Wrong iterator returned. "
<< "Expected " << item << " but got " << *(p.first) << endl;
@@ -102,8 +98,6 @@ int main() {
cout << "success!" << endl;
*/
// ADD MORE TESTS HERE. You might also want to change what is input
// into the vector v.
View
Binary file not shown.

0 comments on commit e02c12b

Please sign in to comment.