C#数据结构与算法学习笔记 优先队列

本文介绍了使用C#语言实现数据结构中的优先队列,主要探讨了基于最大堆Maxheap和最小堆Minheap的实现细节,帮助读者理解优先队列的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以最大堆Maxheap为底层的优先队列:

    class MaxPQ<E>:IQueue<E> where E:IComparable<E>
    {
        private MaxHeap<E> heap;

        public int Count { get { return heap.Count; } }

        public bool IsEmpty { get { return heap.IsEmpty; } }

        public MaxPQ(int capacity)
        {
            heap = new MaxHeap<E>(capacity);
        }

        public MaxPQ()
        {
            heap = new MaxHeap<E>();
        }

        public void Enqueue(E e)
        {
            heap.Insert(e);
        }

        public E Dequeue()
        {
            return heap.RemoveMax();
        }

        public E Peek()
        {
            return heap.Max();
        }

        public override string ToString()
        {
            return heap.ToString();
        }
    }

最小堆:

    //最小堆
    class MinHeap<E> where E : IComparable<E>
    {
        private E[] heap;
        private int N;

        public MinHeap(int capacity)
        {
            heap = new E[capacity + 1];
            N = 0;
        }

        public MinHeap() : this(10) { }

        public int Count { get { return N; } }

        public bool IsEmpty { get { return N == 0; } }

        //往堆中插入元素
        public void Insert(E e)
        {
            if (N == (heap.Length - 1))
                ResetCapacity(heap.Length * 2);

            heap[N + 1] = e;
            N++;
            Swim(N);
        }

        //删除堆顶元素
        public E RemoveMin()
        {
            if (IsEmpty)
                throw new ArgumentException("堆为空!");

            Swap(1, N);
            E max = heap[N];
            heap[N] = default(E);
            N--;

            Sink(1);

            if (N == (heap.Length - 1) / 4)
                ResetCapacity(heap.Length / 2);

            return max;
        }

        //查看最小值
        public E Min()
        {
            if (IsEmpty)
                throw new ArgumentException("堆为空!");

            return heap[1];
        }

        //输出数组类的信息
        public override string ToString()
        {
            StringBuilder res = new StringBuilder();
            res.Append("[");
            for (int i = 1; i <= N; i++)
            {
                res.Append(heap[i]);
                if (i != N)
                    res.Append(", ");
            }
            res.Append("]");
            return res.ToString();
        }

        //调整数组容量的大小
        private void ResetCapacity(int newCapacity)
        {
            E[] newHeap = new E[newCapacity];
            for (int i = 1; i <= N; i++)
                newHeap[i] = heap[i];

            heap = newHeap;
        }

        //元素上游
        private void Swim(int k)
        {
            while (k > 1 && heap[k].CompareTo(heap[k / 2]) < 0)
            {
                Swap(k, k / 2);
                k = k / 2;
            }
        }

        //元素下沉
        private void Sink(int k)
        {
            while (2 * k <= N)
            {
                int j = 2 * k;

                if (j + 1 <= N && heap[j + 1].CompareTo(heap[j]) < 0) j++;

                if (heap[k].CompareTo(heap[j]) <= 0) break;

                Swap(k, j);

                k = j;
            }
        }

        private void Swap(int i, int j)
        {
            E e = heap[i];
            heap[i] = heap[j];
            heap[j] = e;
        }
    }

以最小堆Minheap为底层的优先队列:

    class MinPQ<E> : IQueue<E> where E : IComparable<E>
    {
        private MinHeap<E> heap;

        public int Count { get { return heap.Count; } }

        public bool IsEmpty { get { return heap.IsEmpty; } }

        public MinPQ(int capacity)
        {
            heap = new MinHeap<E>(capacity);
        }

        public MinPQ()
        {
            heap = new MinHeap<E>();
        }

        public void Enqueue(E e)
        {
            heap.Insert(e);
        }

        public E Dequeue()
        {
            return heap.RemoveMin();
        }

        public E Peek()
        {
            return heap.Min();
        }

        public override string ToString()
        {
            return heap.ToString();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            int[] a = TestSearch.ReadFile("测试文件3/TopM.txt");
            QuickSort3.Sort(a);
            for (int i = 0; i < 10; i++)
                Console.Write(a[i] + ", ");
            Console.WriteLine();

            MaxPQ<int> pq = new MaxPQ<int>(10);
            FileStream fs = new FileStream("测试文件3/TopM.txt",FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            while (!sr.EndOfStream)
            {
                int value = int.Parse(sr.ReadLine());
                if (pq.Count < 10)
                    pq.Enqueue(value);
                else if (value < pq.Peek())
                {
                    pq.Dequeue();
                    pq.Enqueue(value);
                }
            }
            Console.WriteLine(pq);

            Console.Read();
        }
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值