数据结构_二叉树删除节点
//二叉树BST class Node { constructor (data) { this.data = data this.left = null this.right = null } } class BST { constructor () { this.root = null } insert (data) { let newNode = new Node(data) if (!this.root) this.root = newNode else this.insertNode(this.root, newNode) return true } //插入节点的辅助函数 insertNode (root, newNode) { if (newNode.data < root.data) { if (root.left == null) root.left = newNode else this.insertNode(root.left, newNode) }else { if (root.right == null) root.right = newNode else this.insertNode(root.right, newNode) } } //先序遍历 preOrderTraverse (callback) { this.perOrderTraverseNode(this.root, callback) } perOrderTraverseNode (node, callback) { if (node) { callback(node.data) this.perOrderTraverseNode(node.left, callback) this.perOrderTraverseNode(node.right, callback) } } //中序遍历 inOrderTraverse () { this.inOrderTraverseNode(this.root) } inOrderTraverseNode (node) { if (node) { this.inOrderTraverseNode(node.left) console.log(node.data) this.inOrderTraverseNode(node.right) } } //后续遍历 postOrderTraverse () { this.postOrderTraverseNode(this.root) } postOrderTraverseNode (node) { if (node) { this.postOrderTraverseNode(node.left) this.postOrderTraverseNode(node.right) console.log(node.data) } } //最小值 getMin () { let current = this.root while (current.left) { current = current.left } return current.data } //最大值 getMax () { let current = this.root while (current.right) { current = current.right } return current.data } //查找特定值 searchValue (data) { let current = this.root while (current) { if (current.data < data) { current = current.right }else if (current.data > data) { current = current.left }else { return current } } return null } //删除节点 remove (data) { let current = this.root let parent = null let isLeftChild = true while (current.data != data) { parent = current if (current.data < data) { current = current.right isLeftChild = false }else{ current = current.left isLeftChild = true } if (current == null) return false } if (current.left == null && current.right == null) { if (current == this.root) this.root = null else if (isLeftChild) parent.left = null else parent.right = null }else if (current.left == null) { // 只有一个叶子节点,只有左子节点 if (current == this.root) this.root = current.right else if (isLeftChild) parent.left = current.right else parent.right = current.right }else if (current.right == null) { // 只有右子节点 if (current == this.root) this.root = current.left else if (isLeftChild) parent.left = current.left else parent.right = current.left }else { // 有两个子节点的节点 // 知道到后继节点 let successorNode = this.getSuccessor(current) if (current == this.root) this.root = successorNode else if (isLeftChild) parent.left = successorNode else parent.right = successorNode successorNode.left = current.left } return true } // 找到被删除节点的后继,被删除节点的右子树 getSuccessor (node) { let successor = null let current = node.right let successorParent = null while (current) { successorParent = successor successor = current current = current.left } if (successor != node.right) { successorParent.left = successor.right successor.right = node.right } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!