view src/tree/AVLTreeMap.java @ 2:36f2373f867b

added AVLTree.java remove operation
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Wed, 10 Aug 2011 20:14:19 +0900
parents 15920e9b562d
children 24613dfbaeab
line wrap: on
line source

package tree;

public class AVLTreeMap<K extends Comparable<K>,V> implements Map<K,V>
{
	private Node<K,V> root;
	private final Node<K,V> NULL_NODE = new Node<K,V>(null,null,-1,-1,null,null);
	
	public static void main(String args[])
	{
		AVLTreeMap<Integer,String> map = new AVLTreeMap<Integer,String>();
		
		map.put(10,"10");
		map.put(20,"20");
		map.put(30,"30");
		map.put(5,"5");
		map.put(7,"7");
		
		map.remove(20);
	}
	
	public AVLTreeMap()
	{
		root = null;
	}
	
	@Override
	public void put(K key,V value)
	{
		if(root == null){
			root = new Node<K,V>(key,value,0,0,NULL_NODE,NULL_NODE);
			return;
		}
		
		root = _put(root,key,value);
	}
	
	public Node<K,V> _put(Node<K,V> cur,K key,V value)
	{
		int result = cur.key.compareTo(key);
		
		if(result < 0){
			//right
			if(cur.right == NULL_NODE){
				cur.right = new Node<K,V>(key,value,0,0,NULL_NODE,NULL_NODE);
				cur.rdepth = 1;
				return cur;
			}
			
			cur.right = _put(cur.right,key,value);
			cur.rdepth = Math.max(cur.right.ldepth,cur.right.rdepth) + 1;
			int diff = cur.rdepth - cur.ldepth;
			
			if(diff > 1){
				//一重回転か二重回転か判断する。
				//以下の式は一重回転の場合は rottype が正になるはず。負の場合は二重回転、0の場合はあり得ない。
				int rottype = result * cur.right.key.compareTo(key);
				if(rottype > 0){
					return singleRotate(cur,OP_RIGHT);
				}else{
					return doubleRotate(cur,OP_RIGHTLEFT);
				}
			}
		}else if(result > 0){
			//left
			if(cur.left == NULL_NODE){
				cur.left = new Node<K,V>(key,value,0,0,NULL_NODE,NULL_NODE);
				cur.ldepth = 1;
				return cur;
			}
			
			cur.left = _put(cur.left,key,value);
			cur.ldepth = Math.max(cur.left.ldepth,cur.left.rdepth) + 1;
			int diff = cur.ldepth - cur.rdepth;
			
			if(diff > 1){
				//一重回転か二重回転か判断する。
				//以下の式は一重回転の場合は rottype が正になるはず。負の場合は二重回転、0の場合はあり得ない。
				int rottype = result * cur.left.key.compareTo(key);
				if(rottype > 0){
					return singleRotate(cur,OP_LEFT);
				}else{
					return doubleRotate(cur,OP_LEFTRIGHT);
				}
			}
		}else{
			//find
			cur.value = value;
		}
		
		return cur;
	}
	
	public V remove(K _key)
	{
		RemoveResult<K,V> r = removeTarget(root,_key);
		root = r.cur;
		return r.removed.value;
	}
	
