美文网首页
二叉排序树

二叉排序树

作者: RalapHao | 来源:发表于2019-06-18 12:14 被阅读0次
  1. 每个节点都保证大于其做节点,小于其右节点
  2. 插入:比较然后递归就可实现
  3. 遍历:中序遍历是有序的
  4. 搜索:同理比较然后递归
  5. 删除:
    1. 删除叶子节点
      直接删除
    2. 只有一个节点
      将当前节点的子节点上移即可
    3. 双节点
      找到删除节点的右子树的最小值,也就是右子树一直找左节点即可,然后将这个值赋值个删除节点,删除这个最小值节点即可;
public class OrderTree {

    public static void main(String[] args) {
        OrderTree ot = new OrderTree();
        ot.add(12);
        ot.add(8);
        ot.add(6);
        ot.add(10);
        ot.add(5);
        ot.add(7);
        ot.add(9);
        ot.add(11);
        ot.infixOrder();
        System.out.println("======================");
        ot.del(8);
        ot.infixOrder();
    }

    public Node root;

    public void add(int value) {
        Node curNode = new Node(value);
        if (root == null) {
            root = curNode;
            return;
        }
        root.add(curNode);
    }

    public void preOrder() {
        root.preOrder();
    }

    public void infixOrder() {
        root.infixOrder();
    }

    public Node serach(int value) {
        return root.serach(value);
    }

    public Node serachParent(int value) {
        if (value == root.value) {
            return null;
        }
        return root.seraceParent(value);
    }

    public void del(int value) {
        if (value == root.value) {
            root = null;
            return;
        }
        root.del(value);
    }
}

class Node {

    public int value;
    public Node left;
    public Node right;

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

    public void add(Node node) {
        if (node == null) {
            return;
        }
        if (node.value < this.value) {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }
        } else {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
    }

    public void preOrder() {
        System.out.println(this.value);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.print(this.value + " ");
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    public Node serach(int value) {
        if (this.value == value) {
            return this;
        }
        if (value < this.value) {
            return this.left.serach(value);
        } else {
            return this.right.serach(value);
        }
    }

    public Node seraceParent(int value) {
        if ((this.left != null && this.left.value == value)
                || (this.right != null && this.right.value == value)) {
            return this;
        }
        if (this.left != null && this.left.value > value) {
            return this.left.seraceParent(value);
        } else if (this.right != null && this.right.value > value) {
            return this.right.seraceParent(value);
        } else {
            return null;
        }
    }

    public void del(int value) {

        Node parent = this.seraceParent(value);
        if (parent == null) {
            return;
        }
        Node delNode;
        if (value < parent.value) {
            delNode = parent.left;
            if (delNode.left == null && delNode.right == null) {
                //删除的是叶子节点
                parent.left = null;
            } else if (delNode.left == null || delNode.right == null) {
                //有一个节点
                parent.left = delNode.left == null ? delNode.right : delNode.right;
            } else {
                //俩个节点(规定所有的右节点上移)
                Node minNodeParent = parent.left;
                Node minNode = parent.left.right;
                while (minNode.left != null) {
                    minNodeParent = minNode;
                    minNode = minNode.left;
                }

                if (parent.left.right.left == null) {
                    minNodeParent.right = null;
                } else {
                    minNodeParent.left = null;
                }
                delNode.value = minNode.value;
            }
        } else {
            delNode = parent.right;
            //删除的是叶子节点
            if (delNode.left == null && delNode.right == null) {
                parent.right = null;
            } else if (delNode.left == null || delNode.right == null) {
                parent.right = delNode.left == null ? delNode.right : delNode.right;
            } else {
                //俩个节点(规定所有的右节点上移)
                Node minNodeParent = parent.right;
                Node minNode = parent.right.right;
                while (minNode.left != null) {
                    minNodeParent = minNode;
                    minNode = minNode.left;
                }
                if (parent.left.right.left == null) {
                    minNodeParent.right = null;
                } else {
                    minNodeParent.left = null;
                }
                delNode.value = minNode.value;
            }
        }
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Node{");
        sb.append("value=").append(value);
        sb.append('}');
        return sb.toString();
    }
}

相关文章

  • 详解Java二叉排序树

    姓名: 李小娜 [嵌牛导读] :这篇文章主要介绍了Java二叉排序树,包括二叉排序树的定义、二叉排序树的性质、二叉...

  • Java数据结构:二叉排序树(BST)

    一、基本介绍 二叉排序树 BST: (Binary Sort(Search) Tree), 对于二叉排序树的任何一...

  • 2018-06-19/20 机试准备09

    数据结构 四、二叉排序树 对二叉排序树进行中序遍历 结果必然是一个递增序列 所以通过建立二叉排序树可以对无序序列进...

  • 二叉搜索树(BST)

    构造一棵二叉排序树的目的,其实并不是为了排序,而是为了提高查找的效率。 那么什么是二叉排序树呢?二叉排序树具有以下...

  • Binary Search Tree

    如果二叉排序树是平衡的,则n个节点的二叉排序树的高度为 ,其查找效率为 ,近似于折半查找。如果二叉排序树完全不平衡...

  • 红黑树

    二叉排序树 非空二叉排序树具有如下特点: 二叉排序树中,如果其根结点有左子树,那么左子树上所有结点的值都小于根结点...

  • 数据结构之二叉排序树

    二叉排序数 1.二叉排序树介绍 二叉排序树:BST: (Binary Sort(Search) Tree), 对于...

  • 数据结构学习第四弹 二叉排序树

    二叉排序树又称为二叉搜索树或二叉查找树,这是一种插入、删除和检索记录效率都很高的树结构 二叉排序树概念 二叉排序树...

  • 数据结构与算法——基础篇(五)

    二叉排序树——BST——Binary Sort(Search) Tree 二叉排序树的出现是为了解决数组的查询快,...

  • 看图说话之平衡二叉排序树

    本文在看图说话之二叉排序树的基础上介绍了平衡二叉排序树,结构性较好的二叉排序树其插入和删除操作的时间复杂度接近Lo...

网友评论

      本文标题:二叉排序树

      本文链接:https://www.haomeiwen.com/subject/qezxqctx.html