数据结构与算法分析-C++描述 第4章 伸展树(splay树)

       伸展树(splay tree)是一种相对简单的数据结构,它保证从空树开始任意连接M次对树的操作最多花费O( M\log N )时间。伸展树的基本思路:当一个节点被访问后,它就要经过一系列AVL树的旋转被推到根上。如果一个结点很深,那么在其路径上就存在相对较深的结点,通过重新构造可以使对所有这些结点的访问所花费的时间减少。另外,由于伸展树不要求保留高度或平衡信息,因此在某种程度上可以节约空间并简化代码。

       伸展树的特点:

       1)伸展树属于二叉查找树,即它具有和二叉查找树一样的性质:左子树的值小于右子树的值,递归判断依然成立;
       2)除了拥有二叉查找树的性质之外,伸展树还具有的一个特点是:当某个节点被访问时,伸展树会通过旋转使该节点成为树根。这样做的好处是,下次要访问该节点时,能够迅速的访问到该节点。。

       伸展树的旋转:

      1)伸展树中存在键值为data的结点:将该结点旋转为根节点

      2)伸展树中不存在键值为data的结点, 且data小于根节点的键值

             2-1)该结点存在前驱节点(小于该结点键值的最大键值对应的结点),将前驱节点旋转为根节点

             2-2)该结点不存在前驱节点,即该结点为最小值结点,将最小值旋转为根节点

     3)伸展树中不存在键值为data的结点,且data大于根节点的键值

             3-1)该结点存在后继结点(大于该结点键值的最小键值对应的结点),将后继结点旋转为根节点

             3-2)该结点不存在后继节点,即该结点为最大值结点,将最大值旋转为根节点
      之字形旋转:

 

      一字型旋转: 

实例:建立splay树,设计遍历函数、插入数据、删除数据、查找最值、打印树结构等功能。 

1、splay.h

//splay.h
#ifndef SPLAY_H_
#define SPLAY_H_

#include<iostream>
#include<iomanip>

using std::cout;
using std::endl;
using std::setw;

template<class T>
struct Node{
	T data;
	Node *left;
	Node *right;
	Node():left(NULL), right(NULL){};
	Node(T d, Node<T> *l, Node<T> *r):data(d), left(l), right(r){};
};

template<class T>
class Splay{
private:
	Node<T> *root;
public:
	//Splay constructor
	Splay():root(NULL){
	
	};
	
	//Splay destructor
	~Splay(){
		destroy();
	};
	
	//preOrder 
	void preOrder(){
		preOrder(root);
	};
	
	//inOrder
	void inOrder(){
		inOrder(root);
	};
	
	//postOrder
	void postOrder(){
		postOrder(root);
	};
	
	//Splay search without iterative
	Node<T>* search(T d){
		search(root, d);
	};
	
	//Splay search with iterative
	Node<T>* iterativeSearch(T d){
		iterativeSearch(root, d);
	};
	
	//find maximum value
	T max(){
		Node<T> *temp = max(root);
		if(temp != NULL){
			return temp -> data;
		}
		return (T)NULL;
	};
	
	//find minimum value
	T min(){
		Node<T> *temp = min(root);
		if(temp != NULL){
			return temp -> data;
		}
		return (T)NULL;
	};
	
	//splay the value
	void splay(T d){
		root = splay(root, d);
	};
	
	//Splay insert
	void insert(T d){
		Node<T> *temp = NULL;
		if((temp = new Node<T>(d, NULL, NULL)) == NULL){
			return;
		}
		//from root insert d
		root = insert(root, temp);
		//splay d as new root
		root = splay(root, d);
	};
	
	//Splay remove
	void remove(T d){
		root = remove(root, d);
	};
	
	//Splay destroy
	void destroy(){
		destroy(root);
	};
	//Splay print
	void print(){
		if(root != NULL){
			print(root, root -> data, 0);
		}
	};

private:
	void preOrder(Node<T> *node) const;
	void inOrder(Node<T> *node) const;
	void postOrder(Node<T> *node) const;
	Node<T>* search(Node<T> *node, T d) const;
	Node<T>* iterativeSearch(Node<T> *node, T d) const;
	Node<T>* min(Node<T> *node);
	Node<T>* max(Node<T> *node);
	Node<T>* splay(Node<T> *node, T d);
	Node<T>* insert(Node<T>* &node, Node<T> *d);
	Node<T>* remove(Node<T>* &node, T d);
	void destroy(Node<T>* &node);
	void print(Node<T> *node, T d, int direction);
};

template<class T>
void Splay<T>::preOrder(Node<T> *node) const{
	if(node != NULL){
		cout << node -> data << " ";
		preOrder(node -> left);
		preOrder(node -> right);
	}
}

template<class T>
void Splay<T>::inOrder(Node<T> *node) const{
	if(node != NULL){
		inOrder(node -> left);
		cout << node -> data << " ";
		inOrder(node -> right);
	}
}

template<class T>
void Splay<T>::postOrder(Node<T> *node) const{
	if(node != NULL){
		postOrder(node -> left);
		postOrder(node -> right);
		cout << node -> data << " ";
	}
}

template<class T>
Node<T>* Splay<T>::search(Node<T> *node, T d) const{
	if(node == NULL || d == node -> data){
		return node;
	}
	if(d < node -> data){
		search(node -> left, d);
	}else{
		search(node -> right, d);
	}
}

