定义
二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。

性质
1,任意节点x,其左子树中的key不大于x.key,其右子树中的key不小于x.key。
2,不同的二叉搜索树可以代表同一组值的集合。
3,二叉搜索树的基本操作和树的高度成正比,所以如果是一棵完全二叉树的话最坏运行时间为Θ(lgn),但是若是一个n个节点连接成的线性树,那么最坏运行时间是Θ(n)。
4,根节点是唯一一个parent指针指向NIL节点的节点。
5,每一个节点至少包括key、left、right与parent四个属性,构建二叉搜索树时,必须存在针对key的比较算法。


简单实现(curd操作)

TreeNode.java

public class TreeNode {
private int data;
private TreeNode leftChild;
private TreeNode rightChild;
public TreeNode parent; public int getData() {
return data;
} public void setData(int data) {
this.data = data;
} public TreeNode getLeftChild() {
return leftChild;
} public void setLeftChild(TreeNode leftChild) {
this.leftChild = leftChild;
} public TreeNode getRightChild() {
return rightChild;
} public void setRightChild(TreeNode rightChild) {
this.rightChild = rightChild;
} public TreeNode getParent() {
return parent;
} public void setParent(TreeNode parent) {
this.parent = parent;
} public TreeNode(int data) {
super();
this.data = data;
} }

BinarySearchTree.java(不含main类,可以自己写main类)

public class BinarySearchTree {
private TreeNode root; //构造二叉搜索树
public TreeNode creatSearchBinaryTree(int data) {
TreeNode node = null;
TreeNode parent = null;
if (root == null) {
node = new TreeNode(data);
root = node;
}
node = root;
while (node != null) {
parent = node;
if (data > node.data) {
node = node.rightChild;
} else if (data < node.data) {
node = node.leftChild;
} else {
return node;
}
}
node = new TreeNode(data);
if (data < parent.data) {
parent.leftChild = node;
} else {
parent.rightChild = node;
}
node.parent = parent;
return node;
} //中序遍历
public void inOrder(TreeNode n) {
if (n != null) {
inOrder(n.getLeftChild());
System.out.print(n.data + " ");
inOrder(n.getRightChild());
}
} // 添加节点
public boolean insertNode(int data) {
TreeNode node = new TreeNode(data);
if (root == null) {
root = node;
return true;
}
TreeNode parent = root;
TreeNode current = root;
while (true) {
parent = current;
if (data == current.data) {
return true;
}
if (data < current.data) {
current = current.leftChild;
if (current == null) {
parent.leftChild = node;
return true;
}
} else {
current = current.rightChild;
if (current == null) {
parent.rightChild = node;
return true;
}
}
} } // 删除节点
public boolean deleteNode(int data) {
TreeNode current = root;
TreeNode parent = root;
boolean isLeftChild = true;
// 找到要删除的点,并记录该节点是否为左节点
while (current.data != data) {
parent = current;
if (data < current.data) {
isLeftChild = true;
current = current.leftChild;
} else {
isLeftChild = false;
current = current.rightChild;
}
if (current == null) {
return false;
}
}
// 如果删除节点为子节点
if (current.leftChild == null && current.rightChild == null) {
if (current == root) {
root = null;
} else {
if (isLeftChild == true) {
parent.leftChild = null;
} else {
parent.rightChild = null;
}
}
// 如果删除节点只有一个子节点
} else if ((current.leftChild != null && current.rightChild == null)
|| (current.leftChild == null && current.rightChild != null)) {
if (current.rightChild == null) {
if (root == current) {
root = current.leftChild;
} else {
if (isLeftChild == true) {
parent.leftChild = current.leftChild;
} else {
parent.rightChild = current.leftChild;
}
}
} else {
if (root == current) {
root = current.rightChild;
} else {
if (isLeftChild == true) {
parent.leftChild = current.rightChild;
} else {
parent.rightChild = current.rightChild;
}
}
}
// 如果删除节点同时有左右节点,找后继节点
} else if (current.leftChild != null && current.rightChild != null) {
TreeNode processer = processer(current);
if (current == root) {
root = processer;
} else {
if (isLeftChild == true) {
parent.leftChild = processer;
} else {
parent.rightChild = processer;
}
}
processer.leftChild = current.leftChild;
}
return true;
} //寻找后继节点
private TreeNode processer(TreeNode delNode) {
TreeNode parent = delNode;
TreeNode success = delNode;
TreeNode current = delNode.rightChild;
while (current != null) {
parent = current;
success = current;
current = current.leftChild;
}
if (success != delNode.rightChild) {
parent.leftChild = success.rightChild;
success.rightChild = delNode.rightChild;
}
return success;
} // 修改节点
public boolean updateNode(int oldData, int newData) {
boolean del = deleteNode(oldData);
insertNode(newData);
if (del == true) {
return true;
} else {
return false;
}
} // 查找节点
public TreeNode findNode(int data) {
TreeNode current = root;
while (current.data != data) {
if (data < current.data) {
current = current.leftChild;
} else {
current = current.rightChild;
}
if (current == null) {
return null;
}
}
return current;
}
}

