Browse Source

adding cs235

master
Derek McQuay 4 years ago
parent
commit
cf99ec6565
100 changed files with 8920 additions and 0 deletions
  1. +26
    -0
      cs235/exam1/Makefile
  2. +68
    -0
      cs235/exam1/PolynomialListInterface.h
  3. +84
    -0
      cs235/exam1/PolynomialManagerInterface.h
  4. +433
    -0
      cs235/exam1/main.cpp
  5. +42
    -0
      cs235/exam1/node.cpp
  6. +19
    -0
      cs235/exam1/node.h
  7. +438
    -0
      cs235/exam1/polylist.cpp
  8. +31
    -0
      cs235/exam1/polylist.h
  9. +107
    -0
      cs235/exam1/polyman.cpp
  10. +22
    -0
      cs235/exam1/polyman.h
  11. +31
    -0
      cs235/final/Makefile
  12. +43
    -0
      cs235/final/RedBlackNodeInterface.h
  13. +20
    -0
      cs235/final/RedBlackTreeFactory.cpp
  14. +23
    -0
      cs235/final/RedBlackTreeFactory.h
  15. +78
    -0
      cs235/final/RedBlackTreeInterface.h
  16. +21
    -0
      cs235/final/TwoThreeTreeFactory.cpp
  17. +25
    -0
      cs235/final/TwoThreeTreeFactory.h
  18. +58
    -0
      cs235/final/TwoThreeTreeInterface.h
  19. +19
    -0
      cs235/final/br_node.cpp
  20. +21
    -0
      cs235/final/br_node.h
  21. +500
    -0
      cs235/final/br_tree.cpp
  22. +34
    -0
      cs235/final/br_tree.h
  23. +235
    -0
      cs235/final/main.cpp
  24. +7
    -0
      cs235/final/test.cpp
  25. BIN
      cs235/lab02/Lab 2 README.pdf
  26. +33
    -0
      cs235/lab02/Run Test Driver.bat
  27. +20
    -0
      cs235/lab02/Student_Code/Factory.cpp
  28. +23
    -0
      cs235/lab02/Student_Code/Factory.h
  29. +54
    -0
      cs235/lab02/Student_Code/JosephusInterface.h
  30. +25
    -0
      cs235/lab02/Student_Code/Makefile
  31. +88
    -0
      cs235/lab02/Student_Code/circle.cpp
  32. +19
    -0
      cs235/lab02/Student_Code/circle.h
  33. +1974
    -0
      cs235/lab02/Student_Code/main.cpp
  34. +661
    -0
      cs235/lab03/DONT_DELETE.supp
  35. +16
    -0
      cs235/lab03/Run_Test_Driver.sh
  36. +21
    -0
      cs235/lab03/Student_Code/Factory.cpp
  37. +23
    -0
      cs235/lab03/Student_Code/Factory.h
  38. +80
    -0
      cs235/lab03/Student_Code/LinkedListInterface.h
  39. +37
    -0
      cs235/lab03/Student_Code/Makefile
  40. +158
    -0
      cs235/lab03/Student_Code/linkedlist.cpp
  41. +25
    -0
      cs235/lab03/Student_Code/linkedlist.h
  42. +24
    -0
      cs235/lab03/Student_Code/main.cpp
  43. +3
    -0
      cs235/lab03/Student_Code/node.cpp
  44. +14
    -0
      cs235/lab03/Student_Code/node.h
  45. +5
    -0
      cs235/lab03/Student_Code/pwnd.c
  46. +37
    -0
      cs235/lab03/Student_Code/test.cpp
  47. +21
    -0
      cs235/lab03/sm/Factory.cpp
  48. +23
    -0
      cs235/lab03/sm/Factory.h
  49. +80
    -0
      cs235/lab03/sm/LinkedListInterface.h
  50. +27
    -0
      cs235/lab03/sm/Makefile
  51. +158
    -0
      cs235/lab03/sm/ll.cc
  52. +42
    -0
      cs235/lab03/sm/ll.h
  53. +5
    -0
      cs235/lab03/sm/pwnd.c
  54. +174
    -0
      cs235/lab03/sm/test.cc
  55. +65
    -0
      cs235/lab04/ExpressionManagerInterface.h
  56. +22
    -0
      cs235/lab04/Factory.cpp
  57. +22
    -0
      cs235/lab04/Factory.h
  58. +35
    -0
      cs235/lab04/Makefile
  59. +32
    -0
      cs235/lab04/Run Test Driver.bat
  60. +7
    -0
      cs235/lab04/bin/runner.py
  61. +274
    -0
      cs235/lab04/expman.cpp
  62. +28
    -0
      cs235/lab04/expman.h
  63. BIN
      cs235/lab04/ignoreme.lib
  64. +165
    -0
      cs235/lab04/lab04.html
  65. +13
    -0
      cs235/lab04/lab04_files/labs.css
  66. +344
    -0
      cs235/lab04/lab04_files/main.css
  67. BIN
      cs235/lab04/lab04_files/title.png
  68. +14
    -0
      cs235/lab04/main.cpp
  69. +5
    -0
      cs235/lab04/pwnd.c
  70. +11
    -0
      cs235/lab04/setup.py
  71. +23
    -0
      cs235/lab04/shunting/__init__.py
  72. +10
    -0
      cs235/lab04/shunting/test_shunt.py
  73. +117
    -0
      cs235/lab04/test.cc
  74. +2
    -0
      cs235/lab04/test1.txt
  75. +4
    -0
      cs235/lab04/tests/part1.txt
  76. +4
    -0
      cs235/lab04/tests/part2.txt
  77. +3
    -0
      cs235/lab04/tests/part3.txt
  78. +3
    -0
      cs235/lab04/tests/part4.txt
  79. +661
    -0
      cs235/lab04/valgrind.supp
  80. +22
    -0
      cs235/lab05/Factory.cpp
  81. +21
    -0
      cs235/lab05/Factory.h
  82. +35
    -0
      cs235/lab05/Makefile
  83. +14
    -0
      cs235/lab05/Run_Test_Driver.sh
  84. +160
    -0
      cs235/lab05/StationInterface.h
  85. +98
    -0
      cs235/lab05/StationInterfaceExtra.h
  86. +60
    -0
      cs235/lab05/deque.cpp
  87. +18
    -0
      cs235/lab05/deque.h
  88. +52
    -0
      cs235/lab05/irdeque.cpp
  89. +17
    -0
      cs235/lab05/irdeque.h
  90. +102
    -0
      cs235/lab05/linkedlist.cpp
  91. +24
    -0
      cs235/lab05/linkedlist.h
  92. +4
    -0
      cs235/lab05/main.cpp
  93. +3
    -0
      cs235/lab05/node.cpp
  94. +14
    -0
      cs235/lab05/node.h
  95. +46
    -0
      cs235/lab05/ordeque.cpp
  96. +16
    -0
      cs235/lab05/ordeque.h
  97. +5
    -0
      cs235/lab05/pwnd.c
  98. +38
    -0
      cs235/lab05/queue.cpp
  99. +15
    -0
      cs235/lab05/queue.h
  100. +37
    -0
      cs235/lab05/stack.cpp

+ 26
- 0
cs235/exam1/Makefile View File

