目录
代码如下面实现
删除节点要从查找要删的节点开始, 找到节点后, 需要考虑三种情况:
该节点是叶结点(没有子节点) 没有子节点的根节点
该节点有一个子节点
该节点有两个子节点.
- 我们需要检测current的left以及right是否都为null.
- 都为null之后还要检测一个东西, 就是是否current就是根, 都为null, 并且为跟根, 那么相当于要清空二叉树(当然, 只是清空了根, 因为只有它).
- 否则就把父节点的left或者right字段设置为null即可.
- 要删除的current结点, 只有2个连接(如果有两个子结点, 就是三个连接了), 一个连接父节点, 一个连接唯一的子节点.
- 需要从这三者之间: 爷爷 - 自己 - 儿子, 将自己(current)剪短, 让爷爷直接连接儿子即可.
- 这个过程要求改变父节点的left或者right, 指向要删除节点的子节点.
- 当然, 在这个过程中还要考虑是否current就是根.
如果我们要删除的节点有两个子节点, 甚至子节点还有子节点, 需要从下面的子节点中找到一个节点, 来替换当前的节点.
这个节点==> 应该是current节点下面所有节点中最接近current节点的.
要么比current节点小一点点, 要么比current节点大一点点.
总结最接近current, 你就可以用来替换current的位置.
这个节点怎么找呢?
比current小一点点的节点, 一定是current左子树的最大值.
比current大一点点的节点, 一定是current右子树的最小值.
前驱&后继
而在二叉搜索树中, 这两个特别的节点, 有两个特比的名字.
比current小一点点的节点, 称为current节点的前驱.
比current大一点点的节点, 称为current节点的后继.
也就是为了能够删除有两个子节点的current, 要么找到它的前驱, 要么找到它的后继.
- class Node {
- constructor(data) {
- this.left = null;
- this.data = data;
- this.right = null;
- }
- }
- class BST {
- constructor() {
- this.root = null;
- }
- // 1.插入
- insert(ele) {
- // 创建新节点
- let newnode = new Node(ele);
- // console.log(newnode);
- if (this.root == null) { // 空树
- this.root = newnode
- } else {
- this.insertNode(this.root, newnode)
- }
- }
- 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)
- }
- }
- }
- // 2.前序遍历
- preOrderTraversal() {
- this.preOrderTraversalNode(this.root)
- }
- preOrderTraversalNode(root) {
- if (root != null) { // {left:node,data:11,right:node} != null
- // 1.根
- console.log(root.data); //11 7 15
- // 2.前序遍历左子树
- this.preOrderTraversalNode(root.left)
- // 3.前序遍历右子树
- this.preOrderTraversalNode(root.right)
- }
- }
- // 3.中序遍历
- inOrderTraversal() {
- this.inOrderTraversalNode(this.root)
- }
- inOrderTraversalNode(root) {
- if (root != null) {
- // 1.中序遍历左子树
- this.inOrderTraversalNode(root.left)
- // 2.根
- console.log(root.data);
- // 3.中序遍历右子树
- this.inOrderTraversalNode(root.right)
- }
- }
- // 4.后序遍历
- postOrderTraversal() {
- this.postOrderTraversalNode(this.root)
- }
- postOrderTraversalNode(root) {
- if (root != null) {
- // 1.后序遍历左子树
- this.postOrderTraversalNode(root.left)
-
- // 2.后序遍历右子树
- this.postOrderTraversalNode(root.right)
- // 3.根
- console.log(root.data);
- }
- }
- // 5.最值
- getMin() {
- if (this.root == null) {
- return
- } else {
- let current = this.root;
-
- while (current.left != null) {
- current = current.left
- }
- return current.data
- }
- }
- getMax() {
- if (this.root == null) {
- return
- } else {
- let current = this.root;
-
- while (current.right != null) {
- current = current.right
- }
- return current.data
- }
- }
- remove(ele) {
- if (this.root == null) return
- let current = this.root,
- parent, isLeftChild;
- // 找到删除节点和父节点,判断是左子节点吗?
- while (ele != current.data) {
- if (ele < current.data) {
- //左边找
- parent = current;
- current = current.left;
- isLeftChild = true;
- } else {
- parent = current;
- current = current.right;
- isLeftChild = false;
- }
- }
- let delNode = current;
- // console.log(`当前删除的元素是${current.data},删除元素的父节点是${parent.data},是左孩子吗?${isLeftChild}`);
- // 1.删除叶结点 左右子树都为空
- if (delNode.left == null && delNode.right == null) {
- if (delNode == this.root) {
- this.root = null;
- } else {
- if (isLeftChild) {
- //左孩子
- parent.left = null;
- } else {
- parent.right = null;
- }
- }
- } else if (delNode.left != null && delNode.right == null) { //2.只有一个左子节点的节点
- if (delNode == this.root) {
- this.root = delNode.left
- } else {
- if (isLeftChild) {
- parent.left = delNode.left
- } else {
- parent.right = delNode.left
- }
- }
- } else if (delNode.left == null && delNode.right != null) { //只有一个右子节点的节点
- if (delNode == this.root) {
- this.root = delNode.right
- } else {
- if (isLeftChild) {
- parent.left = delNode.right
- } else {
- parent.right = delNode.right
- }
- }
- } else { //删除有两个子节点
- let houji = this.gethouji(delNode);//后继为右子树中的最小值
- if (delNode == this.root) {
- this.root = houji;
- } else {
- if (isLeftChild) {
- parent.left = houji;
- } else {
- parent.right = houji;
- }
- }
- houji.left = delNode.left;
- }
- }
- gethouji(delNode) {
- let current = delNode.right; //后继
- let houjiparent = delNode;
- while (current.left != null) {
- houjiparent = current;
- current = current.left;
- }
- if (current != delNode.right) { //当后继为删除节点的右节点
- houjiparent.left = current.right;
- current.right = delNode.right
- }
- return current
- }
- seacrh(ele) {
- if (this.root == null) return
- let current = this.root;
- while (current) {
- if (ele < current.data) {
- //左边找
- current = current.left
- } else if (ele > current.data) {
- current = current.right;
- } else {
- return true
- }
- }
- return false
- }
- }
- let bst = new BST();
- bst.insert(11)
- bst.insert(7)
- console.log(bst.seacrh(9));
- console.log(bst.seacrh(100));