java数据结构——BinarySearchTree(二叉查找树)

package com.tig.tree;

/**
 * 二叉查找树
 * @author Tig
 * @param <E>
 *
 */
public class BinarySearchTree<E extends Comparable<? super E>> {
	
	private BinaryNode<E> root;
	
	public BinarySearchTree() {
		root = null;
	}
	
	public void makeEmpty() {
		root = null;
	}
	
	public boolean isEmpty() {
		return root == null;
	}
	
	public boolean contains(E e) {
		
		return contains(e, root);
	}
	
	public boolean contains(E e, BinaryNode<E> t) {
		
		if (t == null) {
			return false;
		}
		
		int compareResult = e.compareTo(t.element);
		
		if (compareResult < 0) {
			
			return contains(e, t.left);
		} else if (compareResult > 0) {
			
			return contains(e, t.right);
		} else {
			
			return true;
		}
	}
	
	public E findMin() {
		
		if ( isEmpty() ) {
			throw new RuntimeException("树为空");
		}
		
		
		return findMin(root).element;
	}
	
	private BinaryNode<E> findMin(BinaryNode<E> t) {
		
		if (t == null) {
			
			return null;
		} else if (t.left == null) {
			
			return t;
		}
		
		return findMin(t.left);
	}

	public E findMax() {
		
		if ( isEmpty() ) {
			throw new RuntimeException("树为空");
		}
		
		return findMax(root).element;
	}
	
	private BinaryNode<E> findMax(BinaryNode<E> t) {
		
		if (t != null) {
			while (t.right != null) {
				t = t.right;
			}
		}
		
		return t;
	}
	
	public void insert(E e) {
		root = insert(e, root);
	}
	
	private BinaryNode<E> insert(E e, BinaryNode<E> t) {
		
		if (t == null) {
			return new BinaryNode<E>(e);
		}
		
		int compareResult = e.compareTo(t.element);
		
		if (compareResult < 0) {
			
			t.left = insert(e, t.left);
		} else if (compareResult > 0) {
			
			t.right = insert(e, t.right);
		} else 
		
			;	// do nothing
		
		return t;
	}
	
	public void remove(E e) {
		
		root = remove(e, root);
	}

	private BinaryNode<E> remove(E e, BinaryNode<E> t) {
		
		if (t == null) {
			return t;	
		}
		
		int compareResult = e.compareTo(t.element);
		
		if (compareResult < 0) {
			
			t.left = remove(e, t.left);
		} else if (compareResult > 0) {
			
			t.right = remove(e, 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 {	//One child
			
			t = (t.left != null) ? t.left : t.right;
		}
		
		return t;
	}

	public void printTree() {
		
		if ( isEmpty() ) {
			
			System.out.println("Empty tree");
		} else {
			
			printTree(root);
		}
	}
	
	private void printTree(BinaryNode<E> t) {
		
		if (t != null) {
			
			printTree(t.left);
			System.out.println(t.element);
			printTree(t.right);
		}
	}

	private static class BinaryNode<E> {
		
		E element;
		
		BinaryNode<E> left;
		
		BinaryNode<E> right;
		
		public BinaryNode(E element) {
			this(element, null, null);
		}

		public BinaryNode(E element, BinaryNode<E> left, BinaryNode<E> right) {
			this.element = element;
			this.left = left;
			this.right = right;
		}
	}
	
}

测试case:

package com.tig.tree;

import org.junit.Test;

public class BinarySearchTreeTest {

	@Test
	public void test() {
		
		BinarySearchTree<Integer> tree = new BinarySearchTree<>();
	
		for (int i = 0; i < 10; i++) {
			tree.insert(i);
		}
		
		System.out.println("最大:" + tree.findMax());
		System.out.println("最小:" + tree.findMin());
	
		tree.remove(5);
		tree.printTree();
	}

}


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