@@ -0,0 +1,26 @@
CXXFLAGS= -Wall -g -std=c++0x
OBJECTS=main.o node.o polylist.o polyman.o
EXE=main

all: $(EXE)


$(EXE): main.o
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@
main.o: main.cpp node.o polylist.o polyman.o
polylist.o: polylist.cpp polylist.h
polyman.o: polyman.cpp polyman.h
node.o: node.cpp node.h

run: $(EXE)
@./$(EXE)

clean:
@rm -vf *.o
@rm -vf $(EXE)

drun: $(EXE)
gdb ./$(EXE)

valgrind: $(EXE)
valgrind --tool=memcheck --leak-check=yes ./$(EXE)

+ 68
- 0
cs235/exam1/PolynomialListInterface.h View File

@@ -0,0 +1,68 @@
//YOU MAY NOT MODIFY THIS DOCUMENT
#pragma once
#include <string>

using namespace std;

class PolynomialListInterface
{

public:
/*
* You must create a Node class that will hold each polynomial segment and will be used to create the
* links in your PolynomialList.
* Your Node will likely have 3 private variables in addition to a Node * next you may have.
* These 3 variables will be for the Coefficient, the Variable, and the Exponent respectively.
* i.e. 2x^7
* Coefficient = 2
* Variable = x
* Exponent = 7
*/
PolynomialListInterface(void){}
virtual ~PolynomialListInterface(void){}
/*
insert

A node with the given term should be inserted at the appropriate spot in the list.
A term can be accepted only with Coefficient value > 0, and exponent value non-negative.
If the term is invalid, do not add it to the list.
i.e. If the given term segment is 3x^5 and your list contains 2x^8, x^4, 11x^2
the node should be added after 2x^8. Your updated list should look like 2x^8, 3x^5, x^4, 11x^2.

*/
virtual void insert(string term) = 0;

/*
clear

Remove all nodes from the list.
*/
virtual void clear() = 0;

/*
at

Returns the polynomial of the node at the given index. The list begins at
index 0.

If the given index is out of range of the list, return "invalid";
*/
virtual string at(int index) = 0;

/*
size

Returns the number of nodes in the list.
*/
virtual int size() = 0;

/*
* This function should string together all of the nodes and print out the entire polynomial separated with '+' signs.
* If an exponent of 0 exists however, you should print only the Coefficient.
* i.e. list of Nodes = (2x^4)->(4x^2)->(3x^1)->(11x^0)
* printList() = return "2 x ^ 4 + 4 x ^ 2 + 3 x ^ 1 + 11 x";
*/
virtual string printList() = 0;

};

+ 84
- 0
cs235/exam1/PolynomialManagerInterface.h View File

@@ -0,0 +1,84 @@
//YOU MAY NOT MODIFY THIS DOCUMENT
#ifndef POLYNOMIALMANAGERINTERFACE_H_
#define POLYNOMIALMANAGERINTERFACE_H_
#include "PolynomialListInterface.h"
#include <string>

using namespace std;

class PolynomialManagerInterface {
public:
PolynomialManagerInterface(){}
virtual ~PolynomialManagerInterface(){}

/*
* addLists()
* This function goes through the process of adding your two list's together and returning the resulting list.
* Like terms should be added together correctly, creating one Node for the final list.
* If both of the lists are empty, return an empty list. This list must be sorted in correct polynomial order by degree.
* i.e.
* (2x^4)->(4x^2)->(3x^1)->(11x^0), not (2x^4)->(3x^1)->(11x^0)->(4x^2) or anything else.
*/
virtual PolynomialListInterface * addLists() = 0;

/*
* subtractLists()
* This function goes through the process of subtracting your list two from your list one and returning the resulting list.
* Like terms should be subtracted correctly, creating one Node for the final list. If a resulting coefficient is 0, that node should be removed.
* If both of the lists are empty, return an empty list. This list must be sorted in correct polynomial order by degree.
* i.e.
* (2x^4)->(4x^2)->(3x^1)->(11x^0), not (2x^4)->(3x^1)->(11x^0)->(4x^2) or anything else.
*/
virtual PolynomialListInterface * subtractLists() = 0;

/*
* fillListOne(string term)
* This adds a single term at a time.
* This function will be called many times while your first list is being filled.
* You should add the new term at the appropriate spot in the list
* which may involve adding it to an existing polynomial list, the list should be ordered by degrees.
* All segments will have at least a variable and an exponent, however the Coefficient may be an implied 1.
* i.e. x^6 is valid, but 2x is not.
* If the term is not valid, it should not be added to the list.
*
*/
virtual void fillListOne(string term) = 0;

/*
* fillListTwo(string term)
* This adds a single term at a time.
* This function will be called many times while your second list is being filled.
* You should add the new term at the appropriate spot in the list
* which may involve adding it to an existing polynomial list, the list should be ordered by degrees.
* All segments will have at least a variable and an exponent, however the Coefficient may be an implied 1.
* i.e. x^6 is valid, but 2x is not.
* If the term is not valid, it should not be added to the list.
*/
virtual void fillListTwo(string term) = 0;

/*
* clearListOne()
* This function should empty your first list entirely.
*/
virtual void clearListOne() = 0;

/*
* clearListTwo()
* This function should empty your second list entirely.
*/
virtual void clearListTwo() = 0;

/*
* getListOne()
* Returns a pointer to the first list.
*/
virtual PolynomialListInterface * getListOne() = 0;

/*
* getListTwo()
* Returns a pointer to the second list.
*/
virtual PolynomialListInterface * getListTwo() = 0;
};

#endif /* POLYNOMIALMANAGERINTERFACE_H_ */

+ 433
- 0
cs235/exam1/main.cpp View File

@@ -0,0 +1,433 @@
#include <iostream>
#include <cassert>
#include <string>
#include <sstream>
#include "node.h"
#include "polylist.h"
#include "polyman.h"

using namespace std;

