-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.cpp
More file actions
44 lines (43 loc) · 751 Bytes
/
tree.cpp
File metadata and controls
44 lines (43 loc) · 751 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "tree.h"
Node::Node(int n)
{
v=n;
visited=0;
}
Node::~Node()
{
for( Node* c : children )
delete c;
children.clear();
}
Node* Node::find_num(int n)
{
for( Node* c : children )
if( c->v == n )
return c;
return nullptr;
}
Node* Node::insert( int n )
{
Node* node = new Node(n);
children.push_back(node);
return node;
}
// return how many times this path was added;
int Node::add_path(const vector<int>& vec)
{
Node* node = this; // root
for( int n : vec )
{
Node* child = node->find_num(n);
if(child)
{
node = child;
}
else
{
node = node->insert(n);
}
}
return ++node->visited;
}