二叉查找树
2012-07-12 15:32
127 查看
package net.liuyx.algorithm; import java.util.Comparator; public class BinarySearchTree<T> { private static class BinaryNode<E>{ public BinaryNode(E theElement) { this(theElement,null,null); } BinaryNode(E theElement, BinaryNode<E> lt, BinaryNode<E> rt){ element = theElement; left = lt; right = rt; } E element; BinaryNode<E> left; BinaryNode< E> right; } private BinaryNode<T> root; private Comparator<? super T> cmp; public BinarySearchTree() { this(null); } public BinarySearchTree(Comparator<? super T>c) { root = null; cmp = c; } @SuppressWarnings("unchecked") private int myCompare(T lhs, T rhs) { if(cmp != null) return cmp.compare(lhs, rhs); else return ((Comparable<T>)lhs).compareTo(rhs); } public void makeEmpty() { root = null; } public boolean isEmpty() { return root == null; } public boolean contains(T x) { return contains(x,root); } public T findMin() { if(isEmpty()) throw new RuntimeException(new NullPointerException("There is no elements to find min")); return findMin(root).element; } public T findMax() { if(isEmpty()) throw new RuntimeException(new NullPointerException("There is no elements to find max one")); return findMax(root).element; } public void insert(T x) { root = insert(x, root); } public void remove(T x) { root = remove(x, root); } private boolean contains(T x, BinaryNode<T> t) { if(t == null) return false; int comareResult = myCompare(x,t.element); if(comareResult < 0) return contains(x,t.left); else if(comareResult > 0) return contains(x,t.right); else return true; } private BinaryNode<T> findMin(BinaryNode<T> t){ if(t == null) return null; else if(t.left == null) return t; return findMin(t.left); } private BinaryNode<T> findMax(BinaryNode<T> t){ if(t != null) while(t.right != null) t = t.right; return t; } private BinaryNode<T> insert(T x, BinaryNode<T> t){ if(t == null) return new BinaryNode<T>(x); int compareResult = myCompare(x,t.element); if(compareResult < 0) t.left = insert(x,t.left); else if(compareResult > 0) t.right = insert(x,t.right); else ;// Duplicate; do nothing return t; } private BinaryNode<T> remove(T x, BinaryNode<T> t){ if(t == null) return t; //Item not found, do nothing int compareResult = myCompare(x,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) { t.element = findMax(t.right).element; t.right = remove(t.element,t.right); }else t = (t.left != null) ? t.left : t.right; return t; } public static void main(String[] args) { BinarySearchTree<Integer> bst = new BinarySearchTree<Integer>(); int[]a = {1,3,5,9,4,6}; for(int i: a) { bst.insert(i); } int min = bst.findMin(); System.out.println(min); int max = bst.findMax(); System.out.println(max); System.out.println(bst.contains(4)); bst.remove(9); System.out.println(bst.findMax()); } }
相关文章推荐
- erl_tree-通用二叉查找树 gb_tree
- [数据结构]二叉查找树 简单实现
- 二叉搜索树(二叉排序树,二叉查找树,二叉检索树)的查找,插入,删除
- 二叉查找树的实现
- 二叉查找树
- 二叉查找树
- C语言实现二叉查找树(BST)的基本操作
- bzoj 1564 [NOI2009]二叉查找树(树形DP)
- 检查一颗二叉树是否是二叉查找树
- 链表——有序链表化为平衡二叉查找树
- lintcode ----在二叉查找树中插入节点
- hdu 5444 继续二叉查找树
- 二叉查找树(一)之 C语言的实现
- 二叉查找树的基本操作实现
- 二叉查找树查找指定排名元素的算法
- 二叉查找树 _ 二叉排序树 _ 二叉搜索树_C++
- 二叉查找树(一)
- BST-二叉查找树
- 数据结构进阶(四)二叉排序树(二叉查找树)
- 每日一省之——使用递归法实现二叉查找树(BST),API齐全