adding cs235

This commit is contained in:
dm
2016-04-06 20:46:10 -07:00
parent 8e52ce1982
commit cf99ec6565
178 changed files with 13079 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
//YOU MAY NOT MODIFY THIS DOCUMENT
#pragma once
#include "NodeInterface.h"
using namespace std;
class AVLInterface {
public:
AVLInterface() {}
virtual ~AVLInterface() {}
//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 AVL tree
* Rebalances the tree if data is successfully added
*
* @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 AVL tree
* Rebalances the tree if data is successfully removed
*
* @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;
/*
* ROTATIONS:
* LEFT
*
* IDENTIFY NODES TO ROTATE
*
* ROT.RIGHT=CUR.LEFT
* CUR.LEFT=ROT
* CUR=ROT
*
*
*
*
* RIGHT
*
*
*
*/
};
+20
View File
@@ -0,0 +1,20 @@
#include "Factory.h"
#include "avl.h"
//You may add #include statements here
/*
You will MODIFY THIS DOCUMENT.
*/
//=======================================================================================
/*
getAVL()
Creates and returns an object whose class extends AVLInterface.
This should be an object of a class you have created.
Example: If you made a class called "AVL", you might say, "return new AVL();".
*/
AVLInterface * Factory::getAVL()
{
return new avl();//Modify this line
}
+23
View File
@@ -0,0 +1,23 @@
#pragma once
#include "AVLInterface.h"
using namespace std;
/*
WARNING: It is expressly forbidden to modify any part of this document, including its name
*/
//=======================================================================================
/*
getAVL()
Creates and returns an object whose class extends AVLInterface.
This should be an object of a class you have created.
Example: If you made a class called "AVL", you might say, "return new AVL();".
*/
class Factory
{
public:
static AVLInterface * getAVL();
};
//=======================================================================================
+32
View File
@@ -0,0 +1,32 @@
CXXFLAGS= -Wall -g -std=c++0x
OBJECTS=Factory.o avl.o node.o main.o pwnd.o
EXE=main
all: $(EXE)
$(EXE): $(OBJECTS)
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@
test: test.cpp avl.o node.o
rtest: test
./test
Factory.o: Factory.cpp Factory.h
avl.o: avl.cpp avl.h
node.o: node.cpp node.h
main.o: main.cpp
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)
+40
View File
@@ -0,0 +1,40 @@
//YOU MAY NOT MODIFY THIS DOCUMENT
#pragma once
#include<string>
using namespace std;
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;
/*
* Returns the height of this node. The height is the number of edges
* from this node to this nodes farthest child.
*/
virtual int getHeight() = 0;
};
+383
View File
@@ -0,0 +1,383 @@
#include "avl.h"
avl::avl(): root(NULL) {}
avl::~avl() {
deltree(root);
}
void avl::deltree (node* root) {
// if(root != NULL) {
// deltree(root->left);
// deltree(root->right);
// }
// delete root;
}
NodeInterface* avl::getRootNode() {
return root;
}
bool avl::add(int data) {
bool found = 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;
}
root = build(root, data);
return true;
}
node* avl::build(node* root, int data) {
node* temp1 = NULL;
node* temp2 = NULL;
if(root == NULL) {
root = new node(data);
h = true;
return root;
}
if(data < root->data) {
root->left = build(root->left, data);
if(h) {
switch(root->height) {
case 1:
temp1 = root->left;
if(temp1->height == 1) {
root->left = temp1->right;
temp1->right = root;
root->height = 0;
root = temp1;
}
else {
temp2 = temp1->right;
temp1->right = temp2->left;
temp2->left = temp1;
root->left = temp2->right;
temp2->right = root;
if(temp2->height == 1) {
root->height = -1;
}
else {
root->height = 0;
}
if(temp2->height == -1) {
temp1->height = 1;
}
else {
temp1->height = 0;
}
root = temp2;
}
root->height = 0;
h = false;
break;
case 0:
root->height = 1;
break;
case -1:
root->height = 0;
h = false;
}
}
}
if(data > root->data) {
root->right = build(root->right, data);
if(h) {
switch(root->height) {
case 1:
root->height = 0;
h = false;
break;
case 0:
root->height = -1;
break;
case -1:
temp1 = root->right;
if(temp1->height == -1) {
root->right = temp1->left;
temp1->left = root;
root->height = 0;
root = temp1;
}
else {
temp2 = temp1->left;
temp1->left = temp2->right;
temp2->right = temp1;
root->right = temp2->left;
temp2->left = root;
if(temp2->height == -1) {
root->height = 1;
}
else {
root->height = 0;
}
if(temp2->height == 1) {
temp1->height = -1;
}
else {
temp1->height = 0;
}
root = temp2;
}
root->height = 0;
h = false;
}
}
}
return root;
}
bool avl::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;
}
root = del_leaf(root, data);
return true;
}
node* avl::del_leaf(node* root, int data) {
node* temp;
if(root == NULL) {
return root;
}
else {
if(data < root->data) {
root->left = del_leaf(root->left, data);
if(h) {
root = bal_right(root);
}
}
else {
if(data > root->data) {
root->right = del_leaf(root->right, data);
if(h) {
root = bal_left(root);
}
}
else {
temp = root;
if(temp->right == NULL) {
root = temp->left;
h = true;
delete temp;
}
else {
if(temp->left == NULL) {
root = temp->right;
h = true;
delete temp;
}
else {
temp->left = del(temp->left, temp);
if(h) {
root = bal_right(root);
}
}
}
}
}
}
return root;
}
node* avl::del(node* succ, node* n) {
node* temp = succ;
if(succ->right != NULL) {
succ->right = del(succ->right, n);
if(h) {
succ = bal_left(succ);
}
}
else {
node* current;
node* parent;
current = root;
while(current) {
if(current->data == succ->data) {
break;
}
else {
parent = current;
if(succ->data > current->data) {
current = current->right;
}
else if(succ->data < current->data) {
current = current->left;
}
}
}
if(root->left->data == succ->data) {
root->data = succ->data;
n->right = root->right;
succ = succ->left;
delete temp;
h = true;
return succ;
}
else if(root->right->data == succ->data) {
root->data = succ->data;
n->right = root->right;
succ = succ->right;
delete temp;
h = false;
return succ;
}
temp = succ;
n->data = succ->data;
succ = succ->left;
delete temp;
h = true;
}
return succ;
}
node* avl::bal_right(node* root) {
node* temp1 = NULL;
node* temp2 = NULL;
switch(root->height) {
case 1:
root->height = 0;
break;
case 0:
root->height = -1;
h = false;
break;
case -1:
temp1 = root->right;
if(temp1->height <= 0) {
root->right = temp1->left;
temp1->left = root;
if(temp1->height == 0) {
root->height = -1;
temp1->height = 1;
h = false;
}
else {
root->height = 0;
temp1->height = 0;
}
root = temp1;
}
else {
temp2 = temp1->left;
temp1->left = temp2->right;
temp2->right = temp1;
root->right = temp2->left;
temp2->left = root;
if(temp2->height == -1) {
root->height = 1;
}
else {
root->height = 0;
}
if(temp2->height == 1) {
temp1->height = -1;
}
else {
temp1->height = 0;
}
root = temp2;
temp2->height = 0;
}
}
return root;
}
node* avl::bal_left(node* root) {
node* temp1 = NULL;
node* temp2 = NULL;
switch(root->height) {
case -1:
root->height = 0;
break;
case 0:
root->height = 1;
h = false;
break;
case 1:
temp1 = root->left;
if(temp1->height >= 0) {
root->left = temp1->right;
temp1->right = root;
if(temp1->height == 0) {
root->height = 1;
temp1->height = -1;
h = false;
}
else {
root->height = 0;
temp1->height = 0;
}
root = temp1;
}
else {
temp2 = temp1->right;
temp1->right = temp2->left;
temp2->left = temp1;
root->left = temp2->right;
temp2->right = root;
if(temp2->height == 1) {
root->height = -1;
}
else {
root->height = 0;
}
if(temp2->height == -1) {
temp1->height = 1;
}
else {
temp1->height = 0;
}
root = temp2;
temp2->height = 0;
}
}
return root;
}
void avl::display(node* root) {
if(root != NULL) {
display(root->left);
cout << root->data << "\t";
display(root->right);
}
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef __AVL_H__
#define __AVL_H__
#include "AVLInterface.h"
#include "node.h"
#include <iostream>
class avl : public AVLInterface {
public:
avl();
~avl();
node* root;
bool h;
NodeInterface * getRootNode();
void deltree (node* root);
node* build(node* root, int data);
bool add(int data);
node* del_leaf(node* root, int data);
void display(node* root);
node* del(node* succ, node* n);
node* bal_left(node* root);
node* bal_right(node* root);
bool remove(int data);
};
#endif
+24
View File
@@ -0,0 +1,24 @@
#include <iostream>
#include "avl.h"
using namespace std;
int main() {
avl a;
for(int i = 50; i > 0; i--) {
a.add(i);
a.add(-i);
}
for(int i = 0; i < 5; i++) {
a.remove(i);
a.remove(-i);
}
a.remove(5);
// for(int i = 0; i < 50; i++) {
// a.remove(i);
// a.remove(-i);
// }
}
+19
View File
@@ -0,0 +1,19 @@
#include "node.h"
node::node(int data): data(data), height(0), right(NULL), left(NULL) {}
int node::getData() {
return data;
}
NodeInterface* node::getLeftChild() {
return left;
}
NodeInterface* node::getRightChild() {
return right;
}
int node::getHeight() {
return height;
}
+18
View File
@@ -0,0 +1,18 @@
#ifndef __NODE_H__
#define __NODE_H__
#include "NodeInterface.h"
class node : public NodeInterface {
public:
int data;
int height;
node* right;
node* left;
node(int data);
int getData();
NodeInterface * getLeftChild();
NodeInterface * getRightChild();
int getHeight();
};
#endif
+5
View File
@@ -0,0 +1,5 @@
#include <unistd.h>
int usleep(useconds_t usec) {
return 0;
}