int main() {
//node::node(const int exponent, const int coefficient, const char variable, node* next) :
node n(0,0,'x',NULL);
node n1(0,1,'x',NULL);
node n2(1,0,'x',NULL);
node n3(1,1,'x',NULL);
node n4(1,'x',NULL);
node n5(0,'x',NULL);
node n6(1,'x',NULL);

stringstream s;

s << n;
assert(s.str() == "");
s.str("");

s << n1;
assert(s.str() == "1");
s.str("");

s << n2;
assert(s.str() == "");
s.str("");

s << n3;
assert(s.str() == "x");
s.str("");

string a1 = "1 x ^ 1";
vector<string> v = parse_expression(a1);
assert(v[0] == "1");
assert(v[1] == "x");
assert(v[3] == "1");
assert(is_valid(v) == true);
string a2 = "123 g ^ 152";
v = parse_expression(a2);
assert(is_valid(v) == true);

string a3 = "a b c";
v = parse_expression(a3);
assert(is_valid(v) == false);

string a4 = "";
v = parse_expression(a4);
assert(is_valid(v) == false);
string a5 = "1 x";
v = parse_expression(a5);
assert(is_valid(v) == true);

string a6 = "x";
v = parse_expression(a6);
assert(is_valid(v) == true);

string a7 = "morethanonecharacter";
v = parse_expression(a7);
assert(is_valid(v) == false);
string a8 = "$";
v = parse_expression(a8);
assert(is_valid(v) == false);

string a9 = "1 & ^ x";
v = parse_expression(a9);
assert(is_valid(v) == false);

string b1 = "123 g ^ %";
v = parse_expression(b1);
assert(is_valid(v) == false);

string b2 = "1r32 g ^ 152";
v = parse_expression(b2);
assert(is_valid(v) == false);
string b3 = "123 ^ 152";
v = parse_expression(b3);
assert(is_valid(v) == false);

string b4 = "123 g ^";
v = parse_expression(b4);
assert(is_valid(v) == false);

string b5 = "5 x ^ -2";
v = parse_expression(b5);
assert(is_valid(v) == false);
polylist p;
stringstream sr;

p.insert("x");
sr << p.printList();
assert(sr.str() == "1");

p.insert("2 x");
sr.str("");
sr << p.printList();
assert(sr.str() == "3");

p.insert("-5 x");
sr.str("");
sr << p.printList();
assert(sr.str() == "-2");

assert(p.size() == 1);

p.insert("x ^ 1");
sr.str("");
sr << p.printList();
assert(sr.str() == "x + -2");

assert(p.size() == 2);

p.insert("x ^ 4");
sr.str("");
sr << p.printList();
assert(sr.str() == "x ^ 4 + x + -2");


assert(p.size() == 3);

p.insert("x ^ 5");
sr.str("");
sr << p.printList();
assert(sr.str() == "x ^ 5 + x ^ 4 + x + -2");

p.insert("x ^ -1");
sr.str("");
sr << p.printList();
assert(sr.str() == "x ^ 5 + x ^ 4 + x + -2");

p.insert("x ^ 4");
sr.str("");
sr << p.printList();
assert(sr.str() == "x ^ 5 + 2 x ^ 4 + x + -2");

p.insert("x ^ 4");
sr.str("");
sr << p.printList();
assert(sr.str() == "x ^ 5 + 3 x ^ 4 + x + -2");

p.insert("x ^ 4");
sr.str("");
sr << p.printList();
assert(sr.str() == "x ^ 5 + 4 x ^ 4 + x + -2");

p.insert("x ^ 5");
sr.str("");
sr << p.printList();
assert(sr.str() == "2 x ^ 5 + 4 x ^ 4 + x + -2");


assert(p.size() == 4);

p.insert("x ^ 1");
sr.str("");
sr << p.printList();
assert(sr.str() == "2 x ^ 5 + 4 x ^ 4 + 2 x + -2");

p.insert("x");
sr.str("");
sr << p.printList();
assert(sr.str() == "2 x ^ 5 + 4 x ^ 4 + 2 x + -1");

p.insert("x ^ 7");
sr.str("");
sr << p.printList();
assert(sr.str() == "x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 2 x + -1");

p.insert("x ^ -7");
sr.str("");
sr << p.printList();
assert(sr.str() == "x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 2 x + -1");

p.insert("x ^ 7");
sr.str("");
sr << p.printList();
assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 2 x + -1");

assert(p.size() == 5);

p.insert("2 x ^ 3");
sr.str("");
sr << p.printList();
assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 2 x ^ 3 + 2 x + -1");


p.insert("2 x ^ 3");
sr.str("");
sr << p.printList();
assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 4 x ^ 3 + 2 x + -1");


p.insert("2 x ^ 2");
sr.str("");
sr << p.printList();
assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 4 x ^ 3 + 2 x ^ 2 + 2 x + -1");

p.insert("2 x ^ 7");
sr.str("");
sr << p.printList();
assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 4 x ^ 3 + 2 x ^ 2 + 2 x + -1");

p.insert("2 x ^ 0");
sr.str("");
sr << p.printList();
assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 4 x ^ 3 + 2 x ^ 2 + 2 x + 1");

p.insert("-4 x ^ 4");
sr.str("");
sr << p.printList();
assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 4 x ^ 3 + 2 x ^ 2 + 2 x + 1");

p.insert("2 x ^ 3");
sr.str("");
sr << p.printList();
assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 1");

p.insert("8 x ^ 4");
sr.str("");
sr << p.printList();
assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 1");

p.insert("2 x ^ -3");
sr.str("");
sr << p.printList();
assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 1");

p.insert("-1 x ^ 3");
sr.str("");
sr << p.printList();
assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 1");

p.insert("-2 x ^ 7");
sr.str("");
sr << p.printList();
assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 1");

p.insert("2 x ^ 9");
sr.str("");
sr << p.printList();
assert(sr.str() == "2 x ^ 9 + 2 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 1");

p.insert("-2 x ^ 9");
sr.str("");
sr << p.printList();
assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 1");

p.insert("10 x ^ 7");
sr.str("");
sr << p.printList();
assert(sr.str() == "12 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 1");

p.insert("x");
sr.str("");
sr << p.printList();
assert(sr.str() == "12 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 2");

p.insert("x ^ 7");
sr.str("");
sr << p.printList();
assert(sr.str() == "13 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 2");

p.insert("-10 x ^ 7");
sr.str("");
sr << p.printList();
assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 2");

p.insert("10 x");
sr.str("");
sr << p.printList();
assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 12");

p.insert("-10 x");
sr.str("");
sr << p.printList();
assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 2");

p.insert("-2 x");
sr.str("");
sr << p.printList();
assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x");

p.insert("2 x ^ 0");
sr.str("");
sr << p.printList();
assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 2");

p.insert("x ^ 3");
sr.str("");
sr << p.printList();
assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 2");

p.insert("");
sr.str("");
sr << p.printList();
assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 2");

p.insert("-3 x ^ 7");
sr.str("");
sr << p.printList();
assert(sr.str() == "2 x ^ 5 + 8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 2");
p.insert("-2 x ^ 5");
sr.str("");
sr << p.printList();
assert(sr.str() == "8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 2");

p.insert("-2 x ^ 2");
sr.str("");
sr << p.printList();
assert(sr.str() == "8 x ^ 4 + 6 x ^ 3 + 2 x + 2");

p.insert("-3 x ^ 7");
sr.str("");
sr << p.printList();
assert(sr.str() == "-3 x ^ 7 + 8 x ^ 4 + 6 x ^ 3 + 2 x + 2");

p.insert("-3 x ^ 7");
sr.str("");
sr << p.printList();
assert(sr.str() == "-6 x ^ 7 + 8 x ^ 4 + 6 x ^ 3 + 2 x + 2");

p.insert("9 x ^ 7");
sr.str("");
sr << p.printList();
assert(sr.str() == "3 x ^ 7 + 8 x ^ 4 + 6 x ^ 3 + 2 x + 2");

p.clear();
sr.str("");
sr << p.printList();
assert(sr.str() == "");

assert(p.size() == 0);

p.insert("9 x ^ 7");
sr.str("");
sr << p.printList();
assert(sr.str() == "9 x ^ 7");

assert(p.size() == 1);

p.clear();
sr.str("");
sr << p.printList();
assert(sr.str() == "");

assert(p.size() == 0);

p.insert("x ^ 7");
sr.str("");
sr << p.printList();
assert(sr.str() == "x ^ 7");

p.insert("-x ^ 7");
sr.str("");
sr << p.printList();
assert(sr.str() == "");

p.clear();
sr.str("");
sr << p.printList();
assert(sr.str() == "");

assert(p.size() == 0);

p.insert("2 x");
sr.str("");
sr << p.printList();
assert(sr.str() == "2");

p.clear();
sr.str("");
sr << p.printList();
assert(sr.str() == "");

assert(p.size() == 0);

p.insert("x");
sr.str("");
sr << p.printList();
assert(sr.str() == "1");

p.remove(0);
sr.str("");
sr << p.printList();
assert(sr.str() == "");

p.insert("-x");
sr.str("");
sr << p.printList();
assert(sr.str() == "-1");

p.insert("2 x");
sr.str("");
sr << p.printList();
assert(sr.str() == "1");

node n7(3, -1, 'x',NULL);

polyman m;
m.addLists();
m.subtractLists();
m.fillListOne("x");
m.fillListOne("2 x");
m.fillListOne("3 x ^ 3");
m.fillListOne("4aqkjx");
m.fillListOne("-4 x ^ 323");
m.fillListOne("-2 x ^ 400");
m.fillListOne("x");
m.fillListOne("x");
m.fillListOne("x");
m.fillListOne("x");
m.fillListTwo("-6 x");
m.fillListTwo("-4 x ^ 323");
m.fillListTwo("-x ^ 3");
m.fillListTwo("7 x ^ 8");
m.fillListTwo("4 x ^ 3");
m.fillListTwo("-x");
m.fillListTwo("14 x");
m.addLists();
m.subtractLists();
}

