////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// // // copyright : (C) 2008 by Eran Ifrah // file name : tree_node.h // // ------------------------------------------------------------------------- // A // _____ _ _ _ _ // / __ \ | | | | (_) | // | / \/ ___ __| | ___| | _| |_ ___ // | | / _ \ / _ |/ _ \ | | | __/ _ ) // | \__/\ (_) | (_| | __/ |___| | || __/ // \____/\___/ \__,_|\___\_____/_|\__\___| // // F i l e // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// #ifndef CODELITE_TREE_NODE_H #define CODELITE_TREE_NODE_H #include #include #include template class TreeNode { TKey m_key; TData m_data; TreeNode* m_parent; std::map m_childs; typename std::map::iterator m_pos; public: /** * Constructor, constructs a tree node with key and data. * If parent is left NULL, node is assumed to be root. * \param key Node key, must have operator =, < * \param data Node data, this class must have operator= * \param parent Parent node */ TreeNode(const TKey& key, const TData& data, TreeNode* parent = NULL); /** * Destructor, deletes this node and all its children. */ virtual ~TreeNode(); //--------------------------------------------------- // Setters / Getters //--------------------------------------------------- /** * Get node data. * \return node data */ TData& GetData() { return m_data; }; /** * Get node key. * \return key */ TKey& GetKey() { return m_key; }; /** * Set node data. * \param data data */ void SetData(const TData& data) { m_data = data; }; /** * Set node key. * \param key Key */ void SetKey(const TKey& key) { m_key = key; }; /** * Return the parent node of this node (or null if root). * \return pointer to parent node */ TreeNode* GetParent() { return m_parent; } //--------------------------------------------- // Misc //--------------------------------------------- /** * Print the tree to stream (default is stdout). */ void Print(std::ostream& stream = std::cout , int depth = 0); /** * Check if this node is root. * \return true if node is root */ bool IsRoot() const { return m_parent == NULL; }; //---------------------------------------------- // Operations //---------------------------------------------- /** * Add new child node to this node. * Duplicate nodes are allowed. However, Remove() will remove the first occurance of a node by a given key. * \param key Node key * \param data Node data * \return newly added node */ TreeNode* AddChild(const TKey& key, const TData& data) ; /** * \brief Append new child to this tree node * \param newNode node to append, must be allocated on the heap * \return the newly added tree node */ TreeNode* AddChild(TreeNode* newNode); /** * Remove first occurance of node with a given key. * If the node to be removed is the root, a std::exception* is thrown, which must be deleted by caller. * Remove does not delete the memory allocated for the node. The user must delete the removed node. * \param key Node key * \return removed node */ TreeNode* Remove(const TKey& key); /** * Find a node by a key. * \param key Node key * \return node, or NULL if non exist */ TreeNode* Find(const TKey& key) ; /** * Set the pointer to the first child of this node . */ void First(); /** * Return the next child of this node. */ TreeNode* Next(); /** * Test if this node has more childs. * This call is usually used before issuing a Next() call. * \return true if the next call to Next() will return a valid child pointer */ bool HasMore(); std::map& GetChilds() { return m_childs; } }; template TreeNode::TreeNode(const TKey& key, const TData& data, TreeNode* parent) : m_key(key), m_data(data), m_parent(parent) { } template TreeNode::~TreeNode() { typename std::map::iterator iter = m_childs.begin(); for(; iter != m_childs.end(); iter++) { delete iter->second; } m_childs.clear(); } template TreeNode* TreeNode::AddChild(const TKey& key, const TData& data) { TreeNode* newNode = new TreeNode(key, data, this); m_childs[newNode] = newNode; return newNode; } template TreeNode* TreeNode::AddChild(TreeNode* newNode) { m_childs[newNode] = newNode; return newNode; } template TreeNode* TreeNode::Remove(const TKey& key) { TreeNode* node = Find(key); if( node ) { if(NULL == node->m_parent) { // Cant remove root return NULL; } typename std::map::iterator iter = node->m_parent->m_childs.find(node); if(iter != node->m_parent->m_childs.end()) node->m_parent->m_childs.erase(iter); return node; } return NULL; } template TreeNode* TreeNode::Find(const TKey& key) { if(m_key == key) return this; typename std::map::iterator iter; // Scan first the childs of this node for(iter = m_childs.begin(); iter != m_childs.end(); iter++) { if(((TreeNode*)iter->second)->GetKey() == key) return iter->second; } // Scan level below for(iter = m_childs.begin(); iter != m_childs.end(); iter++) { TreeNode* node = NULL; node = iter->second->Find(key); if(node) return node; } return NULL; } template void TreeNode::Print(std::ostream& stream, int depth) { std::string tab = " "; for(int i=0; i::iterator iter = m_childs.begin(); for(; iter != m_childs.end(); iter++) iter->second->Print(stream, depth); } /// Prepare for tree iteration in the current node template void TreeNode::First() { m_pos = m_childs.begin(); } template TreeNode* TreeNode::Next() { if(!HasMore()) return NULL; TreeNode* nextElem = m_pos->second; m_pos++; return nextElem; } template bool TreeNode::HasMore() { return (m_pos != m_childs.end()); } //---------------------------------------------------------------- // TreeWalker class //---------------------------------------------------------------- template class TreeWalker { private: void GetChildren(TreeNode* node); std::vector* > m_children; size_t m_pos; public: TreeWalker(TreeNode* node); virtual ~TreeWalker(); bool End(); void operator++(int); TreeNode* GetNode() { if(m_pos < m_children.size()) return m_children[m_pos]; return NULL; } }; template TreeWalker::TreeWalker(TreeNode* node) : m_pos(0) { m_children.push_back(node); GetChildren(node); } template TreeWalker::~TreeWalker() { } template void TreeWalker::GetChildren(TreeNode* node) { if(node == NULL) return; typename std::map*, TreeNode*>::iterator iter = node->GetChilds().begin(); for(; iter != node->GetChilds().end(); iter++) { m_children.push_back(iter->second); GetChildren(iter->second); } } template bool TreeWalker::End() { return m_pos == m_children.size(); } template void TreeWalker::operator++(int) { m_pos++; } #endif // CODELITE_TREE_NODE_H