二叉搜索树基本操作

求树中的结点个数
判断节点是否为空
向树中插入新结点key-value
树中是否存在key
返回树中key对应的value值
先序遍历
中序遍历
后续遍历
层序遍历
求树中key最小的结点
求树中key最大的结点
删除树中key最小的结点
删除树中key最大的结点
树中删除一个结点

  代码:

/**
* @param <Key> 键的泛型
* @param <Value> 值的泛型
*/
public class BinarySearchTree<Key extends Comparable<? super Key>, Value> {
private class Node {
private Key key; // 键,相当于词典里的单词
private Value value; // 值,相当于词典里的解释
private Node left; // 左孩子结点
private Node right;// 右孩子结点 //结点的构造函数
public Node(Key key, Value value) {
this.key = key;
this.value = value;
this.left = null;
this.right = null;
} // getters and setters
public Key getKey() {
return key;
} public void setKey(Key key) {
this.key = key;
} public Value getValue() {
return value;
} public void setValue(Value value) {
this.value = value;
} public Node getLeft() {
return left;
} public void setLeft(Node left) {
this.left = left;
} public Node getRight() {
return right;
} public void setRight(Node right) {
this.right = right;
} @Override
public String toString() {
return "Node{" +
"key=" + key +
", value=" + value +
'}';
}
} private Node root; // 二叉搜索树的根节点
private int size; //二叉搜索树中的结点个数 //树的构造函数
public BinarySearchTree() {
this.root = null;
this.size = 0;
} //求树中的结点个数
public int size() {
return this.size;
} //判断节点是否为空
public boolean isEmpty() {
return this.size == 0;
} //以node结点为根的树(子树)中插入key-value
private Node insert(Node node, Key key, Value value) {
if (node == null) {
size++;
return new Node(key, value);
} else if (node.key.compareTo(key) > 0) {
node.setLeft(insert(node.getLeft(), key, value));
} else if (node.key.compareTo(key) < 0) {
node.setRight(insert(node.getRight(), key, value));
} else { // 相等的时候
node.value = value;
}
return node;
} //向树中插入新结点key-value
public void insert(Key key, Value value) {
root = insert(root, key, value);
} // node结点为根的树(子树)中是否存在key
private boolean contain(Node node, Key key) {
if (node == null) {
return false;
} else if (node.key.compareTo(key) == 0) {
return true;
} else if (node.key.compareTo(key) > 0) {
return contain(node.getLeft(), key);
} else {
return contain(node.getRight(), key);
}
} //树中是否存在key
public boolean contain(Key key) {
return contain(root, key);
} //返回node结点为根的树(子树)中key对应的value值
private Value search(Node node, Key key) {
if (node == null) {
return null;
} else if (node.getKey().compareTo(key) == 0) {
return node.getValue();
} else if (node.getKey().compareTo(key) > 0) {
return search(node.getLeft(), key);
} else {
return search(node.getRight(), key);
}
}
//返回树中key对应的value值
public Value search(Key key) {
return search(root, key);
} //先序遍历
private void preOrder(Node node) {
if (node == null) return;
System.out.println(node.key);
preOrder(node.getLeft());
preOrder(node.getRight());
} //先序遍历
public void preOrder() {
preOrder(root);
} //中序遍历
private void inOrder(Node node) {
if (node == null) return;
inOrder(node.getLeft());
System.out.println(node.getKey());
inOrder(node.getRight());
} //中序遍历
public void inOrder() {
inOrder(root);
} //后续遍历
private void postOrder(Node node) {
if (node == null) return;
postOrder(node.getLeft());
postOrder(node.getRight());
System.out.println(node.getKey());
} //后续遍历
public void postOrder() {
postOrder(root);
} //层序遍历
public void levelOrder() {
if (root == null) return;
java.util.LinkedList<Node> queue = new java.util.LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
Node node = queue.pop();
System.out.println(node);
if (node.getLeft() != null) {
queue.add(node.getLeft());
}
if (node.getRight() != null) {
queue.add(node.getRight());
}
}
} //以node为根的树中,key最小的结点
private Node minNode(Node node) {
if (node.getLeft() == null) return node; return minNode(node.getLeft());
} //树中key最小的结点
public Node minNode() {
return minNode(root);
} //以node为根的树中,key最大的结点
private Node maxNode(Node node) {
if (node.getRight() == null) return node; return maxNode(node.getRight());
} //树中key最大的结点
public Node maxNode() {
return maxNode(root);
} //删除node为根的树(子树)中key最小的结点
private Node removeMin(Node node) {
if (node == null) return null;
if (node.getLeft() == null) {
Node rightNode = node.getRight();
node.setRight(null);
size--;
return rightNode;
}
node.setLeft(removeMin(node.getLeft()));
return node;
} //删除树中key最小的结点
public void removeMin() {
root = removeMin(root);
} //删除node为根的树(子树)中key最大的结点
private Node removeMax(Node node) {
if (node == null) return null;
if (node.getRight() == null) {
Node leftNode = node.getLeft();
node.setLeft(null);
size--;
return leftNode;
}
node.setRight(removeMax(node.getRight()));
return node;
} //删除树中key最大的结点
public void removeMax() {
root = removeMax(root);
} //删除一个结点
private Node remove(Node node, Key key) {
if (node == null) return null;
if (key.compareTo(node.key) < 0) {//如果比根小,去左子树删除,然后返回根
node.setLeft(remove(node.getLeft(), key));
return node;
} else if (key.compareTo(node.key) > 0) {//如果比根大,去右子树删除,然后返回根
node.setRight(remove(node.getRight(), key));
return node;
} else { //key == node.key //如果与当点子树的根key相等
if (node.getLeft() == null) { //判断当前子树的根是否有左子树
Node rightNode = node.getRight();
node.setRight(null);
size--;
return rightNode;
} else if (node.getRight() == null) {//判断当前子树的根是否有右子树
Node leftNode = node.getLeft();
node.setLeft(null);
size--;
return leftNode;
} else { // 如果当前子树既有左子树,又有右子树
Node successor = minNode(node.getRight());//寻找后继结点,也就是右子树中最小的结点
successor = new Node(successor.getKey(), successor.getValue());//新建一个与后继结点一模一样的,为了替换
size++;//相当于添加一个结点
successor.setRight(removeMin(node.getRight()));//删除后继结点,然后连上原来的右边
successor.setLeft(node.getLeft());//连上原来的左边
node.left = node.right = null;//原来的结点已经被替换了,可以不要了。
size--;//相当于删除一个节点
return successor;//当前新的子树根就是successor
}
}
} public void remove(Key key) {
root = remove(root, key);
} public static void main(String[] args) {
//建立一颗二叉搜索树
BinarySearchTree<String, String> bst = new BinarySearchTree<>(); //插入数据
bst.insert("dog", "狗,一种动物");
bst.insert("password", "密码");
bst.insert("cat", "猫,一种动物");
bst.insert("cup", "杯子");
bst.insert("tom", "汤姆,《猫和老鼠》中的猫");
bst.insert("jerry", "杰瑞,《猫和老鼠》中的老鼠");
bst.insert("a", "26个英文字母中的第一个"); //search()
System.out.println(bst.search("a"));//26个英文字母中的第一个
System.out.println(bst.search("dog"));//狗,一种动物
System.out.println(bst.search("ttttt"));//null //contain()
System.out.println(bst.contain("cup"));//true
System.out.println(bst.contain("tom"));//true
System.out.println(bst.contain("tttttt"));//false //maxNode()
System.out.println(bst.maxNode());//Node{key=tom, value=汤姆,《猫和老鼠》中的猫} //minNode()
System.out.println(bst.minNode());//Node{key=a, value=26个英文字母中的第一个} //removeMax()
bst.removeMax();
System.out.println(bst.maxNode());//Node{key=password, value=密码} //removeMin()
bst.removeMin();
System.out.println(bst.minNode());//Node{key=cat, value=猫,一种动物} //size()
System.out.println(bst.size());//5 //levelOrder()
bst.levelOrder(); //remove();
System.out.println("--------------");
bst.remove("password");
bst.preOrder();
}
}

  

