查找二叉树之JAVA实现
2017-04-12 16:02
405 查看
Java实现查找二叉树的相关操作
//树的节点类 class BinaryNode<T> { T element; BinaryNode<T> left; BinaryNode<T> right; public BinaryNode(T theElement) { element = theElement; } public BinaryNode(T theElement, BinaryNode<T> left, BinaryNode<T> right) { this.element = theElement; this.left = left; this.right = right; } } //查找二叉树 public class BinarySearchTree<T extends Comparable> { //私有属性 private BinaryNode<T> root;//根节点 //构造方法 BinarySearchTree() { this.root = null; } //置空操作 public void makeEmpty() { this.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("searchTree is empty"); return findMin(root).element; } //查找最大的元素 public T findMax() { if (isEmpty()) throw new RuntimeException("searchTree is empty"); return findMax(root).element; } //插入操作 public void insert(T x) { this.root = insert(x, root); } //删除操作 public void remove(T x) { this.root = remove(x, root); } //获取树 4000 的最大深度 public int maxDeep() { return maxDeep(root); } //获取树的最小深度 public int minDeep() { return minDeep(root); } //先序打印操作 public void prePrintTree() { prePrintTree(root); System.out.println(); } //中序打印操作 public void infPrintTree() { infPrintTree(root); System.out.println(); } //后序打印操作 public void epiPrintTree() { epiPrintTree(root); System.out.println(); } //判断是否是AVL树 public boolean isAVL() { return Math.abs(maxDeep(root.right) - maxDeep(root.left)) < 2; } public 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; } public BinaryNode<T> findMin(BinaryNode<T> t) { if (t == null) throw new RuntimeException("this binaryNode is Empty"); if (t.left == null) return t; return findMin(t.left); } public BinaryNode<T> findMax(BinaryNode<T> t) { if (t == null) throw new RuntimeException("this binaryNode is Empty"); if (t.right == null) return t; return findMax(t.right); } public BinaryNode<T> insert(T x, BinaryNode<T> t) { if (t == null)//传入节点为空,则将该节点作为根节点返回 return new BinaryNode<>(x, null, null); int compareResult = x.compareTo(t.element); if (compareResult < 0)//传入元素x小于t t.left = insert(x, t.left); else if (compareResult > 0)//传入元素x大于t t.right = insert(x, t.right); return t; } public BinaryNode<T> remove(T x, BinaryNode<T> t) { if (!contains(x, root)) throw new RuntimeException("this searchTree is not contains element:" + x); if (t == null)//传入节点为空 throw new RuntimeException("this binaryNode is empty"); 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 = findMax(t.left).element; remove(t.element, t.left); } else //单儿子情形 t = t.left == null ? t.right : t.left; return t; } public int maxDeep(BinaryNode<T> t) { int dl, dr;//记录左右树的深度 if (t == null) return 0; else { dl = maxDeep(t.left); dr = maxDeep(t.right); } return dl > dr ? dl + 1 : dr + 1; } public int minDeep(BinaryNode<T> t) { int dl, dr;//记录左右树的深度 if (t == null) return 0; else { dl = maxDeep(t.left); dr = maxDeep(t.right); } return dl < dr ? dl + 1 : dr + 1; } public void prePrintTree(BinaryNode<T> t) { if (t == null) return; System.out.print(t.element + " "); prePrintTree(t.left); prePrintTree(t.right); } public void infPrintTree(BinaryNode<T> t) { if (t == null) return; infPrintTree(t.left); System.out.print(t.element + " "); infPrintTree(t.right); } public void epiPrintTree(BinaryNode<T> t) { if (t == null) return; epiPrintTree(t.left); epiPrintTree(t.right); System.out.print(t.element + " "); } //测试方法 public static void main(String[] args) { BinarySearchTree bst = createBinarySearchTree(); System.out.println("先序打印出该树===>>>"); bst.prePrintTree(); System.out.println("中序打印出该树===>>>"); bst.infPrintTree(); System.out.println("后序打印出该树===>>>"); bst.epiPrintTree(); System.out.println("查找最大值===>>>" + bst.findMax()); System.out.println("查找最小值===>>>" + bst.findMin()); System.out.println("树中是否包含元素:" + bst.contains(5)); System.out.println("树的最大深度" + bst.maxDeep()); System.out.println("树的最大深度" + bst.minDeep()); System.out.println("删除某一元素===>>>"); bst.remove(6); bst.prePrintTree(); bst.makeEmpty(); System.out.println(bst.maxDeep()); } /** * 创造一个如下的查找二叉树: * 6 * / \ * 2 8 * / \ * 1 4 * / * 3 * * */ private static BinarySearchTree createBinarySearchTree() { BinarySearchTree<Integer> bst = new BinarySearchTree(); bst.insert(6); bst.insert(2); bst.insert(1); bst.insert(4); bst.insert(3); bst.insert(8); return bst; }
相关文章推荐
- Java 实现二叉树排序和查找
- java实现二叉树查找,统计结点个数,统计树的深度及判断两棵树是否相等
- java实现二叉树查找树
- java实现二叉树查找,先,中,后序编列
- 数据算法之二叉树查找(BinaryTreeL Search)的Java实现
- java实现二叉树查找树
- java实现二叉树及(层序,先序,中序,后序,节点查找)
- Java实现二叉树查找
- Java+Mysql的数据库查找实现
- 用java实现,查找字符串中第一个没有重复出现的字符
- java 递归实现二叉树
- 字符串处理是许多程序中非常重要的一部分,它们可以用于文本显示,数据表示,查找键和很多目的.在Unix下,用户可以使用正则表达式的强健功能实现这些 目的,从Java1.4起,Java核心API就引入了java.util.regex程序包,它是一种有价值的基础
- java实现的递归方法逆序对查找
- java实现查找统计文本文件
- 根据前序遍历和中序遍历重建二叉树的Java实现
- JAVA语言实现二叉树的层次遍历的非递归算法及递归算法。
- 二分查找的Java实现
- java实现常用的查找(线性查找,折半查找)
- JAVA实现二分查找
- JAVA实现二叉树(二叉树的存储、二叉树的遍历)