当前位置: 首页 > news >正文

数据结构:二叉查找树

文章目录

  • 二叉查找树
    • 一,概述
    • 二,添加数据
    • 三,删除数据


二叉查找树

一,概述

二叉查找树,也称为二叉搜索树,是一种特殊的二叉树,它或者是一颗空树,或者具有以下性质:对于每个非空节点,其左子树中所有节点的值都小于该节点的值,而右子树中所有节点的值都大于该节点的值。这种特性使得在二叉查找树中查找一个特定的值变得相对简单和高效。

二叉查找树的查找操作如下:

  1. 从根节点开始查找。
  2. 如果当前节点为空,说明查找失败,返回NULL。
  3. 如果当前节点的值等于要查找的值,说明查找成功,返回当前节点。
  4. 如果要查找的值小于当前节点的值,则在左子树中继续查找。
  5. 如果要查找的值大于当前节点的值,则在右子树中继续查找。

在二叉查找树中,每个节点的左子树和右子树的高度最多相差1,因此,二叉查找树的查找时间复杂度是O(log n),其中n是树中节点的数量。在最坏的情况下,当树完全不平衡时,可能退化成O(n)的时间复杂度。为了保持二叉查找树的效率,通常会使用一些平衡的策略,如AVL树和红黑树。

总的来说,二叉查找树是一种常见的数据结构,它具有很好的查找性能,但是需要注意平衡的问题,以避免效率的降低。

简介

  • 二叉查找树是一种自我平衡的二叉树,它的中序遍历会得到一个升序的序列。
  • 二叉查找树的每个节点都包含一个键和一个值,其中键用于保持树的排序,而值则可以是任何类型的数据。
  • 二叉查找树的主要操作包括插入、查找和删除。

图示

以下是一个简单的二叉查找树的图示:

      50/ \30  70/ \  / \10 40 60 80

在这个二叉查找树中,每个节点的键都大于其左子树中所有节点的键,且小于其右子树中所有节点的键。

示例

以下是一个在Java中实现二叉查找树的简单示例:

public class BinarySearchTree {class Node {int key;Node left, right;public Node(int item) {key = item;left = right = null;}}Node root;BinarySearchTree(int key) {root = new Node(key);}BinarySearchTree() {root = null;}void insert(int key) {root = insertRec(root, key);}Node insertRec(Node root, int key) {if (root == null) {root = new Node(key);return root;}if (key < root.key) {root.left = insertRec(root.left, key);} else if (key > root.key) {root.right = insertRec(root.right, key);}return root;}void inorder()  {inorderRec(root);}void inorderRec(Node root) {if (root != null) {inorderRec(root.left);System.out.println(root.key);inorderRec(root.right);}}
}

在这个示例中,定义了一个内部类Node来表示二叉查找树的节点。每个节点都有一个键key和两个子节点leftright。还定义了两个方法insertinorderinsert方法用于向二叉查找树中插入一个新的节点,而inorder方法则用于按中序遍历顺序打印出树中的所有节点的键。

二,添加数据

在Java中,二叉查找树(Binary Search Tree)是一种常见的数据结构。在二叉查找树中,每个节点包含一个关键字和两个指向其子树的链接。树也必须满足二叉查找树性质:左子树上的所有关键字都小于其根节点的关键字,右子树上的所有关键字都大于其根节点的关键字。

下面是一个简单的Java类,表示一个二叉查找树节点:

public class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) {val = x;}
}

然后可以创建一个二叉查找树的类,并实现添加数据的方法:

public class BinarySearchTree {private TreeNode root;public BinarySearchTree() {root = null;}public void add(int value) {root = addRecursive(root, value);}private TreeNode addRecursive(TreeNode current, int value) {if (current == null) {return new TreeNode(value);}if (value < current.val) {current.left = addRecursive(current.left, value);} else if (value > current.val) {current.right = addRecursive(current.right, value);} else {// value already exists in the tree, do nothingreturn current;}return current;}
}

在这个类中,使用了递归方法addRecursive来找到应该插入新节点的位置。如果树为空,就在根节点插入新值。如果新值小于当前节点的值,将其插入到左子树;如果新值大于当前节点的值,将其插入到右子树。如果新值已经存在于树中,什么也不做。

三,删除数据

在Java中,二叉查找树(Binary Search Tree)是一种常见的数据结构。在二叉查找树中,每个节点包含一个关键字和两个指向其子树的链接。树也必须满足二叉查找树性质:左子树上的所有关键字都小于其根节点的关键字,右子树上的所有关键字都大于其根节点的关键字。

下面是一个简单的Java类,表示一个二叉查找树节点:

public class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) {val = x;}
}

然后可以创建一个二叉查找树的类,并实现删除数据的方法:

public class BinarySearchTree {private TreeNode root;public BinarySearchTree() {root = null;}public void remove(int value) {root = removeRecursive(root, value);}private TreeNode removeRecursive(TreeNode current, int value) {if (current == null) {return null;}if (value == current.val) {// Node with the given value found, remove it from the tree.if (current.left == null && current.right == null) {return null;} else if (current.left == null) {return current.right;} else if (current.right == null) {return current.left;} else {// Find the minimum value in the right subtree and replace it with the current node's value.TreeNode minNode = findMin(current.right);current.val = minNode.val;current.right = removeRecursive(current.right, minNode.val);return current;}} else if (value < current.val) {current.left = removeRecursive(current.left, value);return current;} else {current.right = removeRecursive(current.right, value);return current;}}private TreeNode findMin(TreeNode node) {while (node.left != null) {node = node.left;}return node;}
}

在这个类中,使用了递归方法removeRecursive来找到应该删除节点的位置。如果要删除的节点没有子节点,直接返回null。如果要删除的节点只有一个子节点,将这个子节点返回作为新的节点。如果要删除的节点有两个子节点,找到右子树中的最小节点,用它替换要删除的节点,然后在右子树中递归删除这个最小节点。

http://www.lryc.cn/news/168377.html

相关文章:

  • Redis的介绍,安装Redis的方式
  • 深入理解CI/CD流程:改变你的开发生命周期
  • 【React】React入门
  • 面相面试知识--Lottery项目
  • 《Python趣味工具》——自制emoji2(2)
  • 【面试刷题】——C++四种类型转化
  • 集成Activiti-Modeler流程设计器
  • 【深度学习】 Python 和 NumPy 系列教程(十一):NumPy详解:3、数组数学(元素、数组、矩阵级别的各种运算)
  • python难题切片处理
  • 《研发效能(DevOps)工程师(中级)认证》证书查询方式和路径丨IDCF
  • NVR添加rtsp流模拟GB28181视频通道
  • 浅谈C++|文件篇
  • C++ QT qml 学习之 做个登录界面
  • LLM 06-大模型架构
  • openGauss学习笔记-71 openGauss 数据库管理-创建和管理普通表-删除表中数据
  • 【k8s】kube-proxy 工作模式
  • Linux:Centos9 《下载-安装》
  • 数字化管理平台建设实践
  • Linux命令(80)之sort
  • [k8s] kubectl port-forward 和kubectl expose的区别
  • vscode如何设置文件折叠
  • Linux centos7 bash编程训练
  • k8s集群换ip
  • 选择HAL库还是标准库
  • 计算机竞赛 机器视觉的试卷批改系统 - opencv python 视觉识别
  • Mapbox gl HTML经纬度点渲染,动态轨迹播放,自定义图形以及轨迹上显示箭头方向
  • kubernetes部署(kubeadmin)
  • Leetcode168. Excel表列名称
  • 碎片笔记 | 大模型攻防简报
  • 【100天精通Python】Day63:Python可视化_Matplotlib绘制子图,子图网格布局属性设置等示例+代码