+ 42
- 0
cs235/exam1/node.cpp View File

@@ -0,0 +1,42 @@
//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1
#include "node.h"

node::node(const int exponent, const int coefficient, const char variable, node* next) :
exponent(exponent), coefficient(coefficient), variable(variable), next(next) {}

node::node(const int exponent, const char variable, node* next) : //construtor for when coefficient is assumed 1
exponent(exponent), coefficient(1), variable(variable), next(next) {}

ostream & operator<<(ostream & os, node n) { //used to correctly print out each node
if(n.coefficient == 0) {
os << "";
}
else if(n.coefficient == 1 && n.exponent == 0) {
os << n.coefficient;
}
else if(n.coefficient == -1 && n.exponent == 0) {
os << n.coefficient;
}
else if(n.exponent == 0) {
os << n.coefficient;
}
else if(n.coefficient == -1) {
os << "-" << n.variable << " ^ " << n.exponent;
}
else if(n.coefficient < 0 && n.exponent == 1) {
os << n.coefficient << n.variable;
}
else if(n.coefficient == 1 && n.exponent == 1) {
os << n.variable;
}
else if(n.exponent == 1) {
os << n.coefficient << " " << n.variable;
}
else if(n.coefficient == 1) {
os << n.variable << " ^ " << n.exponent;
}
else {
os << n.coefficient << " " << n.variable << " ^ " << n.exponent;
}
return os;
}

+ 19
- 0
cs235/exam1/node.h View File

@@ -0,0 +1,19 @@
//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1
#ifndef __NODE_H__
#define __NODE_H__

#include <iostream>

using namespace std;

class node {
public:
node(const int, const int, const char, node*);
node(const int, const char, node*);
int exponent;
int coefficient;
char variable;
node* next;
friend ostream & operator<<(ostream & os, node n);
};
#endif

+ 438
- 0
cs235/exam1/polylist.cpp View File

@@ -0,0 +1,438 @@
//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1
#include "polylist.h"

polylist::polylist(): head(NULL) {}
polylist::~polylist() {clear();}

void polylist::insert(string term) {
vector<string> v = parse_expression(term);
if(!is_valid(v)) {
return;
}
if(v.size() == 1) { //expression of the form x
if(v[0][0] == '-') {
insertTail(0, -1, v[0][1]);
}
else {
insertTail(0, 1, v[0][0]);
}
}
if(v.size() == 2) { // expresion of the form 2 x
int exp = atoi(v[0].c_str());
insertTail(0, exp, v[1][0]);
}
if(v.size() == 3) { // expression of the form x ^ 4
int exp = atoi(v[2].c_str());
int after_exp = 0;
if(exp == 0) {
if(v[0][0] == '-') {
insertTail(exp, -1, v[0][0]);
}
else {
insertTail(exp, 1, v[0][0]);
}
}
node* node_ptr = head;
while(node_ptr != NULL) {
if(node_ptr->exponent <= exp) {
after_exp++;
break;
}
after_exp++;
node_ptr = node_ptr->next;
}
string before = at(after_exp - 1);
vector<string> previous_vector = parse_expression(before);
if(previous_vector.size() == 1) {
if(node_ptr == head) {
if(node_ptr == head && size() == 0) {
if(v[0][0] == '-') {
insertHead(exp, -1, v[0][0]);
}
else {
insertHead(exp, 1, v[0][0]);
}
}
else if(node_ptr->exponent == exp) {
node_ptr->coefficient += 1;
if(node_ptr->coefficient == 0) {
remove(exp);
}
}
else {
if(v[0][0] == '-') {
insertHead(exp, -1, v[0][1]);
}
else {
insertHead(exp, 1, v[0][0]);
}
}
}
else {
node_ptr->coefficient += 1;
if(node_ptr->coefficient == 0) {
remove(exp);
}
}
}
if(previous_vector.size() == 2) {
if(v[0][0] == '-') {
insertHead(exp, -1, v[0][1]);
}
else {
insertHead(exp, 1, v[0][0]);
}
}
if(previous_vector.size() == 3) {
int insertionNodeExponent = atoi(previous_vector[2].c_str());
if(insertionNodeExponent == exp) {
node* node_ptr2 = head;
while(node_ptr2 != NULL) {
if(node_ptr2->exponent == exp) {
if(v[0][0] == '-') {
node_ptr2->coefficient -= 1;
}
else {
node_ptr2->coefficient += 1;
}
if(node_ptr2->coefficient == 0) {
remove(exp);
}
}
node_ptr2 = node_ptr2->next;
}
}
else{
if(v[0][0] == '-') {
insertAfter(exp, -1, v[0][1], insertionNodeExponent);
}
else {
insertAfter(exp, 1, v[0][0], insertionNodeExponent);
}
}
}
if(previous_vector.size() == 4) {
int insertionNodeExponent = atoi(previous_vector[3].c_str());
if(insertionNodeExponent == exp) {
node* node_ptr2 = head;
while(node_ptr2 != NULL) {
if(node_ptr2->exponent == exp) {
node_ptr2->coefficient += 1;
if(node_ptr2->coefficient == 0) {
remove(exp);
}
}
node_ptr2 = node_ptr2->next;
}
}
else {
if(v[0][0] == '-') {
insertAfter(exp, -1, v[0][1], insertionNodeExponent);
}
else {
insertAfter(exp, 1, v[0][0], insertionNodeExponent);
}
}
}
}
if(v.size() == 4) { //expression of the form "2 x ^ 3"
int coef = atoi(v[0].c_str());
int exp = atoi(v[3].c_str());
int after_exp = 0;
node* node_ptr = head;
if(exp == 0) {
insertTail(exp, coef, v[1][0]);
return;
}
while(node_ptr != NULL) {
if(node_ptr->exponent <= exp) {
break;
}
after_exp++;
node_ptr = node_ptr->next;
}
string before = at(after_exp - 1);
vector<string> previous_vector = parse_expression(before);
if(previous_vector.size() == 1) {
if(node_ptr == head) {
if(node_ptr == head && size() == 0) {
insertHead(exp, coef, v[1][0]);
}
else if(node_ptr->exponent == exp) {
node_ptr->coefficient += coef;
if(node_ptr->coefficient == 0) {
remove(exp);
}
}
else {
insertHead(exp, coef, v[1][0]);
}
}
else {
node_ptr->coefficient += coef;
if(node_ptr->coefficient == 0) {
remove(exp);
}
}
}
if(previous_vector.size() == 2) {
insertHead(exp, coef, v[1][0]);
}
if(previous_vector.size() == 3) {
int insertionNodeExponent = atoi(previous_vector[2].c_str());
if(node_ptr->exponent == exp) {
node* node_ptr2 = head;
while(node_ptr2 != NULL) {
if(node_ptr2->exponent == exp) {
node_ptr2->coefficient += coef;
if(node_ptr2->coefficient == 0) {
remove(exp);
}
}
node_ptr2 = node_ptr2->next;
}
}
else{
insertAfter(exp, coef, v[1][0], insertionNodeExponent);
}
}
if(previous_vector.size() == 4) {
int insertionNodeExponent = atoi(previous_vector[3].c_str());
if(node_ptr == NULL) {
insertTail(exp, coef, v[1][0]);
}
else {
if(node_ptr->exponent == exp) {
node* node_ptr2 = head;
while(node_ptr2 != NULL) {
if(node_ptr2->exponent == exp) {
node_ptr2->coefficient += coef;
if(node_ptr2->coefficient == 0) {
remove(exp);
}
}
node_ptr2 = node_ptr2->next;
}
}
else {
insertAfter(exp, coef, v[1][0], insertionNodeExponent);
}
}
}
}
}

