顺序树的一些常用方法实现

#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;
};

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值