/*
* node.cpp
* abcd
*
* The node class is an abstract representation of a node in a hierarchical structure.
* Its basic properties is parent, children, depth and name, but also booleans that
* tells us whether or not it has a parent or if it is a root node.
*
* The class "lexicalnode" is an extension of the "node" class.
*
* Created by Me tomorrow.
* Copyrighted to no one. All rights preserved.
*
*/
#include <iostream>
#include <set>
using namespace std;
class node {
//Attributes
protected:
string name;
int depth;
bool isRoot;
bool hasParent;
//Relation
node* parent;
set<node*> children;
public:
node () {depth = 0; isRoot = true; hasParent = false; parent = NULL;}
void assignChild (node* target) {
cout << endl << "FUNCTION CALLED: assignChild" << endl;
cout << "\t" << name << " = caller. " << target->name << " is target." << endl;
if (depth == 0) {
cout << "\t\t" << "Caller is root. No recursion possible." << endl;
assign(target);
return;
}
else if (this->depth <= 0 || isRoot == true) {
cout << "\t\t" << "Callers depth shallower or equal to target depth. Caller: " << depth << " - Target: " << target->depth << endl;
assign(target);
return;
}
else if (target->depth < depth) {
cout << "\t\t" << "Checking recursion: " << endl << "\t\t" << name << "'s depth is deeper than target." << endl;
node* n = this;
while (n->depth > target->depth) {
cout << "\t\t\t\t" << "Looping through the structure. Now at " << n->name << "." << endl;
n = n->parent;
}
if (n == target) {
cout << "\t\t\t" << "Recursion! Target dominates caller." << endl;
return;
}
else {
cout << "\t\t\t" << "No recursion. Caller does not dominate target." << endl;
assign(target);
return;
}
}
cout << endl;
}
void assign (node* target) {
cout << "FUNCTION CALLED: assign" << endl;
if (target->hasParent == true) {
target->parent->removeChild(target);
}
children.insert(target);
target->parent = this;
target->isRoot = false;
target->hasParent = true;
target->assignDepth();
}
void removeChild (node* target) {
cout << "FUNCTION CALLED: removeChild" << endl;
children.erase (children.find(target));
target->parent = NULL;
target->hasParent = false;
}
void breakLinks () {
cout << "FUNCTION CALLED: breakLinks" << endl;
if (hasParent == true) {
cout << "Node has parent: " << parent->name << " - removing " << this->name << " from parents child-list." << endl;
parent->removeChild(this);
}
cout << "Removing children from list, and the childrens parent tag." << endl;
for (set<node*>::iterator i = children.begin(); i != children.end(); i++) {
children.erase((*i));
(*i)->parent;
}
isRoot = true;
cout << name << " is now a root node with no relations." << endl;
cout << endl;
}
void assignDepth () {
cout << "FUNCTION CALLED: assignDepth" << endl;
depth = parent->depth + 1;
for (set<node*>::iterator i = children.begin(); i != children.end(); i++) {
(*i)->assignDepth();
}
}
void print () {
cout << "\t" << "Node: " << name << "\tDepth: " << depth << "\tHas parent: " << hasParent << "\tIs root: " << isRoot << "\tChildren --> ";
for (set<node*>::iterator i = children.begin(); i != children.end(); i++) {
cout << (*i)->name << ", ";
}
cout << endl;
for (set<node*>::iterator i = children.begin(); i != children.end(); i++) {
(*i)->print();
}
}
};
class lexicalnode: public node {
protected:
string syntactic_category;
string label;
bool phraselevel;
bool intermediate;
bool terminalnode;
bool head;
lexicalnode* specifier;
lexicalnode* adjunct;
lexicalnode* complement;
lexicalitem* word;
public:
lexicalnode () {}
};
class lexicalitem {
protected:
node* parent;
void update_category () {} //G
};