void polylist::clear() {
node* node_ptr = head;
while(head) {
node_ptr = head;
head = head->next;
delete node_ptr;
}
}

string polylist::at(int index) {
if(index < 0) {
return "invalid";
}
if(size() <= index) {
return "invalid";
}
node* node_ptr = head;
for(int i = 0; i < index; i++){
node_ptr = node_ptr->next;
}
if(head == NULL)
return "invalid";
stringstream s;
s << *node_ptr;
return s.str();
}

int polylist::size() {
int size_of_list = 0;
node* node_ptr = head;
while(node_ptr != NULL) {
size_of_list++;
node_ptr = node_ptr->next;
}
return size_of_list;
}

string polylist::printList() {
stringstream s;
if(head == NULL)
return "";
node* node_ptr = head;
while(node_ptr != NULL) {
if(node_ptr->next == NULL) {
s << *node_ptr;
return s.str();
}
else {
s << *node_ptr << " + ";
node_ptr = node_ptr->next;
}
}
return s.str();
}

void polylist::remove(int exponent) {
node* node_ptr = head;
if(node_ptr->exponent == exponent) {
node* ptr = head;
head = head->next;
delete ptr;
return;
}
while(node_ptr != NULL) {
if(node_ptr->next == NULL)
return;
if(node_ptr->next->exponent == exponent) {
node* ptr = node_ptr->next;
node_ptr->next = node_ptr->next->next;
delete ptr;
}
node_ptr = node_ptr->next;
}
}

void polylist::insertHead(int exponent, int coefficient, char variable) {
head = new node(exponent, coefficient, variable, head);
}

void polylist::insertTail(int exponent, int coefficient, char variable) {
if(head == NULL) {
insertHead(exponent, coefficient, variable);
return;
}
node* node_ptr = head;
while(node_ptr != NULL) {
if(node_ptr->next == NULL) {
if(node_ptr->exponent == exponent) {
node_ptr->coefficient += coefficient;
if(node_ptr->coefficient == 0) {
remove(exponent);
}
}
else{
node_ptr->next = new node(exponent, coefficient, variable, NULL);
break;
}
}
node_ptr = node_ptr->next;
}
}

void polylist::insertAfter(int exponent, int coefficient, char variable, int insertionNodeExponent) {
node* node_ptr = head;
while(node_ptr != NULL) {
if(node_ptr->exponent == insertionNodeExponent) {
if(head == node_ptr) {
insertHead(exponent, coefficient, variable);
}
else{
node* temp = new node(exponent, coefficient, variable, NULL);
temp->next = node_ptr->next;
node_ptr->next = temp;
}
}
node_ptr = node_ptr->next;
}
}

bool is_valid(vector<string> expression) {
if(expression.empty()) { //if expression is size 0
return false;
}
if(expression.size() == 1) { //if expression is size one. only case if "x"
if(expression[0][0] == '-') {
if(expression[0] == "-x")
return true;
if(!isalpha(expression[0][1]) or expression[0].size() > 1) {
return false;
}
}
else if(!isalpha(expression[0][0]) or expression[0].size() > 1) {
return false;
}
return true;
}
if(expression.size() == 2) { //if expression is size two, only case is of form "2 x"
for(unsigned int i = 0; i < expression[0].size(); i++) {
if(expression[0][0] != '-') {
if(!isdigit(expression[0][i])) {
return false;
}
}
}
if(!isalpha(expression[1][0]) or expression[1].size() > 1) {
return false;
}
return true;
}
if(expression.size() == 3) { //if expression is size three, only case is of form "x ^ 1"
if(expression[0][0] == '-') {
if(expression[0] == "-x")
return true;
if(!isalpha(expression[0][1]) or expression[0].size() > 1) {
return false;
}
}
else if(!isalpha(expression[0][0]) or expression[0].size() > 1) {
return false;
}
if(expression[1] != "^") {
return false;
}
for(unsigned int i = 0; i < expression[2].size(); i++) {
if(!isdigit(expression[2][i])) {
return false;
}
}
return true;
}
if(expression.size() == 4) { //if expression is size four, only case is of form "1 x ^ 1"
for(unsigned int i = 0; i < expression[0].size(); i++) {
if(expression[0][0] != '-') {
if(!isdigit(expression[0][i])) {
return false;
}
}
}
if(!isalpha(expression[1][0]) or expression[1].size() > 1) {
return false;
}
if(expression[2] != "^") {
return false;
}
for(unsigned int i = 0; i < expression[3].size(); i++) {
if(!isdigit(expression[3][i])) {
return false;
}
}
return true;
}
return false;
}

vector<string> parse_expression(string expression) { //parses expression with " " being the delimeter
vector<string> results;
string s;
for(unsigned int i = 0; i < expression.length(); i++) {
char c = expression[i];
if(c != ' ') {
s += c;
}
else {
if(s != "") {
results.push_back(s);
s.clear();
}
}
}
if(s != "") {
results.push_back(s);
}
return results;
}

+ 31
- 0
cs235/exam1/polylist.h View File

