二叉搜索树(Java实现)
二叉搜索树基本操作
求树中的结点个数
判断节点是否为空
向树中插入新结点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实现)的更多相关文章
- 二叉搜索树Java实现(查找、插入、删除、遍历)
由于最近想要阅读下 JDK1.8 中 HashMap 的具体实现,但是由于 HashMap 的实现中用到了红黑树,所以我觉得有必要先复习下红黑树的相关知识,所以写下这篇随笔备忘,有不对的地方请指出- ...
- leetcode- 将有序数组转换为二叉搜索树(java)
将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定有序数组: [-10,-3,0, ...
- 数据结构-二叉搜索树Java实现
1,Node.java 生成基础二叉树的结构 package com.cnblogs.mufasa.searchTree; /** * 节点配置父+左+右 */ public class Node{ ...
- 有序链表转换二叉搜索树(LeetCode)
将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定有序数组: [-10,-3,0, ...
- 【算法与数据结构】二叉搜索树的Java实现
为了更加深入了解二叉搜索树,博主自己用Java写了个二叉搜索树,有兴趣的同学可以一起探讨探讨. 首先,二叉搜索树是啥?它有什么用呢? 二叉搜索树, 也称二叉排序树,它的每个节点的数据结构为1个父节点指 ...
- Java实现二叉搜索树的添加,前序、后序、中序及层序遍历,求树的节点数,求树的最大值、最小值,查找等操作
什么也不说了,直接上代码. 首先是节点类,大家都懂得 /** * 二叉树的节点类 * * @author HeYufan * * @param <T> */ class Node<T ...
- Java与算法之(13) - 二叉搜索树
查找是指在一批记录中找出满足指定条件的某一记录的过程,例如在数组{ 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 }中查找数字15,实现代码很简单 ...
- Java创建二叉搜索树,实现搜索,插入,删除操作
Java实现的二叉搜索树,并实现对该树的搜索,插入,删除操作(合并删除,复制删除) 首先我们要有一个编码的思路,大致如下: 1.查找:根据二叉搜索树的数据特点,我们可以根据节点的值得比较来实现查找,查 ...
- 自己动手实现java数据结构(六)二叉搜索树
1.二叉搜索树介绍 前面我们已经介绍过了向量和链表.有序向量可以以二分查找的方式高效的查找特定元素,而缺点是插入删除的效率较低(需要整体移动内部元素):链表的优点在于插入,删除元素时效率较高,但由于不 ...
随机推荐
- 我的Java设计模式-单例模式
就算不懂设计模式的兄弟姐妹们,想必也听说过单例模式,并且在项目中也会用上.但是,真正理解和熟悉单例模式的人有几个呢?接下来我们一起来学习设计模式中最简单的模式之一--单例模式 一.为什么叫单例模式? ...
- R语言︱词典型情感分析文本操作技巧汇总(打标签、词典与数据匹配等)
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 笔者寄语:情感分析中对文本处理的数据的小技巧要 ...
- HighCharts之2D条状图
HighCharts之2D条状图 1.HighCharts之2D条状图源码 bar.html: <!DOCTYPE html> <html> <head> < ...
- eclipse中的System.getProperty("user.dir")
eclipse中的System.getProperty("user.dir") 1.在Java Application中,上述中的获取的是Java项目的路径 (1)运行源码 /** ...
- Initialization error SQL*Net not properly installed
1.错误描述 2.错误原因 由于我在64位操作系统上安装了32位PL/SQL导致出错,出现兼容性连接问题 3.解决办法 安装一个32位的Oracle客户端,Tools-Preferences-Orac ...
- ArgumentError: Error #1063: BasicChart/dataFunc() 的参数数量不匹配。应该有 2 个,当前为 3 个。
1.错误描述 ArgumentError: Error #1063: BasicChart/dataFunc() 的参数数量不匹配.应该有 2 个,当前为 3 个. at mx.charts.char ...
- Hibernate【映射】知识要点
前言 前面的我们使用的是一个表的操作,但我们实际的开发中不可能只使用一个表的...因此,本博文主要讲解关联映射 集合映射 需求分析:当用户购买商品,用户可能有多个地址. 数据库表 我们一般如下图一样设 ...
- pat1091-1100
1091bfs傻逼题,dfs会爆栈 #include<iostream> #include<cstdio> #include<cstring> #include&l ...
- 浅谈js中null和undefined的区别
在JS中,null和undefined是经常让人摸不着头脑的东西,尤其是在数据初始化以及处理的过程中,经常稍微不注意,就会让页面在渲染时出现报错,下面来细说下,这两者之间的区别: null 表示一个对 ...
- webpack3配置字体图标和打包相关问题
webpak配置字体图标有两种方式 一.将字体图标和css打包到同一个文件中. 1.首先需要安装url-loader npm install --save-dev url-loader 2.相关配置如 ...