以前写的AVL树实现map练习

AVL树的资料见这:http://blog.csdn.net/laziercs/article/details/8280074

实现代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

template <typename Tk,typename Tv>
struct Type
{
	Tk key;
	Tv val;
};
template <typename Tk,typename Tv>
Type<Tk,Tv> makepair(Tk key,Tv val)
{
	Type <Tk,Tv> temp;
	temp.key=key;
	temp.val=val;
	return temp;
}
template <typename Tk,typename Tv>
bool operator< (Type<Tk,Tv> a,Type<Tk,Tv> b)
{
	return a.key<b.key;
}
template <typename Tk,typename Tv>
struct Treenode
{
	struct Treenode<Tk,Tv> *left,*right;
	int height;
	Type<Tk,Tv> data;
};
#define node Treenode<Tk,Tv>
#define Node Treenode<Tk,Tv>*
int Max(int a,int b){return a<b?b:a;}

template <typename Tk,typename Tv>
class map
{
public:
	Node head;
	map(){head=NULL;}
	map( map<Tk,Tv> &T)
	{
		head=Cpmem(T.head);
	}
	map<Tk,Tv> & operator=(map<Tk,Tv> &T)
	{
		Distory(head);
		head=Cpmem(T.head);
		return *this;
	}
	bool find(Type<Tk,Tv> pair)
	{
		Node t=Find(pair.key,head);
		return t&&(t->data.val==pair.val);
	}
	void insert(Type<Tk,Tv> pair)
	{
		head=Insert(pair,head);
	}
	Tv &operator[](Tk key)
	{
		Node t=Find(key,head);
		if(!t)
		{
			head=Insert(makepair(key,0),head);
			t=Find(key,head);
		}
		return t->data.val;
	}
	~map()
	{
		Distory(head);
	}
private:
	int H(Node T);
	Node Cpmem(Node T);
	Node Rotate(Node T);
	Node RightRotate(Node a);
	Node LeftRotate(Node a);
	Node LeftRightRotate(Node a);
	Node RightLeftRotate(Node a);
	Node Insert(Type<Tk,Tv> x, Node t);
	Node Delete(Type<Tk,Tv> x, Node t);
	Node Find(Tk key,Node t);
	void Distory(Node t);
};
template <typename Tk,typename Tv>
Node map<Tk,Tv>::Cpmem(Node T)
{
	if(!T)return NULL;
	Node temp=new node;
	temp->data=T->data;
	temp->height=T->height;
	temp->left=Cpmem(T->left);
	temp->right=Cpmem(T->right);
	return temp;
}
template <typename Tk,typename Tv>
void map<Tk,Tv>::Distory(Node t)
{
	if(!t)return;
	Distory(t->left);
	Distory(t->right);
	delete t;
}

template <typename Tk,typename Tv>
Node map<Tk,Tv>::Find(Tk key,Node t)
{
	if(!t)return NULL;
	if(key==t->data.key)return t;
	else if(key<(t->data).key)
		return Find(key,t->left);
	else
		return Find(key,t->right);
}

template <typename Tk,typename Tv>
int map<Tk,Tv>::H(Node T)
{
	if(!T)return 0;
	return T->height;
}

template <typename Tk,typename Tv>
Node map<Tk,Tv>::RightRotate(Node a)
{
	Node b = a->left;
	a->left = b->right;
	b->right = a;
	a->height = Max(H(a->left), H(a->right));
	b->height = Max(H(b->left), H(b->right));
	return b;
}
template <typename Tk,typename Tv>
Node map<Tk,Tv>::LeftRotate(Node a)
{
	Node b = a->right;
	a->right = b->left;
	b->left = a;
	a->height = Max(H(a->left), H(a->right));
	b->height = Max(H(b->left), H(b->right));
	return b;
}
template <typename Tk,typename Tv>
Node map<Tk,Tv>::LeftRightRotate(Node a)
{
	a->left = LeftRotate(a->left);
	return RightRotate(a);
}
template <typename Tk,typename Tv>
Node map<Tk,Tv>::RightLeftRotate(Node a)
{
	a->right = RightRotate(a->right);
	return LeftRotate(a);
}
template <typename Tk,typename Tv>
Node map<Tk,Tv>::Insert(Type<Tk,Tv> x, Node t)
{
	if(t == NULL)
	{
		t = new node;
		t->data=x;
		t->height=1;
		t->left=t->right=NULL;
		return t;
	}
	else if(x.key < t->data.key)
		t->left = Insert(x,t->left);
	else
		t->right = Insert(x,t->right);
	t=Rotate(t);
	t->height = Max(H(t->left), H(t->right)) + 1;
	return t;
}
template <typename Tk,typename Tv>
Node map<Tk,Tv>::Rotate(Node T)
{
    if(H(T->left) - H(T->right) == 2)
    {
         if(H(T->left->left) >= H(T->left->right))
             T = RightRotate(T);
         else
             T = LeftRightRotate(T);
     }
     if(H(T->right) - H(T->left) == 2)
     {
         if(H(T->right->right) >= H(T->right->left))
             T = LeftRotate(T);
         else
             T = RightLeftRotate(T);
     }
    return T;
}
template <typename Tk,typename Tv>
Node map<Tk,Tv>::Delete(Type<Tk,Tv> x, Node t)
{
	if(t == NULL) return NULL;
	if(t->data == x)
	{
		if(t->right == NULL)
		{
			Node temp = t;
			t = t->left;
			free(temp);
		}
		else
		{
			Node head = t->right;
			while(head->left) head = head->left;
			t->data = head->data;
			t->right = Delete(t->data, t->right);
			t->height = Max(H(t->left), H(t->right)) + 1;
		}
  		return t;
	}
	else if(t->data < x)
		Delete(x, t->right);
	else
		Delete(x, t->left);
	if(t->left) t->left = Rotate(t->left);
	if(t->right) t->right = Rotate(t->right);
	t=Rotate(t);
	return t;
}
int main()
{
	int i,k,n;
	while(scanf("%d",&n)!=EOF)
	{
		map <int ,int > temp;
		for(i=1;i<=n;i++)
		{
			scanf("%d",&k);temp[i]=k;
			temp.insert(makepair(i,k));
		}
		map <int ,int > t;
		map <int, int > tt=t=temp;
		for(i=1;i<=n;i++)
			printf("%d\n",t[i]);
        for(i=1;i<=n;i++)
			printf("%d\n",tt[i]);
	}
	return 0;
}

    原文作者:AVL树
    原文地址: https://blog.csdn.net/zkfzkfzkfzkfzkfzkfzk/article/details/9025005
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