算法导论——区间树上的重叠区间查找算法

一、   算法设计与分析:

(1)   数据结构设计:

//区间
struct Interval{
  int low;
  int high;
};
//节点
struct Node{
  Node(int low,int high){
         this->key=low;
         this->Int.low=low;
         this->Int.high=high;
         this->max=high;
  }
  int key;
  int color;
  int max;
  Interval Int;
  Node* parent;
  Node* left;
  Node* right;
};

//树
struct Tree{
  Tree(){
  }
  Node* root;
  Node* nil;
};

(2)区间树上的重叠区间查找算法设计如下:

OS_SELECT(x,i)

  r=x.left.size+1;

  if i==r

return x;

else if i<r

 return OS_SELECT(x.left,i)

else

 return OS_SELECT(x.right,i-r)

二、算法实现

#pragma once
#define RED 0
#define BLACK 1

//区间
struct Interval{
	int low;
	int high;
};

//节点
struct Node{
	Node(int low, int high){
		this->key=low;
		this->Int.low=low;
		this->Int.high=high;
		this->max=high;
	}
	int key;
	int color;
	int max;
	Interval Int;
	Node* parent;
	Node* left;
	Node* right;
};

//树
struct Tree{
	Tree(){
	}
	Node* root;
	Node* nil;
};

#define MAXSIZE 10
#include "Tree.h"
#include <iostream>
#include <climits>
#include <string>
#include <map>
using namespace std;
void RB_INSERT(Tree* &T,Node* z);
void RB_INSERT_FIXUP(Tree* &T, Node* z);
void LEFT_ROTATE(Tree* &T, Node* x);
void RIGHT_ROTATE(Tree* &T, Node* y);
void InOrder(Node* tree,Tree* T);
void PreOrder(Node* tree,Tree* T);
//为区间树添加的方法
void FixMax(Node* node, Tree* T);
int Max(int a, int b, int c);
Node* INTERVAL_SEARCH(Tree* T, Interval i);
bool Overlap(Interval a, Interval b);
Node* INTERVAL_SEARCH(Tree* T, Interval i);

int main(){
	Tree* T;
	Node* z[MAXSIZE];

	T=new Tree();
	T->nil=new Node(0,0);
	T->nil->color=BLACK;
	T->nil->max=INT_MIN;
	T->root=T->nil;

	int i=0;
		z[i]=new Node(16,21);
		RB_INSERT(T,z[i]);
		i++;

		z[i]=new Node(8,9);
		RB_INSERT(T,z[i]);
		i++;

		z[i]=new Node(25,30);
		RB_INSERT(T,z[i]);
		i++;

		z[i]=new Node(5,8);
		RB_INSERT(T,z[i]);
		i++;

		z[i]=new Node(15,23);
		RB_INSERT(T,z[i]);
		i++;

		z[i]=new Node(17,19);
		RB_INSERT(T,z[i]);
		i++;
		z[i]=new Node(26,26);
		RB_INSERT(T,z[i]);
		i++;
		z[i]=new Node(0,3);
		RB_INSERT(T,z[i]);
		i++;
		z[i]=new Node(6,10);
		RB_INSERT(T,z[i]);
		i++;
		z[i]=new Node(19,20);
		RB_INSERT(T,z[i]);
	
	PreOrder(T->root,T);
	cout << endl;
	InOrder(T->root,T);
	
	cout << endl;
	Node* node;
	Interval Int;
	Int.low=11;
	Int.high=14;
	node=INTERVAL_SEARCH(T,Int);
	if(node!=T->nil)
		cout << node->Int.low << "," << node->Int.high << endl;
	else
		cout << "未找到与该区间重叠的区间" << endl;
	
	return 0;
}

//插入节点
void RB_INSERT(Tree* &T,Node* z){
	Node* y=T->nil;
	Node* x=T->root;
	while(x!=T->nil){
		y=x;
		if(z->key < x->key)
			x=x->left;
		else
			x=x->right;
	}
	z->parent=y;
	if(y==T->nil)//插入第一个元素
		T->root=z;
	else if(z->key < y->key)
		y->left=z;
	else
		y->right=z;
	z->left=T->nil;
	z->right=T->nil;
	z->color=RED;
	FixMax(z,T);//插入完成自底向上调整max
	RB_INSERT_FIXUP(T,z);//调整颜色过程只有旋转需要改变max值,所以该算法不用改变
}

