数据结构与算法之二叉排序树的增加,删除,遍历

顺序二叉树:子树的左节点小于子树节点,子树的右节点大于子树节点,(和子树相同大小的节点可在左也可在右一般不提倡出现相同大小的节点)

优点:
数组遍历快而插入删除效率不高;
链表插入删除效率高,而遍历效率不高;
而二叉排序树在遍历和插入删除效率都较高;

package com.yg.tree.binarySortTree;/*
@author  Mu_Mu
@date    2020/3/11  9:50
*/

public class BinarySortTreeDemo {
    public static void main(String[] args) {
        //顺序二叉树的创建

        int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
        BinarySortTree binarySortTree = new BinarySortTree();
        for (int i = 0; i < arr.length; i++) {
            binarySortTree.add(new Node(arr[i]));
        }

        //顺序二叉树的删除
        binarySortTree.del(2);
        binarySortTree.del(3);
        binarySortTree.del(7);
        binarySortTree.del(10);
        // binarySortTree.del(1);
        //顺序二叉树的遍历
        binarySortTree.infixOrder();
    }
}

class BinarySortTree {
    private Node root;

    //添加节点的方法
    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    //中序遍历
    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("二叉排序树为空!");
        }
    }

    //查找元素
    public Node search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    //查找要删除节点的父节点
    public Node searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    //查找子树中最小value值并删除对应节点
    /*
     * @param node 子树的根节点
     * @return : int 返回查找的最小value值
     * @date : 2020/3/11 13:42
     */
    public int searchMinValue(Node node) {
        Node targetNode = node;
        while (targetNode.left != null) {
            targetNode = targetNode.left;
        }
        del(targetNode.value);
        return targetNode.value;
    }

    /*
    targetNode:待删除节点   parentNode:待删除节点的父节点
    * 如果删除节点是:
    * 叶子节点: 1.parentNode的左叶子节点:parentNode.left=null;2.parentNode的右叶子节点:parentNode.right=null
    * 有两个叶子节点的子树:
    *1.找到该子树的右子树的最小值 minValue,删除最小值对应的节点
    2.targetNode.value=minValue;
    只有一个叶子节点的子树:
    1.如果该子树是parentNode的右子树:
    (1)targetNode只有右节点:parentNode.right=targetNode.right;
    (2)targetNode只有左节点:parentNode.right=targetNode.left;
    2.如果该子树是parentNode的左子树:
    (1)targetNode只有右节点:parentNode.left=targetNode.right;
    (2)targetNode只有左节点:parentNode.left=targetNode.left;
    * */
    //删除节点
    public void del(int value) {
        if (root == null) {
            return;
        } else {
            Node targetNode = search(value);
            if (targetNode == null) {
                return;
            }
            //该树只有root节点且root节点就是待删除节点
            if (root.left == null && root.right == null) {
                root = null;
                return;
            }
            Node parentNode = searchParent(value);
            //待删除节点是叶子节点
            if (targetNode.left == null && targetNode.right == null) {
                //targetNode是parentNode的左节点
                if (parentNode.left == targetNode) {
                    parentNode.left = null;
                } else {
                    parentNode.right = null;
                }
                //targetNode左右节点都存在
            } else if (targetNode.left != null && targetNode.right != null) {
                int minValue = searchMinValue(targetNode.right);
                targetNode.value = minValue;
            } else {//targetNode只有一个节点
                //targetNode只有左节点时
                if (targetNode.left != null) {
                    if (parentNode != null) {
                        //targetNode是parentNode的左节点
                        if (parentNode.left.value == targetNode.value) {
                            parentNode.left = targetNode.left;
                        } else {
                            parentNode.right = targetNode.left;
                        }
                    } else {
                        root = targetNode.left;
                    }

                } else {//targetNode右节点不为空
                    if (parentNode != null) {
                        if (parentNode.right.value == targetNode.value) {
                            parentNode.right = targetNode.right;
                        } else {
                            parentNode.left = targetNode.right;
                        }
                    } else {
                        root = targetNode.right;
                    }

                }

            }

        }

    }

}

class Node {
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    //查找要删除的节点
    //value:要删除节点的值
    public Node search(int value) {
        if (this.value == value) {
            return this;
        } else if (value < this.value && this.left != null) {//value小于当前节点则向左查找
            return this.left.search(value);
        } else if (value >= this.value && this.right != null) {
            return this.right.search(value);
        } else {
            return null;
        }
    }

    //查找要删除节点的父节点
    public Node searchParent(int value) {
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
            return this;
        } else if (value < this.value && this.left != null) {
            return this.left.searchParent(value);
        } else if (value >= this.value && this.right != null) {
            return this.right.searchParent(value);
        } else {
            return null;
        }
    }

    //添加节点
    public void add(Node node) {
        if (node == null) {
            return;
        }
        //如果node节点的值小于当前节点则与当前节点的左子树进行比较
        if (node.value < this.value) {
            //如果左子树为空则添加为当前节点的左子树
            if (this.left == null) {
                this.left = node;
            } else {//左子树不为空则待添加的节点递归的与当前节点的左子树比较
                this.left.add(node);
            }
        } else {//如果node节点的值小于当前节点则与当前节点的左子树进行比较
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
    }

    //中序遍历
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.print(this + " ");
        if (this.right != null) {
            this.right.infixOrder();
        }

    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值