TreeSet类的实现(迭代器使用二叉查找树)

本文详细介绍了一种自定义的红黑树实现——MyTreeSet类。该类使用泛型和自定义异常处理,提供了插入、删除、查找、迭代等功能。文章深入探讨了二叉搜索树节点的结构,以及如何通过递归算法进行元素的插入、查找和删除操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.*;
class UnderflowException extends Exception { };
public class MyTreeSet<AnyType extends Comparable<? super AnyType>>{
	private static class BinaryNode<AnyType> {
		BinaryNode( AnyType theElement ) { 
			this( theElement, null, null, null ); }
		BinaryNode( AnyType theElement,BinaryNode<AnyType> lt,BinaryNode<AnyType>rt,BinaryNode<AnyType> pt ){
		element = theElement; left = lt; right = rt; parent = pt; }
		AnyType element;
		BinaryNode<AnyType> left;
		BinaryNode<AnyType> right;
		BinaryNode<AnyType> parent;
	}
	public java.util.Iterator<AnyType> iterator(){
		return new MyTreeSetIterator( );
	}
	private class MyTreeSetIterator implements java.util.Iterator<AnyType>{
		private BinaryNode<AnyType> current = findMin(root);
		private BinaryNode<AnyType> previous;
		private int expectedModCount = modCount;
		private boolean okToRemove = false;
		private boolean atEnd = false;
		public boolean hasNext(){
			 return !atEnd;
	    }
		public AnyType next(){
			if( modCount != expectedModCount )
				throw new java.util.ConcurrentModificationException( );
			if( !hasNext( ) )
				throw new java.util.NoSuchElementException( );
				
			AnyType nextItem = current.element;
			previous = current;
			// if there is a right child, next node is min in right subtree
			if (current.right != null){
				current = findMin(current.right);
			}
			else
			{
				// else, find ancestor that it is left of
				BinaryNode<AnyType> child = current;
				current = current.parent;
				while ( current != null && current.left != child){
					child = current;
					current = current.parent;
				}
				if (current == null)
					atEnd = true;
			}
			okToRemove = true;
			return nextItem;
		}
		public void remove(){
			if( modCount != expectedModCount )
				throw new java.util.ConcurrentModificationException( );
			if( !okToRemove )
				throw new IllegalStateException( );
			MyTreeSet.this.remove( previous.element );
			okToRemove = false;
		}
	}
	public MyTreeSet(){ 
		root = null; 
	}
	public void makeEmpty(){
		modCount++;
		root = null;
	}
	public boolean isEmpty(){ 
		return root == null; 
	}
	public boolean contains( AnyType x ){ 
		return contains( x, root ); 
	}
	public AnyType findMin() throws UnderflowException {
		if ( isEmpty() )
			throw new UnderflowException();
		else
			return findMin( root ).element;
	}
	public AnyType findMax() throws UnderflowException{
		if ( isEmpty() )
			throw new UnderflowException();
		else
			return findMax( root ).element;
	}
	public void insert( AnyType x ){ 
		root = insert( x, root, null );
	 }
	public void remove( AnyType x ){ 
		root = remove( x, root );
	 }
	public void printTree(){
		if ( isEmpty() )
			System.out.println( "Empty tree" );
		else
			printTree( root );
	}
	private void printTree( BinaryNode<AnyType> t ){
		if ( t != null ) {
			printTree( t.left );
			System.out.println( t.element );
			printTree( t.right );
		}
	}
	private boolean contains( AnyType x, BinaryNode<AnyType> t ) {
		if ( t == null )
			return false;
		int compareResult = x.compareTo( t.element );
		if ( compareResult < 0)
			return contains( x, t.left );
		else   if ( compareResult > 0)
			return contains( x, t.right );
		else
			return true; // match
	}
	private BinaryNode<AnyType> findMin( BinaryNode<AnyType> t ){
		if ( t == null )
			return null;
		else if ( t.left == null )
			return t;
		return findMin( t.left );
	}
	private BinaryNode<AnyType> findMax( BinaryNode<AnyType> t ){
		if ( t == null )
			return null;
		else if ( t.right == null )
			return t;
		return findMax( t.right );
	}
	private BinaryNode<AnyType> insert( AnyType x, BinaryNode<AnyType> t,BinaryNode<AnyType> pt ){
		if ( t == null ){
			modCount++;
			return new BinaryNode<AnyType>( x, null, null, pt);
		}
		int compareResult = x.compareTo( t.element );
		if ( compareResult < 0)
			t.left = insert( x, t.left, t );
		else if ( compareResult > 0)
			t.right = insert( x, t.right, t );
		else ; // duplicate
		return t;
	}
	private BinaryNode<AnyType> remove( AnyType x, BinaryNode<AnyType> t ){
		if ( t == null )
			return t; // not found
		int compareResult = x.compareTo( t.element );
		if ( compareResult < 0)
			t.left = remove( x, t.left );
		else if ( compareResult > 0)
			t.right = remove( x, t.right );
		else if ( t.left != null && t.right != null ) {   // two children
			t.element = findMin( t.right ).element;
			t.right = remove( t.element, t.right );
		}
		else
		{
			modCount++;
			BinaryNode<AnyType> oneChild;
			oneChild = ( t.left != null ) ? t.left : t.right;
			oneChild.parent = t.parent; // update parent link
			t = oneChild;
		}
		return t;
	}
	private BinaryNode<AnyType> root;
	int modCount = 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值