-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTTree.hpp
205 lines (169 loc) · 3.59 KB
/
TTree.hpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/******************************************************************************
** (C) Chris Oldwood
**
** MODULE: TREE.HPP
** COMPONENT: Windows C++ Library.
** DESCRIPTION: The CTree class declaration.
**
*******************************************************************************
*/
// Check for previous inclusion
#ifndef TREE_HPP
#define TREE_HPP
#if _MSC_VER > 1000
#pragma once
#endif
#include "TArray.hpp"
/******************************************************************************
**
** The template class used to hold a node in the tree.
**
*******************************************************************************
*/
template<class T> class TTreeNode
{
public:
//
// Constructors/Destructor.
//
TTreeNode();
virtual ~TTreeNode();
//
// Accessors.
//
size_t NumNodes() const;
TTreeNode<T>* Node(size_t n) const;
TTreeNode<T>* Parent() const;
//
// Mutators.
//
void Node(size_t n, TTreeNode<T>* pNode);
void AddNode(TTreeNode<T>* pNode);
void Parent(TTreeNode<T>* pNode);
//
// Members.
//
T m_oData;
protected:
// Template typedefs.
typedef TPtrArray<TTreeNode> CNodes;
//
// Members.
//
CNodes m_apNodes;
TTreeNode<T>* m_pParent;
private:
//
// Disallow copies.
//
TTreeNode(const TTreeNode<T>&);
void operator=(const TTreeNode<T>&);
};
/******************************************************************************
**
** The template class used for tree collections.
**
*******************************************************************************
*/
template<class T> class TTree
{
public:
//
// Constructors/Destructor.
//
TTree();
virtual ~TTree();
//
// Accessors.
//
TTreeNode<T>* Root() const;
//
// Mutators.
//
void Root(TTreeNode<T>* pNode);
//
// Methods.
//
void Clear();
protected:
//
// Members.
//
TTreeNode<T>* m_pRoot; // The root node.
private:
//
// Disallow copies.
//
TTree(const TTree<T>&);
void operator=(const TTree<T>&);
};
/******************************************************************************
**
** Implementation of TTreeNode inline functions.
**
*******************************************************************************
*/
template<class T> inline TTreeNode<T>::TTreeNode()
: m_pParent(NULL)
{
}
template<class T> inline TTreeNode<T>::~TTreeNode()
{
for (size_t i = 0; i < m_apNodes.Size(); ++i)
delete m_apNodes[i];
}
template<class T> inline size_t TTreeNode<T>::NumNodes() const
{
return m_apNodes.Size();
}
template<class T> inline TTreeNode<T>* TTreeNode<T>::Node(size_t n) const
{
return m_apNodes[n];
}
template<class T> inline TTreeNode<T>* TTreeNode<T>::Parent() const
{
return m_pParent;
}
template<class T> inline void TTreeNode<T>::Node(size_t n, TTreeNode<T>* pNode)
{
pNode->Parent(this);
m_apNodes.Set(n, pNode);
}
template<class T> inline void TTreeNode<T>::AddNode(TTreeNode<T>* pNode)
{
pNode->Parent(this);
m_apNodes.Add(pNode);
}
template<class T> inline void TTreeNode<T>::Parent(TTreeNode<T>* pNode)
{
m_pParent = pNode;
}
/******************************************************************************
**
** Implementation of TTree inline functions.
**
*******************************************************************************
*/
template<class T> inline TTree<T>::TTree()
: m_pRoot(NULL)
{
}
template<class T> inline TTree<T>::~TTree()
{
Clear();
}
template<class T> inline TTreeNode<T>* TTree<T>::Root() const
{
return m_pRoot;
}
template<class T> inline void TTree<T>::Root(TTreeNode<T>* pNode)
{
ASSERT(m_pRoot == NULL);
m_pRoot = pNode;
}
template<class T> inline void TTree<T>::Clear()
{
delete m_pRoot;
m_pRoot = NULL;
}
#endif //TREE_HPP