AVL樹(模板題)—— POJ 3481 Double Queue

對應POJ題目:點擊打開鏈接

Double Queue
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11741   Accepted: 5335

Description

The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of the bank is identified by a positive integer K and, upon arriving to the bank for some services, he or she receives a positive integer priority P. One of the inventions of the young managers of the bank shocked the software engineer of the serving system. They proposed to break the tradition by sometimes calling the serving desk with the lowest priority instead of that with the highest priority. Thus, the system will receive the following types of request:

0 The system needs to stop serving
K P Add client K to the waiting list with priority P
2 Serve the client with the highest priority and drop him or her from the waiting list
3 Serve the client with the lowest priority and drop him or her from the waiting list

Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy.

Input

Each line of the input contains one of the possible requests; only the last line contains the stop-request (code 0). You may assume that when there is a request to include a new client in the list (code 1), there is no other request in the list of the same client or with the same priority. An identifier K is always less than 106, and a priority P is less than 107. The client may arrive for being served multiple times, and each time may obtain a different priority.

Output

For each request with code 2 or 3, the program has to print, in a separate line of the standard output, the identifier of the served client. If the request arrives when the waiting list is empty, then the program prints zero (0) to the output.

Sample Input

2
1 20 14
1 30 3
2
1 10 99
3
2
2
0

Sample Output

0
20
30
10
0

題意:

有3個操作:(1  x   y)表示以y爲優先級在隊列裏插入一對數(x   y),其中每一對數的x和y均不同;(2)表示輸出優先級最高的一對數中的x,(3)表示輸出優先級最低的一對數中的x;(0)表示退出。


思路:

各種二叉排序樹,還有STL裏的map,set也可AC,貼AVL模板


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX(x, y) ((x)>(y)?(x):(y))

typedef struct NODE
{
	int val, data;
	int bf; //平衡因子(左子樹高度與右子樹高度之差)
	int h; //以當前結點爲根結點的數的高度
	NODE *l, *r;
}Node;

