| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2008 by Eran Ifrah |
| 5 |
// file name : tree_node.h |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
|
| 26 |
#ifndef CODELITE_TREE_NODE_H |
| 27 |
#define CODELITE_TREE_NODE_H |
| 28 |
|
| 29 |
|
| 30 |
#include <map> |
| 31 |
#include <iostream> |
| 32 |
#include <vector> |
| 33 |
|
| 34 |
template <class TKey, class TData> |
| 35 |
class TreeNode |
| 36 |
{ |
| 37 |
TKey m_key; |
| 38 |
TData m_data; |
| 39 |
TreeNode* m_parent; |
| 40 |
std::map<TreeNode*, TreeNode*> m_childs; |
| 41 |
typename std::map<TreeNode*, TreeNode*>::iterator m_pos; |
| 42 |
|
| 43 |
public: |
| 44 |
/** |
| 45 |
* Constructor, constructs a tree node with key and data. |
| 46 |
* If parent is left NULL, node is assumed to be root. |
| 47 |
* \param key Node key, must have operator =, < |
| 48 |
* \param data Node data, this class must have operator= |
| 49 |
* \param parent Parent node |
| 50 |
*/ |
| 51 |
TreeNode(const TKey& key, const TData& data, TreeNode* parent = NULL); |
| 52 |
|
| 53 |
/** |
| 54 |
* Destructor, deletes this node and all its children. |
| 55 |
*/ |
| 56 |
virtual ~TreeNode(); |
| 57 |
|
| 58 |
//--------------------------------------------------- |
| 59 |
// Setters / Getters |
| 60 |
//--------------------------------------------------- |
| 61 |
|
| 62 |
/** |
| 63 |
* Get node data. |
| 64 |
* \return node data |
| 65 |
*/ |
| 66 |
TData& GetData() |
| 67 |
{ |
| 68 |
return m_data; |
| 69 |
}; |
| 70 |
|
| 71 |
/** |
| 72 |
* Get node key. |
| 73 |
* \return key |
| 74 |
*/ |
| 75 |
TKey& GetKey() |
| 76 |
{ |
| 77 |
return m_key; |
| 78 |
}; |
| 79 |
|
| 80 |
/** |
| 81 |
* Set node data. |
| 82 |
* \param data data |
| 83 |
*/ |
| 84 |
void SetData(const TData& data) |
| 85 |
{ |
| 86 |
m_data = data; |
| 87 |
}; |
| 88 |
|
| 89 |
/** |
| 90 |
* Set node key. |
| 91 |
* \param key Key |
| 92 |
*/ |
| 93 |
void SetKey(const TKey& key) |
| 94 |
{ |
| 95 |
m_key = key; |
| 96 |
}; |
| 97 |
|
| 98 |
/** |
| 99 |
* Return the parent node of this node (or null if root). |
| 100 |
* \return pointer to parent node |
| 101 |
*/ |
| 102 |
TreeNode* GetParent() |
| 103 |
{ |
| 104 |
return m_parent; |
| 105 |
} |
| 106 |
|
| 107 |
//--------------------------------------------- |
| 108 |
// Misc |
| 109 |
//--------------------------------------------- |
| 110 |
|
| 111 |
/** |
| 112 |
* Print the tree to stream (default is stdout). |
| 113 |
*/ |
| 114 |
void Print(std::ostream& stream = std::cout , int depth = 0); |
| 115 |
|
| 116 |
/** |
| 117 |
* Check if this node is root. |
| 118 |
* \return true if node is root |
| 119 |
*/ |
| 120 |
bool IsRoot() const |
| 121 |
{ |
| 122 |
return m_parent == NULL; |
| 123 |
}; |
| 124 |
|
| 125 |
//---------------------------------------------- |
| 126 |
// Operations |
| 127 |
//---------------------------------------------- |
| 128 |
/** |
| 129 |
* Add new child node to this node. |
| 130 |
* Duplicate nodes are allowed. However, Remove() will remove the first occurance of a node by a given key. |
| 131 |
* \param key Node key |
| 132 |
* \param data Node data |
| 133 |
* \return newly added node |
| 134 |
*/ |
| 135 |
TreeNode* AddChild(const TKey& key, const TData& data) ; |
| 136 |
|
| 137 |
/** |
| 138 |
* \brief Append new child to this tree node |
| 139 |
* \param newNode node to append, must be allocated on the heap |
| 140 |
* \return the newly added tree node |
| 141 |
*/ |
| 142 |
TreeNode* AddChild(TreeNode* newNode); |
| 143 |
|
| 144 |
/** |
| 145 |
* Remove first occurance of node with a given key. |
| 146 |
* If the node to be removed is the root, a std::exception* is thrown, which must be deleted by caller. |
| 147 |
* Remove does not delete the memory allocated for the node. The user must delete the removed node. |
| 148 |
* \param key Node key |
| 149 |
* \return removed node |
| 150 |
*/ |
| 151 |
TreeNode* Remove(const TKey& key); |
| 152 |
|
| 153 |
/** |
| 154 |
* Find a node by a key. |
| 155 |
* \param key Node key |
| 156 |
* \return node, or NULL if non exist |
| 157 |
*/ |
| 158 |
TreeNode* Find(const TKey& key) ; |
| 159 |
|
| 160 |
/** |
| 161 |
* Set the pointer to the first child of this node . |
| 162 |
*/ |
| 163 |
void First(); |
| 164 |
|
| 165 |
/** |
| 166 |
* Return the next child of this node. |
| 167 |
*/ |
| 168 |
TreeNode* Next(); |
| 169 |
|
| 170 |
/** |
| 171 |
* Test if this node has more childs. |
| 172 |
* This call is usually used before issuing a Next() call. |
| 173 |
* \return true if the next call to Next() will return a valid child pointer |
| 174 |
*/ |
| 175 |
bool HasMore(); |
| 176 |
std::map<TreeNode*, TreeNode*>& GetChilds() { return m_childs; } |
| 177 |
}; |
| 178 |
|
| 179 |
template <class TKey, class TData> |
| 180 |
TreeNode<TKey, TData>::TreeNode(const TKey& key, const TData& data, TreeNode* parent) |
| 181 |
: m_key(key), m_data(data), m_parent(parent) |
| 182 |
{ |
| 183 |
} |
| 184 |
|
| 185 |
template <class TKey, class TData> |
| 186 |
TreeNode<TKey, TData>::~TreeNode() |
| 187 |
{ |
| 188 |
typename std::map<TreeNode*, TreeNode*>::iterator iter = m_childs.begin(); |
| 189 |
for(; iter != m_childs.end(); iter++) |
| 190 |
{ |
| 191 |
delete iter->second; |
| 192 |
} |
| 193 |
m_childs.clear(); |
| 194 |
} |
| 195 |
|
| 196 |
template <class TKey, class TData> |
| 197 |
TreeNode<TKey, TData>* TreeNode<TKey, TData>::AddChild(const TKey& key, const TData& data) |
| 198 |
{ |
| 199 |
TreeNode* newNode = new TreeNode(key, data, this); |
| 200 |
m_childs[newNode] = newNode; |
| 201 |
return newNode; |
| 202 |
} |
| 203 |
|
| 204 |
template <class TKey, class TData> |
| 205 |
TreeNode<TKey, TData>* TreeNode<TKey, TData>::AddChild(TreeNode* newNode) |
| 206 |
{ |
| 207 |
m_childs[newNode] = newNode; |
| 208 |
return newNode; |
| 209 |
} |
| 210 |
|
| 211 |
template <class TKey, class TData> |
| 212 |
TreeNode<TKey, TData>* TreeNode<TKey, TData>::Remove(const TKey& key) |
| 213 |
{ |
| 214 |
TreeNode* node = Find(key); |
| 215 |
if( node ) |
| 216 |
{ |
| 217 |
if(NULL == node->m_parent) |
| 218 |
{ |
| 219 |
// Cant remove root |
| 220 |
return NULL; |
| 221 |
} |
| 222 |
|
| 223 |
typename std::map<TreeNode*, TreeNode*>::iterator iter = node->m_parent->m_childs.find(node); |
| 224 |
if(iter != node->m_parent->m_childs.end()) |
| 225 |
node->m_parent->m_childs.erase(iter); |
| 226 |
return node; |
| 227 |
} |
| 228 |
return NULL; |
| 229 |
} |
| 230 |
|
| 231 |
template <class TKey, class TData> |
| 232 |
TreeNode<TKey, TData>* TreeNode<TKey, TData>::Find(const TKey& key) |
| 233 |
{ |
| 234 |
if(m_key == key) |
| 235 |
return this; |
| 236 |
|
| 237 |
typename std::map<TreeNode*, TreeNode*>::iterator iter; |
| 238 |
|
| 239 |
// Scan first the childs of this node |
| 240 |
for(iter = m_childs.begin(); iter != m_childs.end(); iter++) |
| 241 |
{ |
| 242 |
if(((TreeNode*)iter->second)->GetKey() == key) |
| 243 |
return iter->second; |
| 244 |
} |
| 245 |
|
| 246 |
// Scan level below |
| 247 |
for(iter = m_childs.begin(); iter != m_childs.end(); iter++) |
| 248 |
{ |
| 249 |
TreeNode* node = NULL; |
| 250 |
node = iter->second->Find(key); |
| 251 |
if(node) return node; |
| 252 |
} |
| 253 |
return NULL; |
| 254 |
} |
| 255 |
|
| 256 |
template <class TKey, class TData> |
| 257 |
void TreeNode<TKey, TData>::Print(std::ostream& stream, int depth) |
| 258 |
{ |
| 259 |
std::string tab = " "; |
| 260 |
for(int i=0; i<depth; i++) |
| 261 |
tab += " "; |
| 262 |
|
| 263 |
stream << tab.c_str() << m_key << std::endl; |
| 264 |
depth++; |
| 265 |
|
| 266 |
typename std::map<TreeNode*, TreeNode*>::iterator iter = m_childs.begin(); |
| 267 |
for(; iter != m_childs.end(); iter++) |
| 268 |
iter->second->Print(stream, depth); |
| 269 |
} |
| 270 |
|
| 271 |
/// Prepare for tree iteration in the current node |
| 272 |
template <class TKey, class TData> |
| 273 |
void TreeNode<TKey, TData>::First() |
| 274 |
{ |
| 275 |
m_pos = m_childs.begin(); |
| 276 |
} |
| 277 |
|
| 278 |
template <class TKey, class TData> |
| 279 |
TreeNode<TKey, TData>* TreeNode<TKey, TData>::Next() |
| 280 |
{ |
| 281 |
if(!HasMore()) |
| 282 |
return NULL; |
| 283 |
TreeNode* nextElem = m_pos->second; |
| 284 |
m_pos++; |
| 285 |
return nextElem; |
| 286 |
} |
| 287 |
|
| 288 |
template <class TKey, class TData> |
| 289 |
bool TreeNode<TKey, TData>::HasMore() |
| 290 |
{ |
| 291 |
return (m_pos != m_childs.end()); |
| 292 |
} |
| 293 |
|
| 294 |
//---------------------------------------------------------------- |
| 295 |
// TreeWalker class |
| 296 |
//---------------------------------------------------------------- |
| 297 |
|
| 298 |
template <class TKey, class TData> |
| 299 |
class TreeWalker |
| 300 |
{ |
| 301 |
private: |
| 302 |
void GetChildren(TreeNode<TKey, TData>* node); |
| 303 |
std::vector<TreeNode<TKey, TData>* > m_children; |
| 304 |
size_t m_pos; |
| 305 |
|
| 306 |
public: |
| 307 |
TreeWalker(TreeNode<TKey, TData>* node); |
| 308 |
virtual ~TreeWalker(); |
| 309 |
bool End(); |
| 310 |
void operator++(int); |
| 311 |
TreeNode<TKey, TData>* GetNode() |
| 312 |
{ |
| 313 |
if(m_pos < m_children.size()) |
| 314 |
return m_children[m_pos]; |
| 315 |
return NULL; |
| 316 |
} |
| 317 |
}; |
| 318 |
|
| 319 |
template <class TKey, class TData> |
| 320 |
TreeWalker<TKey, TData>::TreeWalker(TreeNode<TKey, TData>* node) |
| 321 |
: m_pos(0) |
| 322 |
{ |
| 323 |
m_children.push_back(node); |
| 324 |
GetChildren(node); |
| 325 |
} |
| 326 |
|
| 327 |
template <class TKey, class TData> |
| 328 |
TreeWalker<TKey, TData>::~TreeWalker() |
| 329 |
{ |
| 330 |
} |
| 331 |
|
| 332 |
template <class TKey, class TData> |
| 333 |
void TreeWalker<TKey, TData>::GetChildren(TreeNode<TKey, TData>* node) |
| 334 |
{ |
| 335 |
if(node == NULL) |
| 336 |
return; |
| 337 |
typename std::map<TreeNode<TKey, TData>*, TreeNode<TKey, TData>*>::iterator iter = node->GetChilds().begin(); |
| 338 |
for(; iter != node->GetChilds().end(); iter++) |
| 339 |
{ |
| 340 |
m_children.push_back(iter->second); |
| 341 |
GetChildren(iter->second); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
template <class TKey, class TData> |
| 346 |
bool TreeWalker<TKey, TData>::End() |
| 347 |
{ |
| 348 |
return m_pos == m_children.size(); |
| 349 |
} |
| 350 |
|
| 351 |
template <class TKey, class TData> |
| 352 |
void TreeWalker<TKey, TData>::operator++(int) |
| 353 |
{ |
| 354 |
m_pos++; |
| 355 |
} |
| 356 |
|
| 357 |
#endif // CODELITE_TREE_NODE_H |