算法 3.2节 二叉查找树

import java.util.Iterator;
import java.util.Stack;


public class BST {

    private Node root;

    private class Node {
        private int key;
        private int value;
        private Node left;
        private Node right;
        private int count;

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

    }

    public BST() {
        root.count = 1;
        root.left = null;
        root.right = null;
    }

    public int size() {
        return size(root);
    }

    private int size(Node x) {
      return  x == null   ? 0 : x.count;
    }

    public int get(int key) {
        return get(root, key);
    }

    private int get(Node x, int key) {
        if (x == null) return -1;
       else if (x.key == key) return x.value;
        else if (x.key > key) return get(x.left, key);
        else if (x.key < key) return get(x.right,key);
    }

    public void insert(int key, int value) {
        root = insert(key, value, root);
    }

    private Node insert(int key, int value, Node x) {
        if (x == null)
            return new Node(key, value, 1);
        else if (x.key > key)
            x.left = insert(key, value, x.left);
        else if (x.key < key)
            x.right = insert(key,value,x.right);
        else x.value = value;

        x.count =size(x.left) + size(x.right) +1;
        return x;
    }

    private void insert_without_recursion(int key, int value) {
        Node temp = new Node(key,value,1);
        if (root == null) root = temp;
        Node parent =null,t= root;
        while (t != null) {
            parent = t;
            if (t.key < key) t= t.right;
            if (t.key > key) t=t.left;
            if (t.key == key) {
                t.value = value; // t的类型不是基础类型,所以是传递引用而不是值,相当于指针
                return;
            }
        }
        boolean cmp = parent.key > key;
        if (cmp)
            parent.left = temp;
        else
            parent.right = temp;


    }

    public int min() {
        return min(root).value;
    }

    private Node min(Node x) {
        if(x.left == null) return x;
        else return min(x.left);
    }

    public Node floor(int key) {
        return floor(key,root);
    }

    private Node floor(int key, Node x) {
        if (x == null) return null;
        if (x.key == key) return x;
        else if (x.key > key) {
            return floor(key, x.left);
        } else {
            Node t = floor(key, x.right);
            if (t != null) return t;
            else return x;
        }

    }

    public Node select_by_rank(int k) {
        return select_by_rank(k,root);
    }

    private Node select_by_rank(int k,Node x) { //有k个小于这个键的键
        if (x == null) return null;
        int cmp = size(x.left);
        if (cmp == k) return x;
        else if (cmp > k ) return select_by_rank(k,x.left);
        else  return select_by_rank(k - cmp -1,x.right);
    }

    public void delectMin() {
        root= delectMin(root);
    }

    private Node delectMin(Node x) {
        if (x.left == null) return x.right;
        else {
            x.left  = delectMin(x.left);
            x.count = size(x.left) +size(x.right) +1;
            return x;
        }
    }

    public void delect(int key) {
        root = delect(key, root);
    }

    private Node delect(int key, Node x) {
        if (x.key > key) x.left = delect(key, x.left);
        else if (x.key < key) {
            x.right = delect(key, x.right);
        } else {
            if (x.left == null) return x.right;
            if (x.right == null) return x.left;
            Node t = x;
            x= min(t.right);
            x.right = delectMin(t.right);
            x.left = t.left;
        }
        x.count = size(x.left) + size(x.right) +1;
        return x;

    }

    public Iterable<Integer> keys(int low, int high) {
        Stack<Integer> stack = new Stack<>() ;
        keys(low, high, stack, root);
        return stack;
    }

    public Iterable<Integer> traverse_without_recursion() {
        Stack<Node> stack = new Stack<Node>();
        Stack<Integer> queue = new Stack<Integer>(); 
        Node x = root;
        while (x != null || !stack.isEmpty()) {
            if (x != null) {
                stack.push(x);
                x = x.left;
            }
            else {
                x = stack.pop();
                queue.push(x.key);
                x = x.right;
            }
        }
        return queue;
    }
    
    private void keys(int low, int high, Stack stack, Node x) {
        if (x == null) return;
        if (x.key > low) keys(low, high, stack, x.left);
        if (x.key >= low && x.key <= high) stack.push(x.key);
        if (x.key < high) keys(low, high, stack, x.right);
    }

    public int height() {
        return height(root);
    }

    private int height(Node x) {
       if (x == null) return -1;
       int a = height(x.left);
       int b = height(x.right);
       return 1+ a>b ? a : b;
    }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值