Node class … can make a tree of strings and search the whole tree for a certain term

 

#include <iostream>
using namespace std;
class Node
{
char * m_value;
int m_children;
Node* children;
public:
Node();
void Text(char* text);
char* Text() const;
void AddChild(Node& nod);
bool GetNode(Node& to, char* like);
};
Node::Node()
{
m_children = 0;
children = new Node[];
cout << “BUILD” << endl;
}
void Node::Text(char *text)
{
m_value = text;
cout << “SET VALUE : ” << m_value << endl;
}
char* Node::Text() const
{
return m_value;
}
void Node::AddChild(Node& nod)
{
children[m_children] = nod;
m_children++;
}
bool Node::GetNode(Node &to, char *like)
{
if(m_value == like)
{
to = *this;
return true;
}
for(int i=0; i<m_children; i++)
if(children[i].GetNode(to, like))
return true;
return false;
}
int main()
{
Node n;
n.Text(“www”);
Node a;
a.Text(“bubu”);
Node b;
b.Text(“qwertyKeyboard”);
Node c;
c.Text(“apple”);
Node aa;
aa.Text(“sunny”);
Node bb;
bb.Text(“NAN”);
Node cc;
cc.Text(“Smiley”);
a.AddChild(aa);
a.AddChild(bb);
a.AddChild(cc);
n.AddChild(a);
n.AddChild(b);
n.AddChild(c);
Node final;
if(a.GetNode(final, “alina”))
{
cout << final.Text() << endl;
}
return 0;
}

who knows .. maybe usefull sometime :)

#include <iostream>

using namespace std;

class Node

{

char * m_value;

int m_children;

Node* children;

public:

Node();

void Text(char* text);

char* Text() const;

void AddChild(Node& nod);

bool GetNode(Node& to, char* like);

};

Node::Node()

{

m_children = 0;

children = new Node[];

cout << “BUILD” << endl;

}

void Node::Text(char *text)

{

m_value = text;

cout << “SET VALUE : ” << m_value << endl;

}

char* Node::Text() const

{

return m_value;

}

void Node::AddChild(Node& nod)

{

children[m_children] = nod;

m_children++;

}

bool Node::GetNode(Node &to, char *like)

{

if(m_value == like)

{

to = *this;

return true;

}

 

for(int i=0; i<m_children; i++)

if(children[i].GetNode(to, like))

return true;

 

return false;

}

 

int main()

{

Node n;

n.Text(“www”);

Node a;

a.Text(“bubu”);

Node b;

b.Text(“qwertyKeyboard”);

Node c;

c.Text(“apple”);

 

Node aa;

aa.Text(“sunny”);

Node bb;

bb.Text(“NAN”);

Node cc;

cc.Text(“Smiley”);

 

a.AddChild(aa);

a.AddChild(bb);

a.AddChild(cc);

 

n.AddChild(a);

n.AddChild(b);

n.AddChild(c);

 

Node final;

if(a.GetNode(final, “someTerm”))

{

cout << final.Text() << endl;

}

return 0;

}

Advertisement

~ by sinea on May 14, 2009.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.