二叉树

/* CLASS_BEGIN *****************************************************
   类名:TBinaryMap
   功能:二叉树,支持 增、 删、改、查、及查 < 查询值的第一个节点
    实现:	Snight Q:51171107
   CLASS_END  ******************************************************/
#ifndef H_TBinaryMap_H
#define H_TBinaryMap_H

typedef	int						BOOL;

 #ifndef TRUE
 	#define TRUE (1)
 #endif
 
 #ifndef FALSE
 	#define FALSE (1)
 #endif


 template <class TKey, class TValue>
 class TreeNode
 {
 public:
 	TKey	m_oKey;
 	TValue	m_oValue;
 };
 
 template <class TKey,class TVale>
 class TBinaryMap
 {
 public:
 	TBinaryMap(void) 
 	: m_iVolume(10)
 	, m_iObjCnt(0)
 	{
 		m_poObjArray = new TreeNode<TKey, TVale>[m_iVolume];
 		memset(m_poObjArray, 0 , sizeof(TreeNode<TKey, TVale>)*m_iVolume);
 	}
 	~TBinaryMap(void)
 	{
 		m_iVolume = 0;
 		m_iObjCnt = 0;
 		delete [] m_poObjArray;
 		m_poObjArray = NULL;
 	}
 
 	// []操作符重载
 	TVale& operator [] (int aiIndex) 
 	{ return m_poObjArray[aiIndex].m_oValue; } 
 
 	// 值查找
 	BOOL Find(TKey aoKey, TVale& aoValue,int& aiPos)
 	{ return  find(aoKey, aoValue, m_iObjCnt, 0, aiPos); }
 
 	// 范围查找
 	int LeftNearby(TKey aoKey, TVale& aoValueNearby) 
 	{ return leftNearby(aoKey, aoValueNearby, m_iObjCnt, 0);}
 
 	// 插入
 	int Insert(TKey aoKey, TVale& aoValue)
 	{ return insert (aoKey, aoValue); }
 
 	// 删除
 	BOOL Erase(TKey aoKey)
 	{ return erase(aoKey);}
 
 	// 大小
 	int Size(void)
 	{ return m_iObjCnt ;}
 
 	// 容积
 	int Volume(void)
 	{ return m_iVolume ;}
 
 private:
 	int insert(TKey aoKey, TVale& aoValue)
 	{
 		// 扩充容积
 		if (m_iObjCnt >= (m_iVolume-1))
 		{
 			// 容积扩张两倍
 			m_iVolume *= 2;
 			TreeNode<TKey, TVale>* lpTempTreeNode = m_poObjArray;
 			m_poObjArray = new TreeNode<TKey, TVale>[m_iVolume];
 			memset(m_poObjArray, 0 , sizeof(TreeNode<TKey, TVale>)*m_iVolume);
 			memcpy(m_poObjArray, lpTempTreeNode, sizeof(TreeNode<TKey, TVale>)*m_iObjCnt);
 		}
 
 		// 尚无数据呢
 		if (0 == m_iObjCnt)
 		{
 			m_poObjArray[0].m_oKey = aoKey;
 			m_poObjArray[0].m_oValue = aoValue;
 			++m_iObjCnt;
 			return 0;
 		}
 
 		int liPos =  leftNearbyNoValue(aoKey, m_iObjCnt, 0);
  		
 		// 替换
 		if (m_poObjArray[liPos].m_oKey == aoKey)
  		{
 			m_poObjArray[liPos].m_oValue = aoValue;
 			return liPos;
  		}
 
 		if(liPos < m_iVolume)
 		{
 			// 首部
 			if (-1 == liPos)
 			{
 				TreeNode<TKey, TVale>* lpoObjArrayTemp = new TreeNode<TKey, TVale>[m_iObjCnt];
 				memcpy(lpoObjArrayTemp, m_poObjArray, sizeof(TreeNode<TKey, TVale>)*m_iObjCnt);
 				memcpy(m_poObjArray+1, lpoObjArrayTemp,  sizeof(TreeNode<TKey, TVale>)*m_iObjCnt);
 				m_poObjArray[0].m_oKey = aoKey;
 				m_poObjArray[0].m_oValue = aoValue;
 				delete []lpoObjArrayTemp;
 				++m_iObjCnt;
 			}
 			// 尾部
 			else if ((m_iObjCnt-1) == liPos) 
 			{
 				m_poObjArray[m_iObjCnt].m_oKey = aoKey;
 				m_poObjArray[m_iObjCnt].m_oValue = aoValue;
 				liPos = m_iObjCnt;
 				++m_iObjCnt;
 			}
 			// 腹地
 			else
 			{
 				int liTailObj =  m_iObjCnt- liPos-1;
 				TreeNode<TKey, TVale>* lpoObjArrayTemp = new TreeNode<TKey, TVale>[liTailObj];
 				memcpy(lpoObjArrayTemp, m_poObjArray+liPos+1, sizeof(TreeNode<TKey, TVale>)*liTailObj);
 				memcpy(m_poObjArray+liPos+2, lpoObjArrayTemp, sizeof(TreeNode<TKey, TVale>)*liTailObj);
 				m_poObjArray[liPos+1].m_oKey = aoKey;
 				m_poObjArray[liPos+1].m_oValue = aoValue;
 				delete [] lpoObjArrayTemp;
 				++m_iObjCnt;
 			}
 		}
 		return liPos;
 	}
 
 	BOOL erase(TKey aoKey)
 	{
 		TVale loValue;
 		int liPos = 0;
 		// 如果有节点
 		if (find(aoKey, loValue, m_iObjCnt,0, liPos))
 		{
 			if ((liPos >= 0 ) && (liPos < m_iObjCnt))
 			{
 				memcpy(m_poObjArray+liPos, m_poObjArray+(liPos+1), (m_iObjCnt - liPos - 1)* sizeof(TreeNode<TKey, TVale>));
 				m_iObjCnt--;
 				return TRUE;
 			}
 		}
 		return FALSE;
 	}
 
 	// 查找指定节点
 	BOOL find(TKey aoKey, TVale& aoValue,  int aiObjCnt, int aiBegPos, int& aiPos)
 	{
 		// 最后一个节点
 		if ((1 >= aiObjCnt)&&(aiBegPos >= 0))
 		{
 			TreeNode<TKey, TVale> loNode = m_poObjArray[aiBegPos];
 			// 查找到目标 或者查找到了最接近的值
 			if (loNode.m_oKey == aoKey)
 			{
 				aiPos = aiBegPos;
 				aoValue	 = loNode.m_oValue; 
 				return TRUE;
 			}
 			return FALSE;
 		}
 		//中间位置
 		int liMidPos = 0;
 		// 奇数节点
 		if (0 < (aiObjCnt%2))
 		{
 			liMidPos = (aiObjCnt/2) + aiBegPos;
 
 			if ((0 > liMidPos)||(m_iObjCnt  <= liMidPos))  
 				return FALSE;
 
 			// 中间节点
 			TreeNode<TKey, TVale> loMidNode =  m_poObjArray[liMidPos];
 
 			// 查找到目标 或者查找到了最接近的值
 			if (loMidNode.m_oKey == aoKey)
 			{
 				aiPos = liMidPos;
 				aoValue	 = loMidNode.m_oValue; 
 				return TRUE;
 			}
 			// 右向查找
 			else if(loMidNode.m_oKey < aoKey)
 				return find(aoKey, aoValue, (aiObjCnt/2), liMidPos+1, aiPos);
 			// 左向查找
 			else
 				return find(aoKey, aoValue, (aiObjCnt/2)  ,aiBegPos, aiPos);
 		}
 		// 偶数节点
 		else
 		{
 			// 计算出中间位置
 			liMidPos = (aiObjCnt/2) - 1 + aiBegPos;
 
 			if ((0 > liMidPos)||(m_iObjCnt  <= liMidPos)) 
 				return FALSE;
 
 			// 中间节点
 			TreeNode<TKey, TVale> loMidNode =  m_poObjArray[liMidPos];
 
 			// 查找到目标 或者查找到了最接近的值
 			if (loMidNode.m_oKey == aoKey)
 			{
 				aiPos = liMidPos;
 				aoValue	=	loMidNode.m_oValue; 
 				return TRUE;
 			}
 			// 右向查找 
 			else if(loMidNode.m_oKey < aoKey)
 				return find(aoKey, aoValue, (aiObjCnt/2), liMidPos+1, aiPos);
 			// 左向查找
 			else
 				return find(aoKey, aoValue, (aiObjCnt/2)-1  ,aiBegPos, aiPos);	
 		}
 		return FALSE;
 	}
 
 
 	// 找到 >= aoKey 的节点
 	int leftNearby(TKey aoKey, TVale& aoValue, int aiObjCnt, int aiBegPos)
 	{
 		// 最后一个节点
 		if ((1 >= aiObjCnt)&&(aiBegPos >= 0))
 		{
 			TreeNode<TKey, TVale> loNode = m_poObjArray[aiBegPos];
  			if (aoKey < loNode.m_oKey)
  			{
 				if (aiBegPos > 0)
 				{
 					loNode = m_poObjArray[aiBegPos-1];
 					aoValue	 = loNode.m_oValue; 
 					return (aiBegPos-1);
 				}
 				else
 				{
 					aoValue	 = loNode.m_oValue; 
 					return -1;
 				}
  			}
 			aoValue	 = loNode.m_oValue; 
 			return aiBegPos;
 		}
 		//中间位置
 		int liMidPos = 0;
 		// 奇数节点
 		if (0 < (aiObjCnt%2))
 		{
 			liMidPos = (aiObjCnt/2) + aiBegPos;
 
 			if ((0 > liMidPos)||(m_iObjCnt  <= liMidPos))  
 				return -2;
 
 			// 中间节点
 			TreeNode<TKey, TVale> loMidNode =  m_poObjArray[liMidPos];
 
 			// 查找到目标 或者查找到了最接近的值
 			if (loMidNode.m_oKey == aoKey)
 			{
 				aoValue	 = loMidNode.m_oValue; 
 				return liMidPos;
 			}
 			// 右向查找
 			else if(loMidNode.m_oKey < aoKey)
 				return leftNearby(aoKey, aoValue, (aiObjCnt/2), liMidPos+1);
 			// 左向查找
 			else
 				return leftNearby(aoKey, aoValue, (aiObjCnt/2)  ,aiBegPos);
 		}
 		// 偶数节点
 		else
 		{
 			// 计算出中间位置
 			liMidPos = (aiObjCnt/2) - 1 + aiBegPos;
 
 			if ((0 > liMidPos)||(m_iObjCnt  <= liMidPos)) 
 				return -2;
 
 			// 中间节点
 			TreeNode<TKey, TVale> loMidNode =  m_poObjArray[liMidPos];
 
 			// 查找到目标 或者查找到了最接近的值
 			if (loMidNode.m_oKey == aoKey)
 			{
 				aoValue	=	loMidNode.m_oValue; 
 				return liMidPos;
 			}
 			// 右向查找 
 			else if(loMidNode.m_oKey < aoKey)
 				return leftNearby(aoKey, aoValue, (aiObjCnt/2), liMidPos+1);
 			// 左向查找
 			else
 				return leftNearby(aoKey, aoValue, (aiObjCnt/2)-1  ,aiBegPos);	
 		}
 		return -2;
 	}
 
 	// 找到 >= aoKey 的节点
 	int leftNearbyNoValue(TKey aoKey, int aiObjCnt, int aiBegPos)
 	{
 		// 最后一个节点
 		if ((1 >= aiObjCnt)&&(aiBegPos >= 0))
 		{
 			TreeNode<TKey, TVale> loNode = m_poObjArray[aiBegPos];
 			if (aoKey < loNode.m_oKey)
 			{
 				if (aiBegPos > 0)
 					return (aiBegPos-1);
 				else
 					return -1;
 			}
 			return aiBegPos;
 		}
 		//中间位置
 		int liMidPos = 0;
 		// 奇数节点
 		if (0 < (aiObjCnt%2))
 		{
 			liMidPos = (aiObjCnt/2) + aiBegPos;
 
 			if ((0 > liMidPos)||(m_iObjCnt  <= liMidPos))  
 				return -2;
 
 			// 中间节点
 			TreeNode<TKey, TVale> loMidNode =  m_poObjArray[liMidPos];
 
 			// 查找到目标 或者查找到了最接近的值
 			if (loMidNode.m_oKey == aoKey)
 				return liMidPos;
 			// 右向查找
 			else if(loMidNode.m_oKey < aoKey)
 				return leftNearbyNoValue(aoKey, (aiObjCnt/2), liMidPos+1);
 			// 左向查找
 			else
 				return leftNearbyNoValue(aoKey, (aiObjCnt/2)  ,aiBegPos);
 		}
 		// 偶数节点
 		else
 		{
 			// 计算出中间位置
 			liMidPos = (aiObjCnt/2) - 1 + aiBegPos;
 
 			if ((0 > liMidPos)||(m_iObjCnt  <= liMidPos)) 
 				return -2;
 
 			// 中间节点
 			TreeNode<TKey, TVale> loMidNode =  m_poObjArray[liMidPos];
 
 			// 查找到目标 或者查找到了最接近的值
 			if (loMidNode.m_oKey == aoKey)
 				return liMidPos;
 
 			// 右向查找 
 			else if(loMidNode.m_oKey < aoKey)
 				return leftNearbyNoValue(aoKey, (aiObjCnt/2), liMidPos+1);
 			// 左向查找
 			else
 				return leftNearbyNoValue(aoKey, (aiObjCnt/2)-1  ,aiBegPos);	
 		}
 		return -2;
 	}
 
 private:
 	TreeNode<TKey, TVale>*		m_poObjArray;	//	对象列表
 	int							m_iVolume;		//	容积
 	int							m_iObjCnt;		//  对象个数
 };
#endif//H_TBinaryMap_H


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值