黄毅然的JAVA学习(六)

本文详细介绍了黄毅然关于红黑二叉查找树的学习,涵盖了树的创建、插入、查找、最小键值删除和最大键值删除等关键操作。代码实现参考了《算法导论》,适合个人和学习者研究。
摘要由CSDN通过智能技术生成

红黑二叉查找树构造与操作

(黄毅然的半月更!)
代码功能如下:
(1)红黑二叉查找树的创建;
(2)红黑二叉查找树插入功能;
(3)红黑二叉查找树的查找功能;
(4)红黑二叉查找树的最小键值删除功能;
(5)红黑二叉查找树的最大键值删除功能;

以下为定义节点类以及各个操作的算法

public class RBTree<T extends Comparable<T>> {
   
    private RBTNode<T> mRoot;    // 根结点
    private static final boolean RED   = false;
    private static final boolean BLACK = true;
    public class RBTNode<T extends Comparable<T>> {
   
        boolean color;        // 颜色
        T key;                // 关键字(键值)
        RBTNode<T> left;    // 左孩子
        RBTNode<T> right;    // 右孩子
        RBTNode<T> parent;    // 父结点

        public RBTNode(T key, boolean color, RBTNode<T> parent, RBTNode<T> left, RBTNode<T> right) {
   
            this.key = key;
            this.color = color;
            this.parent = parent;//构造节点结构类型为RBTNode
            this.left = left;
            this.right = right;
        }
        public T getKey() {
   
            return key;//返回关键字
        }

        public String toString() {
   
            return ""+key+(this.color==RED?"(R)":"B");
        }
    }

    public RBTree() {
   
        mRoot=null;//创建根节点,根节点为空
    }

    private RBTNode<T> parentOf(RBTNode<T> node) {
   
        return node!=null ? node.parent : null;//返回该节点的父母节点
    }
    private boolean colorOf(RBTNode<T> node) {
   
        return node!=null ? node.color : BLACK;//返回颜色
    }
    private boolean isRed(RBTNode<T> node) {
   
        return ((node!=null)&&(node.color==RED)) ? true : false;//判断节点是否为红色
    }
    private boolean isBlack(RBTNode<T> node) {
   
        return !isRed(node);//判断节点是否为黑色
    }
    private void setBlack(RBTNode<T> node) {
   
        if (node!=null)
            node.color = BLACK;//将节点设为黑色
    }
    private void setRed(RBTNode<T> node) {
   
        if (node!=null)
            node.color = RED;//将节点设为红色
    }
    private void setParent(RBTNode<T> node, RBTNode<T> parent) {
   
        if (node!=null)
            node.parent = parent;//将节点设置为父母节点
    }
    private void setColor(RBTNode<T> node, boolean color) {
   
        if (node!=null)
            node.color = color;
    }
    private void preOrder(RBTNode<T> tree) {
   //前序遍历"红黑树"
        if(tree != null) {
   
            System.out.print(tree.key+" ");
            preOrder(tree.left);
            preOrder(tree.right);
        }
    }
    public void preOrder() {
   
        preOrder(mRoot);
    }
    
    private RBTNode<T> iterativeSearch(RBTNode<T> x, T key) {
   //非递归实现查找"红黑树"中键值为key的节点
        while (x!=null) {
   
            int cmp = key.compareTo(x.key);
            if (cmp < 0)
                x = x.left;
            else if (cmp > 0)
                x = x.right;
            else
                return x;
        }
        return x;
    }
    public RBTNode<T> iterativeSearch(T key) {
   
        return iterativeSearch(mRoot, key);
    }

    private RBTNode<T> minimum(RBTNode<T> tree) {
   
        if (tree == null)
            return null;
        while(tree.left != null)//查找最小结点:返回tree为根结点的红黑树的最小结点。
            tree = tree.left;
        return tree;
    }
    public T 
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值