zoukankan      html  css  js  c++  java
  • 数据结构和算法之最大堆

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MaxHeap
    {
    
        public class MaxHeap<T>
        {
            private IComparer<T> mComparer;
            private List<T> mItems;
    
            private int mCount;
    
            public MaxHeap()
            {
                mComparer = Comparer<T>.Default;
                mItems = new List<T>();
                mItems.Add(default(T));
                mCount = 0;
            }
            public MaxHeap(IComparer<T> comparer)
            {
                mComparer = comparer;
                mItems = new List<T>();
                mItems.Add(default(T));
                mCount = 0;
            }
    
    
            public void Insert(T n)
            {
                mCount++;
                mItems.Add(n);
                ShiftUp(mCount);
            }
    
            public T ExtractMax()
            {
                T ret = mItems[1];
                Swap(1, mCount);
                mCount--;
    
                ShiftDown(1);
                mItems.Remove(ret);
                return ret;
            }
    
            public bool IsEmpty()
            {
                return mCount == 0;
            }
    
            private void ShiftDown(int n)
            {
                //是否有孩子节点
                while(n*2<=mCount)
                {
                    int j = n * 2;
                    if(j+1<=mCount && mComparer.Compare(mItems[j + 1],mItems[j])>0)
                    {
                        j++;//最大的节点
                    }
                    if(mComparer.Compare(mItems[n],mItems[j])>0)
                    {
                        break;
                    }
                    Swap(n, j);
                    n = j;
                }
            }
    
            private void ShiftUp(int n)
            {
                while (n > 1 && mComparer.Compare(mItems[n], mItems[n / 2]) > 0)
                {
                    Swap(n, n / 2);
                    n = n / 2;
                }
            }
    
            private void Swap(int i,int j)
            {
                T tmpValue = mItems[i];
                mItems[i] = mItems[j];
                mItems[j] = tmpValue;
            }
    
    
    
    
        }
    }
    // See https://aka.ms/new-console-template for more information
    namespace MaxHeap
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World!");
                MaxHeap<int> maxHeap = new MaxHeap<int>();
                for (int i = 0; i < 10; i++)
                {
                    maxHeap.Insert(i);
                }
                maxHeap.Insert(11);
                while(maxHeap.IsEmpty() == false)
                {
                    Console.WriteLine(maxHeap.ExtractMax());
                }
                Console.ReadKey();
    
            }
        }
    }
  • 相关阅读:
    55、分页查询employees表,每5行一页,返回第2页的数据
    54、查找排除当前最大、最小salary之后的员工的平均工资avg_salary
    53、按照分组拼接字段
    52、获取Employees中的first_name
    51、查找字符串'10,A,B' 中逗号','出现的次数cnt
    图片素材
    软件下载
    一款高效卸载软件
    《单独.17 人的困境》(摘抄)
    Markdown的简单使用
  • 原文地址:https://www.cnblogs.com/weiqiangwaideshijie/p/15792853.html
Copyright © 2011-2022 走看看