#ifndef SAFE_DELARR
#define SAFE_DELARR(p) {if(p){delete []p;p=nullptr;}}
#endif
enum TRAVERSEKIND
{
TK_FRONT,
TK_MID,
TK_END
};
template<class T>
class arraytree
{
public:
arraytree()
{
m_pData=nullptr;
m_DataNum=0;
m_Capacity=0;
}
void Init()
{
SAFE_DELARR(m_pData);
m_DataNum=0;
m_Capacity=0;
}
int GetRoot()
{
return 0;
}
int GetParent(int nodeindex)
{
if(nodeindex < 1)
{
return -1;
}
else
{
return (nodeindex-1) / 2;
}
}
int GetChild(int nodeindex,bool Isleft)
{
if(Isleft)
{
int Lchild=nodeindex*2 + 1;
if(Lchild < m_DataNum)
{
return Lchild;
}
else
{
return -1;
}
}
else
{
int Rchlid=nodeindex*2 + 2;
if(Rchild < m_DataNum)
{
return Rchild;
}
else
{
return -1;
}
}
}
int GetRightSinLing(int nodeindex)//获取右边兄弟节点
{
if(nodeindex < 1)
{
return -1;
}
else
{
int rightsinling= nodeindex + 1;
if(rightsinling < m_DataNum)
{
if((nodeindex-1)/2 ==(rightsinling-1)/2)//判断是否是同一个父亲
{
return rightsinling;
}
else
{
return -1;
}
}
else
{
return -1;
}
}
}
void Insert(T const &data)
{
if (m_DataNum >= m_Capacity)
{
int _newsize = (m_Capacity * 2 > 0) ? (m_Capacity * 2) : 1;
T *pNewData = new T[_newsize];
for (int i = 0; i < m_DataNum; ++i)
{
pNewData[i] = pData[i];
}
SAFE_DELARR(pData);
pData = pNewData;
m_Capacity = _newsize;
}
pData[m_DataNum++] = data;
}
void Delete()
{
if(m_DataNum > 0)
{
--m_DataNum;
}
}
void Traverse(TRAVERSEKIND tk)//遍历 思路:建立一个大小为m_DataNum的bool型数组,如果数组里面的false全部为ture则遍历完成
{
bool *pIsFind=new bool[m_DataNum];
memset(pIsFind,0,sizeof(bool)*m_DataNum);
//假设数据为 1 2 3 4 5 6 7
swith(tk)
{
case :TK_FRONT:先序遍历 1 2 4 5 3 6 7
{
bool IsOver=false;
int currentindex=0;
while(!IsOver)
{
if(!pIsFind[currentindex])
{
printf("%d ",m_pData[currentindex]);
pIsFind[currentindex]=true;
}
if(GetChild(currentindex,true)!=-1)//左孩子存在
{
if(!pIsFind[GetChild(cuurentindex,true)])//左孩子存在,为false
{
currentindex=GetChild(cuurentindex,true);
}
else//左孩子存在,为true
{
if(GetChild(currentindex,false)!=-1)//右孩子存在
{
if(!pIsFind[GetChild(cuurentindex,false)])//右孩子存在,为false
{
currentindex=GetChild(cuurentindex,false);
}
else
{
currentindex=GetParent(currentindex);
}
}
else//右孩子不存在
{
currentindex=GetParent(currentindex);
}
}
}
else
{
currentindex=GetParent(currentindex);
}
bool IsOver=ture;
for(int i=0;i<m_DataNum;++i)
{
IsOver=IsOver&&pIsFind(m_DataNum);
}
}
}
break;
case TK_MID://4 2 5 1 6 3 7
{
bool IsOver = false;
int currentindex = 0;
while (!IsOver)
{
if (GetChild(currentindex, true) != -1)
{
if (!pIsFind[GetChild(currentindex, true)])
{
currentindex = GetChild(currentindex, true);
}
else
{
if (!pIsFind[currentindex])
{
printf("%d ", pData[currentindex]);
pIsFind[currentindex] = true;
}
if (GetChild(currentindex, false) != -1)
{
if (!pIsFind[GetChild(currentindex, false)])
{
currentindex = GetChild(currentindex, false);
}
else
{
currentindex = GetParent(currentindex);
}
}
else
{
currentindex = GetParent(currentindex);
}
}
}
else
{
if (!pIsFind[currentindex])
{
printf("%d ", pData[currentindex]);
pIsFind[currentindex] = true;
}
currentindex = GetParent(currentindex);
}
IsOver = true;
for (int i = 0; i < m_DataNum; ++i)
{
IsOver = IsOver&&pIsFind[i];
}
}
}
break;
case TK_END:
{
bool IsOver = false;
int currentindex = 0;
while (!IsOver)
{
if (GetChild(currentindex,true)!=-1)//先判断该节点有没有左孩子
{
if (!pIsFind[GetChild(currentindex, true)])
{
currentindex = GetChild(currentindex);
}
else
{
if (GetChild(currentindex, false) != -1)
{
if (!pIsFind[GetChild(currentindex, false)])
{
currentindex = GetChild(currentindex);
}
else
{
if (!pIsFind[currentindex])
{
printf("%d ", pData[currentindex]);
pIsFind[currentindex] = true;
}
currentindex = GetParent(currentindex);
}
}
else
{
if (!pIsFind[currentindex])
{
printf("%d ", pData[currentindex]);
pIsFind[currentindex] = true;
}
currentindex = GetParent(currentindex);
}
}
}
else //没有左孩子
{
if (!pIsFind[currentindex])
{
printf("%d ", pData[currentindex]);
pIsFind[currentindex] = true;
}
currentindex = GetParent(currentindex);
}
IsOver = true;
for (int i = 0; i < m_DataNum; ++i)
{
IsOver = IsOver&&pIsFind[i];
}
}
}
break;
}
printf("\n");
SAFE_DELARR(pIsFind);
}
void Clear()
{
m_DataNum=0;
}
~arraytree()
{
SAFE_DELARR(m_pData);
m_DataNum=0;
m_Capacity=0;
}
protected:
T *m_pData;
int m_DataNum;
int m_Capacity;
};
顺序树的一些常用方法实现
最新推荐文章于 2024-07-04 11:10:43 发布