二叉搜索树BStree
二叉搜索树,实际上是有点类似于二分查找。实际上很简单,就是递归。直接上代码,有点要注意的就是删除的时候,如果是左子树和右子树都存在的话,要寻找继承者(successor).
import java.util.HashMap;
import java.util.Map;
import java.util.Stack; public class BSTreeComparable<T extends Comparable> { private transient Node root; private static final String SUCCESSOR="successor"; private static final String SUCCESSOR_PARENT="parent"; private static final Node EMPTY_NODE = new Node(null,null,null,"empty node"); public BSTreeComparable() { //初始化为一个空树
root = null;
} /**
* 查询数据方法
* @param key
* @return
*/
public Node get(T key){
if(root ==null) return null;
Node node = getRecursive(root,key);
if(node ==null) return EMPTY_NODE;
return node;
} /**
* 往BST中插入数据
* @param key
* @param value
*/
public void put(T key,Object value){
if (root == null){
root = new Node(null,null,key,value);
}else{
putRecursive(root,key,value);
} } public void remove(T key){
Node newTree = remove(root,key);
root = newTree;
} private Node remove(Node tree ,T key){
if(tree==null) return null; //先递归去找到这个节点
if (key.compareTo(tree.key) < 0) {
tree.leftNode = remove(tree.leftNode, key);
return tree;
}
else if (key.compareTo(tree.key) > 0) {
tree.rightNode = remove(tree.rightNode, key);
return tree;
}// 相等说明已经找到这个节点了
else{
//case 1 假如这个节点左子树为空,则右子树顶包
if (tree.leftNode == null && tree.rightNode!=null) {
Node rightTree = tree.rightNode;
tree=null;
return rightTree;
}
//case 2 假如这个节点右子树为空,则左子树顶包
else if (tree.rightNode == null&& tree.leftNode!=null){
Node leftTree = tree.leftNode;
tree=null;
return leftTree;
//case 3 是叶子节点
}else if(tree.rightNode == null&& tree.leftNode==null){
tree=null; return null; }
//case 4 假如这个节点左右子树都不为空,则找到右子树种的最小值顶包
else{
Node toBeDeleted = tree;
Map map = getSuccessor(tree.rightNode,null);
Node successor = (Node) map.get(SUCCESSOR);
Node successorParent = (Node) map.get(SUCCESSOR_PARENT);
//如果successorParent是null的话,说明successor就是右节点
if(successorParent==null){
successor.leftNode = toBeDeleted.leftNode;
tree=null;
toBeDeleted =null;
return successor;
}else{
successor.leftNode = toBeDeleted.leftNode;
successor.rightNode = toBeDeleted.rightNode;
successorParent.leftNode =null;
tree=null;
toBeDeleted =null;
return successor;
}
}
}
} private Map<String,Node> getSuccessor(Node tree,Map<String,Node> map) {
if (map == null) {
map = new HashMap<>();
}
if (tree.leftNode == null){
map.put(SUCCESSOR,tree);
return map;
}
map.put(SUCCESSOR_PARENT,tree);
Map<String,Node> map2 = getSuccessor(tree.leftNode,map);
return map2;
} private Node putRecursive(Node tree,T key,Object value){
if(tree==null) return new Node(null,null,key,value);
if(key.compareTo(tree.key)<0){
tree.leftNode = putRecursive(tree.leftNode,key,value);
}else if(key.compareTo(tree.key)>0){
tree.rightNode = putRecursive(tree.rightNode,key,value);
}else{
tree.content = value;
}
return tree;
} /**
* 递归比较大小,如果小于该节点,则拿左节点继续比较
* 如果大于该节点,就拿右节点继续比较
* @param tree
* @param key
* @return
*/
private Node getRecursive(Node tree,T key){
if(tree==null) return null;
if(key.compareTo(tree.key)<0){
return getRecursive(tree.leftNode,key);
}else if(key.compareTo(tree.key)>0){
return getRecursive(tree.rightNode,key);
}else{
return tree;
}
} public static int getHeight(Node root) { if (root == null) {
return 0;
}
int leftHeight = 0;//记录左子树的树高
int rightHeight = 0;//记录右子树树高
if (root.leftNode != null) {//左子树不为空
leftHeight += getHeight(root.leftNode) + 1;//实际就是左子树树高的累计,加上root节点,如果不加1,得到的就是最大子树的树高,不好root节点
}
if (root.rightNode != null) {
rightHeight += getHeight(root.rightNode) + 1;
}
return leftHeight >= rightHeight ? leftHeight : rightHeight;
}
public void displayTree(){
Stack globalStack = new Stack();
globalStack.push(root);
int treeHeight = getHeight(root);
int nBlanks = (int) Math.pow(2d,(double)treeHeight);
boolean isRowEmpty = false;
System.out.println("=========================================================================");
while(!isRowEmpty) {
Stack localStack = new Stack();
isRowEmpty = true;
for (int j =0;j<nBlanks;j++) {
System.out.print(" ");
} while (!globalStack.isEmpty()) {
Node temp = (Node)globalStack.pop();
if (temp!=null) {
System.out.print(temp.key);
localStack.push(temp.leftNode);
localStack.push(temp.rightNode);
if (temp.leftNode != null || temp.rightNode !=null) {
isRowEmpty = false;
}
}
else {
System.out.print("--");
localStack.push(null);
localStack.push(null);
}
for (int j = 0;j<nBlanks*2-2;j++) {
System.out.print(" ");
}
}
System.out.println();
nBlanks /= 2;
while (!localStack.isEmpty()) {
globalStack.push(localStack.pop());
}
}
System.out.println("=========================================================================");
} static final class Node<T>{
public Node leftNode;
public Node rightNode;
public T key;
public Object content; public Node(Node leftNode, Node rightNode, T key, Object content) {
this.leftNode = leftNode;
this.rightNode = rightNode;
this.key = key;
this.content = content;
}
}
}
下面是测试代码:
public class BSTreeComparableTest {
public static void main(String[] args) {
BSTreeComparable tree = new BSTreeComparable();
tree.put(20,"20");
tree.put(10,"10");
tree.put(7,"7");
tree.put(4,"4");
tree.put(30,"30");
tree.put(40,"40");
tree.put(14,"14");
tree.put(12,"12");
tree.put(15,"15");
//tree.remove(12);
//tree.remove(30);
//tree.remove(7);
tree.remove(10);
tree.displayTree();
}
}
觉得写的好的,点个赞,谢谢,大冬天晚上,很辛苦的(◔◡◔)
二叉搜索树BStree的更多相关文章
- 基于visual Studio2013解决算法导论之029二叉搜索树
题目 二叉搜索树 解决代码及点评 #include <stdio.h> #include <malloc.h> #include <stdlib.h> ty ...
- 二叉搜索树(Binary Search Tree)--C语言描述(转)
图解二叉搜索树概念 二叉树呢,其实就是链表的一个二维形式,而二叉搜索树,就是一种特殊的二叉树,这种二叉树有个特点:对任意节点而言,左孩子(当然了,存在的话)的值总是小于本身,而右孩子(存在的话)的值总 ...
- 【剑指offer】二叉搜索树转双向链表
转载请注明出处:http://blog.csdn.net/ns_code/article/details/26623795 题目描写叙述: 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表. ...
- 数据结构-二叉树(应用篇)-之二叉搜索树 C和C++的实现
一.概念 二叉搜索树(Binary Sort Tree/Binary Search Tree...),是二叉树的一种特殊扩展.也是一种动态查找表. 在二叉搜索树中,左子树上所有节点的均小于根节点,右子 ...
- Java与算法之(13) - 二叉搜索树
查找是指在一批记录中找出满足指定条件的某一记录的过程,例如在数组{ 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 }中查找数字15,实现代码很简单 ...
- 【Java】 大话数据结构(11) 查找算法(2)(二叉排序树/二叉搜索树)
本文根据<大话数据结构>一书,实现了Java版的二叉排序树/二叉搜索树. 二叉排序树介绍 在上篇博客中,顺序表的插入和删除效率还可以,但查找效率很低:而有序线性表中,可以使用折半.插值.斐 ...
- 使用C++实现二叉搜索树的数据结构
需要注意的地方: ①二叉搜索树删除一个指定结点R,若R为叶子结点,则将R的父结点中指向R的指针改为指向nullptr:若R的左右子结点一个为空,一个非空,则将R的父结点中指向R的指针改为指向R的非空子 ...
- 使用二叉搜索树实现一个简单的Map
之前看了刘新宇大大的<算法新解>有了点收获,闲来无事,便写了一个二叉搜索树实现的Map类. java的Map接口有很多不想要的方法,自己定义了一个 public interface IMa ...
- 数据结构---二叉搜索树BST实现
1. 二叉查找树 二叉查找树(Binary Search Tree),也称为二叉搜索树.有序二叉树(ordered binary tree)或排序二叉树(sorted binary tree),是指一 ...
随机推荐
- Windows下基于python3使用word2vec训练中文维基百科语料(二)
在上一篇对中文维基百科语料处理将其转换成.txt的文本文档的基础上,我们要将为文本转换成向量,首先都要对文本进行预处理 步骤四:由于得到的中文维基百科中有许多繁体字,所以我们现在就是将繁体字转换成简体 ...
- 使用ubuntun16.04代码笔记
(1)cd /代表到根目录下面:ls表示将盘中内容列出:cd /home表是打开根目录下的home文件夹:(注意:凡是根目录下的文件夹前面都要加 /) (2)快捷键方式:可以用tab自动补全 (1 ...
- CentOS7安装MySQL5.7以及修改密码
CentOS7安装mysql [root@bd005 ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch. ...
- 日常开发技巧:在远程机器上直接使用adb
背景 嵌入式开发中,开发工作是在远程服务器上进行的.当需要adb推送一个文件到开发板时,则需要重新在本地机器中找到该文件,再执行命令.这样的操作比较麻烦. 下面介绍我的解决方式. sshfs挂载 首先 ...
- STM32接口FSMC/FMC难点详解
STM32接口FSMC/FMC难点详解 转载 http://blog.sina.com.cn/s/blog_808bca130102x94k.html STM32F767的FMC将外部存储器划分为 ...
- Linux内核死锁检测机制【转】
转自:http://www.oenhan.com/kernel-deadlock-check 死锁就是多个进程(线程)因为等待别的进程已占有的自己所需要的资源而陷入阻塞的一种状态,死锁状态一旦形成,进 ...
- caffe Python API 之激活函数ReLU
import sys import os sys.path.append("/projects/caffe-ssd/python") import caffe net = caff ...
- 流程控制--while
/* while 是在有条件控制的情况下 进行的循环 */ [root@localhost test1]# vim .py //ADD #!/usr/bin/python n = while True ...
- 关于进度管理工具Gantt图
关于进度管理工具Gantt图 18.以下关于进度管理工具图的叙述中,不正确的是( D). A.能清晰地表达每个任务的开始时间.结束时间和持续时间 B.能清晰地表达任务之间的并行关系 C.不能清晰地确定 ...
- leetcode 之Single Number(14)
这题并不难,但需要注意细节. ListNode* addTwo(ListNode *l1, ListNode *l2) { ListNode dummy(-); ; ListNode *prev = ...