zoukankan      html  css  js  c++  java
  • 使用二叉树实现CMap类封装

    #pragma once
    
    template<typename KEY, typename VALUE, typename ARG_KEY = KEY&, typename ARG_VALUE = VALUE&>
    class CMap
    {
    	struct SNode
    	{
    		KEY key;
    		VALUE value;
    		SNode *pLeft, *pRight;
    	};
    	SNode *m_pRoot;
    	int m_nSize;
    public:
    	typedef SNode * POSTION;
    	void SetAt(KEY key, ARG_VALUE value);
    	bool Lookup(KEY key, ARG_VALUE rValue) const;
    	ARG_VALUE operator[](KEY key);
    	void RemoveAll();
    	void RemoveKey(SNode * p);
    	CMap();
    	virtual ~CMap();
    	int GetSize()const
    	{
    		return m_nSize;
    	}
    	void * GetRootPoint() const
    	{
    		return m_pRoot;
    	}
    
    };
    
    template<typename KEY, typename VALUE, typename ARG_KEY, typename ARG_VALUE>
    void CMap<KEY, VALUE, ARG_KEY, ARG_VALUE>::SetAt(KEY key, ARG_VALUE value)
    {
    	(*this)[key] = value;
    }
    
    template<typename KEY, typename VALUE, typename ARG_KEY, typename ARG_VALUE>
    bool CMap<KEY, VALUE, ARG_KEY, ARG_VALUE>::Lookup(KEY key, ARG_VALUE rValue) const
    {
    	SNode *p = m_pRoot;
    	while (p)
    	{
    		if (p->key == key)
    		{
    			rValue = p->value;
    			return true;
    		}
    		else if (p->key > key)
    			p = p->pLeft;
    		else
    			p = p->pRight;
    	}
    	return false;
    }
    
    template<typename KEY, typename VALUE, typename ARG_KEY, typename ARG_VALUE>
    ARG_VALUE CMap<KEY, VALUE, ARG_KEY, ARG_VALUE>::operator[](KEY key)
    {
    	SNode **p = &m_pRoot;
    	while (*p)
    	{
    		if ((*p)->key == key)
    			break;
    		else if ((*p)->key > key)
    			p = &((*p)->pLeft);
    		else
    			p = &((*p)->pRight);
    	}
    	if (*p)
    	{
    		return (*p)->value;
    	}
    	SNode *pNew = new SNode();
    	pNew->key = key;
    	*p = pNew;
    	++m_nSize;
    	return (*p)->value;
    }
    
    template<typename KEY, typename VALUE, typename ARG_KEY, typename ARG_VALUE>
    void CMap<KEY, VALUE, ARG_KEY, ARG_VALUE>::RemoveAll()
    {
    	if (m_pRoot)
    		RemoveKey(m_pRoot);
    	m_pRoot = nullptr;
    	m_nSize = 0;
    }
    
    template<typename KEY, typename VALUE, typename ARG_KEY, typename ARG_VALUE>
    void CMap<KEY, VALUE, ARG_KEY, ARG_VALUE>::RemoveKey(SNode * p)
    {
    	if (p->pLeft)
    		RemoveKey(p->pLeft);
    	if (p->pRight)
    		RemoveKey(p->pRight);
    	delete p;
    }
    
    template<typename KEY, typename VALUE, typename ARG_KEY, typename ARG_VALUE>
    CMap<KEY, VALUE, ARG_KEY, ARG_VALUE>::CMap()
    {
    	m_pRoot = nullptr;
    	m_nSize = 0;
    }
    
    template<typename KEY, typename VALUE, typename ARG_KEY, typename ARG_VALUE>
    CMap<KEY, VALUE, ARG_KEY, ARG_VALUE>::~CMap()
    {
    	RemoveAll();
    }
    

      

  • 相关阅读:
    PCB 奥宝LDI 输出正负片转换关系
    PCB拼板之多款矩形排样算法实现--学习
    PCB 3D PCB 后续改进与扩展功能一些想法
    PCB 周期日历
    PCB LDI文件 自动化输出(改造)实现思路
    PCB Windows远程桌面一键登录
    PCB MS SQL 排序应用---SQL相邻数据区间值求解
    PCB MS SQL 排序应用---相邻数据且相同合并处理
    SpringMVC快速入门
    linux安装jdk
  • 原文地址:https://www.cnblogs.com/veis/p/12583131.html
Copyright © 2011-2022 走看看