函数模板实现映射表Map(平衡二叉树)

函数模板

首先创建一个结构体模板,其中包含两个任意类型变量的元素,一个作为索引,一个作为它本身的值,用来当作平衡二叉树的结点。接着创建一个模板类map,这个map中包含该结构体指针root作为根节点,用来访问平衡二叉树,对平衡二叉树中的元素进行增删查改。其中,在它的成员函数里,设置中序遍历函数,验证该平衡二叉树是否正常工作,也可以查找一个值是存在map中,若在,则返回它的下标值,若不在,则返回-1。

功能设计

在这里插入图片描述

代码

main.cpp

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "mymap.h"
using namespace std;

 
int main()
{
	cout<<"创建一个map映射a"<<endl; 
	map<int,int> a;  
    int datas[] = {45, 32, 16, 77, 94, 38, 44};
    for (int x : datas)
    {
        cout << "Insert " << x << endl;
        a.Insert(x ,x ); 
        cout <<"中序遍历结果:"<<endl;
        a.InOrder();
        cout<<endl;
    }
    cout<<"输出map中元素的个数"<<endl<<a.size()<<endl<<endl;
    cout<<"在map中查找有无值为45,若有,则返回位置,若没有,返回-1"<<endl;
    cout<<a.find(45)<<endl;
    cout<<"判断map是否为空,若为空,则true,若没有,输出flase"<<endl;
    cout<<a.empty()<<endl; 
    cout<<"在map中删除有无值为45,若有,则返回位置,若没有,返回-1"<<endl;
    a.erase(45);
    cout <<"中序遍历结果:"<<endl;
    a.InOrder();
    cout <<"通过下标,访问第一个元素"<<endl;
    cout<<a[0]<<endl; 
    cout<<endl<<"输出map的最大容量:"<<endl;
	cout<<a.max_size()<<endl;
    a.clear();
    cout<<endl<<"清空map"<<endl;
	cout<<"判断map是否为空,若为空,则true,若没有,输出flase"<<endl;
    cout<<a.empty()<<endl;  
    return 0;
}  

mymap.h

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
 
typedef int ElementType; 
 
