数据结构与算法基础入门——手写实现二叉搜索树增删查

二叉查找树特点:

1、如果它的左子树不为空,则左子树节点的值都小于根节点。
2、如果它的右子树不为空,则左子树节点的值都大于根节点。
3、子树同样也要遵循以上两点
4、中序遍历有序。
5、后继节点:根据中序找第一个比当前节点大的数
6、前继节点:根据中序找第一个比当前节点小的数

二叉树增删查;

1、增:插入的时候每次都是和根节点比较。一直要找到它应该插入的位置,并且只能插在叶子节点上(O(nlogn))
2、删:
      1、叶子节点可以直接删除(O(1))
      2、要删除的结点只有一个子树(O(1))。
      3、删除的结点有两个子树:找后继结点,而且后继结点左子树一定为空(O(logn))。
3、查询(O(logn))
package com.example.demo;

public class MyTwoQueryTree {

    int data;

    private MyTwoQueryTree left;
    private MyTwoQueryTree right;

    public MyTwoQueryTree(int data){
        this.data = data;
        this.left = null;
        this.right = null;
    }

    /**
     * 插入
     */
    public void insert(MyTwoQueryTree root,int data){
        //大于根放右
        if(root.data < data){
            if(root.right == null){
                root.right = new MyTwoQueryTree(data);
            }else {
                insert(root.right,data);
            }
        }else{ //其它放左
            if(root.left == null){
                root.left = new MyTwoQueryTree(data);
            }else {
                insert(root.left,data);
            }
        }
    }

    /**
     * 查询
     * @param root
     * @param data
     */
    public void query(MyTwoQueryTree root,int data){
        if(root != null){
            if(root.data < data){
                query(root.right,data);
            }else if(root.data > data){
                query(root.left,data);
            }else {
                System.out.println("值为:"+root.data);
                return;
            }
        }else {
            System.out.println("值为:"+data+"不存在");
        }
    }

    /**
     * 根节点删除
     * @param root
     * @param no
     * @return
     */
    public boolean deleteRoot(MyTwoQueryTree root,int no) {
        // 要删除的节点是根节点
        if (root.data == no) {
            
            // 要删除的根节点,没有左子树和右子树
            if (root.left == null && root.right == null) {
                root = null;
                // 要删除的根节点,都有左右子树
            } else if (root.left != null && root.right != null) {
                MyTwoQueryTree left = root.left;
                root = root.right;// 实现删除原来的 root
                combinationBinaryTreeTry(root, left);
                // 要删除的根节点,只有左子树
            } else if (root.left != null) {
                root = root.left;

                // 要删除的根节点,只有右子树
            } else {
                root = root.right;
            }
            return true;
        }
        return false;

    }
    
    public void combinationBinaryTreeTry(MyTwoQueryTree parent, MyTwoQueryTree left) {


        // 如果parent的左子节点为空,那么left的no 一定小于等于 parent的no
        if (parent.left == null) {
            parent.left = left;


            // 如果这个条件满足,证明parent的左子节点不为空,那么left的no 一定小于等于 parent的no
            // 所以left不能作为parent右子节点
        } else if (parent.right == null) {
            combinationBinaryTree(parent.left, left);

            // parent的左右子节点不为空
        } else {
            combinationBinaryTree(parent.left, left);
        }
    }


    /**
     *
     * @param parent
     * @param child
     * @return
     */
    public boolean combinationBinaryTree(MyTwoQueryTree parent, MyTwoQueryTree child) {
        int childNo = child.data;
        int parentNo = parent.data;


        // 如果 “将要作为父节点” 的序号大于等于 “将要作为子节点”的序号
        // 并且 “将要作为父节点” 的左子节点是空的
        if (childNo <= parentNo && parent.left == null) {
            parent.left = child;
            return true;


            // 如果 “将要作为父节点” 的序号小于于 “将要作为子节点”的序号
            // 并且 “将要作为父节点” 的右子节点是空的
        } else if (childNo > parentNo && parent.right == null) {
            parent.right = child;
            return true;
        }

        boolean result = false;

        if (parent.left != null) {


            // 递归调用 combinationBinaryTree 方法,有可能要将child挂载在parent的左子节点
            //如果已经挂载成功,就不必在遍历 右子节点了,也就是不去执行注释1的代码了
            result = combinationBinaryTree(parent.left, child);
            if (result) {
                return true;
            }
        }

        //1、
        if (parent.right != null) {


            // 递归调用 combinationBinaryTree 方法,有可能要将child挂载在parent的右子节点
            result = combinationBinaryTree(parent.right, child);
        }

        return result;
    }