@@ -0,0 +1,31 @@
//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1
#ifndef __POLYLIST_H__
#define __POLYLIST_H__
#include "PolynomialListInterface.h"
#include "node.h"
#include <cctype>
#include <string>
#include <sstream>
#include <vector>
#include <iostream>

using namespace std;

class polylist : public PolynomialListInterface {
public:
polylist();
~polylist();
node* head;
void insert(string term);
void clear();
string at(int index);
int size();
string printList();
void remove(int exponent);
void insertAfter(int exponent, int coefficient, char value, int insertionNodeExponent);
void insertTail(int exponent, int coefficient, char value);
void insertHead(int exponent, int coefficient, char value);
};
vector<string> parse_expression(string expression); //these are included outside of the class because i used them in my test.cpp
bool is_valid(vector<string> expression); //and wanted them not to be included in the class
#endif

+ 107
- 0
cs235/exam1/polyman.cpp View File

@@ -0,0 +1,107 @@
//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1
#include "polyman.h"

PolynomialListInterface * polyman::addLists() {
node * node_ptr1 = l1.head;
node * node_ptr2 = l2.head;
polylist l3;
stringstream exp;
while(node_ptr1 != NULL) {
if(node_ptr1->coefficient != 0) {
exp << node_ptr1->coefficient;
exp << " ";
}
exp << node_ptr1->variable;
if(node_ptr1->exponent != 0) {
exp << " ^ ";
exp << node_ptr1->exponent;
}
l3.insert(exp.str());
exp.str("");
node_ptr1 = node_ptr1->next;
}
while(node_ptr2 != NULL) {
if(node_ptr2->coefficient != 0) {
exp << node_ptr2->coefficient;
exp << " ";
}
exp << node_ptr2->variable;
if(node_ptr2->exponent != 0) {
exp << " ^ ";
exp << node_ptr2->exponent;
}
l3.insert(exp.str());
exp.str("");
node_ptr2 = node_ptr2->next;
}
polylist * p = NULL;
p = &l3;
return p;
}

PolynomialListInterface * polyman::subtractLists() {
node * node_ptr1 = l1.head;
node * node_ptr2 = l2.head;
polylist l3;
stringstream exp;
while(node_ptr1 != NULL) {
if(node_ptr1->coefficient != 0) {
exp << node_ptr1->coefficient;
exp << " ";
}
exp << node_ptr1->variable;
if(node_ptr1->exponent != 0) {
exp << " ^ ";
exp << node_ptr1->exponent;
}
l3.insert(exp.str());
exp.str("");
node_ptr1 = node_ptr1->next;
}
while(node_ptr2 != NULL) {
if(node_ptr2->coefficient != 0) {
node_ptr2->coefficient = node_ptr2->coefficient * -1; // will cause it to be subtracting
exp << node_ptr2->coefficient;
exp << " ";
}
exp << node_ptr2->variable;
if(node_ptr2->exponent != 0) {
exp << " ^ ";
exp << node_ptr2->exponent;
}
l3.insert(exp.str());
exp.str("");
node_ptr2 = node_ptr2->next;
}
polylist * p = NULL;
p = &l3;
return p;
}

void polyman::fillListOne(string term) {
l1.insert(term);
}

void polyman::fillListTwo(string term) {
l2.insert(term);
}

void polyman::clearListOne() {
l1.clear();
}

void polyman::clearListTwo() {
l2.clear();
}

PolynomialListInterface * polyman::getListOne() {
polylist * p = NULL;
p = &l1;
return p;
}

PolynomialListInterface * polyman::getListTwo() {
polylist * p = NULL;
p = &l2;
return p;
}

+ 22
- 0
cs235/exam1/polyman.h View File

@@ -0,0 +1,22 @@
//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1
#ifndef __POLYMAN_H__
#define __POLYMAN_H__
#include "PolynomialManagerInterface.h"
#include "polylist.h"

using namespace std;

class polyman : public PolynomialManagerInterface {
public:
polylist l1;
polylist l2;
PolynomialListInterface * addLists();
PolynomialListInterface * subtractLists();
void fillListOne(string term);
void fillListTwo(string term);
void clearListOne();
void clearListTwo();
PolynomialListInterface * getListOne();
PolynomialListInterface * getListTwo();
};
#endif

+ 31
- 0
cs235/final/Makefile View File

@@ -0,0 +1,31 @@
CXXFLAGS= -Wall -g -std=c++0x
OBJECTS=RedBlackTreeFactory.o br_tree.o br_node.o main.o
EXE=main
all: $(EXE)

$(EXE): $(OBJECTS)
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@
test: test.cpp RedBlackTreeFactory.o br_node.o
rtest: test
./test
RedBlackTreeFactory.o: RedBlackTreeFactory.cpp RedBlackTreeFactory.h
br_node.o: br_node.cpp br_node.h
br_tree.o: br_tree.cpp br_tree.h
main.o: main.cpp

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)

+ 43
- 0
cs235/final/RedBlackNodeInterface.h View File

@@ -0,0 +1,43 @@
//YOU MAY NOT MODIFY THIS DOCUMENT
#pragma once
#include <string>
using namespace std;
class RedBlackNodeInterface {
public:
RedBlackNodeInterface() {}
virtual ~RedBlackNodeInterface() {}
/*
* Returns the word that is stored in this node
*
* @return the word that is stored in this node.
*/
virtual string getWord() = 0;
/*
* Returns the color of the node. A red node should return 0
* and a black node should return 1.
*
* @return the color of the node
*/
virtual int getColor() = 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 RedBlackNodeInterface * 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 RedBlackNodeInterface * getRightChild() = 0;
};

+ 20
- 0
cs235/final/RedBlackTreeFactory.cpp View File

@@ -0,0 +1,20 @@
#include "RedBlackTreeFactory.h"
#include "br_tree.h"
//You may add #include statements here
/*
You will MODIFY THIS DOCUMENT.
*/
//=======================================================================================
/*
getRedBlackTree()
Creates and returns an object whose class extends RedBlackTreeInterface.
This should be an object of a class you have created.
Example: If you made a class called "RedBlackTree", you might say, "return new RedBlackTree();".
*/
RedBlackTreeInterface * RedBlackTreeFactory::getRedBlackTree()
{
return new br_tree();//Modify this line
}

+ 23
- 0
cs235/final/RedBlackTreeFactory.h View File

@@ -0,0 +1,23 @@
#pragma once
#include "RedBlackTreeInterface.h"
using namespace std;
/*
WARNING: It is expressly forbidden to modify any part of this document, including its name
*/
//=======================================================================================
/*
getRedBlackTree()
Creates and returns an object whose class extends RedBlackTreeInterface.
This should be an object of a class you have created.
Example: If you made a class called "RedBlackTree", you might say, "return new RedBlackTree();".
*/
class RedBlackTreeFactory
{
public:
static RedBlackTreeInterface * getRedBlackTree();
};
//=======================================================================================

+ 78
- 0
cs235/final/RedBlackTreeInterface.h View File

@@ -0,0 +1,78 @@
//YOU MAY NOT MODIFY THIS DOCUMENT
#pragma once

#include <string>
#include "RedBlackNodeInterface.h"

using namespace std;

