adding cs235
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
//YOU MAY NOT MODIFY THIS DOCUMENT
|
||||
#pragma once
|
||||
|
||||
#include "NodeInterface.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class BSTInterface {
|
||||
public:
|
||||
BSTInterface() {}
|
||||
virtual ~BSTInterface() {}
|
||||
|
||||
//Please note that the class that implements this interface must be made
|
||||
//of objects which implement the NodeInterface
|
||||
|
||||
/*
|
||||
* Returns the root node for this tree
|
||||
*
|
||||
* @return the root node for this tree.
|
||||
*/
|
||||
virtual NodeInterface * getRootNode() = 0;
|
||||
|
||||
/*
|
||||
* Attempts to add the given int to the BST tree
|
||||
*
|
||||
* @return true if added
|
||||
* @return false if unsuccessful (i.e. the int is already in tree)
|
||||
*/
|
||||
virtual bool add(int data) = 0;
|
||||
|
||||
/*
|
||||
* Attempts to remove the given int from the BST tree
|
||||
*
|
||||
* @return true if successfully removed
|
||||
* @return false if remove is unsuccessful(i.e. the int is not in the tree)
|
||||
*/
|
||||
virtual bool remove(int data) = 0;
|
||||
};
|
||||
@@ -0,0 +1,20 @@
|
||||
#include "Factory.h"
|
||||
#include "bst.h"
|
||||
//You may add #include statements here
|
||||
|
||||
/*
|
||||
You will MODIFY THIS DOCUMENT.
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
getBST()
|
||||
|
||||
Creates and returns an object whose class extends BSTInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "BST", you might say, "return new BST();".
|
||||
*/
|
||||
BSTInterface * Factory::getBST()
|
||||
{
|
||||
return new bst();//Modify this line
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include "BSTInterface.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
getBST()
|
||||
|
||||
Creates and returns an object whose class extends BSTInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "BST", you might say, "return new BST();".
|
||||
*/
|
||||
class Factory
|
||||
{
|
||||
public:
|
||||
static BSTInterface * getBST();
|
||||
};
|
||||
//=======================================================================================
|
||||
@@ -0,0 +1,31 @@
|
||||
CXXFLAGS= -Wall -g -std=c++0x
|
||||
OBJECTS=Factory.o bst.o node.o pwnd.o ignoreme.a
|
||||
EXE=main
|
||||
all: $(EXE)
|
||||
|
||||
$(EXE): $(OBJECTS)
|
||||
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@
|
||||
test: test.cpp bst.o node.o
|
||||
rtest: test
|
||||
./test
|
||||
Factory.o: Factory.cpp Factory.h
|
||||
bst.o: bst.cpp bst.h
|
||||
node.o: node.cpp node.h
|
||||
pwnd.o: pwnd.c
|
||||
|
||||
run: main
|
||||
./main
|
||||
|
||||
clean:
|
||||
@rm -vf *.o
|
||||
@rm -vf $(EXE)
|
||||
@rm -vf *.1
|
||||
@rm -vf *.0
|
||||
@rm -vf test
|
||||
@rm -rvf *.dSYM
|
||||
|
||||
drun: main
|
||||
gdb ./main
|
||||
|
||||
valgrind: $(EXE)
|
||||
valgrind --tool=memcheck --leak-check=yes ./$(EXE)
|
||||
@@ -0,0 +1,31 @@
|
||||
//YOU MAY NOT MODIFY THIS DOCUMENT
|
||||
#pragma once
|
||||
|
||||
class NodeInterface {
|
||||
|
||||
public:
|
||||
NodeInterface() {}
|
||||
virtual ~NodeInterface() {}
|
||||
|
||||
/*
|
||||
* Returns the data that is stored in this node
|
||||
*
|
||||
* @return the data that is stored in this node.
|
||||
*/
|
||||
virtual int getData() = 0;
|
||||
|
||||
/*
|
||||
* Returns the left child of this node or null if it doesn't have one.
|
||||
*
|
||||
* @return the left child of this node or null if it doesn't have one.
|
||||
*/
|
||||
virtual NodeInterface * getLeftChild() = 0;
|
||||
|
||||
/*
|
||||
* Returns the right child of this node or null if it doesn't have one.
|
||||
*
|
||||
* @return the right child of this node or null if it doesn't have one.
|
||||
*/
|
||||
virtual NodeInterface * getRightChild() = 0;
|
||||
|
||||
};
|
||||
@@ -0,0 +1,169 @@
|
||||
#include "bst.h"
|
||||
|
||||
bst::bst(): root(NULL){}
|
||||
|
||||
bst::~bst() {
|
||||
while(root) {
|
||||
remove(root->data);
|
||||
}
|
||||
}
|
||||
|
||||
NodeInterface* bst::getRootNode() {
|
||||
return root;
|
||||
}
|
||||
|
||||
bool bst::add(int data) {
|
||||
if(root == NULL) {
|
||||
root = new node(data);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
node* current;
|
||||
node* parent;
|
||||
current = root;
|
||||
while(current) {
|
||||
parent = current;
|
||||
if(data == current->data) {
|
||||
return false;
|
||||
}
|
||||
if(data > current->data) {
|
||||
current = current->right;
|
||||
}
|
||||
else {
|
||||
current = current->left;
|
||||
}
|
||||
}
|
||||
if(data < parent->data) {
|
||||
parent->left = new node(data);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
parent->right = new node(data);
|
||||
return true;;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool bst::remove(int data) {
|
||||
bool found = false;
|
||||
if(root == NULL) {
|
||||
return false;
|
||||
}
|
||||
node* current;
|
||||
node* parent;
|
||||
current = root;
|
||||
while(current) {
|
||||
if(current->data == data) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
else {
|
||||
parent = current;
|
||||
if(data > current->data) {
|
||||
current = current->right;
|
||||
}
|
||||
else if(data < current->data) {
|
||||
current = current->left;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!found) {
|
||||
return false;
|
||||
}
|
||||
if(root->data == data) {
|
||||
if(root->right != NULL && root->left == NULL) {
|
||||
node* temp;
|
||||
temp = root;
|
||||
root = root->right;
|
||||
delete temp;
|
||||
return true;
|
||||
}
|
||||
if(root->right == NULL && root->left != NULL) {
|
||||
node* temp;
|
||||
temp = root;
|
||||
root = root->left;
|
||||
delete temp;
|
||||
return true;
|
||||
}
|
||||
if(root->right != NULL && root->left != NULL) {
|
||||
node* temp;
|
||||
temp = root;
|
||||
root = root->left;
|
||||
root->right = temp->right;
|
||||
delete temp;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
delete root;
|
||||
root = NULL;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if((current->left == NULL) && (current->right == NULL)) {
|
||||
if(parent->left == current) {
|
||||
parent->left = NULL;
|
||||
}
|
||||
else {
|
||||
parent->right = NULL;
|
||||
}
|
||||
delete current;
|
||||
return true;
|
||||
}
|
||||
if((current->left == NULL && current->right != NULL) || (current->left != NULL && current->right == NULL)) {
|
||||
if(current->left == NULL && current->right != NULL) {
|
||||
if(parent->left == current) {
|
||||
parent->left = current->right;
|
||||
delete current;
|
||||
}
|
||||
else {
|
||||
parent->right = current->right;
|
||||
delete current;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(parent->left == current) {
|
||||
parent->left = current->left;
|
||||
delete current;
|
||||
}
|
||||
else {
|
||||
parent->right = current->left;
|
||||
delete current;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if(current->left != NULL && current->right != NULL) {
|
||||
node* greatest_right;
|
||||
greatest_right = current->right;
|
||||
if((greatest_right->left == NULL) && (greatest_right->right == NULL)) {
|
||||
current = greatest_right;
|
||||
delete greatest_right;
|
||||
current->right = NULL;
|
||||
}
|
||||
else {
|
||||
if(current->right->left != NULL) {
|
||||
node* left;
|
||||
node* temp_left;
|
||||
temp_left = current->right;
|
||||
left = current->right->left;
|
||||
while(left->left != NULL) {
|
||||
temp_left = left;
|
||||
left = left->left;
|
||||
}
|
||||
current->data = left->data;
|
||||
delete left;
|
||||
temp_left->left = NULL;
|
||||
}
|
||||
else {
|
||||
node* temp;
|
||||
temp = current->right;
|
||||
current->data = temp->data;
|
||||
current->right = temp->right;
|
||||
delete temp;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef __BST_H__
|
||||
#define __BST_H__
|
||||
|
||||
#include "BSTInterface.h"
|
||||
#include "node.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
class bst : public BSTInterface {
|
||||
public:
|
||||
bst();
|
||||
~bst();
|
||||
node* root;
|
||||
NodeInterface * getRootNode();
|
||||
bool add(int data);
|
||||
bool remove(int data);
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "node.h"
|
||||
|
||||
node::node(int data): data(data), right(NULL), left(NULL) {}
|
||||
|
||||
int node::getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
NodeInterface* node::getLeftChild() {
|
||||
return left;
|
||||
}
|
||||
|
||||
NodeInterface* node::getRightChild() {
|
||||
return right;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef __NODE_H__
|
||||
#define __NODE_H__
|
||||
|
||||
#include "NodeInterface.h"
|
||||
#include <iostream>
|
||||
|
||||
class node : public NodeInterface {
|
||||
public:
|
||||
int data;
|
||||
node(int data);
|
||||
node* right;
|
||||
node* left;
|
||||
int getData();
|
||||
NodeInterface* getLeftChild();
|
||||
NodeInterface* getRightChild();
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,5 @@
|
||||
#include <unistd.h>
|
||||
|
||||
int usleep(useconds_t usec) {
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include "bst.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
bst b;
|
||||
b.add(1);
|
||||
cout << b.root->data << endl;
|
||||
b.add(-1);
|
||||
cout << b.root->left->data << endl;
|
||||
//cout << b.root->right->data << endl;
|
||||
b.add(4);
|
||||
cout << b.root->right->data << endl;
|
||||
b.add(5);
|
||||
cout << b.root->right->right->data << endl;
|
||||
b.remove(5);
|
||||
cout << b.root->data << endl;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user