template<class T>
Node<T>* Splay<T>::iterativeSearch(Node<T> *node, T d) const{
	while( (node != NULL) && (d != node -> data)){
		if(d < node -> data){
			node = node -> left;
		}
		else{
			node = node -> right;
		}
	}
	return node;
}

template<class T>
Node<T>* Splay<T>::min(Node<T> *node){
	if(node == NULL){
		return node;
	}
	while(node -> left != NULL){
		node = node -> left;
	}
	return node;
}

template<class T>
Node<T>* Splay<T>::max(Node<T> *node){
	if(node == NULL){
		return node;
	}
	while(node -> right != NULL){
		node = node -> right;
	}
	return node;
}

template<class T>
Node<T>* Splay<T>::splay(Node<T> *node, T d){
	Node<T> temp, *l ,*r, *cur;
	if(node == NULL){
		return node;
	}
	temp.left = temp.right = NULL;
	l = r = &temp;
	
	for(;;){
		if(d < node -> data){
			//right rotation
			if(node -> left == NULL){
				break;
			}
			if(d < node -> left -> data){
				cur = node -> left;
				node -> left = cur -> right;
				cur -> right = node;
				node = cur;
				if(node -> left == NULL){
					break;
				}
			}
			//link right
			r -> left = node;
			r = node;
			node = node -> left;
		}else if(d > node -> data){
			if(node -> right == NULL){
				break;
			}
			//left rotation
			if(d > node -> right -> data){
				cur = node -> right;
				node -> right = cur -> left;
				cur -> left = node;
				node = cur;
				if(node -> right == NULL){
					break;
				}
			}
			//link left
			l -> right = node;
			l = node;
			node = node -> right;	
		}else{
			break;
		}
	}
	//assemble
	l -> right = node -> left;
	r -> left = node -> right;
	node -> left = temp.right;
	node -> right = temp.left;
	return node;
}

template<class T>
Node<T>* Splay<T>::insert(Node<T>* &node,Node<T> *d){
	Node<T> *temp1 = NULL;
	Node<T> *temp2 = node;
	while(temp2 != NULL){
		temp1 = temp2;
		if(d -> data < temp2 -> data){
			temp2 = temp2 -> left;
		}else if(d -> data > temp2 -> data){
			temp2 = temp2 -> right;
		}else{
			cout << " can't insert the existed node !" << endl;
			delete d;
			return node;
		}
	}
	if(temp1 == NULL){
		node = d;
	}else if(d -> data < temp1 -> data){
		temp1 -> left = d;
	}else{
		temp1 -> right = d;
	}
	return node;
}

template<class T>
Node<T>* Splay<T>::remove(Node<T>* &node, T d){
	Node<T> *temp;
	//judge whether the node is root
	if(node == NULL){
		return node;
	}
	
	// judge whether there existed d
	if(search(node, d) == NULL){
		return NULL;
	}
	
	//splay the node to root
	node = splay(node, d);
	if(node -> left != NULL){
		temp = splay(node -> left, d);
		temp -> right = node -> right;
	}else{
		temp = node -> right;
	}
	delete node;
	return temp;
}

template<class T>
void Splay<T>::destroy(Node<T>* &node){
	if(node == NULL){
		return;
	}
	if(node -> left != NULL){
		destroy(node -> left);
	}
	if(node -> right != NULL){
		destroy(node -> right);
	}
	delete node;
}

template<class T>
void Splay<T>::print(Node<T> *node, T d, int direction){
	if(node != NULL){	
		if(direction == 0){
			cout << setw(2) << node -> data << " is root " << endl;
		}else{
			cout << setw(2) << node -> data << " is " << setw(2) << d << " 's " << setw(12) << (direction == 1 ? " right child " : " left child ") << endl;  
		}
		print(node -> left, node -> data, -1);
		print(node -> right, node -> data, 1);
	}
}

#endif

2、main.cpp

//main.cpp
#include<iostream>
#include"splay.h"

using namespace std;

int main()
{
	Splay<int> *splay = new Splay<int>();
	int arr[6] = {10, 50, 40, 30, 20, 60};
	
	cout << "*********** insert **************" << endl;
	for(int i = 0; i < 6; i++){
		splay -> insert(arr[i]);
	}
	
	cout << "*********** print **************" << endl;
	splay -> print();
	cout << endl;
	
	cout << "*********** preOrder **************" << endl;
	splay -> preOrder();
	cout << endl;
	
	cout << "*********** inOrder **************" << endl;
	splay -> inOrder();
	cout << endl;
	
	cout << "*********** postOrder **************" << endl;
	splay -> postOrder();
	cout << endl;
	
	cout << " the maximum value in Splay is " << splay -> max() << endl;
	cout << " the minimum value in Splay is " << splay -> min() << endl;
	
	cout << "*********** insert **************" << endl;
	splay -> insert(35);
	splay -> inOrder();
	cout << endl;
	splay -> print();
	
	cout << "*********** remove **************" << endl;
	splay -> remove(50);
	splay -> inOrder();
	cout << endl;
	splay -> print();
	
	cout << "*********** destroy **************" << endl;
	splay -> destroy();
	
	cout << "done ." << endl;
	
	return 0;
}

practice makes perfect !

参考博客:伸展树(二)之 C++的实现

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值