0.目录
1.树中属性操作的实现
2.树形结构的层次遍历
3.小结
1.树中属性操作的实现
树中结点的数目:
- 定义功能:count(node)
- 在 node 为根结点的树中统计结点数目
树结点数目的计算示例:
- count(A) = count(B) + count(C) + count(D) + 1
在GTree.h中实现统计结点数目:
protected:
int count(GTreeNode<T>* node) const
{
int ret = 0;
if( node != NULL )
{
ret = 1;
for(node->child.move(0); !node->child.end(); node->child.next())
{
ret += count(node->child.current());
}
}
return ret;
}
public:
int count() const
{
return count(root());
}
树的高度:
- 定义功能:height(node)
- 获取 node 为根结点的树的高度
树的高度计算示例:
- height(A) = MAX{ height(B), height(C), height(D) } + 1
在GTree.h中实现获取树的高度:
protected:
int height(GTreeNode<T>* node) const
{
int ret = 0;
if( node != NULL )
{
for(node->child.move(0); !node->child.end(); node->child.next())
{
int h = height(node->child.current());
if( ret < h )
{
ret = h;
}
}
ret = ret + 1;
}
return ret;
}
public:
int height() const
{
return height(root());
}
树的度数:
- 定义功能:degree(node)
- 获取 node 为根结点的树的度数
树的度计算示例:
- degree(A) = MAX{ degree(B), degree(C), degree(D), 3 }
在GTree.h中实现获取树的度数:
protected:
int degree(GTreeNode<T>* node) const
{
int ret = 0;
if( node != NULL )
{
ret = node->child.length();
for(node->child.move(0); !node->child.end(); node->child.next())
{
int d = degree(node->child.current());
if( ret < d )
{
ret = d;
}
}
}
return ret;
}
public:
int degree() const
{
return degree(root());
}
统一mian.cpp测试:
#include <iostream>
#include "GTree.h"
using namespace std;
using namespace StLib;
int main()
{
GTree<char> t;
GTreeNode<char>* node = NULL;
GTreeNode<char> root;
root.value = 'A';
root.parent = NULL;
t.insert(&root);
node = t.find('A');
t.insert('B', node);
t.insert('C', node);
t.insert('D', node);
node = t.find('B');
t.insert('E', node);
t.insert('F', node);
node = t.find('E');
t.insert('K', node);
t.insert('L', node);
node = t.find('C');
t.insert('G', node);
node = t.find('D');
t.insert('H', node);
t.insert('I', node);
t.insert('J', node);
node = t.find('H');
t.insert('M', node);
cout << t.count() << endl;
cout << t.height() << endl;
cout << t.degree() << endl;
return 0;
}
运行结果为:
13
4
3
2.树形结构的层次遍历
问题:
- 如何按层次遍历通用树结构中的每一个数据元素?
当前的事实:
- 树是非线性的数据结构,树的结点没有固定的编号方式
新的需求:
- 为通用树结构提供新的方法,快速遍历每一个结点
设计思路(游标):
- 在树中定义一个游标 ( GTreeNode
* ) - 遍历开始前将游标指向根结点 ( root() )
- 获取游标指向的数据元素
- 通过结点中的 child 成员移动游标
提供一组遍历相关的函数,按层次访问树中的数据元素。
层次遍历算法:
- 原料:
class LinkQueue<T>;
- 游标:
LinkQueue<T>: :front();
- 思想:
- begin() → 将根结点压入队列中
- current() → 访问队头元素指向的数据元素
- next() → 队头元素弹出,将队头元素的孩子压入队列中 ( 核心 )
- end() → 判断队列是否为空
层次遍历算法示例:
在GTree.h中实现树形结构的层次遍历:
protected:
LinkQueue<GTreeNode<T>*> m_queue;
public:
bool begin()
{
bool ret = (root() != NULL);
if( ret )
{
m_queue.clear();
m_queue.add(root());
}
return ret;
}
bool end()
{
return (m_queue.length() == 0);
}
bool next()
{
bool ret = (m_queue.length() > 0);
if( ret )
{
GTreeNode<T>* node = m_queue.front();
m_queue.remove();
for(node->child.move(0); !node->child.end(); node->child.next())
{
m_queue.add(node->child.current());
}
}
return ret;
}
T current()
{
if( !end() )
{
return m_queue.front()->value;
}
else
{
THROW_EXCEPTION(InvalidOperationException, "No value at current position ...");
}
}
然后在其他地方加入对应的清空队列的代码:
public:
SharedPointer< Tree<T> > remove(const T& value)
{
GTree<T>* ret = NULL;
GTreeNode<T>* node = find(value);
if( node == NULL )
{
THROW_EXCEPTION(InvalidParameterException, "Can not find the node via parameter value ...");
}
else
{
remove(node, ret);
m_queue.clear();
}
return ret;
}
SharedPointer< Tree<T> > remove(TreeNode<T>* node)
{
GTree<T>* ret = NULL;
node = find(node);
if( node == NULL )
{
THROW_EXCEPTION(InvalidParameterException, "Parameter node is invalid ...");
}
else
{
remove(dynamic_cast<GTreeNode<T>*>(node), ret);
m_queue.clear();
}
return ret;
}
最后把GTree和GTreeNode类的拷贝构造和赋值操作都声明为保护成员函数:
GTree.h
protected:
GTree(const GTree<T>&);
GTree<T>& operator = (const GTree<T>&);
public:
GTree()
{
}
GTreeNode.h
protected:
GTreeNode(const GTreeNode<T>&);
GTreeNode<T>& operator = (const GTreeNode<T>&);
最终main.cpp测试
#include <iostream>
#include "GTree.h"
using namespace std;
using namespace StLib;
int main()
{
GTree<char> t;
GTreeNode<char>* node = NULL;
GTreeNode<char> root;
root.value = 'A';
root.parent = NULL;
t.insert(&root);
node = t.find('A');
t.insert('B', node);
t.insert('C', node);
t.insert('D', node);
node = t.find('B');
t.insert('E', node);
t.insert('F', node);
node = t.find('E');
t.insert('K', node);
t.insert('L', node);
node = t.find('C');
t.insert('G', node);
node = t.find('D');
t.insert('H', node);
t.insert('I', node);
t.insert('J', node);
node = t.find('H');
t.insert('M', node);
for(t.begin(); !t.end(); t.next())
{
cout << t.current();
}
cout << endl;
return 0;
}
运行结果为:
ABCDEFGHIJKLM
3.小结
- 树的结点没有固定的编号方式
- 可以按照层次关系对树中的结点进行遍历
- 通过游标的思想设计遍历成员函数
- 遍历成员函数是相互依赖,相互配合的关系
- 遍历算法的核心为队列的使用
最终的GTree.h代码:
#ifndef GTREE_H
#define GTREE_H
#include "Tree.h"
#include "GTreeNode.h"
#include "Exception.h"
#include "LinkQueue.h"
namespace StLib
{
template <typename T>
class GTree : public Tree<T>
{
protected:
LinkQueue<GTreeNode<T>*> m_queue;
GTree(const GTree<T>&);
GTree<T>& operator = (const GTree<T>&);
GTreeNode<T>* find(GTreeNode<T>* node, const T& value) const
{
GTreeNode<T>* ret = NULL;
if( node != NULL )
{
if( node->value == value )
{
return node;
}
else
{
for(node->child.move(0); !node->child.end() && (ret == NULL); node->child.next())
{
ret = find(node->child.current(), value);
}
}
}
return ret;
}
GTreeNode<T>* find(GTreeNode<T>* node, GTreeNode<T>* obj) const
{
GTreeNode<T>* ret = NULL;
if( node == obj )
{
return node;
}
else
{
if( node != NULL )
{
for(node->child.move(0); !node->child.end() && (ret == NULL); node->child.next())
{
ret = find(node->child.current(), obj);
}
}
}
return ret;
}
void free(GTreeNode<T>* node)
{
if( node != NULL )
{
for(node->child.move(0); !node->child.end(); node->child.next())
{
free(node->child.current());
}
if( node->flag() )
{
delete node;
}
}
}
void remove(GTreeNode<T>* node, GTree<T>*& ret)
{
ret = new GTree<T>();
if( ret == NULL )
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create new tree ...");
}
else
{
if( root() == node )
{
this->m_root = NULL;
}
else
{
LinkList<GTreeNode<T>*>& child = dynamic_cast<GTreeNode<T>*>(node->parent)->child;
child.remove(child.find(node));
node->parent = NULL;
}
ret->m_root = node;
}
}
int count(GTreeNode<T>* node) const
{
int ret = 0;
if( node != NULL )
{
ret = 1;
for(node->child.move(0); !node->child.end(); node->child.next())
{
ret += count(node->child.current());
}
}
return ret;
}
int height(GTreeNode<T>* node) const
{
int ret = 0;
if( node != NULL )
{
for(node->child.move(0); !node->child.end(); node->child.next())
{
int h = height(node->child.current());
if( ret < h )
{
ret = h;
}
}
ret = ret + 1;
}
return ret;
}
int degree(GTreeNode<T>* node) const
{
int ret = 0;
if( node != NULL )
{
ret = node->child.length();
for(node->child.move(0); !node->child.end(); node->child.next())
{
int d = degree(node->child.current());
if( ret < d )
{
ret = d;
}
}
}
return ret;
}
public:
GTree()
{
}
bool insert(TreeNode<T>* node)
{
bool ret = true;
if( node != NULL )
{
if( this->m_root == NULL )
{
node->parent = NULL;
this->m_root = node;
}
else
{
GTreeNode<T>* np = find(node->parent);
if( np != NULL )
{
GTreeNode<T>* n = dynamic_cast<GTreeNode<T>*>(node);
if( np->child.find(n) < 0 )
{
np->child.insert(n);
}
}
else
{
THROW_EXCEPTION(InvalidOperationException, "Invalid parent tree node ...");
}
}
}
else
{
THROW_EXCEPTION(InvalidParameterException, "Parameter node cannot be NULL ...");
}
return ret;
}
bool insert(const T& value, TreeNode<T>* parent)
{
bool ret = true;
GTreeNode<T>* node = GTreeNode<T>::NewNode();
if( node != NULL )
{
node->value = value;
node->parent = parent;
insert(node);
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create new tree node ...");
}
return ret;
}
SharedPointer< Tree<T> > remove(const T& value)
{
GTree<T>* ret = NULL;
GTreeNode<T>* node = find(value);
if( node == NULL )
{
THROW_EXCEPTION(InvalidParameterException, "Can not find the node via parameter value ...");
}
else
{
remove(node, ret);
m_queue.clear();
}
return ret;
}
SharedPointer< Tree<T> > remove(TreeNode<T>* node)
{
GTree<T>* ret = NULL;
node = find(node);
if( node == NULL )
{
THROW_EXCEPTION(InvalidParameterException, "Parameter node is invalid ...");
}
else
{
remove(dynamic_cast<GTreeNode<T>*>(node), ret);
m_queue.clear();
}
return ret;
}
GTreeNode<T>* find(const T& value) const
{
return find(root(), value);
}
GTreeNode<T>* find(TreeNode<T>* node) const
{
return find(root(), dynamic_cast<GTreeNode<T>*>(node));
}
GTreeNode<T>* root() const
{
return dynamic_cast<GTreeNode<T>*>(this->m_root);
}
int degree() const
{
return degree(root());
}
int count() const
{
return count(root());
}
int height() const
{
return height(root());
}
void clear()
{
free(root());
this->m_root = NULL;
m_queue.clear();
}
bool begin()
{
bool ret = (root() != NULL);
if( ret )
{
m_queue.clear();
m_queue.add(root());
}
return ret;
}
bool end()
{
return (m_queue.length() == 0);
}
bool next()
{
bool ret = (m_queue.length() > 0);
if( ret )
{
GTreeNode<T>* node = m_queue.front();
m_queue.remove();
for(node->child.move(0); !node->child.end(); node->child.next())
{
m_queue.add(node->child.current());
}
}
return ret;
}
T current()
{
if( !end() )
{
return m_queue.front()->value;
}
else
{
THROW_EXCEPTION(InvalidOperationException, "No value at current position ...");
}
}
~GTree()
{
clear();
}
};
}
#endif // GTREE_H