数据结构--二叉查找树的java实现
来源:互联网 发布:中亚五斯坦知乎 编辑:程序博客网 时间:2024/05/01 05:41
上代码:
package com.itany.erchachazhaoshu;public class BinarySearchTree<T extends Comparable<? super T>>{ //定义二叉查找树的根节点 每一个查找二叉树都有一个自己的root 节点 root外界看不到 private BinaryNode<T> root; public BinarySearchTree() { root=null; } //节点类 private static class BinaryNode<T> { private T element; private BinaryNode<T> left; private BinaryNode<T> right; public BinaryNode(T element) { this(element, null, null); } public BinaryNode(T element,BinaryNode<T> left,BinaryNode<T> right) { this.element=element; this.left=left; this.right=right; } } public void makeEmpty() { root=null; } public boolean isEmpty() { return root==null; } public boolean contains(T t) { return contains(t,root); } //外界是不认识节点的 只会返回T 布尔 或者根本无返回值 public T findMax() throws Exception { if(isEmpty()) throw new Exception(); return findMax(root).element; } public T findMin() throws Exception { if(isEmpty()) throw new Exception(); return findMin(root).element; } public void insert(T t) { root=insert(t,root); } public void remove(T t) { root=remove(t,root); } public void printTree() { if(isEmpty()) System.out.println("Empty Tree"); else printTree(root); } //中序遍历 private void printTree(BinaryNode<T> root) { //如果递归到了叶子节点 没有左右的部分就不执行遍历 而且if也完成了对非空的判断 if(null!=root) { printTree(root.left); System.out.println(root.element); printTree(root.right); } } //此处使用的是尾递归 private boolean contains(T t,BinaryNode<T> root) { //必须在一开始就判断是否为null 否则在调用方法或元素时 会产生空指针异常 也是一个基准条件 if(root==null) return false; int compareRes=t.compareTo(root.element); if(compareRes==0) return true; else if(compareRes<0) return contains(t,root.left);//不会使栈频繁进出 只会覆盖当前栈 else return contains(t,root.right); } // 方法二 使用循环代替尾递归找出最大 是返回对应的那个节点 private BinaryNode<T> findMax(BinaryNode<T> root) { if(root==null) return null; else { while(root.right!=null) { root=root.right ; } } return root; } //方法一 使用递归查找 返回最小节点的引用 private BinaryNode<T> findMin(BinaryNode<T> root) { //必须在一开始就判断是否为null 否则在调用方法或元素时 会产生空指针异常 if(root==null) return null; //基准条件 else if(root.left==null) return root; else return findMin(root.left); } //返回一个插入了之后的整个节点 逐级返回 private BinaryNode<T> insert(T t,BinaryNode<T> root) { if(root==null) return new BinaryNode<T>(t,null,null); else { int com=t.compareTo(root.element); if(com==0) ; else if(com<0) //不断更新当前root的left值 并返回root root.left=insert(t,root.left); else root.right=insert(t,root.right); return root; } } //删除和增加一样 都要返回修改之后的节点 private BinaryNode<T> remove(T t,BinaryNode<T> root) { //没有找到删除的 什么也不做 直接返回该root if(root==null) return root; int com=t.compareTo(root.element); if(com<0) { root.left=remove(t,root.left); } else if(com>0) { root.right=remove(t,root.right); } else { //有两个儿子的情况 if(root.left!=null && root.right!=null) { root.element=findMin(root.right).element; root.right=remove(root.element,root.right);//更新删除之后 } else//包括一个儿子都没有的情况 有一个儿子的情况 return (root.left!=null)?root.left:root.right; } return root; }}
package com.itany.erchachazhaoshu;public class Test{ public static void main(String[] args) { BinarySearchTree bt=new BinarySearchTree(); bt.insert(3); bt.insert(13); bt.insert(1); try { System.out.println("max:"+bt.findMax()); System.out.println("max:"+bt.findMin()); System.out.println(bt.contains(3)); bt.remove(13); System.out.println(bt.contains(13)); } catch (Exception e) { e.printStackTrace(); } } }
0 0
- 数据结构--二叉查找树的java实现
- 二叉查找树的数据结构以及实现(JAVA)
- 重温数据结构:二叉查找树的java实现
- Java实现数据结构之二叉查找树
- 数据结构实战java实现二叉查找树
- 数据结构之Java实现二叉查找树
- java数据结构二叉树的遍历和二叉查找树
- 数据结构学习笔记之Java实现二叉查找树
- Java实现数据结构——二叉查找树
- 数据结构(java语言描述)-- 二叉查找树的链式存储结构的实现
- (数据结构与算法分析 五)------二叉查找树的实现( Java语言描述)
- 面试准备--数据结构与算法(二)--二叉查找树的图文解析及其java实现
- 二叉查找树的Java实现
- java 实现的二叉查找树
- 二叉查找树的java实现
- 二叉查找树的Java实现
- 二叉查找树的java实现
- 二叉查找树Java的实现
- OC中的description和sel
- 通过UrlRewriter配置MVC4伪静态
- 软件测试面试题三:常见的逻辑思维面试题
- round函数 和trunc函数
- 如何在IDE(Eclipse,MyEclipse)快速查看源码---安装Jad插件
- 数据结构--二叉查找树的java实现
- Python正则表达式指南
- Nagle 算法(TCP中用于拥塞控制)详解
- 异步流程控制-7行代码学会co模块
- PHP中file_get_contents如何带上cookies
- ZooKeeper系列之三:ZooKeeper的安装
- android-ScrollView中嵌套EditText使用问题
- apache 使用 mod_fcgid.so模块时 配置指令
- 软件工程师经验分享