tree
来源:互联网 发布:网络机房设备回收 编辑:程序博客网 时间:2024/04/28 05:51
template<typename ValueType>
struct Node
{
public:
ValueType key;//树节点值
Node * m_pParent;//父节点
Node * m_pLChild;//左孩子节点,即第一个孩子节点
Node * m_pRChild;//右孩子节点,即父节点的兄弟节点
bool m_bIsFirstChild;//是否是其父节点的第一个孩子节点--> 是:true 否:false,根节点默认是第一个孩子节点
public:
Node()
{
m_pParent = m_pLChild = m_pRChild = 0x0;
m_bIsFirstChild = false;
}
Node(ValueType value)
{
m_pParent = m_pLChild = m_pRChild = 0x0;
key = value;
m_bIsFirstChild = false;
}
Node(ValueType value,bool isFirstChild)
{
m_pParent = m_pLChild = m_pRChild = 0x0;
key = value;
m_bIsFirstChild = isFirstChild;
}
virtual ~Node()
{
}
};
#pragma once
#include "Node.h"
template<typename ValueType>
class tree
{
public:
Node<ValueType> * m_Root;//根节点,物理上根节点没有有孩子节点,因为根节点没有兄弟节点
// enum DIRECT{LEFT,RIGHT};//LEFT:左孩子 RIGHT:右孩子
public:
tree(void)
{
m_Root = 0x0;
}
tree(Node<ValueType> * root)
{
m_Root = root;
}
tree(ValueType key)
{
m_Root = new Node<ValueType>(key,true);
}
~tree(void)
{
if(m_Root)
{//删除整棵树
physicalDel(m_Root->m_pLChild);
physicalDel(m_Root->m_pRChild);
delete m_Root;
}
}
bool physicalDel(Node<ValueType> * node)//物理删除
{//删除成功返回true ,否则返回 false
if( 0x0 == node)
{
return true;
}
if(node->m_pLChild)
{
physicalDel(node->m_pLChild);
}
if(node->m_pRChild)
{
physicalDel(node->m_pRChild);
}
if(node->m_bIsFirstChild)
{//被删除的节点是左孩子,则消除该将被删除节点的父节点的左孩子指针滞空
node->m_pParent->m_pLChild = 0x0;
}
else
{
node->m_pParent->m_pRChild = 0x0;
}
delete node;
node = 0x0;
return true;
}
bool insert(Node<ValueType> * node,Node<ValueType> * parent)//将node 节点作为parent的孩子节点插入
{//插入成功返回true ,否则返回 false
if(0x0 == node)//如果插入的节点为空,则直接返回true
return true;
if(0x0 == parent)//如果父节点为空
{
if(0x0 == m_Root)//如果根节点为空,则插入的节点作为根节点,返回true
{
m_Root = node;
m_Root->m_pLChild = m_Root->m_pParent = m_Root->m_pRChild = 0x0;
m_Root->m_bIsFirstChild = true;
return true;
}
else
{//如果根节点不为空,则返回false
return false;
}
}
node->m_pParent = parent;//指向父节点
node->m_pLChild = 0x0;//滞空被插入节点的左孩子节点,因为它没有逻辑上的孩子节点了
if(node->m_bIsFirstChild)//如果是作为第一个孩子节点插入,即作为父节点的左孩子节点
{
if(parent->m_pLChild)//如果被插入的父节点已经有了左孩子节点
{
parent->m_pLChild->m_bIsFirstChild = false;//原来的第一个孩子节点将作为第二个孩子节点了
node->m_pRChild = parent->m_pLChild;//原来的第一个孩子节点被当成被插入的节点的第一个兄弟节点
parent->m_pLChild->m_pParent = node;
parent->m_pLChild = node;
}
else
{//如果父节点的左孩子节点为空,则被插入节点直接作为左孩子节点
parent->m_pLChild = node;
node->m_pRChild = 0x0;
}
}
else
{//作为兄弟节点插入
if(parent->m_pRChild)//如果被插入的父节点已经有了右孩子节点
{
node->m_pRChild = parent->m_pRChild;//原来的第一个孩子节点被当成被插入的节点的第一个兄弟节点
parent->m_pRChild->m_pParent = node;
parent->m_pRChild = node;
}
else
{//如果父节点的右孩子节点为空,则被插入节点直接作为右孩子节点
parent->m_pRChild = node;
node->m_pRChild = 0x0;
}
}
return true;
}
bool insert(ValueType value,bool isFirstChild,Node<ValueType> * parent)
{
Node<ValueType> * pNode = new Node<ValueType>(value,isFirstChild);
return insert(pNode,parent);
}
bool logicalDel(Node<ValueType> * node)
{
bool bRet = physicalDel(node->m_pLChild);//删除该节点的所有孩子节点,包括所有 子孙 节点
if(node == m_Root)
{//如果是逻辑删除根节点,则和物理删除没有差别
bRet = bRet && physicalDel(node->m_pRChild);
delete node;
return bRet;
}
//node->m_pParent->m_pLChild = node->m_pRChild;
//node->m_pRChild->m_pParent = node->m_pParent;
if( node->m_bIsFirstChild && node->m_pRChild )
{//如果被删除的节点是它父节点的第一个孩子节点,并且 被删除的该节点有兄弟节点
node->m_pParent->m_pLChild = node->m_pRChild;
node->m_pRChild->m_pParent = node->m_pParent;
node->m_pRChild->m_bIsFirstChild = true;//则该兄弟节点成为其父节点的第一个孩子节点
}
else
{
node->m_pParent->m_pRChild = node->m_pRChild;
node->m_pRChild->m_pParent = node->m_pParent;
}
delete node;
node = 0x0;
return bRet;
}
Node<ValueType> * firstChild(Node<ValueType> * parent)
{//查找第一个逻辑孩子节点
return parent ? parent->m_pLChild : Ox0;
}
Node<ValueType> * nextBrother(Node<ValueType> * brother)
{//查找下一个逻辑兄弟
return brother ? brother->m_pRChild : Ox0;
}
Node<ValueType> * preBrother(Node<ValueType> * brother)
{//查找上一个逻辑兄弟
if(brother)
return brother->m_bIsFirstChild ? 0x0 : brother->m_pParent;
return 0x0;
}
};
- Tree
- tree
- tree
- TREE
- Tree
- Tree
- tree
- tree
- tree
- tree
- tree
- tree
- Tree
- Tree
- tree
- TREE
- tree
- tree
- oracle集合类型
- AIX5L_11GR2_rac_安装文档(STEP BY STEP)
- struts 2配置文件详解
- JDK,Tomcat,Apache安装与配置
- (转载)Android下 Affinities和Task
- tree
- AIX5L_11GR2_rac_安装文档(STEP BY STEP)
- C#:导入文本格式到sql数据库
- android布局技巧 ViewStub
- 同样的一辈子,不同的感受
- grep、sed与sort常用方法
- [转]零基础学Qt 4编程实例之四:理解并正确使用名字空间
- Java抓取网页 解析网页HtmlParser
- virtualbox下ubuntu与windows XP共享目录