版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/starjingweij/article/details/80115326
定义:
二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。
实现:
首先看看如何实现一个二叉查找树的节点,节点类分别包含节点的数据,节点的左节点,节点的右节点,实现起来不难
public class BinaryNode<AnyType> { AnyType element; BinaryNode<AnyType> left; BinaryNode<AnyType> right; BinaryNode(AnyType theElement){ this(theElement, null, null); } BinaryNode(AnyType theElement,BinaryNode<AnyType> lt, BinaryNode<AnyType> rt){ element = theElement; left = lt; right = rt; } }
下面实现一个二叉查找树的类
public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> { private BinaryNode<AnyType> root; // private Comparator<? super AnyType> cmp; // public BinarySearchTree(Comparator<?super AnyType> c){ // root = null; // cmp = c; // } private static class BinaryNode<AnyType>{ AnyType element; BinaryNode<AnyType> left; BinaryNode<AnyType> right; BinaryNode(AnyType theElement){ this(theElement, null, null); } BinaryNode(AnyType theElement,BinaryNode<AnyType> lt, BinaryNode<AnyType> rt){ element = theElement; left = lt; right = rt; } } public BinarySearchTree(){ root = null; } public void makeEmpty(){ root = null; } public boolean isEmpty(){ return root == null; } public boolean contains(AnyType x){ return contains(x,root); } public AnyType findMin() throws Exception { if(isEmpty()) throw new Exception(); return findMin(root).element; } public AnyType findMax() throws Exception { if(isEmpty()) throw new Exception(); return findMax(root).element; } public void insert(AnyType x){ root = insert(x,root); } public void remove(AnyType x){ root = remove(x,root); } public void printTree(){ } // private int myCompare(AnyType lhs, AnyType rhs){ // if(cmp!=null) // return cmp.compare(lhs,rhs); // else return ((Comparable)lhs).compareTo(rhs); // } 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; } 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) while (t.right!=null) t = t.right; return t; } private BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> t){ if(t == null) return new BinaryNode<>(x , null ,null); int compareResult = x.compareTo(t.element); if(compareResult<0) insert(x, t.left); else if(compareResult>0) insert(x,t.right); else ; return t; } private BinaryNode<AnyType> remove(AnyType x, BinaryNode<AnyType> t){ if(t == null) return t; 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; } private void print(AnyType x, BinaryNode<AnyType> t){ } }