class RedBlackTreeInterface {
public:
RedBlackTreeInterface() {}
virtual ~RedBlackTreeInterface() {}

//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 RedBlackNodeInterface * getRootNode() = 0;

/*
* Attempts to add the given word to the tree
*
* Addition should be consistent with the instructions found in the exam instructions.
*/
virtual void add(string word) = 0;

/*
* Attempts to add the given string of words to the tree
*
* A string of words consists of alphabetical characters, no punctuation marks,
* and white space as the delimiter separating words. The string should be parsed and
* each word should be added to the tree.
*/
virtual void addPhrase(string words) = 0;

/*
* Attempts to remove the given word from the tree
*
* Removal should be consistent with the instructions found in the exam instructions.
*/
virtual void remove(string word) = 0;

/*
* The tree should print in the following format:
*
* Root (value & color)
* Left subtree root (value & color)
* Left left subtree root (value & color)
* Left right subtree root (value & color)
* Right subtree root (value & color)
* Right left subtree root (value & color)
* Right right subtree root (value & color)
*
* For example:
* The tree:
*
* jumps(b)
* brown(r) quick(r)
* The(b) fox(b) over(b) the(b)
* lazy(r)
*
* Will print:
* jumps(b)
* brown(r)
* The(b)
* fox(b)
* quick(r)
* over(b)
* lazy(r);
* the(b)
*
*/
virtual string printTree() = 0;
};

+ 21
- 0
cs235/final/TwoThreeTreeFactory.cpp View File

@@ -0,0 +1,21 @@
#include "TwoThreeTreeFactory.h"
//You may add #include statements here
/*
You will MODIFY THIS DOCUMENT.
*/
//=======================================================================================
/*
getTwoThreeTree()
Creates and returns an object whose class extends TwoTreeTreeInterface.
This should be an object of a class you have created.
Example: If you made a class called "TwoThreeTree", you might say, "return new TwoThreeTree();".
This method should return NULL or 0 if you do not intent to do the extra credit
*/
TwoThreeTreeInterface * TwoThreeTreeFactory::getTwoThreeTree()
{
return 0;//Modify this line
}

+ 25
- 0
cs235/final/TwoThreeTreeFactory.h View File

@@ -0,0 +1,25 @@
#pragma once
#include "TwoThreeTreeInterface.h"
using namespace std;
/*
WARNING: It is expressly forbidden to modify any part of this document, including its name
*/
//=======================================================================================
/*
getTwoThreeTree()
Creates and returns an object whose class extends TwoThreeTreeInterface.
This should be an object of a class you have created.
Example: If you made a class called "TwoThreeTree", you might say, "return new TwoThreeTree();".
This method should return NULL or 0 if you do not intent to do the extra credit
*/
class TwoThreeTreeFactory
{
public:
static TwoThreeTreeInterface * getTwoThreeTree();
};
//=======================================================================================

+ 58
- 0
cs235/final/TwoThreeTreeInterface.h View File

@@ -0,0 +1,58 @@
//YOU MAY NOT MODIFY THIS DOCUMENT
#pragma once
#include <string>
using namespace std;
class TwoThreeTreeInterface {
public:
TwoThreeTreeInterface() {}
virtual ~TwoThreeTreeInterface() {}
//This class may use a node of your own creation.
/*
* Attempts to add the given word to the tree
*
* Addition should be consistent with the instructions found in the exam instructions.
*/
virtual void add(string word) = 0;
/*
* Attempts to add the given string of words to the tree
*
* A string of words consists of alphabetical characters, no punctuation marks,
* and white space as the delimiter separating words. The string should be parsed and
* each word should be added to the tree.
*/
virtual void addPhrase(string words) = 0;
/*
* Attempts to remove the given word from the tree
*
* Removal should be consistent with the instructions found in the exam instructions.
*/
virtual void remove(string word) = 0;
/*
* The tree should print in the following format:
*
* Root (value)
* Left subtree root (value)
* Left left subtree root (value)
* Left middle subtree root (value)
* Left right subtree root (value)
* Middle subtree root (value)
* Middle left subtree root (value)
* Middle middle subtree root (value)
* Middle right subtree root (value)
*
* Right subtree root (value)
* Right left subtree root (value)
* Right middle subtree root (value)
* Right right subtree root (value)
*
*/
virtual string printTree() = 0;
};

+ 19
- 0
cs235/final/br_node.cpp View File

@@ -0,0 +1,19 @@
#include "br_node.h"

br_node::br_node(string data): data(data), color(0), parent(NULL), left(NULL), right(NULL) {}

string br_node::getWord() {
return data;
}

int br_node::getColor() {
return color;
}

RedBlackNodeInterface* br_node::getLeftChild() {
return left;
}

RedBlackNodeInterface* br_node::getRightChild() {
return right;
}

+ 21
- 0
cs235/final/br_node.h View File

@@ -0,0 +1,21 @@
#ifndef __BR_NODE_H__
#define __BR_NODE_H__

#include "RedBlackNodeInterface.h"

using namespace std;

class br_node : public RedBlackNodeInterface {
public:
br_node(string data);
string data;
int color; //red = 0, black = 1
br_node* parent;
br_node* left;
br_node* right;
string getWord();
int getColor();
RedBlackNodeInterface * getLeftChild();
RedBlackNodeInterface * getRightChild();
};
#endif

+ 500
- 0
cs235/final/br_tree.cpp View File

@@ -0,0 +1,500 @@
#include "br_tree.h"

br_tree::br_tree():root(NULL), h(false){}

vector<string> parse_expression(string expression) { //parses expression with " " being the delimeter
vector<string> results;
string s;
for(unsigned int i = 0; i < expression.length(); i++) {
char c = expression[i];
if(c != ' ') {
s += c;
}
else {
if(s != "") {
results.push_back(s);
s.clear();
}
}
}
if(s != "") {
results.push_back(s);
}
return results;
}

bool br_tree::in_tree(string data) {
bool found = false;
br_node* current;
br_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;
}
}
}
return found;
}

RedBlackNodeInterface* br_tree::getRootNode() {
return root;
}

void br_tree::add(string word) {
if(in_tree(word)) {
return;
}
if(root == NULL) {
root = new br_node(word);
root->color = 1;
return;
}
br_node* current;
br_node* cur_parent;
current = root;
while(current) {
cur_parent = current;
if(word > current->data) {
current = current->right;
}
else {
current = current->left;
}
}
if(word < cur_parent->data) {
cur_parent->left = new br_node(word);
cur_parent->left->parent = cur_parent;
br_node* child_added = cur_parent->left;
if(cur_parent->color == 0 && child_added->color == 0) {
balance(cur_parent->left);
}
return;
}
else {
cur_parent->right = new br_node(word);
cur_parent->right->parent = cur_parent;
br_node* child_added = cur_parent->right;
if(cur_parent->color == 0 && child_added->color == 0) {
balance(cur_parent->right);
}
return;
}
}

