• C# 二叉堆


    二叉堆数据结构讲解: http://www.cnblogs.com/yc_sunniwell/archive/2010/06/28/1766751.html

    C#代码实现

    using System;
    using System.Collections.Generic;
    
    namespace 二叉堆
    {
        //从小到大
        public class BinaryHeap
        {
            private int[] heap;
            private int index = 1;
    
            public void Init(int count)
            {
                count++;
                heap = new int[count];
    
            }
    
            
            public void Enqueue(int value) 
            {
                heap[index] = value;
    
                if (index == 1) {
                    index++;
                    return;
                }
    
                //开始从最下层跟父节点对比,往上升级
                int temp = 0;
                int tempIndex = index;
                int parentIndex = 0;
                int tempI = tempIndex % 2 == 0 ? 0 : 1;
    
                //动态改变tempI=0?1,进入左右与父节点的值比较和交换
                while (heap[tempIndex] < heap[(parentIndex = (tempIndex - tempI) / 2)])
                {
                    temp = heap[tempIndex];
                    heap[tempIndex] = heap[parentIndex];
                    heap[parentIndex] = temp;
                    tempIndex = parentIndex;
    
                    tempI = tempIndex % 2 == 0 ? 0 : 1;     //判断我现在是左节点(i * 2),还是右节点(i - 1 * 2)
                }
    
                index++;
            }
    
    
            public int Dequeue() 
            {
                if (index == 1)
                    return heap[index];
    
                int result = heap[1];
                heap[1] = 0;
    
                int temp = 0;
                int tempIndex = 1;
                int indexLeft = 0;
                int indexRight  = 0;
    
                //当堆里面有两个元素的时候,也就是index>=2的时候
                while(tempIndex * 2 < index) 
                {
                    indexLeft = (tempIndex * 2 < index) ? tempIndex * 2 : 0;
                    indexRight = ((tempIndex) * 2 + 1 < index) ? (tempIndex) * 2 + 1 : 0;
    
                    //两个节点都存在情况下
                    if(Convert.ToBoolean(indexLeft) && Convert.ToBoolean(indexRight))
                    {
                        if (heap[indexLeft] < heap[indexRight])
                        {
                            temp = heap[tempIndex];
                            heap[tempIndex] = heap[indexLeft];
                            heap[indexLeft] = temp;
    
                            tempIndex = indexLeft;
                        }
                        else 
                        {
                            temp = heap[tempIndex];
                            heap[tempIndex] = heap[indexRight];
                            heap[indexRight] = temp;
    
                            tempIndex = indexRight;
                        }
                    }
                    else if (Convert.ToBoolean(indexLeft))
                    {
    
                        temp = heap[tempIndex];
                        heap[tempIndex] = heap[indexLeft];
                        heap[indexLeft] = temp;
    
                        tempIndex = indexLeft;
                    }
                    else 
                    {
                        break;
                    }
                }
    
    
                if(tempIndex != index - 1)
                {
                    heap[tempIndex] = heap[index - 1];
                    heap[index - 1] = 0;
    
                    int tempI = tempIndex % 2 == 0 ? 0 : 1;
                    int parentIndex = 0;
    
                    //动态改变tempI=0?1,进入左右与父节点的值比较和交换
                    while (heap[tempIndex] < heap[(parentIndex = (tempIndex - tempI) / 2)])
                    {
                        temp = heap[tempIndex];
                        heap[tempIndex] = heap[parentIndex];
                        heap[parentIndex] = temp;
                        tempIndex = parentIndex;
    
                        tempI = tempIndex % 2 == 0 ? 0 : 1;     //判断我现在是左节点(i * 2),还是右节点(i - 1 * 2)
                    }
                }
    
                index--;
                return result;
            }
    
            public int GetMin() 
            {
                if (index == 1)
                    return 0;
    
                return heap[1];
            }
    
            public override string ToString()
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb = sb.Append("0-");
                for (int i = 1; i < heap.Length; i++)
                {
                    if (heap[i] == 0)
                        break;
                    sb = sb.Append(heap[i] + "-");
                }
    
                sb = sb.Remove(sb.Length - 1, 1);
    
    
                return sb.ToString();
            }
        }
    }
  • 相关阅读:
    TestNG教程网站
    BITE
    软件测试理论
    Java 学习笔记 (八) Java 变量
    封装
    把封装脚本做成jar包
    表现层(jsp)、持久层(类似dao)、业务层(逻辑层、service层)、模型(javabean)、控制层(action)
    IOException parsing XML document from class path resource [WebRoot/WEB-INF/applicationContext.xml];
    java 里面耦合和解耦
    JAVA中Action层, Service层 ,modle层 和 Dao层的功能区分
  • 原文地址:https://www.cnblogs.com/plateFace/p/5177535.html
Copyright © 2020-2023  润新知