    /**
     * 非根节点
     * @param node 二叉树中的当前节点
     * @param no 要删除的节点的序号
     * @return 返回 true 表示删除成功
     */
    public boolean deleteNode(MyTwoQueryTree node, int no) {


        // 如果删除的是当前节点的左子节点(非叶子节点来的),当前节点的左子节点又有左右子节点
        if (node.left != null && node.left.data == no
                && node.left.left != null
                && node.left.right != null) {
            MyTwoQueryTree parent = node.left.right;
            MyTwoQueryTree child = node.left.left;
            node.left = parent;//重置当前节点的左子节点
            combinationBinaryTree(parent, child);
            return true;


            // 如果删除的是当前节点的左子节点(非叶子节点来的),当前节点的左子节点只有左子节点
        } else if (node.left != null && node.left.data == no
                && node.left.left != null
                && node.left.right == null) {
            MyTwoQueryTree child = node.left.left;
            node.left = null;
            combinationBinaryTree(node, child);
            return true;


            // 如果删除的是当前节点的左子节点(非叶子节点来的),当前节点的左子节点只有右子节点
        } else if (node.left != null && node.left.data == no
                && node.left.left == null
                && node.left.right != null) {
            MyTwoQueryTree child = node.left.right;
            node.left = null;//重置当前节点的左子节点
            combinationBinaryTree(node, child);
            return true;


            // 如果删除的是当前节点的右子节点(非叶子节点来的),当前节点的右子节点又有左右子节点
        } else if (node.right != null && node.right.data == no
                && node.right.left != null
                && node.right.right != null) {
            MyTwoQueryTree parent = node.right.right;
            MyTwoQueryTree child = node.right.left;
            node.right = parent;//重置当前节点的右子节点
            combinationBinaryTree(parent, child);
            return true;


            // 如果删除的是当前节点的右子节点(非叶子节点来的),当前节点的右子节点只有左子节点
        } else if (node.right != null && node.right.data == no
                && node.right.left != null
                && node.right.right == null) {
            MyTwoQueryTree child = node.right.left;
            node.right = null;//重置当前节点的右子节点
            combinationBinaryTree(node, child);
            return true;


            // 如果删除的是当前节点的右子节点(非叶子节点来的),当前节点的右子节点只有右子节点
        } else if (node.right != null && node.right.data == no
                && node.right.left == null
                && node.right.right != null) {
            MyTwoQueryTree right = node.right.right;
            node.right = right;
            return true;


            // 删除的是当前节点的左子节点(叶子节点来的)
        } else if (node.left != null && node.left.data == no) {
            node.left = null;
            return true;


            // 删除的是当前节点的右子节点(叶子节点来的)
        } else if (node.right != null && node.right.data == no) {
            node.right =null;
            return true;
        }
        boolean deleteResult = false;

        //向左子节点递归
        if (node.left != null) {
            deleteResult = deleteNode(node.left, no);
            if (deleteResult) {
                return true;
            }
        }

        //向右子节点递归
        if (node.right != null) {
            deleteResult = deleteNode(node.right, no);
        }
        return deleteResult;
    }



    public static void main(String[] args) {
        MyTwoQueryTree myTwoQueryTree = new MyTwoQueryTree(10);
        myTwoQueryTree.insert(myTwoQueryTree,5);
        myTwoQueryTree.insert(myTwoQueryTree,11);
        myTwoQueryTree.query(myTwoQueryTree,12);

        myTwoQueryTree.deleteNode(myTwoQueryTree,11);
        myTwoQueryTree.query(myTwoQueryTree,11);

    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉搜索树,也叫二叉找树(BST,Binary Search Tree),是一种特殊的二叉树。相对于普通的二叉树,它有一个特点:所有左子树的节点都比根节点小,所有右子树的节点都比根节点大。因此,它可以快速地进行找、插入、删除等操作。 具体来说,二叉搜索树的定义如下: - 节点的左子树中所有节点的值都小于该节点的值。 - 节点的右子树中所有节点的值都大于该节点的值。 - 左右子树也都是二叉搜索树。 如下图所示,就是一个二叉搜索树的例子: ``` 8 / \ 3 10 / \ \ 1 6 14 / \ / 4 7 13 ``` 在这个树中,每个节点都满足左子树的节点值小于该节点的值,右子树的节点值大于该节点的值。比如,节点 3 的左子树是 1 和 6,右子树是 4 和 7,都满足要求。 二叉搜索树的主要操作包括找、插入和删除。 找操作可以通过递归或者循环实现。递归实现如下: ``` def search(root, val): if not root or root.val == val: return root if root.val > val: return search(root.left, val) else: return search(root.right, val) ``` 插入操作需要先找到插入的位置,然后创建一个新节点插入到该位置: ``` def insert(root, val): if not root: return TreeNode(val) if root.val > val: root.left = insert(root.left, val) else: root.right = insert(root.right, val) return root ``` 删除操作比较复杂,需要考虑多种情况。如果要删除的节点只有一个子节点,直接将其子节点替换上来即可。如果要删除的节点有两个子节点,可以找到其右子树中的最小节点(或者左子树中的最大节点)来替换该节点,然后再删除该最小节点(或者最大节点)。 ``` def delete(root, val): if not root: return None if root.val == val: if not root.left: return root.right elif not root.right: return root.left else: # 找到右子树中的最小节点 p = root.right while p.left: p = p.left root.val = p.val root.right = delete(root.right, p.val) elif root.val > val: root.left = delete(root.left, val) else: root.right = delete(root.right, val) return root ``` 需要注意的是,在删除节点时,要保证删除后的树仍然是二叉搜索树

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值