java算法02 - 树
树是一类重要的非线性结构。而二叉树是一种比较重要的树,接下来我们来了解二叉树的相关内容。
二叉搜索树:每个节点都不比它左子树的任意元素小,而且不比它的右子树的任意元素大。
/**
* 二叉搜索树 O(n)
*
* @author jade
*
*/
public class BinarySearchTree { private int count;
private Node root; public int size() {
return count;
} public boolean isEmpty() {
return count == 0;
} /**
* 插入元素
*
* @param key
* @param value
*/
public void insert(int key, Object value) {
root = insert(root, key, value);
} private Node insert(Node node, int key, Object value) {
if (node == null) {
count++;
return new Node(key, value);
}
if (key == node.key) {
node.value = value;
} else if (key < node.key) {
node.left = insert(node.left, key, value);
} else {
node.right = insert(node.right, key, value);
}
return node;
} public boolean contain(int key) {
return contain(root, key);
} private boolean contain(Node node, int key) {
if (node == null) {
return false;
}
if (key == node.key) {
return true;
} else if (key < node.key) {
return contain(node.left, key);
} else {
return contain(node.right, key);
}
} public Node search(int key) {
return search(root, key);
} private Node search(Node node, int key) {
if (node == null) {
return null;
}
if (key == node.key) {
return node;
} else if (key < node.key) {
return search(node.left, key);
} else {
return search(node.right, key);
}
} /**
* 前序遍历
*/
public void preOrder() {
preOrder(root);
} private void preOrder(Node node) {
if (node != null) {
System.out.println(node.key);
preOrder(node.left);
preOrder(node.left);
}
} /**
* 中序遍历
*/
public void inOrder() {
inOrder(root);
} private void inOrder(Node node) {
if (node != null) {
inOrder(node.left);
System.out.println(node.key);
inOrder(node.right);
}
} /**
* 后续遍历
*/
public void postOrder() {
postOrder(root);
} private void postOrder(Node node) {
if (node != null) {
postOrder(node.left);
postOrder(node.right);
System.out.println(node.key);
}
} /**
* 层序遍历(广度优先遍历)
*/
public void levelOrder() {
ConcurrentLinkedQueue<Node> queue = new ConcurrentLinkedQueue<Node>();
queue.add(root);
while (!queue.isEmpty()) {
Node node = queue.peek();
System.out.println(node.key);
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
} /**
* key最小值的节点
*
* @return
*/
public Node getMin() {
if (root == null) {
return null;
}
return minNode(root);
} private Node minNode(Node node) {
if (node.left == null) {
return node;
}
return minNode(node.left);
} /**
* key最大值的节点
*
* @return
*/
public Node getMax() {
if (root == null) {
return null;
}
return getMax(root);
} private Node getMax(Node node) {
if (node.right == null)
return node;
return getMax(node.right);
} /**
* 删除最小值节点
*/
public void removeMin() {
if (root != null) {
root = removeMin(root);
}
} private Node removeMin(Node node) {
if (node.left == null) {
Node rightNode = node.right;
count--;
return rightNode;
}
node.left = removeMin(node.left);
return node;
} /**
* 删除最大值所在节点
*/
public void removeMax() {
if (root != null) {
root = removeMax(root);
}
} private Node removeMax(Node node) {
if (node.right == null) {
Node leftNode = node.left;
count--;
return leftNode;
}
node.right = removeMax(node.right);
return node;
} class Node {
int key;
Object value;
Node left;
Node right; public Node(int key, Object value) {
this.key = key;
this.value = value;
}
}
}
java算法02 - 树的更多相关文章
- java算法实现树型目录反向生成(在指定的盘符或位置生成相应的文件结构)
http://www.cnblogs.com/interdrp/p/6702482.html 由于此次文件管理系统的升级确实给我们带来了很多方便且在性能上有很大提升,经过这段时间的使用 也发现了些问题 ...
- 数据结构和算法(Golang实现)(29)查找算法-2-3树和左倾红黑树
某些教程不区分普通红黑树和左倾红黑树的区别,直接将左倾红黑树拿来教学,并且称其为红黑树,因为左倾红黑树与普通的红黑树相比,实现起来较为简单,容易教学.在这里,我们区分开左倾红黑树和普通红黑树. 红黑树 ...
- JAVA算法系列 冒泡排序
java算法系列之排序 手写冒泡 冒泡算是最基础的一个排序算法,简单的可以理解为,每一趟都拿i与i+1进行比较,两个for循环,时间复杂度为 O(n^2),同时本例与选择排序进行了比较,选择排序又叫直 ...
- JAVA算法系列 快速排序
java算法系列之排序 手写快排 首先说一下什么是快排,比冒泡效率要高,快排的基本思路是首先找到一个基准元素,比如数组中最左边的那个位置,作为基准元素key,之后在最左边和最右边设立两个哨兵,i 和 ...
- Java学习02
Java学习02 1.导入内部的包 一.在包的下面加入下面一句话: import java.util.Scanner; 二.在类中 Scanner input=new Sanner(Sy ...
- java算法 蓝桥杯 乘法运算
问题描述 编制一个乘法运算的程序. 从键盘读入2个100以内的正整数,进行乘法运算并以竖式输出. 输入格式 输入只有一行,是两个用空格隔开的数字,均在1~99之间(含1和99). 输出格式 输出为4行 ...
- java算法 蓝桥杯 扶老奶奶街
一共有5个红领巾,编号分别为A.B.C.D.E,老奶奶被他们其中一个扶过了马路. 五个红领巾各自说话: A :我和E都没有扶老奶奶 B :老奶奶是被C和E其中一个扶过大街的 C :老奶奶是被我和D其中 ...
- java算法 蓝桥杯 高精度加法
问题描述 在C/C++语言中,整型所能表示的范围一般为-231到231(大约21亿),即使long long型,一般也只能表示到-263到263.要想计算更加规模的数,就要用软件来扩展了,比如用数组或 ...
- java算法 蓝桥杯 格子位置
问题描述 输入三个自然数N,i,j (1<=i<=N,1<=j<=N),输出在一个N*N格的棋盘中,与格子(i,j)同行.同列.同一对角线的所有格子的位置. 输入格式 输入共三 ...
随机推荐
- 获取占用fd最大的前20个进程
for x in `ps -eF| awk '{ print $2 }'`;do echo `ls /proc/$x/fd 2> /dev/null | wc -l` $x `cat /proc ...
- Vue-admin工作整理(四):路由组件传参
路由组件传参:如果在一个页面中,需要根据路由去获得参数,去对页面进行一些逻辑处理,首先可以通过this.$router来获取路由实例的参数,这样页面组件和路由就进行了耦合,为了进行分离,更大程度复用, ...
- div变成输入框
<style> #test{ width: 150px;; min-height:20px; max-height:70px; outline: 0; border: 1px solid ...
- java.lang.ClassNotFoundException: javax.servlet.SessionCookieConfig
使用 MockMvc 模拟进行单元测试时出现以下的错误,网上看到的资料应该是说Spring4.0需要servlet3.0的支持? 在pom.xml更改servlet的版本依赖后即可
- 2019.4.24(js)
1. 取得正数和负数的绝对值 Math.abs(7.25) Math.abs(-7.25) 2.利用JS刷新页面方法 https://www.cnblogs.com/Chen-XiaoJun/p/62 ...
- English trip V2 - 4. Really Wild Teacher:Maple Key:Adjectives of feeling
In this lesson you will learn how to recognize animals and describe feeling. 课上内容(Lesson) 词汇(Key Wor ...
- pom中Maven插件 配置 maven-dependency-plugin maven-surefire-plugin
使用Maven插件将依赖包 jar包 war包及配置文件输出到指定目录 1|0写在前面 最近遇到一个朋友遇到一个项目需要将 maven 的依赖包和配置文件分开打包然后用脚本执行程序.这样的好处在于 ...
- jmeter简单得压力测试
Jmeter教程 简单的压力测试 Jmeter是一个非常好用的压力测试工具. Jmeter用来做轻量级的压力测试,非常合适,只需要十几分钟,就能把压力测试需要的脚本写好. 阅读目录 什么是压力测试 ...
- 【记录】【3】设置bing为chrome的默认搜索引擎
方法:设置→搜索→管理搜索引擎→其他搜索引擎→设置bing搜索的网址为 http://cn.bing.com/search?q=%s 注:search?q=%s 是必须的,否则无法将其设置为默认 ...
- linux 常用指令
w 指令可以看到目前接入到服务器的用户(终端)history xx 可以查看本用户(本终端)最后执行的xx条指令last 指令可以查看登录的日志grep "str" filName ...