二叉搜索树(Java实现)的更多相关文章

  1. 二叉搜索树Java实现(查找、插入、删除、遍历)

    由于最近想要阅读下 JDK1.8 中 HashMap 的具体实现,但是由于 HashMap 的实现中用到了红黑树,所以我觉得有必要先复习下红黑树的相关知识,所以写下这篇随笔备忘,有不对的地方请指出- ...

  2. leetcode- 将有序数组转换为二叉搜索树(java)

    将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定有序数组: [-10,-3,0, ...

  3. 数据结构-二叉搜索树Java实现

    1,Node.java 生成基础二叉树的结构 package com.cnblogs.mufasa.searchTree; /** * 节点配置父+左+右 */ public class Node{ ...

  4. 有序链表转换二叉搜索树(LeetCode)

    将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定有序数组: [-10,-3,0, ...

  5. 【算法与数据结构】二叉搜索树的Java实现

    为了更加深入了解二叉搜索树,博主自己用Java写了个二叉搜索树,有兴趣的同学可以一起探讨探讨. 首先,二叉搜索树是啥?它有什么用呢? 二叉搜索树, 也称二叉排序树,它的每个节点的数据结构为1个父节点指 ...

  6. Java实现二叉搜索树的添加,前序、后序、中序及层序遍历,求树的节点数,求树的最大值、最小值,查找等操作

    什么也不说了,直接上代码. 首先是节点类,大家都懂得 /** * 二叉树的节点类 * * @author HeYufan * * @param <T> */ class Node<T ...

  7. Java与算法之(13) - 二叉搜索树

    查找是指在一批记录中找出满足指定条件的某一记录的过程,例如在数组{ 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 }中查找数字15,实现代码很简单 ...

  8. Java创建二叉搜索树,实现搜索,插入,删除操作

    Java实现的二叉搜索树,并实现对该树的搜索,插入,删除操作(合并删除,复制删除) 首先我们要有一个编码的思路,大致如下: 1.查找:根据二叉搜索树的数据特点,我们可以根据节点的值得比较来实现查找,查 ...

  9. 自己动手实现java数据结构(六)二叉搜索树

    1.二叉搜索树介绍 前面我们已经介绍过了向量和链表.有序向量可以以二分查找的方式高效的查找特定元素,而缺点是插入删除的效率较低(需要整体移动内部元素):链表的优点在于插入,删除元素时效率较高,但由于不 ...

随机推荐

  1. CentOS6实现路由器功能

    网络之间的通信主要是依靠路由器,当然生成环境中是拥有路由器的,但是系统中的路由配置也是需要了解一下地,今天讲解一下在CentOS6环境下搭建路由器,此乃入门级的简单实验.拓扑如上图已经规划好,暂且使用 ...

  2. 浅谈this关键字

    在我学习this关键字的时候,通过查找资料总结出一些this的特殊用法, 供大家参考,代码里面有我总结的分析过程! 箭头函数里的this: var username = "全局"; ...

  3. java中的mvc和三层结构究竟是什么关系

    一件事,要知其然往往很简单,要知其所以然通常不是那么容易,就如最近重新巩固spring的过程中,就觉得还有许多问题其实并不是十分明了. 屈指一算,手头上做过的正式项目也有了四五六七个了,不管用的数据库 ...

  4. vxWorks 命令

    1.4.1 任务管理    sp( )            用缺省参数创建一个任务(priority="100" 返回值为任务ID,或错误)(taskSpawn) sps( )  ...

  5. Sublime codeIntel 配置支持php自动提示

    Sublime codeIntel 配置支持php自动提示 下载地址:https://github.com/SublimeCodeIntel/SublimeCodeIntel 安装方法:下载后放到su ...

  6. linux命令之 ifconfig

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...

  7. eclipse中添加Java代码注释模板

    eclipse中添加Java代码注释模板 1.Window->Preference->Java->Code Style->Code Template,进入注释编辑界面 2.文件 ...

  8. hi3531的hifb显示1080p60Hz

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h&g ...

  9. windows驱动程序wdf--KMDF大致框架

    继WDM后微软出了WDF,封装了WDM中的一些基本代码逻辑.本人菜鸟,也不知道本质上有何区别,只觉得是多了Wdf开头的函数,基本的编程框架上有点出入. KMDF是WDF的内核级部分,为了理清KMDF的 ...

  10. Django学习-19-缓存

    由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcache中,5 ...