zoukankan      html  css  js  c++  java
  • 斜堆

    斜堆和左式堆的差别是,左式堆仅仅有在右孩子的npl大于左孩子的npl时,交换两个孩子的位置。而斜堆是不管怎样都交换:

    package com.iflytek.heap;
    
    /**
     * 斜堆
     * @author fgtian
     *
     */
    public class SkewHeap {
    	public static class HeapNode {
    		int mValue;
    		int mNpl = 0;
    		HeapNode mLeftChild;
    		HeapNode mRightChild;
    	}
    	
    	HeapNode mRoot = null;
    	
    	public void insert(int value) {
    		HeapNode node = new HeapNode();
    		node.mValue = value;
    		merge(node);
    	}
    	
    	public int delMin() {
    		if (null == mRoot) {
    			throw new NullPointerException("null == mRoot");
    		}
    		int value = mRoot.mValue;
    		mRoot = merge(mRoot.mLeftChild, mRoot.mRightChild);
    		return value;
    	}
    	
    	public void merge(HeapNode node) {
    		mRoot = merge(mRoot, node);
    		HeapNode l = mRoot.mLeftChild;
    		mRoot.mLeftChild = mRoot.mRightChild;
    		mRoot.mRightChild = l;
    		mRoot.mNpl = Math.min(npl(mRoot.mLeftChild), npl(mRoot.mRightChild)) + 1;
    	}
    	
    	public void merge(SkewHeap heap) {
    		merge(heap.mRoot);
    	}
    	
    	public static int npl(HeapNode h) {
    		if (null == h) {
    			return -1;
    		}
    		return h.mNpl;
    	}
    	
    	public static HeapNode merge(HeapNode h1, HeapNode h2) {
    		if (null == h1) {
    			return h2;
    		} else if (h2 == null) {
    			return h1;
    		} else { // 两个都不是null
    			int v1 = h1.mValue;
    			int v2 = h2.mValue;
    			if (v1 <= v2) { // 拿他的
    				h1.mRightChild = merge(h1.mRightChild, h2);
    				HeapNode node = h1.mLeftChild;
    				h1.mLeftChild = h1.mRightChild;
    				h1.mRightChild = node;
    				h1.mNpl = Math.min(npl(h1.mLeftChild), npl(h1.mRightChild)) + 1;
    				return h1;
    			} else {
    				h2.mRightChild = merge(h1, h2.mRightChild);
    				HeapNode node = h2.mLeftChild;
    				h2.mLeftChild = h2.mRightChild;
    				h2.mRightChild = node;
    				h2.mNpl = Math.min(npl(h2.mLeftChild), npl(h2.mRightChild)) + 1;
    				return h2;
    			}
    		}
    	}
    }
    


  • 相关阅读:
    C#多线程编程实战(一):线程基础
    查找算法之顺序查找
    设计模式01 创建型模式
    查找算法之二分查找
    设计模式01 创建型模式
    每天学一个,设计模式概要
    设计模式 01
    汽车电子传感器科普:激光雷达 毫米波雷达 超声波雷达
    C 如何判断编译器是否支持C90 C99?
    Node.js之EventEmiter
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7200320.html
Copyright © 2011-2022 走看看