void br_tree::balance(br_node* cur) {
if((cur->data == root->data) || (cur->parent->data == root->data)) {
return;
}
if((cur->parent->parent->right != NULL) && (cur->parent->parent->left != NULL)) {
if((cur->parent->parent->left->color == 0) && (cur->parent->parent->right->color == 0)) {
cur->parent->parent->left->color = 1;
cur->parent->parent->right->color = 1;
cur->parent->parent->color = 0;
balance(cur->parent->parent);
return;
}
}
if((cur->parent->color == 0) && (cur->color == 0)) {
if((cur->parent->left != NULL) && (cur->parent->parent->left != NULL)) {
if((cur->parent->left->data == cur->data) && (cur->parent->parent->left->data == cur->parent->data)) {
//case III left
br_node* temp = cur->parent->parent;
if(cur->parent->right == NULL) {
temp->left = cur->parent->right;
}
else {
temp->left = cur->parent->right;
cur->parent->right->parent = temp;
}
cur->parent->right = temp;
if(temp->parent != NULL) {
if(temp->parent->right->data == temp->data) {
temp->parent->right = cur->parent;
}
else {
temp->parent->left = cur->parent;
}
}
else {
root = cur->parent;
}
cur->parent->parent = temp->parent;
temp->parent = cur->parent;
cur->parent->color = 1;
temp->color = 0;
balance(cur->parent);
return;
}
}
if((cur->parent->left != NULL) && (cur->parent->parent->right != NULL)) {
if((cur->parent->left->data == cur->data) && (cur->parent->parent->right->data == cur->parent->data)){
//case II right
br_node* temp1 = cur->parent->parent;
br_node* temp2 = cur->parent;
if(cur->right == NULL) {
temp2->left = cur->right;
}
else {
temp2->left = cur->right;
cur->right->parent = temp2;
}
cur->right = temp2;
temp2->parent = cur;
cur->parent = temp1;
temp1->right = cur;
balance(temp2);
return;
}
}
if((cur->parent->right != NULL) && (cur->parent->parent->left != NULL)) {
if((cur->parent->right->data == cur->data) && (cur->parent->parent->left->data == cur->parent->data)){
//case II left
br_node* temp1 = cur->parent->parent;
br_node* temp2 = cur->parent;
if(cur->left == NULL) {
temp2->right = cur->left;
}
else {
temp2->right = cur->left;
cur->left->parent = temp2;
}
cur->left = temp2;
temp2->parent = cur;
cur->parent = temp1;
temp1->left = cur;
balance(temp2);
return;
}
}
if((cur->parent->right != NULL) && (cur->parent->parent->right != NULL)) {
if((cur->parent->right->data == cur->data) && (cur->parent->parent->right->data == cur->parent->data)) {
//case III right
br_node* temp = cur->parent->parent;
if(cur->parent->left == NULL) {
temp->right = cur->parent->left;
}
else {
temp->right = cur->parent->left;
cur->parent->left->parent = temp;
}
cur->parent->left = temp;
if(temp->parent != NULL) {
if(temp->parent->right->data == temp->data) {
temp->parent->right = cur->parent;
}
else {
temp->parent->left = cur->parent;
}
}
else {
root = cur->parent;
}
cur->parent->parent = temp->parent;
temp->parent = cur->parent;
cur->parent->color = 1;
temp->color = 0;
balance(cur->parent);
return;
}
}
}
if(root->color == 0) {
root->color = 1;
return;
}
balance(cur->parent);
}

void br_tree::addPhrase(string words) {
vector<string> parsed_string = parse_expression(words);
for(unsigned int i = 0; i < parsed_string.size(); i++) {
add(parsed_string[i]);
}
}

void br_tree::remove(string word) {
if(!(in_tree(word))) {
return;
}
if(root == NULL) {
return;
}
br_node* current;
br_node* parent;
current = root;
while(current) {
if(current->data == word) {
break;
}
else {
parent = current;
if(word > current->data) {
current = current->right;
}
else if(word < current->data) {
current = current->left;
}
}
}
if(current->color == 1) {
h = true;
}
else {
h = false;
}
root = del_leaf(root, word);
}

br_node* br_tree::del_leaf(br_node* root, string data) {
br_node* temp;
if(root == NULL) {
return root;
}
else {
if(data < root->data) {
root->left = del_leaf(root->left, data);
if(h) {
cout << "2" << endl;
bal_del(root->left);
}
}
else {
if(data > root->data) {
root->right = del_leaf(root->right, data);
if(h) {
cout << "3" << endl;
cout << root->data << endl;
bal_del(root->right);
}
}
else {
temp = root;
if(temp->right == NULL) {
root = temp->left;
delete temp;
}
else {
if(temp->left == NULL) {
root = temp->right;
delete temp;
}
else {
temp->left = del(temp->left, temp);
if(h) {
cout << "4" << endl;
if(h) {
bal_del(temp);
}
}
}
}
}
}
}
return root;
}

void br_tree::bal_del(br_node* x) {
//cout << "bal_del(" << x->data << ")" << endl;
if(x == NULL) {
return;
}
while(x != root && x->color == 1) {
if(x == x->parent->left) {
br_node* w = x->parent->right;
if(w == NULL) {
return;
}
if(w->right == NULL || w->left == NULL) {
return;
}
if(w != NULL && w->right != NULL && w->left != NULL) {
if(w->color == 0) {
cout << "case 1 first" << endl;
w->color = 1; //Case 1
x->parent->color = 0; //Case 1
left_rot(x->parent); //Case 1
w = x->parent->right; //Case 1
}
else if(w->left->color == 1 && w->right->color == 1) {
cout << "case 2 first" << endl;
w->color = 1; //Case 2
x = x->parent; //Case 2
}
else {
if(w->right->color == 1) {
cout << "case 3 first" << endl;
w->left->color = 1; //Case 3
w->color = 0; //Case 3
right_rot(w); //Case 3
w = x->parent->right; //Case 3
}
cout << "case 4 first" << endl;
w->color = x->parent->color; //Case 4
x->parent->color = 1; //Case 4
w->right->color = 1; //Case 4
left_rot(x->parent);
x = root;
}
}
}
else {
br_node* w = x->parent->left;
if(w == NULL) {
return;
}
if(w->right == NULL || w->left == NULL) {
return;
}
if(w->color == 0) {
cout << "case 1 second" << endl;
w->color = 1; //Case 1
x->parent->color = 0; //Case 1
right_rot(x->parent); //Case 1
w = x->parent->left; //Case 1
}
else if(w->right->color == 1 && w->left->color == 1) {
cout << "case 2 second" << endl;
w->color = 0; //Case 2
x = x->parent; //Case 2
}
else {
if(w->left->color == 1) {
cout << "case 3 second" << endl;
w->right->color = 1; //Case 3
w->color = 0; //Case 3
left_rot(w); //Case 3
w = x->parent->left; //Case 3
}
cout << "case 4 second" << endl;
w->color = x->parent->color; //Case 4
x->parent->color = 1; //Case 4
w->left->color = 1; //Case 4
right_rot(x->parent);
x = root;
}
}
}
x->color = 1;
}

br_node* br_tree::del(br_node* succ, br_node* n) {
br_node* temp = succ;
if(succ->right != NULL) {
succ->right = del(succ->right, n);
}
else {
br_node* current;
br_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;
return succ;
}
else if(root->right->data == succ->data) {
root->data = succ->data;
n->right = root->right;
succ = succ->right;
delete temp;
return succ;
}
temp = succ;
n->data = succ->data;