Advertisement
Guest User

Untitled

a guest
Mar 5th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. NODE.H
  2. #ifndef INCLUDED_NODE_H
  3. #define INCLUDED_NODE_H
  4.  
  5. #include <iostream>
  6. #include <vector>
  7. using namespace std;
  8.  
  9.  
  10. class Node {
  11. private:
  12. //the address of this Node, which must be stored for deleting problems
  13. //e.g. infinite loops when a Node points to itself like Noun does
  14. Node* selfP;
  15.  
  16. public:
  17. Node (Node* n);
  18. ~Node ();
  19.  
  20. string getContent () {
  21. return content;
  22. }
  23.  
  24. void setContent (string newContent) {
  25. content = newContent;
  26. }
  27.  
  28. bool getIsLeaf () {
  29. return isLeaf;
  30. }
  31.  
  32. void setIsLeaf (bool newIsLeaf) {
  33. isLeaf = newIsLeaf;
  34. }
  35.  
  36. void addLeaves (vector <Node*> newLeaves) {
  37. children->push_back(newLeaves);
  38. };
  39.  
  40. vector <vector <Node*> >* getChildren () {
  41. return children;
  42. }
  43.  
  44. vector <Node*> getLeaves ();
  45.  
  46. Node* getP () {
  47. return selfP;
  48. }
  49.  
  50. private:
  51. string content;
  52. //true/false if its the end (has any children)
  53. bool isLeaf;
  54. /*
  55. each node will have a grouped pathway of children that must stay
  56. organized. for instance 'sentence' might have 'np' and 'vp' in one rule
  57. meaning that while both are separate children, they must be grouped
  58. together. i solved this problem by creating a vector. each element of this
  59. vector is a vector. each element of those vectors is a Node*.
  60.  
  61. in other words, np and vp on the same rule line would be in one vector
  62. together, representing one element of the vector which contains all of
  63. the groups.
  64. */
  65. vector <vector <Node*> >* children;
  66. //each element of childrenLength corresponds to the length of that
  67. //represented vector in children
  68. vector <int> childrenLength;
  69. };
  70.  
  71. #endif
  72.  
  73.  
  74.  
  75.  
  76.  
  77. NODE.CPP
  78. #include "Node.h"
  79.  
  80. Node :: Node (Node* n) {
  81. content = "";
  82. isLeaf = true;
  83. children = new vector <vector <Node*> >;
  84. selfP = n;
  85. }
  86.  
  87. vector <Node*> Node :: getLeaves () {
  88. //r is the random number for the group of leaves
  89. int r = 0;
  90. if ((children->size()-1) > 0) {
  91. r = rand();
  92. r = r%children->size();
  93. }
  94. cout << "size: " << children->size() << endl;
  95. cout << "r: " << r << endl;
  96. return (*children)[r];
  97.  
  98. }
  99.  
  100. Node :: ~Node () {
  101. for (int i = 0; i < (*children).size(); i++) {
  102. for (int j = 0; j < (*children)[i].size(); j++) {
  103. if ((*children)[i][j] != selfP) {
  104. delete (*children)[i][j];
  105. }
  106. }
  107. }
  108. }
  109.  
  110.  
  111.  
  112.  
  113. MAIN.CPP
  114. #include "Node.h"
  115.  
  116. int main () {
  117. Node* test = new Node(test);
  118. Node* n = new Node (n);
  119. n->setContent("apple");
  120.  
  121. cout << n->getP() << endl;
  122. cout << n << endl;
  123.  
  124. vector <Node*> v;
  125. v.push_back(n);
  126. n->addLeaves(v);
  127.  
  128. delete n;
  129.  
  130.  
  131.  
  132.  
  133. return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement