一、概述

二叉排序树(Binary Sort Tree)又称二叉查找树(Binary Search Tree),亦称二叉搜索树。

1.1、定义

二叉排序树或者是一棵空树,或者是具有下列性质的二叉树:
(1)若左子树不空,则左子树上所有结点的值均小于或等于它的根结点的值;
(2)若右子树不空,则右子树上所有结点的值均大于或等于它的根结点的值;
(3)左、右子树也分别为二叉排序树;

二、二叉查找树的实现

这里的二叉查找树的定义是在上篇博客的二叉树的定义上扩展的,因此这些操作是二叉树已定义的那些操作的补充。

2.1、抽象实现

BinarySearchTreeADT.java:

package sihai;/*** 二叉查找树接口*/
public interface BinarySearchTreeADT<T> extends BinaryTreeADT<T>
{/** * 在树中的适当位置添加一个元素*/public void addElement(T element);/** * 根据特定的元素删除元素*/ public T removeElement(T targetElement);/** * 删除和目标元素有关的所有元素*/public void removeAllOccurrences(T targetElement);/** * 删除树中的最小元素*/public T removeMin();/** * 删除树中的最大元素*/ public T removeMax();/** * 查找树中的最小元素*/ public T findMin();/** *查找树中的最大元素*/public T findMax();
}

三、用链表实现二叉查找树

这里BinaryTreeNode类来表示树中的每个节点。每个BinaryTreeNode对象要维护一个指向节点所存储元素的引用,另外还要维护指向节点的每个孩子的引用。
LinkedBinaryTree提供两个构造函数:一个负责创建空的;另外一个负责创建一颗根节点为特定的元素的链接二叉查找树。

LinkedBinaryTree.java:

package jsjf;import jsjf.exceptions.*;
import jsjf.*;/*** 链接二叉查找树实现了链接二叉树接口和二叉查找树接口*/
public class LinkedBinarySearchTree<T> extends LinkedBinaryTree<T>implements BinarySearchTreeADT<T>
{public LinkedBinarySearchTree() {super();}public LinkedBinarySearchTree(T element) {super(element);if (!(element instanceof Comparable))throw new NonComparableElementException("LinkedBinarySearchTree");}/*** 添加元素*/public void addElement(T element) {if (!(element instanceof Comparable))throw new NonComparableElementException("LinkedBinarySearchTree");Comparable<T> comparableElement = (Comparable<T>)element;if (isEmpty())root = new BinaryTreeNode<T>(element);else {if (comparableElement.compareTo(root.getElement()) < 0){if (root.getLeft() == null) this.getRootNode().setLeft(new BinaryTreeNode<T>(element));elseaddElement(element, root.getLeft());}else{if (root.getRight() == null) this.getRootNode().setRight(new BinaryTreeNode<T>(element));elseaddElement(element, root.getRight());}}modCount++;}/*** 在特定的节点插入元素*/private void addElement(T element, BinaryTreeNode<T> node) {Comparable<T> comparableElement = (Comparable<T>)element;if (comparableElement.compareTo(node.getElement()) < 0){if (node.getLeft() == null) node.setLeft(new BinaryTreeNode<T>(element));elseaddElement(element, node.getLeft());}else{if (node.getRight() == null) node.setRight(new BinaryTreeNode<T>(element));elseaddElement(element, node.getRight());}}/*** 删除目标元素*/public T removeElement(T targetElement)throws ElementNotFoundException {T result = null;if (isEmpty())throw new ElementNotFoundException("LinkedBinarySearchTree");else{BinaryTreeNode<T> parent = null;if (((Comparable<T>)targetElement).equals(root.element)) {result =  root.element;BinaryTreeNode<T> temp = replacement(root);if (temp == null)root = null;else {root.element = temp.element;root.setRight(temp.right);root.setLeft(temp.left);}modCount--;}else {                parent = root;if (((Comparable)targetElement).compareTo(root.element) < 0)result = removeElement(targetElement, root.getLeft(), parent);elseresult = removeElement(targetElement, root.getRight(), parent);}}return result;}/*** 删除元素*/private T removeElement(T targetElement, BinaryTreeNode<T> node, BinaryTreeNode<T> parent)throws ElementNotFoundException {T result = null;if (node == null)throw new ElementNotFoundException("LinkedBinarySearchTree");else{if (((Comparable<T>)targetElement).equals(node.element)) {result =  node.element;BinaryTreeNode<T> temp = replacement(node);if (parent.right == node)parent.right = temp;else parent.left = temp;modCount--;}else {                parent = node;if (((Comparable)targetElement).compareTo(node.element) < 0)result = removeElement(targetElement, node.getLeft(), parent);elseresult = removeElement(targetElement, node.getRight(), parent);}}return result;}/*** 返回一个引用代替将要删除的元素*/private BinaryTreeNode<T> replacement(BinaryTreeNode<T> node) {BinaryTreeNode<T> result = null;if ((node.left == null) && (node.right == null))result = null;else if ((node.left != null) && (node.right == null))result = node.left;else if ((node.left == null) && (node.right != null))result = node.right;else{BinaryTreeNode<T> current = node.right;BinaryTreeNode<T> parent = node;while (current.left != null){parent = current;current = current.left;}current.left = node.left;if (node.right != current){parent.left = current.right;current.right = node.right;}result = current;}return result;}/*** 删除目标元素的所有引用*/public void removeAllOccurrences(T targetElement)throws ElementNotFoundException {removeElement(targetElement);try{while (contains((T)targetElement))removeElement(targetElement);}catch (Exception ElementNotFoundException){}}/***删除最小元素*/public T removeMin() throws EmptyCollectionException {T result = null;if (isEmpty())throw new EmptyCollectionException("LinkedBinarySearchTree");else {if (root.left == null) {result = root.element;root = root.right;}else {BinaryTreeNode<T> parent = root;BinaryTreeNode<T> current = root.left;while (current.left != null) {parent = current;current = current.left;}result =  current.element;parent.left = current.right;}modCount--;}return result;}/*** 删除最大元素*/public T removeMax() throws EmptyCollectionException {// TODO}/*** 查询二叉查询树中的最小值*/public T findMin() throws EmptyCollectionException {// TODO}/*** 查询二叉查询树中的最大值*/public T findMax() throws EmptyCollectionException {// TODO}/*** 查询目标元素*/public T find(T targetElement) throws ElementNotFoundException {// TODO}/*** 查询左节点*/public LinkedBinarySearchTree<T> getLeft(){// TODO}/*** 查询右节点*/public LinkedBinarySearchTree<T> getRight(){// TODO}/*** 查找目标节点*/private BinaryTreeNode<T> findNode(T targetElement, BinaryTreeNode<T> next) {// TODO}
}

***注意:***其中还有一些没有完善,日后再慢慢学习完善。

数据结构与算法-二叉查找树(java描述)相关推荐

  1. java递归单链表查找中间元素_《数据结构与算法——C语言描述》答案 3.11 查找单链表中的特定元素(递归)...

    转载请注明出处:http://blog.csdn.net/xdz78 #include #include //查找单链表中的特定元素,<数据结构与算法--c语言描述> 3.11 答案 in ...

  2. python数据结构题目_《数据结构与算法Python语言描述》习题第二章第三题(python版)...

    ADT Rational: #定义有理数的抽象数据类型 Rational(self, int num, int den) #构造有理数num/den +(self, Rational r2) #求出本 ...

  3. 怎样将树的中序遍历的数输入到一个数组中_数据结构与算法-二叉查找树平衡(DSW)...

    上一节探讨了二叉查找树的基本操作,二叉查找树的查找效率在理想状态下是O(lgn),使用该树进行查找总是比链表快得多.但是,该论点并不总是正确,因为查找效率和二叉树的形状息息相关.就像这样: 图1-1给 ...

  4. 数据结构与算法【Java】06---七大查找算法总结

    文章目录 数据结构与算法[Java]06---查找算法总结 1.查找算法简介 1.1.查找的定义 1.2.查找算法分类 1.3.常用查找算法 2.线性查找算法 2.1.线性查找简介 2.2.线性查找代 ...

  5. 数据结构与算法--二叉查找树转顺序排列双向链表

    二叉查找树转顺序排列双向链表 题目:输入一颗二叉查找树,将二叉查找树转成一个排序的双向链表,要求不能创建任何新节点,只调整树节点中指针的指向.例如下图所示: 本次二叉查找树节点定义使用之前文章 数据结 ...

  6. 数据结构与算法python描述_数据结构与算法——Python语言描述.pdf

    数据结构与算法--Python语言描述.pdf 欢迎加入非盈利Python编学习交流程QQ群783462347,群里免费提供500+本Python书籍! 欢迎加入非盈利Python编程学习交流程QQ群 ...

  7. java环形链表_数据结构和算法(四)Java实现环形链表

    1. 数据结构和算法(四)Java实现环形链表 1.1 约瑟夫问题 约瑟夫问题:公元66年,约瑟夫不情愿地参与领导了犹太同胞反抗罗马统治的起义,后来起义失败,他和一些宁死不降的起义者被困于一个山洞之中 ...

  8. 《数据结构与算法 Python语言描述》 读书笔记

    已经发布博客 <数据结构与算法 Python语言描述> 读书笔记 第二章 抽象数据类型和Python类 2.1 抽象数据类型abstract data type:ADT 2.1.1 使用编 ...

  9. 数据结构与算法【Java】05---排序算法总结

    前言 数据 data 结构(structure)是一门 研究组织数据方式的学科,有了编程语言也就有了数据结构.学好数据结构才可以编写出更加漂亮,更加有效率的代码. 要学习好数据结构就要多多考虑如何将生 ...

  10. 数据结构python课后答案_数据结构与算法:Python语言描述 1~5章课后习题

    数据结构与算法:Python语言描述 1~5章课后习题 发布时间:2018-07-19 20:42, 浏览次数:1885 , 标签: Python MarkDown语法写的,不知道为啥上传到CSDN不 ...

最新文章

  1. 树莓派迅雷远程下载 | 树莓派小无相系列
  2. Log4j显示异常的奇怪问题
  3. 自由自在珍珠奶茶以市场细分来创新品牌
  4. linux lib64被改名,问题解决:Centos误将/lib64更改为lib64.bak
  5. 2020年联通软件研究院校招笔试第三题
  6. 操作系统(二)操作系统的四个特征
  7. gsdfgsdfgsdg
  8. Java黑皮书课后题第10章:*10.10(Queue类)10.6节给出一个Stock类。设计一个名为Queue的类用于存储整数。像栈一样,队列保存元素。在栈中,元素后进先出。队列中元素先进先出
  9. ASP.NET 图片剪辑控件
  10. 数据可视化系列(六):场景案例显神通
  11. 2016下半年网络规划设计师考试下午真题
  12. 高等工程数学(张韵华,汪琥庭,宋立功)—— 第一篇:线性代数
  13. Django报错:'Specifying a namespace in include() without providing an app_name '
  14. 使用GCD(转自唐巧的技术博客)
  15. 一份写给极客的智能家居指南
  16. (亲自整理)如何让你的C盘立马多出来20G空间
  17. 企业数据可视化实现2020用户留存分析
  18. 五大抉择影响女人一生幸福
  19. 安永亚太技术实验室在深圳成立;松下能源将为Lucid Air豪华电动汽车供应锂离子电池 | 美通企业日报...
  20. Go语言头秃之路(五)

热门文章

  1. STL源代码分析(ch 1)组态2
  2. C++ Primer 5th笔记(chap 16 模板和泛型编程)类模板成员函数的实例化
  3. 区块链预言机(5)预言机原理
  4. (chap4 Http状态码) 5XX
  5. java结丹期(15)----javaweb(maven(1))
  6. buu Windows系统密码
  7. python自动化测试locksetting/gatekeeper/keymaster/vts等
  8. [UTCTF2020]Cube Crypto
  9. (44)MessageBoxA 监视器(过写拷贝,不使用 shellcode 注入)
  10. fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include stdafx.h”?