二叉查找树的实现
2016-03-28 14:03
507 查看
package com.meteor.algorithm; import java.nio.BufferUnderflowException; /** * Created by Meteor on 2016/3/26. */ public class BinarySearchTree<T extends Comparable<? super T>> { //inner class for BinaryNode structure private static class BinaryNode<T>{ BinaryNode(T theElement){ this(theElement,null,null); } BinaryNode(T theElement ,BinaryNode<T> lt,BinaryNode<T> rt){ element = theElement; left = lt; right = rt; } //the data in the node T element; //left child BinaryNode<T> left; //right child BinaryNode<T> right; } private BinaryNode<T> root; public BinarySearchTree(){ root = null; } 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 BufferUnderflowException(); } return findMin(root).element; } public T findMax(){ if(isEmpty()){ throw new BufferUnderflowException(); } return findMax(root).element; } public void insert(T x){ root = insert(x,root); } public void remove(T x){ root = remove(x,root); } /** * Internal method to find an item in a subtree * @param x is item to search for * @param t the node that roots the subtree * @return the containing the matched item */ private boolean contains(T x,BinaryNode<T> 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; } } /** * recursion * Internal method to find the smallest item in a subtree * @param t the node thta roots the subtree * @return node containing the smallest item */ private BinaryNode<T> findMin(BinaryNode<T> t){ if(t == null){ return null; }else if(t.left == null){ return t; } return findMin(t.left); } /** * not recursion * Internal method to find the largest item in a subtree * @param t the node that roots the subtree * @return node containing the largest item */ private BinaryNode<T> findMax(BinaryNode<T> t){ if(t != null){ while (t.right != null){ t = t.right; } } return t; } /** * Internal method to insert into a subtree * @param x the item to insert * @param t the node that roots the subtree * @return the new root of the subtree */ private BinaryNode<T> insert(T x,BinaryNode<T> t){ if(t == null){ return new BinaryNode<T>(x,null,null); } int compareResult = x.compareTo(t.element); if(compareResult < 0){ t.left = insert(x,t.left); }else if(compareResult > 0){ t.right = insert(x,t.right); }else{ //element duplicate;do nothing } return t; } /** * Internal method to remove from a subtree * @param x the item to remove * @param t the node that roots the subtree * @return the new root of the subtree */ private BinaryNode<T> remove (T x,BinaryNode<T> t){ if(t == null){ return t; //item not found; do nothing } 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){ t.element = findMin(t.right).element; t.right = remove(t.element,t.right); }else{ t = (t.left != null)?t.left : t.right; } return t; } /** * Print the tree contents in sorted order */ public void printTree(){ if(isEmpty()){ System.out.println("Empty tree"); }else{ printTree(root); } } /** * Internal method to print a subtree in sorted order * @param t the node that roots the subtree */ private void printTree(BinaryNode<T> t){ if(t != null){ printTree(t.left); System.out.print(t.element+"; "); printTree(t.right); } } /** * Internal method to compute height of a subtree * @return t the node that roots the subtree */ private int height(BinaryNode<T> t){ if( t == null){ return -1; }else{ return 1 + Math.max(height(t.left),height(t.right)); } } }
TEST:
package com.meteor.algorithm; / b202 ** * BinarySearhTreeTest * Created by Meteor on 2016/3/26. */ public class BinarySearhTreeTest { public static void main(String[] args) { BinarySearchTree binarySearchTree = new BinarySearchTree(); for (int i = 0; i < 50 ; i++) { binarySearchTree.insert((int)Math.floor(Math.random()*100)); } binarySearchTree.printTree(); binarySearchTree.remove(28); System.out.println(); binarySearchTree.printTree(); } }
the result
first print: 1; 2; 6; 8; 10; 14; 18; 21; 22; 28; 30; 31; 35; 40; 42; 43; 46; 47; 50; 52; 54; 55; 56; 58; 60; 63; 66; 68; 69; 72; 74; 77; 79; 80; 81; 91; 93; 94; second print: 1; 2; 6; 8; 10; 14; 18; 21; 22; 30; 31; 35; 40; 42; 43; 46; 47; 50; 52; 54; 55; 56; 58; 60; 63; 66; 68; 69; 72; 74; 77; 79; 80; 81; 91; 93; 94;
相关文章推荐
- 二叉查找树
- AVL树-自平衡二叉查找树(Java实现)
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- C#数据结构之顺序表(SeqList)实例详解
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法