二叉搜索树BST
import java.util.EmptyStackException; /**example:一棵二查查找树 * 6 * 2 8 * 1 4 10 * 3 * **/ public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> { public static void main(String[] args) { BinarySearchTree<Integer> BST = new BinarySearchTree<>(); BinaryNode<Integer> root = new BinaryNode<>(6); BinaryNode<Integer> n1 = new BinaryNode<>(2); BinaryNode<Integer> n2 = new BinaryNode<>(8); BinaryNode<Integer> n3 = new BinaryNode<>(1); BinaryNode<Integer> n4 = new BinaryNode<>(4); BinaryNode<Integer> n5 = new BinaryNode<>(3); BST.root = root; root.left = n1; root.right = n2; n1.left = n3; n1.right = n4; n4.left = n5; BST.printTree(); System.out.println("the max value of BST is: " + BST.findMax()); System.out.println("the min value of BST is: " + BST.findMin()); BST.insert(7); System.out.println("增加节点7后:"); BST.printTree(); BST.remove(6); System.out.println("删除节点6后:"); BST.printTree(); } //定义节点 private static class BinaryNode<AnyType> { AnyType element; //data in the node BinaryNode<AnyType> left; //left child BinaryNode<AnyType> right; //right child //树节点的构造函数 public BinaryNode(AnyType element) { this.element = element; } //树节点的构造函数 public BinaryNode(AnyType element, BinaryNode<AnyType> left, BinaryNode<AnyType> right) { this.element = element; this.left = left; this.right = right; } } //根节点 private BinaryNode<AnyType> root; //BST的构造函数 public BinarySearchTree() { this.root = null; } //建立空树 public void makeEmpty() { this.root = null; } //判断是否为空 public boolean isEmpty() { return this.root == null; } //判断是否包含某一节点 public boolean contains(AnyType x) { return contains(x, this.root); } //寻找二查搜索树上的最小值 public AnyType findMin() { if (isEmpty()) { throw new EmptyStackException(); } return findMin(root).element; } //寻找二查搜索树上的最大值 public AnyType findMax() { if (isEmpty()) { throw new EmptyStackException(); } 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() { if (isEmpty()) System.out.println("Empty tree"); else printTree(this.root); } private boolean contains(AnyType x, BinaryNode<AnyType> t) { //如果t是空集,就返回false if(t == null) { return false; } //注意:compareTo的用法,可以判断两个元素间的三种情况 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) { //findMin方法的递归实现 if (t == null) return null; else if (t.left == null) return t; return findMin(t.left); } private BinaryNode<AnyType> findMax(BinaryNode<AnyType> t) { //findMax方法的非递归实现(while循环,一直寻找最右端的节点) 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) { t.left = insert(x, t.left); } else if (compareResult > 0){ t.right = insert(x, t.right); }else System.out.println("The insert node has already exist, so we will do noting."); 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 //至多有一个子节点(如果没有子节点,直接用null删除即可;反之,用子节点替代) t = (t.left != null) ? t.left: t.right; return t; } private void printTree(BinaryNode<AnyType> t) { //中序遍历 if (t != null) { printTree(t.left); System.out.println(t.element); printTree(t.right); } } }
注:二叉查找树的中序遍历输出就是有序的。
作者:Ryanjie
出处:http://www.cnblogs.com/ryanjan/
本文版权归作者和博客园所有,欢迎转载。转载请在留言板处留言给我,且在文章标明原文链接,谢谢!
如果您觉得本篇博文对您有所收获,觉得我还算用心,请点击右下角的 [推荐],谢谢!