zoukankan      html  css  js  c++  java
  • 左倾堆(C#)

    参考:http://www.cnblogs.com/skywang12345/p/3638384.html

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Threading;
    using System.IO;
    using System.Collections;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication2
    {
        public class Program
        {
            public static void Main()
            {
    
                LeftListHeapTree<int> leftListTreeA = new LeftListHeapTree<int>();
                leftListTreeA.Insert(10);
                leftListTreeA.Insert(40);
                leftListTreeA.Insert(24);
                leftListTreeA.Insert(30);
                leftListTreeA.Insert(36);
                leftListTreeA.Insert(20);
                leftListTreeA.Insert(12);
                leftListTreeA.Insert(16);
    
    
                LeftListHeapTree<int> leftListTreeB = new LeftListHeapTree<int>();
                leftListTreeB.Insert(17);
                leftListTreeB.Insert(13);
                leftListTreeB.Insert(11);
                leftListTreeB.Insert(15);
                leftListTreeB.Insert(19);
                leftListTreeB.Insert(21);
                leftListTreeB.Insert(23);
    
    
                leftListTreeA.Merge(leftListTreeB);
    
                Console.Read();
            }
        }
    
        public class LeftListHeapNode<T> where T : IComparable
        {
            public T Key { get; set; }
            public int Npl { get; set; }
            public LeftListHeapNode<T> LeftNode { get; set; }
            public LeftListHeapNode<T> RightNode { get; set; }
    
            public LeftListHeapNode(T _key, LeftListHeapNode<T> _leftNode, LeftListHeapNode<T> _rightNode)
            {
                Key = _key;
                LeftNode = _leftNode;
                RightNode = _rightNode;
                Npl = 0;
            }
    
            public override string ToString()
            {
                return Key + "";
            }
        }
    
    
        public class LeftListHeapTree<T> where T : IComparable
        {
            public LeftListHeapNode<T> RootNode { get; set; }
    
    
            private LeftListHeapNode<T> Merge(LeftListHeapNode<T> leftTreeNode, LeftListHeapNode<T> rightTreeNode)
            {
                if (leftTreeNode == null)
                {
                    return rightTreeNode;
                }
    
                if (rightTreeNode == null)
                {
                    return leftTreeNode;
                }
    
                if (leftTreeNode.Key.CompareTo(rightTreeNode.Key) > 0)
                {
                    LeftListHeapNode<T> temp = leftTreeNode;
                    leftTreeNode = rightTreeNode;
                    rightTreeNode = temp;
                }
    
                leftTreeNode.RightNode = Merge(leftTreeNode.RightNode,rightTreeNode);
    
                if (leftTreeNode.LeftNode == null || leftTreeNode.LeftNode.Npl < leftTreeNode.RightNode.Npl)
                {
                    LeftListHeapNode<T> temp = leftTreeNode.LeftNode;
                    leftTreeNode.LeftNode = leftTreeNode.RightNode;
                    leftTreeNode.RightNode = temp;
                }
    
                if (leftTreeNode.RightNode == null || leftTreeNode.LeftNode == null)
                {
                    leftTreeNode.Npl = 0;
                }
                else {
                    leftTreeNode.Npl = (leftTreeNode.LeftNode.Npl > leftTreeNode.RightNode.Npl) ?
                        (leftTreeNode.RightNode.Npl + 1) : (leftTreeNode.LeftNode.Npl + 1);
                }
                return leftTreeNode;
            }
    
            public void Merge(LeftListHeapTree<T> other)
            {
                this.RootNode = Merge(this.RootNode,other.RootNode);
            }
    
            public void Insert(T key)
            {
                LeftListHeapNode<T> node = new LeftListHeapNode<T>(key,null,null);
                this.RootNode = Merge(this.RootNode,node);
            }
        }
    }
  • 相关阅读:
    Android编译选项eng、user、userdebug的区别
    Linux 内核编码规范
    PLM之EVT、DVT、PVT、MP
    fastboot刷机的一般方法
    Android手机拨号测试项
    使用Genymotion安装APK出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE的解决办法
    三星手机列表拖动时出现诡异背景色的问题
    分享android ADT百度云盘下载地址
    关于互联网思维
    分享Nginx在Windows下的管理命令(bat文件)
  • 原文地址:https://www.cnblogs.com/bbvi/p/5151278.html
Copyright © 2011-2022 走看看