Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NODE.H
- #ifndef INCLUDED_NODE_H
- #define INCLUDED_NODE_H
- #include <iostream>
- #include <vector>
- using namespace std;
- class Node {
- private:
- //the address of this Node, which must be stored for deleting problems
- //e.g. infinite loops when a Node points to itself like Noun does
- Node* selfP;
- public:
- Node (Node* n);
- ~Node ();
- string getContent () {
- return content;
- }
- void setContent (string newContent) {
- content = newContent;
- }
- bool getIsLeaf () {
- return isLeaf;
- }
- void setIsLeaf (bool newIsLeaf) {
- isLeaf = newIsLeaf;
- }
- void addLeaves (vector <Node*> newLeaves) {
- children->push_back(newLeaves);
- };
- vector <vector <Node*> >* getChildren () {
- return children;
- }
- vector <Node*> getLeaves ();
- Node* getP () {
- return selfP;
- }
- private:
- string content;
- //true/false if its the end (has any children)
- bool isLeaf;
- /*
- each node will have a grouped pathway of children that must stay
- organized. for instance 'sentence' might have 'np' and 'vp' in one rule
- meaning that while both are separate children, they must be grouped
- together. i solved this problem by creating a vector. each element of this
- vector is a vector. each element of those vectors is a Node*.
- in other words, np and vp on the same rule line would be in one vector
- together, representing one element of the vector which contains all of
- the groups.
- */
- vector <vector <Node*> >* children;
- //each element of childrenLength corresponds to the length of that
- //represented vector in children
- vector <int> childrenLength;
- };
- #endif
- NODE.CPP
- #include "Node.h"
- Node :: Node (Node* n) {
- content = "";
- isLeaf = true;
- children = new vector <vector <Node*> >;
- selfP = n;
- }
- vector <Node*> Node :: getLeaves () {
- //r is the random number for the group of leaves
- int r = 0;
- if ((children->size()-1) > 0) {
- r = rand();
- r = r%children->size();
- }
- cout << "size: " << children->size() << endl;
- cout << "r: " << r << endl;
- return (*children)[r];
- }
- Node :: ~Node () {
- for (int i = 0; i < (*children).size(); i++) {
- for (int j = 0; j < (*children)[i].size(); j++) {
- if ((*children)[i][j] != selfP) {
- delete (*children)[i][j];
- }
- }
- }
- }
- MAIN.CPP
- #include "Node.h"
- int main () {
- Node* test = new Node(test);
- Node* n = new Node (n);
- n->setContent("apple");
- cout << n->getP() << endl;
- cout << n << endl;
- vector <Node*> v;
- v.push_back(n);
- n->addLeaves(v);
- delete n;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement