java红黑二叉树

一、前言:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

package com.enjoy.tree;
import java.util.ArrayList;
import java.util.List;
/**
 * @author 【享学课堂】 King老师
 * 哈夫曼树
 */

public class HuffmanTree {
    //节点
    public static class Node<E> {
        E data; //数据
        int weight; //权重
        Node leftChild; //左子节点
        Node rightChild;//右子节点

        public Node(E data, int weight) {
            super();
            this.data = data;
            this.weight = weight;
        }

        public String toString() {
            return "Node[" + weight + ",data=" + data + "]";
        }
    }

    public static void main(String[] args) {
        List<Node> nodes = new ArrayList<Node>();
        //把节点加入至list中
        nodes.add(new Node("a", 10));
        nodes.add(new Node("b", 15));
        nodes.add(new Node("c", 12));
        nodes.add(new Node("d", 3));
        nodes.add(new Node("e", 4));
        nodes.add(new Node("f", 13));
        nodes.add(new Node("g", 1));
        //进行哈夫曼树的构造
        Node root = HuffmanTree.createTree(nodes);
        //打印哈夫曼树
        printTree(root);

    }

    /**
     * 构造哈夫曼树
     *
     * @param nodes
     *            节点集合
     * @return 构造出来的哈夫曼树的根节点
     */
    private static Node createTree(List<Node> nodes) {
        //如果节点node列表中海油2个和2个以上的节点
        while(nodes.size()>1){
            //什么是最小的,list表进行排序,增序的方式, 0,1,
            sort(nodes);//排序方式是增序的
            Node left = nodes.get(0);//权重最小的
            Node right = nodes.get(1);//权重第二小的
            //生成一个新的节点(父节点),父节点的权重为两个子节点的之和
            Node parent = new Node(null,left.weight+right.weight);
            //树的连接,让子节点与父节点进行连接
            parent.leftChild = left;
            parent.rightChild = right;
            nodes.remove(0);//删除最小的
            nodes.remove(0);//删除第二小的。
            nodes.add(parent);
        }
        return nodes.get(0); //返回根节点
    }
    /**
     * 冒泡排序,用于对节点进行排序(增序排序)
     *
     * @param nodes
     */
    public static void sort(List<Node> nodes) {
        if (nodes.size() <= 1)
            return ;
        /*循环数组长度的次数*/
        for (int i = 0; i < nodes.size(); i++){
            /*从第0个元素开始,依次和后面的元素进行比较
             * j < array.length - 1 - i表示第[array.length - 1 - i]
             * 个元素已经冒泡到了合适的位置,无需进行比较,可以减少比较次数*/
            for (int j = 0; j < nodes.size() - 1 - i; j++){
                /*如果第j个节点比后面的第j+1节点权重大,交换两者的位置*/
                if (nodes.get(j + 1).weight < nodes.get(j).weight) {
                    Node temp = nodes.get(j + 1);
                    nodes.set(j+1,nodes.get(j));
                    nodes.set(j,temp);
                }
            }
        }
        return ;
    }

    /*

     * 递归打印哈夫曼树(先左子树,后右子树打印)
     */

    public static void printTree(Node root) {
        System.out.println(root.toString());
        if(root.leftChild !=null){
            System.out.print("left:");
            printTree(root.leftChild);
        }
        if(root.rightChild !=null){
            System.out.print("right:");
            printTree(root.rightChild);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,我们可以使用自定义类来实现红黑二叉树(Red-Black Tree)。红黑树是一种自平衡的二叉查找树,每个节点都被标记为红色或黑色,满足以下五个性质: 1. 每个节点是红色或黑色。 2. 根节点是黑色。 3. 每个叶节点(空节点)是黑色。 4. 如果一个节点是红色的,则它的两个子节点都是黑色。 5. 从任一节点到其所有后代叶节点的简单路径上,均包含相同数目的黑色节点。 下面是创建一个简单的红黑树节点和类的示例: ```java public class Node { int key; boolean color; // red or black Node left, right, parent; // constructor and getters/setters... } public class RedBlackTree { private Node root; private boolean isRootRed; // to check if root is red // methods for insertion, deletion, balancing (rotate and color flips) public void insert(int key) { root = insert(root, key); } private Node insert(Node node, int key) { // ...insertion logic with proper coloring and balancing } public void delete(int key) { root = delete(root, key); } private Node delete(Node node, int key) { // ...deletion logic with balancing after removing a node } // helper methods for checking balance and rebalancing private boolean isBalanced(Node node) { // ...implementation of the balance check } private void rotateLeft(Node node) { // ...implementation of left rotation } private void rotateRight(Node node) { // ...implementation of right rotation } private void flipColors(Node node) { // ...implementation of color flipping } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值