template <class keytype,class valuetype> struct AVLNode
{
	typedef struct AVLNode< keytype,  valuetype>* Tree;
    int depth;  
    Tree parent;  
    keytype val; 
    valuetype value;
    Tree lchild;
    Tree rchild; 
    AVLNode( keytype x ,  valuetype y)  
    {
        parent=NULL;
        depth=0; 
        lchild=rchild=NULL;
        this->val = x;
        this->value = y; 
    }
};
template <typename keytype,typename T   >  class map{
	protected:
		typedef struct AVLNode< keytype,  T>* Tree;
		Tree root;
		T  data[100] ;
	    int count = 0; 
	public:
		typedef T* iterator;
		Tree Insert( keytype key,  T val  )
		{
//			cout<<"val:"<<val<<endl; 
			data[count] = val  ;
			count++;
		    Tree temp=NULL;
//		    Tree node=new AVLNode( ); 
			Tree node=new AVLNode< keytype,T> (key,val);   
		     
		    temp=insert_val(root,node,NULL);  
		 
		    if (temp)
		    {
		        update_depth(temp);
		        root=rotateAt(root, temp); 
		    }
		    else  
		        delete temp;
		    return root;
		}
		iterator begin()  {return &data[0];}
    	iterator end()  {return &data[count];} 
		iterator rbegin()   {return &data[count];} 
		iterator rend()  {return &data[0];} 
		Tree insert_val(Tree &root,Tree node,Tree parent)
		{
		    if (root==NULL)
		    {
		        root=node;
		        node->parent=parent;  
		        return root;          
		    }
		    if (node->val<root->val)  
		        return insert_val(root->lchild, node,root);
		    else if(node->val>root->val)  
		        return insert_val(root->rchild, node,root);
		    else  
		        return NULL;
		}
		int size(){  
	    	return  count;
		} 
		Tree connect34(Tree &a,Tree &b,Tree &c,Tree &T0,Tree &T1,Tree &T2,Tree &T3)
		{
		    a->lchild=T0;
		    if (T0)
		        T0->parent=a;
		    a->rchild=T1;
		    if(T1)
		        T1->parent=a;
		    update_depth(a);
		    c->lchild=T2;
		    if(T2)
		        T2->parent=c;
		    c->rchild=T3;
		    if(T3)
		        T3->parent=c;
		    update_depth(c);
		    b->lchild=a;
		    a->parent=b;
		    b->rchild=c;
		    c->parent=b;
		    update_depth(b);
		    return b;
		}
		 
		Tree rotateAt(Tree &root,Tree &node)
		{
		    Tree son,temp;
		    Tree grandson;
		    int balance=0;  
		    while (node!=NULL)  
		    {
		        update_depth(node);  
		        balance=is_balance(node);  
		        if (balance>1 || balance<-1)  
		        {
		            if (balance>1)  
		            {
		                if (is_balance(node->lchild)>0)  
		                {
		                    son=node->lchild; 
		                    grandson=son->lchild;  
		                    son->parent=node->parent; 
		                    temp=node; 
		                    node=connect34(grandson, son, node, grandson->lchild, grandson->rchild, son->rchild, node->rchild);
		                    setchild(son, temp, node); 
		                }
		                else   
		                {
		                    son=node->lchild;
		                    grandson=son->rchild;
		                    grandson->parent=node->parent;
		                    temp=node;
		                    node=connect34(son, grandson, node, son->lchild, grandson->lchild, grandson->rchild, node->rchild);
		                    setchild(grandson, temp, node);  
		                }
		            }
		            else  
		            {
		                if (is_balance(node->rchild)<0)  
		                {
		                    son=node->rchild;
		                    grandson=son->rchild;
		                    son->parent=node->parent;
		                    temp=node;
		                    node=connect34(node, son, grandson, node->lchild, son->lchild, grandson->lchild, grandson->rchild);
		                    setchild(son, temp, node); 
		                }
		                else  
		                {
		                    son=node->rchild;
		                    grandson=son->lchild;
		                    grandson->parent=node->parent;
		                    temp=node;
		                    node=connect34(node, grandson, son, node->lchild, grandson->lchild, grandson->rchild, son->rchild);
		                    
		                    setchild(grandson, temp, node); 
		 
		                }
		            }
		            if (node->parent==NULL) 
		            {
		                root=node;  
		                break;  
		            }
		        }
		        node=node->parent; 
		        
		    }
		    return root; 
		}
		void setchild(Tree &g,Tree &temp,Tree &node)
		{
		    if (g->parent)
		    {
		        if (g->parent->lchild==temp)
		            g->parent->lchild=node;
		        else
		            g->parent->rchild=node;
		    }
		} 
		Tree *Find_Min(Tree &root)
		{
		    if (root->lchild)
		    {
		       return Find_Min(root->lchild);
		    }
		    return &root;
		} 
		Tree remove_val(Tree &root,Tree &node)
		{
		    Tree parent=node->parent;
		    Tree temp=NULL; 
		    if (node->rchild==NULL && node->lchild!=NULL)
		    {
		        temp=node;
		        node=node->lchild;  
		        node->parent=temp->parent;
		        delete temp;        
		        update_depth(node); 
		    }
		    else if(node->lchild==NULL && node->rchild!=NULL)  
		    {
		        temp=node;
		        node=node->rchild;  
		        node->parent=temp->parent;
		        delete temp;        
		        update_depth(node);  
		    }
		    else if(node->rchild==NULL && node->lchild==NULL)  
		    {
		        parent=node->parent;  
		        if (parent)  
		        {
		            delete node;
		            node=NULL;
		            update_depth(parent);  
		        }
		        else  
		        {
		            delete root;
		            root=NULL;
		        }
		    }
		    else  
		    {
		        Tree *tmp=Find_Min(node->rchild);  
		        node->val=(*tmp)->val;          
		        parent=(*tmp)->parent;
		        delete *tmp;
		        *tmp=NULL;
		        update_depth(parent);
		    }
		    return parent;
		}
		Tree remove_new(Tree &root,T val){
			static Tree *temp=NULL; 
		    if (root==NULL)
		    {
		        temp=NULL;
		        return NULL;
		    }
		    else if(root->val<val)  
		        remove_new(root->rchild, val);
		    else if(root->val>val) 
		        remove_new(root->lchild, val);
		    else    
		       temp=&root;
		    
		    if (temp)
		    {
		        if (!root->parent)  
		        {
		            Tree tmp=NULL;
		            tmp=remove_val(root,*temp);  
		            return rotateAt(root, tmp);
		        }
		        return *temp;
		    }
		    return NULL;
		} 
		Tree erase( T val)
		{
			int j = -1;
			for(int i=0;i<count;i++)
				if(data[i] = val ) {j = i; break;} 
			for(int i=0;i<count-1;i++)
				if(i>=j) data[i] = data[i+1];
			count --;
		    remove_new( root, val);	
		} 
		int find_(Tree &root,T val)
		{
		    static Tree *temp=NULL;
		    if (root==NULL)
		    {
		        temp=NULL;
		        return 0;
		    }
		    else if(root->val<val) 
		        find_(root->rchild, val);
		    else if(root->val>val)  
		        find_(root->lchild, val);
		    else    
		       temp=&root;
		    
		    if (temp)
		    {
		        return 1;
		    }
		    else return 0;
		} 
		int find( T val)
		{
		    find_( root,  val);
		} 
		 
		int get_balance(Tree node)
		{
		    if (node==NULL)
		        return 0;
		    return node->depth;
		} 
		int is_balance(Tree node)
		{
		    if (node==NULL)
		        return 0;
		    else
		        return get_balance(node->lchild)-get_balance(node->rchild);
		} 
		void update_depth(Tree node)
		{
		    if (node==NULL)
		        return;
		    else
		    {
		        int depth_Lchild=get_balance(node->lchild);  
		        int depth_Rchild=get_balance(node->rchild);  
		        node->depth=max(depth_Lchild,depth_Rchild)+1;
		    }
		} 
		int max_size()  {
			return 100 ;
		} 
		//中序
		void  InOrder_(Tree root)
		{
			if (root==NULL)
		        return;
		    InOrder_(root->lchild);
//		    printf("%d ",root->val);
		    cout<<root->val<<"("<<root->value<<" ) "; 
		    InOrder_(root->rchild);
		}
		T& operator[](int index)  
		{ 
//			cout<<this->data[index]<<endl;
			return this->data[index];
		} 
		void clear(){
			count = 0;
		}
		void InOrder( )
		{
//			cout<<"root:"<< root->val <<endl; 
		    InOrder_(root);
		    cout<<endl; 
		}
		bool empty()
		{
			if(count == 0) return true;
			else return false;
		 } 
			 
};  
 

测试结果

在这里插入图片描述

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值