	private RemoveResult<K,V> removeTarget(Node<K,V> _cur,K _key)
	{
		int result = _cur.key.compareTo(_key);
		
		if(result < 0){
			//right
			if(_cur.right == NULL_NODE){
				//not found
				return new RemoveResult<K,V>(_cur,NULL_NODE);
			}
			
			RemoveResult<K,V> r = removeTarget(_cur.right,_key);
			_cur.right = r.cur;
			_cur.rdepth = Math.max(_cur.right.ldepth,_cur.right.rdepth) + 1;
			
			//要素の削除では,部分木の要素が減ったと考えるのではなく,もう片方の部分木の要素が増えたと考えれば書きやすい
			int diff = _cur.ldepth - _cur.rdepth;
			if(diff > 1){
				//determine that rotation will be single or double
				Node<K,V> left = _cur.left;
				int rottype = left.ldepth - left.rdepth;
				if(rottype >= 0){
					r.cur = singleRotate(_cur,OP_LEFT);
				}else{
					r.cur = doubleRotate(_cur,OP_LEFTRIGHT);
				}
			}
			
			return r;
		}else if(result > 0){
			//left
			if(_cur.left == NULL_NODE){
				//not found
				return new RemoveResult<K,V>(_cur,NULL_NODE);
			}
			
			RemoveResult<K,V> r = removeTarget(_cur.right,_key);
			_cur.left = r.cur;
			_cur.rdepth = Math.max(_cur.right.ldepth,_cur.right.rdepth) + 1;

			int diff = _cur.rdepth - _cur.ldepth;
			if(diff > 1){
				//determine that rotation will be single or double
				Node<K,V> right = _cur.right;
				int rottype = right.rdepth - right.ldepth;
				if(rottype >= 0){
					r.cur = singleRotate(_cur,OP_RIGHT);
				}else{
					r.cur = doubleRotate(_cur,OP_RIGHTLEFT);
				}
			}
		}
		
		//find.
		
		//見つかったので,ノードの代わりのノードを検索する.
		RemoveResult<K,V> r = findNearestNode(_cur.left);
		if(r != null){
			
		}
		
		//見つかった場合は自身を削除対象とする
		
		
		return null;
	}
	
	public RemoveResult<K,V> findNearestNode(Node<K,V> _cur)
	{
		if(_cur == NULL_NODE){
			return null;
		}
		
		return null;
	}
	
	public static class RemoveResult<K,V>
	{
		public Node<K,V> removed;
		public Node<K,V> cur;
		
		public RemoveResult(Node<K,V> _removed,Node<K,V> _cur)
		{
			removed = _removed;
			cur = _cur;
		}
	}
	
	private static final int OP_LEFT = 1;
	private static final int OP_RIGHT = 2;
	
	public Node<K,V> singleRotate(Node<K,V> target,int type)
	{
		switch(type){
			case OP_LEFT:
				Node<K,V> left = target.left;
				target.left = left.right;
				left.right = target;
				
				target.ldepth = Math.max(target.left.rdepth,target.left.ldepth) + 1;
				left.rdepth = Math.max(left.right.rdepth,left.right.ldepth) + 1;
				return left;
			case OP_RIGHT:
				Node<K,V> right = target.right;
				target.right = right.left;
				right.left = target;
				
				target.rdepth = Math.max(target.right.rdepth,target.right.ldepth) + 1;
				right.ldepth = Math.max(right.left.ldepth,right.left.rdepth) + 1;
				return right;
			default:
				throw new IllegalArgumentException("invalid rotation type"); 
		}
	}
	
	private static final int OP_LEFTRIGHT = 1;
	private static final int OP_RIGHTLEFT = 2;
	
	public Node<K,V> doubleRotate(Node<K,V> target,int type)
	{
		switch(type){
			case OP_LEFTRIGHT:
				target.left = singleRotate(target.left,OP_RIGHT);
				return singleRotate(target,OP_LEFT);
			case OP_RIGHTLEFT:
				target.right = singleRotate(target.right,OP_LEFT);
				return singleRotate(target,OP_RIGHT);
			default:
				throw new IllegalArgumentException("invalid rotation type.");
		}
	}
	
	@Override
	public V get(K key)
	{
		Node<K,V> cur = root;
		
		while(cur != NULL_NODE){
			int result = cur.key.compareTo(key);
			if(result < 0){
				cur = cur.right;
			}else if(result > 0){
				cur = cur.left;
			}else{
				//find
				break;
			}
		}
		
		return cur.value;
	}
	
	private static class Node<K,V>
	{
		K key;
		V value;
		
		int rdepth;
		int ldepth;
		
		Node<K,V> left;
		Node<K,V> right;
		
		public Node(K key,V value,int rdepth,int ldepth,Node<K,V> left,Node<K,V> right)
		{
			this.right = right;
			this.left = left;
			this.ldepth = ldepth;
			this.rdepth = rdepth;
			this.key = key;
			this.value = value;
		}
		
		@Override
		public String toString()
		{
			return "[" + key + ":" + value + "]";
		}
	}
}