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

二叉排序树的构建与遍历

二叉排序树是一种特殊的二叉树,它的每个节点都满足:左子树所有节点值小于当前节点,右子树所有节点值大于当前节点。

一、二叉排序树的核心结构

首先定义树节点TreeNode,包含左孩子、右孩子和节点值:

public class TreeNode { public TreeNode lChild; public TreeNode rChild; public Integer data; public TreeNode(Integer data){ this.data = data; } }

二、二叉排序树的构建(插入操作)

构建二叉排序树的过程,本质是依次插入节点并维护 “左小右大” 规则的过程。

BinaryTree类的create方法为例:

public class BinaryTree { TreeNode root; public void create(Integer value) { TreeNode newNode = new TreeNode(value); if (root == null) { root = newNode; return; } TreeNode curNode = root; while (true) { if (curNode.data < newNode.data) { if (curNode.rChild == null) { curNode.rChild = newNode; return; } curNode = curNode.rChild; } else { if (curNode.lChild == null) { curNode.lChild = newNode; return; } curNode = curNode.lChild; } } } }

三、二叉排序树的遍历方式

遍历是按一定规则访问树中所有节点的操作,二叉排序树常用深度优先遍历(先序、中序、后序)和广度优先遍历(层次遍历)。

1. 深度优先遍历

(1)先序遍历(根→左→右)
void beforeOrder(TreeNode root) { if (root == null) return; System.out.println(root.data); beforeOrder(root.lChild); beforeOrder(root.rChild); }
(2)中序遍历(左→根→右)

二叉排序树的中序遍历结果是 “升序序列”

void inOrder(TreeNode root) { if (root == null) return; inOrder(root.lChild); System.out.println(root.data); inOrder(root.rChild);
(3)后序遍历(左→右→根)
void afterOrder(TreeNode root) { if (root == null) return; afterOrder(root.lChild); afterOrder(root.rChild); System.out.println(root.data); }

2. 广度优先遍历(层次遍历)

按 “从上到下、从左到右” 的顺序访问节点,借助队列实现:

void levelOrder(TreeNode root) { LinkedList<TreeNode> queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { root = queue.pop(); System.out.println(root.data); if (root.lChild != null) { queue.add(root.lChild); } if (root.rChild != null) { queue.add(root.rChild); } } }

四、二叉排序树的查找操作

利用 “左小右大” 的特性,查找操作可以快速定位节点:

public TreeNode find(TreeNode root, Integer target) { if (root == null) return null; TreeNode cur = root; while (cur != null) { if (cur.data.equals(target)) { return cur; } else if (cur.data < target) { cur = cur.rChild; } else { cur = cur.lChild; } } return null; }

五、测试验证

package com.qcby; public class Test { public static void main(String[] args) { BinaryTree bt = new BinaryTree(); // 构建二叉排序树 bt.create(5); bt.create(3); bt.create(7); bt.create(0); bt.create(4); bt.create(9); bt.levelOrder(bt.root); Integer target = 8; TreeNode result = bt.find(bt.root, target); if (result != null) { System.out.println("找到了"); } else { System.out.println("没找到"); } } }

结果:

http://www.cnnetsun.cn/news/113430.html

相关文章:

  • AI风险行为识别系统开发:给安全防护装个“智能哨兵”
  • After Effects Roto Brush 3.0:甲方没给绿幕也要“抠人”?AI 帮你 3 秒钟搞定逐帧噩梦
  • 1分钟搞定!用zip命令快速打包你的项目原型
  • 28、Linux 文件和目录管理全解析
  • 雷科电力-REKE610D绝缘油介质损耗电阻率测试仪
  • 对于设计IT系统的相关思路
  • 轻量无负担!2025 年 3 款小巧型文件加密软件分享
  • Canoe-Autosar网络管理自动化测试脚本 Capl源码,全套,修改项目配置可以直接使用...
  • 亚马逊、速卖通采购测评:构建安全环境,保障高效下单指南
  • 软连接vs硬链接:哪种更能提升你的工作效率?
  • 完全合作型博弈:当所有人的利益捆绑在一起 (Fully Cooperative)
  • 挖SRC必须知道的25个漏洞提交平台
  • AI市场舆情分析榜,原圈科技领跑研报神器
  • AI一键生成Python安装包配置脚本
  • 零基础学网安不慌!电脑小白 4 阶段入门路线,分阶段学习不踩坑
  • 传统锁 vs Redisson分布式锁:效率对比实测
  • 封神!从开发转安全渗透工程师,这是我做的最对的职业选择
  • 3、循环与分支:编程中的核心逻辑控制
  • 小白必看:5分钟学会检查你的个人信息是否泄露
  • 效率对比:传统开发vs使用MyBatisPlus代码生成器
  • DeepSeek在线:5分钟打造你的AI应用原型
  • EVS9323-EP伺服变频器
  • AI市场舆情分析榜,原圈科技领跑车企
  • 1900-0711-81触摸屏面板
  • 深圳比亚迪游学|被Zhong国智造狠狠圈粉!新能源黑科技太炸了[特殊字符]✨
  • 小程序项目之捷邻小程序源码(java+ssm+小程序+mysql)
  • 如何用AI技术自动检测个人数据泄漏风险
  • DDoS攻击入门:小白也能懂的防护指南
  • Qwen是“源神”?实际上GLM-4.6才是被低估的黑马
  • 5分钟搭建js for in原型