• 算法导论第6章堆heap


    ]70D%[2K2DUE2Z[B4T`Q{ZE

    DB$UEXQS2RM{4%8A_YOOJ[2

    1NOY`~FM8H)M(1)MNE4XRHX

    C5O@MCZT5_L[UV8MMSZ]@)1

    0R$~`N_55KDK~U39%4I%{HR

    YOR`FJ@QK)][LFB)BY`O}B0

    V{RVL)401E0RN32R9_{9NDK

    JS%XJIH2HYCCZ]479}47()U

    2PO7SJPL~972AY~F6I(]K}X

     

    Z30JYD9BDBM%8U{2`)~@@(S

     

    0)(){34SGU~7N`HI9FCFTGQ

     

    L9QL`@67GZW{Z@ZLI7}9Z4R

     

    G}YV]ZSP2Q{6PW7E]V0H)TH

     

    GT}Q5S$6JO13M$M1T{8(EU0

     

    G0RVV`M0N)N0D]3SDBZ09NW

     

    J`Z9MHK2LC(]8022SA$E$L4

     

    RLZCBD9D2KW}`E]J{PTN]8H

     

    @HE4K4P2~B7I$FV{$)_`I)R

     

     

    heap

    // heap.h
    
    // PARENT(i)
    // return i / 2
    // LEFT(i)
    // return 2 * i
    // RIGHT(i)
    // return 2 * i + 1
    
    // MAX-HEAPIFY(A, i)
    // l = LEFT(i)
    // r = RIGHT(i)
    // if l <= A.heap-size and A[l] > a[i]
    //     largest = l
    // else largest = i
    // if r <= A.heap-size and A[r] > A[largest]
    //     largest = r
    // if largest != i
    //     exchange A[i] with A[largest]
    //     MAX-HEAPIFY(A, largest)
    
    // BUILD-MAX-HEAP(A)
    // A.heap-size = A.length
    // for i = A.length / 2 down to 1
    //     MAX-HEAPIFY(A, i)
    
    // HEAPSORT(A)
    // BUILD-MAX-HEAP(A)
    // for i = A.length down to 2
    //     exchange A[1] with A[i]
    //     A.heap-size = A.heap-size - 1
    //     MAX-HEAPIFY(A, 1)
    
    // HEAP-EXTRACT-MAX
    // if A.heap-size < 1
    //     error "heap underflow"
    // max = A[1]
    // A[1] = A[A.heap-size]
    // A.heap-size--
    // MAX-HEAPIFY(A, 1)
    // return max
    
    // HEAP-INCREASE-KEY(A, i, key)
    // if key < A[i]
    //     error "new key is smaller than current key"
    // A[i] = key
    // while i > 1 and A[PARENT(i)] < A[i]
    //     exchange A[i] with A[PARENT(i)]
    //     i = PARENT(i)
    
    // MAX-HEAP-INSERT(key)
    // A.heap-size = A.heap-size + 1
    // A[A.heap-size] = -MAX
    // HEAP-INCREASE-KEY(A, A.heap-size, key)
    #include <iostream>
    #include <stdint.h>
    
    using std::cout;
    using std::endl;
    
    class IA_heap {
    public:
        IA_heap(int64_t* p, uint64_t n): m_heap_size(0)
        {
            // m_array = p;
            m_length = n;
    
            m_array = new int64_t[n];
            memcpy(m_array, p, n * sizeof(int64_t));
        }
        void build_max_heap()
        {
            m_heap_size = m_length - 1;
            for (int64_t i = m_length / 2 - 1; i >= 0; i--)
            {
                max_heapify(i);
            }
        }
        void max_heapify(int64_t i)
        {
            int64_t l = left(i);
            int64_t r = right(i);
            int64_t largest;
            if (l <= m_heap_size && m_array[l] > m_array[i])
            {
                largest = l;
            }
            else
            {
                largest = i;
            }
            if (r <= m_heap_size && m_array[r] > m_array[largest])
            {
                largest = r;
            }
            if (largest != i)
            {
                swap(i, largest);
                max_heapify(largest);
            }
    
        }
        void heap_sort()
        {
            for (uint64_t i = m_length - 1; i >= 1 ; i--)
            {
                swap(0, i);
                m_heap_size--; // a heap after sorting, no a heap anymore
                max_heapify(0);
            }
        }
        int64_t heap_maximum() { return m_array[0]; }
        int64_t heap_extract_max(int64_t& max)
        {
            if (m_heap_size < 0)
            {
                std::cout << "error underflow" <<  std::endl;
                return -1;
            }
            max = m_array[0];
            m_array[0] = m_array[m_heap_size];
            m_heap_size--;
            max_heapify(0);
            return 0;
        }
        void heap_increase_key(int64_t i, int64_t key)
        {
            if (key < m_array[i])
            {
                std::cout << "new key is smaller than current key" << std::endl;
            }
            m_array[i] = key;
            while (i > 0 && m_array[parent(i)] < m_array[i])
            {
                swap(i, parent(i));
                i = parent(i);
            }
        }
        void max_heap_insert(int64_t key)
        {
            m_heap_size++;
            if (m_heap_size == m_length)
            {
                int64_t* p = new int64_t[sizeof(int64_t) * (m_length + 1)];
                memcpy(p, m_array, sizeof(int64_t) * m_length);
                delete [] m_array;
                m_array = p;
                m_length++;
            }
            m_array[m_heap_size] = INT64_MIN;
            heap_increase_key(m_heap_size, key);
        }
        void print_array()
        {
            std::cout << "print_array" << std::endl;
            for (int64_t i = 0; i < m_length; i++)
            {
                std::cout << m_array[i] << " ";
            }
            std::cout << std::endl;
        }
        void print_heap()
        {
            std::cout << "print_heap" << std::endl;
            if (0 == m_heap_size)
            {
                std::cout << "no heap element" << std::endl;
            }
            for (int64_t i = 0; i <= m_heap_size; i++)
            {
                std::cout << m_array[i] << " ";
            }
            std::cout << std::endl;
        }
    
    private:
        uint64_t left(uint64_t i)
        {
            return 2 * i + 1;
        }
        uint64_t right(uint64_t i)
        {
            return 2 * i + 2;
        }
        uint64_t parent(uint64_t i)
        {
            return (i - 1) / 2;
        }
        void swap(uint64_t i, uint64_t j)
        {
            int64_t tmp = m_array[i];
            m_array[i] = m_array[j];
            m_array[j] = tmp;
        }
        int64_t* m_array;
        int64_t m_heap_size;
        int64_t m_length;
    };
    #include "heap.h"
    int main()
    {
        int64_t array[] = { 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 };
        IA_heap A(array, sizeof(array) / sizeof(int64_t));
        A.print_array();
        A.print_heap();
    
        A.build_max_heap();
        std::cout << "A.heap_maximum: " << A.heap_maximum() << std::endl;
        A.print_array();
        A.print_heap();
    
        A.heap_increase_key(8, 15);
        A.print_array();
        A.print_heap();
    
        A.max_heap_insert(13);
        A.print_array();
        A.print_heap();
    
        int64_t max;
        
        if (0 == A.heap_extract_max(max))
        {
            std::cout << max << std::endl;
        }
        A.print_array();
        A.print_heap();
        while (0 == A.heap_extract_max(max))
        {
            std::cout << max << std::endl;
        }
        
    
    //     A.heap_sort();
    //     A.print_array();
    //     A.print_heap();
    
        getchar();
        return 0;
    }
    

    heap

    f41bbd808b5ff20d021125d53782a79a

    786_1428118_770410 786_1428122_398994 0790c34beddb0c96561b8b9746c56d0a 810de12d-73cc-45d0-928e-a7eb0766ea91 2294b541563f8ed0b6e8ad5bb9e925ef 2471f1ec8f8544861a6cc4c3cec0871b 3198df139bd36a44964ed9df86728774

  • 相关阅读:
    LumaQQ.NET协议过期及解决办法
    帮助中国移动设计10086的排队小模块 Virus
    《宫锁心玉》观后感 Virus
    WCF扩展:行为扩展Behavior Extension<一> Virus
    谈谈我对实体的认识:DTO,DMO,DPO Virus
    自定义ORM系列(三)工具雏形及基本用法 Virus
    随笔写下的开发流程 Virus
    自定义ORM系列(二)发现属性是否修改,有选择的持久化 Virus
    我对DDD的认知(一) Virus
    胡乱说一下我对于 BO VO PO DTO 的理解 Virus
  • 原文地址:https://www.cnblogs.com/sunyongjie1984/p/4271058.html
Copyright © 2020-2023  润新知