zoukankan      html  css  js  c++  java
  • C# 实现简单的 Heap 堆(二叉堆)Implementing a Simple Binary Heap

    如题,C#  实现简单的二叉堆的 Push() 和 Pop(), 如有不足欢迎指正。

    另外,在C#中使用 Heap 的相似功能可以考虑使用:Priority Queues,SortedDictionary,SortedList 。

    using System;
    using System.Collections.Generic;
    
    namespace LeetCode.BaseClass
    {
        public enum HeapType
        {
            MinHeap,
            MaxHeap
        }
    
        public class BinaryHeap<T> where T : IComparable<T>
        {
            List<T> items;
    
            public HeapType HType { get; private set; }
    
            public T Root
            {
                get { return items[0]; }
            }
    
            public BinaryHeap(HeapType type)
            {
                items = new List<T>();
                this.HType = type;
            }
    
            public void Push(T item)
            {
                items.Add(item);
    
                int i = items.Count - 1;
    
                bool flag = HType == HeapType.MinHeap;
    
                while (i > 0)
                {
                    if ((items[i].CompareTo(items[(i - 1) / 2]) > 0) ^ flag)
                    {
                        T temp = items[i];
                        items[i] = items[(i - 1) / 2];
                        items[(i - 1) / 2] = temp;
                        i = (i - 1) / 2;
                    }
                    else
                        break;
                }
            }
    
            private void DeleteRoot()
            {
                int i = items.Count - 1;
    
                items[0] = items[i];
                items.RemoveAt(i);
    
                i = 0;
    
                bool flag = HType == HeapType.MinHeap;
    
                while (true)
                {
                    int leftInd = 2 * i + 1;
                    int rightInd = 2 * i + 2;
                    int largest = i;
    
                    if (leftInd < items.Count)
                    {
                        if ((items[leftInd].CompareTo(items[largest]) > 0) ^ flag)
                            largest = leftInd;
                    }
    
                    if (rightInd < items.Count)
                    {
                        if ((items[rightInd].CompareTo(items[largest]) > 0) ^ flag)
                            largest = rightInd;
                    }
    
                    if (largest != i)
                    {
                        T temp = items[largest];
                        items[largest] = items[i];
                        items[i] = temp;
                        i = largest;
                    }
                    else
                        break;
                }
            }
    
            public T PopRoot()
            {
                T result = items[0];
    
                DeleteRoot();
    
                return result;
            }
        }
        
    }
  • 相关阅读:
    json参数http post请求
    获取文本的节点数据
    mongodb robo3t 查询所有 更改固定的50一页
    mongdb 更新字段类型
    数据库表的统计表更新 解决Sql Timeout 时间已到的问题
    html背景图圆角图片设置方法
    abp.vnext vue 跨域设置
    Springboot结合ESAPI——配置XSS过滤
    centos docker安装rabbitmq
    JAVA byte[]转String 中文问题
  • 原文地址:https://www.cnblogs.com/hydor/p/10634293.html
Copyright © 2011-2022 走看看