This repository has been archived by the owner on Aug 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.h
102 lines (82 loc) · 2.06 KB
/
Tree.h
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef __TREE_H
#define __TREE_H
#include <vector>
#include <boost/pool/pool.hpp>
#include "Utils.h"
template <class T>
class TreeNode : public NoThrowNewDelete {
static boost::pool<> m_pool;
TreeNode *m_parent;
vector<TreeNode*> m_children;
T m_data;
public:
explicit TreeNode(int n = 0, T d = T()) : m_parent(0), m_children(n), m_data(d) { }
TreeNode(const TreeNode &node);
~TreeNode();
T data() const { return m_data; }
int size() const { return m_children.size(); }
void resize(int n);
TreeNode *addChild(int i, T d = T());
TreeNode *setChild(int i, T d);
TreeNode *getChild(int i) const { return m_children[i]; }
using NoThrowNewDelete::operator new;
using NoThrowNewDelete::operator delete;
static void *operator new(std::size_t) { return m_pool.malloc(); }
static void operator delete(void *pMemory) { m_pool.free(pMemory); }
};
template <class T>
inline TreeNode<T>::TreeNode(const TreeNode &node)
: m_parent(node.m_parent),
m_children(node.m_children.size()),
m_data(node.m_data)
{
for (int i=0; i<size(); ++i) {
if (node.m_children[i]) {
m_children[i] = new TreeNode (*node.m_children[i]);
m_children[i]->m_parent = this;
}
}
}
template <class T>
inline TreeNode<T>::~TreeNode()
{
for (int i=0; i<size(); ++i) {
delete m_children[i];
}
}
template <class T>
inline void TreeNode<T>::resize(int n)
{
int i;
for (i=n; i<size(); ++i) {
delete m_children[i];
}
m_children.resize(n);
for (i=0; i<size(); ++i) {
if (m_children[i]) {
m_children[i]->resize(n);
}
}
}
template <class T>
inline TreeNode<T> *TreeNode<T>::addChild(int i, T d)
{
if (!m_children[i]) {
m_children[i] = new TreeNode(size(), d);
m_children[i]->m_parent = this;
}
return m_children[i];
}
template <class T>
inline TreeNode<T> *TreeNode<T>::setChild(int i, T d)
{
if (m_children[i]) {
m_children[i]->m_data = d;
}
else {
m_children[i] = new TreeNode(size(), d);
m_children[i]->m_parent = this;
}
return m_children[i];
}
#endif //__TREE_H