Skip to main content
1 of 3
Sparky
  • 145
  • 1
  • 1
  • 4

C++ Tree Base Node

I've been programming in C++ again (after switching to web languages) for about 2 weeks now. I wrote this simple Node class from which all other objects within the Tree will be derived from. Any critique is very welcome here, however please use clear explanations, don't just point out flaws. Thanks ahead of time! I need all the help I can get.

#include <string>
#include <vector>

using namespace std;

// DEFINITION

class Node 
{
public:
    
    Node(string name_);
    virtual ~Node() = 0;
    
    string GetName() const;
    
    void SetParent(Node * parent);
    Node * GetParent() const;
    
    Node * AddChild(Node * child);
    vector<Node*> FindChildren(string name) const;
    Node * FindFirstChild(string name) const;
    Node * FindLastChild(string name) const;
    Node * FindNthChild(int nth, string name) const;
    vector<string> ChildrenNames() const;
    
    int Depth() const;
    bool IsLeaf() const;
    bool IsRoot() const;
    
protected:
    
    string name;
    
    Node * parent;
    vector<Node*> children;
    
private:
};

// IMPLEMENTATION

Node::Node(string name_)
{
        name = name_;
        parent = NULL;
}

Node::~Node() 
{
    for(vector<Node*>::iterator it = children.begin(); it < children.end(); it++) 
    {
        if (*it) 
        {
            delete * it;
        }
    }
    children.clear();
}

string Node::GetName() const
{
    return name;
}


void Node::SetParent(Node * parent_) 
{
    parent = parent_;
}

Node * Node::GetParent() const
{
    return parent;
}

Node * Node::AddChild(Node * child)
{
    child->SetParent(this);
    children.push_back(child);
    
    return child;
}

vector<Node*> Node::FindChildren(string name) const
{
    vector<Node*> children;
    for(vector<Node*>::const_iterator it = children.begin(); it < children.end(); it++) 
    {
        if (name.compare((*it)->name) == 0)
            children.push_back(*it);
            
    }
    return children;
}

Node * Node::FindFirstChild(string name) const
{
    for(vector<Node*>::const_iterator it = children.begin(); it < children.end(); it++) 
    {
        if (name.compare((*it)->name) == 0)
            return (*it);
        
    }
    return NULL;
}

Node * Node::FindNthChild(int nth, string name) const
{
    int n = 0;
    for(vector<Node*>::const_iterator it = children.begin(); it < children.end(); it--) 
    {
        if (name.compare((*it)->name) == 0)
        {
            if (++n == nth)
                return (*it);
        }
        
    }
    return NULL;
}

vector<string> Node::ChildrenNames() const
{
    vector<string> names;
    for(vector<Node*>::const_iterator it = children.begin(); it < children.end(); it++) 
    {
        names.push_back((*it)->name);
    }
    return names;
}

int Node::Depth() const
{
    int d;
    Node * ancestor = parent;
    for (d = 0; ancestor != NULL; d++)
    {
        ancestor = ancestor->parent;
    }
    return d;
}

bool Node::IsLeaf() const
{
    return (children.size() == 0) ? true : false;
}

bool Node::IsRoot() const
{
    return (parent == NULL) ? true : false;
}
Sparky
  • 145
  • 1
  • 1
  • 4