class BalanceTree
{
public:
	void Init()
	{
		rt = NULL;
	}
	int Height(Node *T)
	{
		if(NULL == T) return 0;
		return T->h;
	}
	int BF(Node *l, Node *r)
	{
		if(NULL == l && NULL == r) return 0;
		else if(NULL == l) return -r->h;
		else if(NULL == r) return l->h;
		else return l->h - r->h;
	}
	Node *LL_rotate(Node *A)
	{
		Node *B;
		B = A->l;
		A->l = B->r;
		B->r = A;
		A->h = MAX(Height(A->l), Height(A->r)) + 1;
		B->h = MAX(Height(B->l), Height(B->r)) + 1;
		A->bf = BF(A->l, A->r);
		B->bf = BF(B->l, B->r);
		return B;
	}
	Node *RR_rotate(Node *A)
	{
		Node *B;
		B = A->r;
		A->r = B->l;
		B->l = A;
		A->h = MAX(Height(A->l), Height(A->r)) + 1;
		B->h = MAX(Height(B->l), Height(B->r)) + 1;
		A->bf = BF(A->l, A->r);
		B->bf = BF(B->l, B->r);
		return B;
	}
	Node *LR_rotate(Node *A)
	{
		Node *C;
		A->l = RR_rotate(A->l);
		C = LL_rotate(A);
		return C;
	}
	Node *RL_rotate(Node *A)
	{
		Node *C;
		A->r = LL_rotate(A->r);
		C = RR_rotate(A);
		return C;
	}
	void Insert(int v, int e)
	{
		Insert_(rt, v, e);
	}
	void Insert_(Node *&T, int v, int e)
	{
		if(NULL == T){
			T = (Node *)malloc(sizeof(Node));
			T->val = v;
			T->data = e;
			T->bf = 0;
			T->h = 1;
			T->l = T->r = NULL;
			return;
		}
		if(e < T->data) Insert_(T->l, v, e);
		else Insert_(T->r, v, e);

		T->h = MAX(Height(T->l), Height(T->r)) + 1;
		T->bf = BF(T->l, T->r);

		if(T->bf > 1 || T->bf < -1){ //T結點失衡
			if(T->bf > 0 && T->l->bf > 0) T = LL_rotate(T); //如果T->bf > 0 則肯定有左兒子
			else if(T->bf < 0 && T->r->bf < 0) T = RR_rotate(T); //如果T->bf < 0 則肯定有右兒子
			else if(T->bf > 0 && T->l->bf < 0) T = LR_rotate(T);
			else if(T->bf < 0 && T->r->bf > 0) T = RL_rotate(T);
		}
	}
	void Delete(int flag) //flag爲1表示找最大值
	{
		if(NULL == rt){
			printf("0\n");
			return;
		}
		Node *tmp = rt;
		if(!flag) while(tmp->l) tmp = tmp->l;
		else while(tmp->r) tmp = tmp->r;
		printf("%d\n", tmp->val);
		Delete_(rt, tmp->data);
	}
	void Delete_(Node *&T, int key)
	{
		if(NULL == T) return;
		if(key < T->data){ //從左邊刪除
			Delete_(T->l, key);
			T->bf = BF(T->l, T->r); //計算刪除後T結點的平衡因子
			if(T->bf < -1){ //T結點左邊高度變小(刪了左邊的元素)導致失衡
				if(1 == T->r->bf) T = RL_rotate(T); //RL型
				else T = RR_rotate(T); //平衡因子爲0或-1
			}
		}
		else if(key > T->data){ //從右邊刪除
			Delete_(T->r, key);
			T->bf = BF(T->l, T->r); //計算刪除後T結點的平衡因子
			if(T->bf > 1){ //T結點右邊高度變小(刪了右邊的元素)導致失衡
				if(-1 == T->l->bf) T = LR_rotate(T); //LR型
				else T = LL_rotate(T); //平衡因子爲0或1
			}
		}
		else{
			if(T->l && T->r){ //左右都不爲空
				Node *tmp = T->l; //用被刪除節點的左子樹的最大值代替被刪除節點
				while(tmp->r) tmp = tmp->r;

				T->data = tmp->data;
				T->val = tmp->val;

				Delete_(T->l, tmp->data);
				T->bf = BF(T->l, T->r); //計算刪除後T結點的平衡因子
				if(T->bf < -1){ //T結點左邊高度變小(刪了左邊的元素)導致失衡
					if(1 == T->r->bf) T = RL_rotate(T); //RL型
					else T = RR_rotate(T); //平衡因子爲0或-1
				}
			}
			else{
				Node *tmp = T;
				if(T->l) T = T->l; //有左兒子
				else if(T->r) T = T->r; //有右兒子
				else{ //T無兒子
					free(T);
					T = NULL;
				}
				if(T) free(tmp);
			}
		}
	}
	void Show()
	{
		InOrder(rt);
		printf("\n");
	}
	void InOrder(Node *T)
	{
		if(NULL == T) return;
		InOrder(T->l);
		printf("%d(%d) ", T->val, T->data);
		InOrder(T->r);
	}
	void Free()
	{
		FreeTree(rt);
	}
	void FreeTree(Node *T)
	{
		if(NULL == T) return;
		FreeTree(T->l);
		FreeTree(T->r);
		free(T);
	}
private:
	Node *rt;
};

BalanceTree bt;

int main()
{
#if 0
	freopen("in.txt","r",stdin);
#endif
	int op, v, e;
	bt.Init();
	while(scanf("%d", &op), op)
	{
		if(1 == op){
			scanf("%d%d", &v, &e);
			bt.Insert(v, e);
		}
		else if(2 == op)
			bt.Delete(1);
		else if(3 == op)
			bt.Delete(0);
	}
	bt.Free();
	return 0;
}



發佈了165 篇原創文章 · 獲贊 42 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章