//红黑树调整
void RB_INSERT_FIXUP(Tree* &T, Node* z){
	Node* y;

	while(z->parent->color==RED){
		if(z->parent==z->parent->parent->left){     //z节点父节点为其祖父节点的左孩子
			y=z->parent->parent->right;
			if(y->color==RED){                      //case1
				z->parent->color=BLACK;
				y->color=BLACK;
				z->parent->parent->color=RED;
				z=z->parent->parent;
			}
			else{                                       
				if(z==z->parent->right){            //case2
					z=z->parent;
					LEFT_ROTATE(T,z);
				}
				z->parent->color=BLACK;             //case3
				z->parent->parent->color=RED;
				RIGHT_ROTATE(T,z->parent->parent);
			}
		}
		else{									    //z节点父节点为其祖父节点的右孩子
			y=z->parent->parent->left;
			if(y->color==RED){						//case 1
				z->parent->color=BLACK;
				y->color=BLACK;
				z->parent->parent->color=RED;
				z=z->parent->parent;
			}
			else{
				if(z==z->parent->left){            //case2
					z=z->parent;
					RIGHT_ROTATE(T,z);
				}
				z->parent->color=BLACK;             //case3
				z->parent->parent->color=RED;
				LEFT_ROTATE(T,z->parent->parent);
			}
		}
	}
	T->root->color=BLACK;
}

//节点左旋
void LEFT_ROTATE(Tree* &T, Node* x){
	Node* y;

	y=x->right;
	x->right=y->left;
	if(y->left!=T->nil)
		y->left->parent=x;
	y->parent=x->parent;
	if(x->parent==T->nil)
		T->root=y;
	else if(x==x->parent->left)
		x->parent->left=y;
	else
		x->parent->right=y;
	y->left=x;
	x->parent=y;
	//调整max
	x->max=Max(x->left->max,x->right->max,x->Int.high);
	x->parent->max=Max(x->max,x->parent->right->max,x->parent->Int.high);
}

//节点右旋
void RIGHT_ROTATE(Tree* &T, Node* y){
	Node *x;

	x=y->left;
	y->left=x->right;
	if(x->right!=T->nil)
		x->right->parent=y;
	x->parent=y->parent;
	if(y->parent==T->nil)
		T->root=x;
	else if(y==y->parent->left)
		y->parent->left=x;
	else
		y->parent->right=x;
	x->right=y;
	y->parent=x;
	//调整max
	y->max=Max(y->left->max,y->right->max,y->Int.high);
	y->parent->max=Max(y->max, y->parent->Int.high, y->parent->left->max);
}

//前序遍历
void PreOrder(Node* tree,Tree * T){
	map<int,string> color;color[0]="red";color[1]="black";

	if(tree!=T->nil){
		cout << tree->Int.low<< "," << tree->Int.high << "," << tree->max <<","<<color[tree->color]<< endl;
		PreOrder(tree->left, T);
		PreOrder(tree->right,T);
	}
}

//中序遍历
void InOrder(Node* tree,Tree* T){
	map<int,string> color;color[0]="red";color[1]="black";
	if(tree!=T->nil){
		InOrder(tree->left,T);
		cout << tree->Int.low<< "," << tree->Int.high << "," << tree->max << color[tree->color]<< endl;
		InOrder(tree->right,T);
	}
}

//自node节点逐步向根调整max值
void FixMax(Node* node, Tree* T){
	Node *temp;
	
	temp=node;
	while(temp!=T->nil){
		temp->max=Max(temp->left->max, temp->right->max, temp->Int.high);
		temp=temp->parent;
	}
}

//找三个整数a,b,c的最大者
int Max(int a, int b, int c){
	int max;

	max=a;
	if(b>max)
		max=b;
	if(c>max)
		max=c;
	return max;
}

//寻找区间i
Node* INTERVAL_SEARCH(Tree* T, Interval i){
	Node* x;

	x=T->root;
	while(x!=T->nil && !Overlap(x->Int, i)){
		if(x->left!=T->nil && x->left->max>=i.low){
			x=x->left;
		}
		else
			x=x->right;
	}
	return x;
}
//判断两个区间a和b是否重叠
bool Overlap(Interval a, Interval b){
	if(a.high<b.low || a.low>b.high || b.high<a.low || b.low>a.high)//a在b左边,a在b右边,b在a左边,b在a右边
		return false;
	return true;
}


三、算法分析

根据以上实验过程发现设计的查找重叠区间算法能正确的找到重叠区间,并且当查找失败是也能正确显示。另外区间树为在红黑树基础上通过调整节点结构以及插入算法,左旋右旋算法来实现,其中的RB_INSERT_FIXUP算法无须做调整

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