二叉查找树(Binary Search Tree)是一种能将链表插入的灵活性和有序数组查找的高效性结合起来的算法。下面是实现BST各种方法的干货纯代码。
二叉查找树(BST)定义
二叉排序树或者是一棵空树,或者是具有下列性质的二叉树:
若左子树不空,则左子树上所有结点的值均小于或等于它的根结点的值
若右子树不空,则右子树上所有结点的值均大于或等于它的根结点的值
左、右子树也分别为二叉排序树
基本结点实现
public class BST<K extends Comparable<K>, V> { private Node root; private class Node { private K key; private V value; private Node left; private Node right; private int N; public Node(K key, V value, int N) { this.key = key; this.value = value; this.N = N; } } public int size() { return size(root); } private int size(Node x) { if (x == null) return 0; else return x.N; } }
查找键获取值——get
public V get(K key) { return get(root, key); } private V get(Node root, K key) { if (root == null) return null; int comp = key.compareTo(root.key); if (comp == 0) return root.value; else if (comp < 0) return get(root.left, key); else return get(root.right, key); }
修改值/插入新值——put
public void put(K key, V value) { root = put(root, key, value); } private Node put(Node root, K key, V value) { if (root == null) return new Node(key, value, 1); int comp = key.compareTo(root.key); if (comp == 0) root.value = value; else if (comp < 0) root.left = put(root.left, key, value); else root.right = put(root.right, key, value); root.N = size(root.left) + size(root.right) + 1; return root; }
最大值/最小值——min/max
public K min() { return min(root).key; } private Node min(Node root) { if (root.left == null) return root; return min(root.left); }
public K max() { return max(root).key; } private Node max(Node root2) { if (root.right == null) return root; return max(root.right); }
向上/向下取整——floor/ceiling
public K floor(K key) { Node x = floor(root, key); if (x == null) return null; return x.key; } private Node floor(Node root, K key) { if (root == null) return null; int comp = key.compareTo(root.key); if (comp < 0) return floor(root.left, key); else if (comp > 0 && root.right != null && key.compareTo(min(root.right).key) >= 0) return floor(root.right, key); else return root; }
public K ceiling(K key) { Node x = ceiling(root, key); if (x == null) return null; return x.key; } private Node ceiling(Node root, K key) { if (root == null) return null; int comp = key.compareTo(root.key); if (comp > 0) return ceiling(root.right, key); else if (comp < 0 && root.left != null && key.compareTo(max(root.left).key) >= 0) return ceiling(root.left, key); else return root; }
选择——select
public K select(int k) { //找出BST中序号为k的键 return select(root, k); } private K select(Node root, int k) { if (root == null) return null; int comp = k - size(root.left); if (comp < 0) return select(root.left, k); else if (comp > 0) return select(root.right, k - (size(root.left) + 1)); else return root.key; }
排名——rank
public int rank(K key) { //找出BST中键为key的序号是多少 return rank(root, key); } private int rank(Node root, K key) { if (root == null) return 0; int comp = key.compareTo(root.key); if (comp == 0) return size(root.left); else if (comp < 0) return rank(root.left, key); else return 1 + size(root.left) + rank(root.right, key); }
删除最小/最大键——deleteMin/deleteMax
public void deleteMin() { root = deleteMin(root); } private Node deleteMin(Node root) { if (root.left == null) return root.right; root.left = deleteMin(root.left); root.N = size(root.left) + size(root.right) + 1; return root; }
public void deleteMax() { root = deleteMax(root); } private Node deleteMax(Node root) { if (root.right == null) return root.left; root.right = deleteMax(root.right); root.N = size(root.left) + size(root.right) + 1; return root; }
删除任意键——delete
public void delete(K key) { root = delete(root, key); } private Node delete(Node root, K key) { if (root == null) return null; int comp = key.compareTo(root.key); if (comp == 0) { if (root.right == null) return root = root.left; if (root.left == null) return root = root.right; Node t = root; root = min(t.right); root.left = t.left; root.right = deleteMin(t.right); } else if (comp < 0) root.left = delete(root.left, key); else root.right = delete(root.right, key); root.N = size(root.left) + size(root.right) + 1; return root; }
中序打印树——print
public void print() { print(root); } private void print(Node root) { if (root == null) return; print(root.left); System.out.println(root.key); print(root.right); }
以上是java-二叉查找树(BST)算法的示例代码分享的详细内容。更多信息请关注PHP中文网其他相关文章!

类加载器通过统一的类文件格式、动态加载、双亲委派模型和平台无关的字节码,确保Java程序在不同平台上的一致性和兼容性,实现平台独立性。

Java编译器生成的代码是平台无关的,但最终执行的代码是平台特定的。1.Java源代码编译成平台无关的字节码。2.JVM将字节码转换为特定平台的机器码,确保跨平台运行但性能可能不同。

多线程在现代编程中重要,因为它能提高程序的响应性和资源利用率,并处理复杂的并发任务。JVM通过线程映射、调度机制和同步锁机制,在不同操作系统上确保多线程的一致性和高效性。

Java的平台独立性是指编写的代码可以在任何安装了JVM的平台上运行,无需修改。1)Java源代码编译成字节码,2)字节码由JVM解释执行,3)JVM提供内存管理和垃圾回收功能,确保程序在不同操作系统上运行。

Javaapplicationscanindeedencounterplatform-specificissuesdespitetheJVM'sabstraction.Reasonsinclude:1)Nativecodeandlibraries,2)Operatingsystemdifferences,3)JVMimplementationvariations,and4)Hardwaredependencies.Tomitigatethese,developersshould:1)Conduc

云计算显着提升了Java的平台独立性。 1)Java代码编译为字节码,由JVM在不同操作系统上执行,确保跨平台运行。 2)使用Docker和Kubernetes部署Java应用,提高可移植性和可扩展性。

Java'splatformindependenceallowsdeveloperstowritecodeonceandrunitonanydeviceorOSwithaJVM.Thisisachievedthroughcompilingtobytecode,whichtheJVMinterpretsorcompilesatruntime.ThisfeaturehassignificantlyboostedJava'sadoptionduetocross-platformdeployment,s

容器化技术如Docker增强而非替代Java的平台独立性。1)确保跨环境的一致性,2)管理依赖性,包括特定JVM版本,3)简化部署过程,使Java应用更具适应性和易管理性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver Mac版
视觉化网页开发工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中