Java数据结构——二叉搜索树的更多相关文章

  1. Java实现二叉搜索树

    原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11406176.html 尝试一下用Java实现二叉搜索树/二叉查找树,记录自己的学习历程. 1 ...

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

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

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

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

  4. 数据结构-二叉搜索树(BST binary search tree)

    本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 二叉搜索树简介 顾名思义,二叉搜索树是以一棵二叉树来组织的,这样的一棵树可以用一个链表数据结构来 ...

  5. 数据结构-二叉搜索树的js实现

    一.树的相关概念 1.基本概念 子树 一个子树由一个节点和它的后代构成. 节点的度 节点所拥有的子树的个数. 树的度 树中各节点度的最大值 节点的深度 节点的深度等于祖先节点的数量 树的高度 树的高度 ...

  6. 数据结构☞二叉搜索树BST

    二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它可以是一棵空树,也可以是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它 ...

  7. 基本数据结构 —— 二叉搜索树(C++实现)

    目录 什么是二叉搜索树 二叉搜索树如何储存数值 二叉搜索树的操作 插入一个数值 查询是否包含某个数值 删除某个数值 测试代码 参考资料 什么是二叉搜索树 二叉搜索树(英语:Binary Search ...

  8. Java实现二叉搜索树及相关操作

    package com.tree; import com.tree.BitNode; /** * * 二叉搜索树:一个节点的左子节点的关键字小于这个节点.右子节点的关键字大于或等于这个父节点 * * ...

  9. 数据结构---二叉搜索树BST实现

    1. 二叉查找树 二叉查找树(Binary Search Tree),也称为二叉搜索树.有序二叉树(ordered binary tree)或排序二叉树(sorted binary tree),是指一 ...

随机推荐

  1. PHP fmod() 函数

    实例 返回 x/y 的浮点数余数: <?php$x = 7;$y = 2;$result = fmod($x,$y);echo $result;// $result equals 1, beca ...

  2. PHP lcg_value() 函数

    实例 返回范围为 (0, 1) 的一个伪随机数: <?phpecho lcg_value();?>高佣联盟 www.cgewang.com 定义和用法 lcg_value() 函数返回范围 ...

  3. PHP strcspn() 函数

    实例 输出在字符串 "Hello world!" 中找到字符 "w" 之前查找的字符数: <?php高佣联盟 www.cgewang.comecho st ...

  4. Ubuntu安装Cloudera Manager以及CDH5.15.2

    一.机子分配 注意,本安装教程是在真机上进行,而非虚拟机.另,此次搭建主要的目的是搭建测试环境,让Hadoop各组件能够运作起来即可,完成搭建后,将用小数据量进行相关数据的计算与测试.线上环境将会使用 ...

  5. Skill 脚本演示 ycChangeLayerToEntry.il

    https://www.cnblogs.com/yeungchie/ ycChangeLayerToEntry.il 快速切换选中 figs 的 lpp(Layer-Purpose Pair). 回到 ...

  6. EC R 86 D Multiple Testcases 构造 贪心 二分

    LINK:Multiple Testcases 得到很多种做法.其中O(n)的做法值得一提. 容易想到二分答案 check的时候发现不太清楚分配的策略. 需要先考虑如何分配 容易发现大的东西会对小的产 ...

  7. luogu P2467 [SDOI2010]地精部落

    很有意思的dp计数题目. 思考一下发现开始时山峰和开始是山谷的方案数是相同的 所以我们只需要统计一个即可. 证明的话可以考虑对于任意一种开始时山峰的方案 每个数字变成n-a[i]+1 那么可以此方案还 ...

  8. luogu P3223 [HNOI2012]排队

    LINK:排队\ 原谅我没学过组合数学 没有高中数学基础水平... 不过凭着隔板法的应用还是可以推出来的. 首先考虑女生 发现一个排列数m! 两个女生不能相邻 那么理论上来说存在无解的情况 而这道题好 ...

  9. NOI On Line 提高组题解

    (话说其实我想填的是去年CSP的坑...但是貌似有一道题我还不会写咕咕咕... 先写一下这一次的题解吧. T1:序列.题意省略. 两种操作.这种题要先分析部分分 给出了全部都是2操作的子任务. 发现A ...

  10. zabbix监控配置一般流程

    目录 zabbix监控配置流程 1. 配置客户端 2. 配置监控 2.1 创建主机组 2.2 添加主机并加入主机组 2.3 添加监控项 2.3.1 模板的方式(不用添加触发器) 2.3.2 手动添加的 ...