search
HomeJavajavaTutorialExamples of heaps of bases for algorithm design and analysis

Use array to store heap data

package cn.xf.algorithm.ch06ChangeRule;
 
import java.util.ArrayList;
import java.util.List;
 
import org.junit.Test;
 
/**
 *
 * 功能:堆的构造
 * 1、堆可以定义为一颗二叉树,树的节点包含键,并且满足一下条件
 *  1) 树的形状要求:这棵二叉树是基本完备的(完全二叉树),树的每一层都是满的,除了最后一层最右边的元素可能缺位
 *  2) 父母优势,堆特性,每一个节点的键都要大于或者等于他子女的键(对于任何叶子我们认为这都是自动满足的)
 * 
 * 对于堆:
 *   只存在一颗n个节点的完全二叉树他的高度:取下界的 log2的n的对数
 *  堆的根总是包含了堆的最大元素
 *  堆的一个节点以及该节点的子孙也是一个堆
 *  可以用数组的来实现堆,方法是从上到下,从左到右的方式来记录堆的元素。
 * @author xiaofeng
 * @date 2017年7月9日
 * @fileName Heap.java
 *
 */
public class Heap {
    /**
     * 堆的数据存放结构
     */
    private List<Double> heap;
 
    /**
     * 自下而上构建一个堆
     */
    private List<Double> createHeadDownToUp(List<Double> heap) {
        if(heap == null || heap.size() <= 0)
            return heap;
         
        //数据个数
        int nums = heap.size();
        //吧数组整体后移一位,方便数据的计算,因为从0开始,那么2*0还是0,没有体现出2*n就是n的左孩子的基本设定
        heap.add(0, 0d);
         
        //构建一个堆,从数组的中间位置开始,因为中间位子mid的两倍正好差不多是这个树的末尾,而在这个2*mid的附近就是mid这个节点的孩子节点
        for(int i = nums / 2 + 1; i > 0; --i) {
            //获取基准节点的地址
            int baseIndex = i;
            //获取这个节点的值
            double vBaseValue = heap.get(baseIndex);
            boolean isHeap = false; //这个用来判断当前遍历的这三个数字是否满足堆的概念
            //进行堆变换,交换树的节点和孩子节点数值,使当前树满足堆的概念
            //2 * baseIndex <= nums 这个用来判断这颗树的子树也满足堆的定义
            while(!isHeap && 2 * baseIndex <= nums) {
                //获取当前遍历到的数据的左孩子节点的位置
                int maxChildIndex = 2 * baseIndex;
                //从两个孩子节点中获取大的那个位置
                if(maxChildIndex < nums) {
                    //如果左孩子的位置比总长还小,由于完全二叉树的属性,那么必定存在右孩子节点
                    //判断那个孩子节点的数据比较大,使max为大的那个
                    if(heap.get(maxChildIndex) < heap.get(maxChildIndex + 1)) {
                        //如果右孩子比较大
                        maxChildIndex += 1;
                    }
                }
                 
                //再判断,当前 节点的值是不是比孩子节点的值要大,如果是那么就当前子树是满足堆的属性
                //maxChildIndex == nums  那还是瞒住条件,可以进行左子树的比较
                if(maxChildIndex > nums || vBaseValue >= heap.get(maxChildIndex)) {
                    isHeap = true;
                } else {
                    //如果不满住,那么交换,吧大的数据交换到节点上,吧节点的数据换到孩子节点上
                    heap.set(baseIndex, heap.get(maxChildIndex));
                    baseIndex = maxChildIndex;
                    heap.set(baseIndex, vBaseValue);
                }
            }
        }
         
        //去除第一个0,然后返回
        heap.remove(0);
        return heap;
    }
     
    private void shifHeadDownToUp(int i) {
        if (heap == null || heap.size() <= 0)
            return;
         
        // 数据个数
        int nums = heap.size();
        // 吧数组整体后移一位,方便数据的计算,因为从0开始,那么2*0还是0,没有体现出2*n就是n的左孩子的基本设定
        heap.add(0, 0d);
        boolean isHeap = false;
        int baseIndex = i;
        double vBaseValue = heap.get(i);
        while (!isHeap && 2 * baseIndex <= nums) {
            // 获取当前遍历到的数据的左孩子节点的位置
            int maxChildIndex = 2 * baseIndex;
            // 从两个孩子节点中获取大的那个位置
            if (maxChildIndex < nums) {
                // 如果左孩子的位置比总长还小,由于完全二叉树的属性,那么必定存在右孩子节点
                // 判断那个孩子节点的数据比较大,使max为大的那个
                if (heap.get(maxChildIndex) < heap.get(maxChildIndex + 1)) {
                    // 如果右孩子比较大
                    maxChildIndex += 1;
                }
            }
             
            // 再判断,当前 节点的值是不是比孩子节点的值要大,如果是那么就当前子树是满足堆的属性
            // maxChildIndex == nums 那还是瞒住条件,可以进行左子树的比较
            if (maxChildIndex > nums || vBaseValue >= heap.get(maxChildIndex)) {
                isHeap = true;
            } else {
                // 如果不满住,那么交换,吧大的数据交换到节点上,吧节点的数据换到孩子节点上
                heap.set(baseIndex, heap.get(maxChildIndex));
                baseIndex = maxChildIndex;
                heap.set(baseIndex, vBaseValue);
            }
        }
         
        // 去除第一个0,然后返回
        heap.remove(0);
    }
     
    //创建堆
    public Heap() {
        heap = new ArrayList<Double>();
        createHeadDownToUp(heap);
    }
     
    public Heap(List<Double> data) {
        if(data == null || data.size() <= 0) {
            data = new ArrayList<Double>();
        }
        heap = data;
        createHeadDownToUp(heap);
    }
     
    @Override
    public String toString() {
        return heap.toString();
    }
     
    public void add(Double value) {
        if(value == null)
            return;
        heap.add(value);
//      int insertInedx = heap.size();
        //自底向上构建堆
        for(int i = heap.size() / 2; i >= 0; --i) {
            shifHeadDownToUp(i + 1);
        }
    }
     
     
    /**
     * 删除一个元素,获取这个元素的索引位置来删除
     * 1、根的键《和》堆的最后一个键K做交换
     * 2、堆的规模减一
     * 3、严格按照自底向上的够着算法的做法,吧K 向下筛选,堆数据进行堆化
     * @param index
     */
    public void delete(int index) {
        //这个是自底向上进行堆化数据
        //吧最后一个数据填入到要删除的数据中
        Double lastValue = heap.get(heap.size() - 1);
        //删除最后一个元素,吧最后一个元素用来取代这个需要删除的元素
        heap.set(index, lastValue);
        heap.remove(heap.size() - 1);
        //自底向上开始堆化
        for(int i = index; i >= 0; --i)
            shifHeadDownToUp(i + 1);
    }
     
}


The above is the detailed content of Examples of heaps of bases for algorithm design and analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment