二叉搜索树

二叉搜索树搜索的最坏时间复杂度为O(n),最好时间复杂度为O(lon n)
查询;
插入;
删除;
中序遍历;

package com.liuheizi.demo;
class BinarySearchTree{
    Node root;
    class Node{
        int value;
        Node left;
        Node right;

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

    }

    public Node searchElement(int key) {//查找
        if (this.root == null) {
            return null;
        }
        Node cur=this.root;
        while (cur != null) {
            if (cur.value == key) {
                return cur;
            }
            if (key > cur.value) {
                cur = cur.right;
            }
            if (key < cur.value) {
                cur = cur.left;
            }
        }
        return null;
    }

    public void insertElement(int key) {//插入元素
        if (this.root == null) {
            this.root = new Node(key);
            return;
        }
        Node parent = null;
        Node cur = this.root;
        while (cur != null) {
            if (cur.value == key) {
                return;
            }
            if (key > cur.value) {
                parent = cur;
                cur = cur.right;
            }else{
                parent = cur;
                cur = cur.left;
            }
        }
        if (key > parent.value) {
            parent.right = new Node(key);
            return;
        }
        if (key < parent.value) {
            parent.left = new Node(key);
            return;
        }
    }

    public void removeElement(int key) {//删除元素
        if (this.root == null) {
            return;
        }
        Node parent = null;
        Node cur = this.root;
        while (cur != null) {
            if (cur.value == key) {
                break;
            }
            if (key > cur.value) {
                parent = cur;
                cur = cur.right;
            }else{
                parent = cur;
                cur = cur.left;
            }
        }
        if (cur == null) {
            return;
        }
        removeInner(cur, parent);
    }
   public void removeInner(Node cur,Node parent) {
       if (cur.left == null) {
           if (cur == this.root) {
               this.root = this.root.right;
               return;
           }
           if (cur == parent.left) {
               parent.left = cur.right;
               return;
           } else {
               parent.right = cur.right;
               return;
           }
       }
       if (cur.right == null) {
           if (cur == this.root) {
               this.root = this.root.left;
               return;
           }
           if (cur == parent.left) {
               parent.left = cur.left;
               return;
           } else {
               parent.right = cur.left;
               return;
           }
       } else {
          Node targetParent=cur;
           Node target = cur.right;




           while (target.left != null) {
               targetParent = target;
               target = target.left;
           }
           cur.value=target.value;
           if (target == targetParent.left) {
               targetParent.left = target.left;
           } else {
               targetParent.right = target.right;
           }
       }
   }

    public void inOrder(Node root) {//中序遍历
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.value+" ");
        inOrder(root.right);
    }
}

public class TestDemo5 {
    public static void main(String[] args) {
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        binarySearchTree.insertElement(1);
        binarySearchTree.insertElement(7);
        binarySearchTree.insertElement(14);
        binarySearchTree.insertElement(3);
        binarySearchTree.insertElement(6);
        binarySearchTree.insertElement(2);
        binarySearchTree.insertElement(8);
        binarySearchTree.insertElement(0);
        binarySearchTree.removeElement(0);
        binarySearchTree.removeElement(8);
        binarySearchTree.inOrder(binarySearchTree.root);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值