`
午刀十
  • 浏览: 33949 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论
阅读更多
package arrayTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class HeapSortApp
{

    public static void main(String args[]) throws IOException
    {
        int size, j;
        System.out.println("Enter number of items");
        size = getInt();
        Heap theHeap = new Heap(size);

        for(j = 0; j < size; j++)
        {
            int random = (int)(java.lang.Math.random() * 100);
            Node newNode = new Node(random);
            theHeap.insertAt(j, newNode);
            theHeap.incrementSize();
        }
        System.out.println("Random: ");
        theHeap.displayArray();
        //构建大顶堆,从最后一个节点的父节点开始向下筛选
        for(j = size / 2 - 1; j >= 0; j--)
            theHeap.trickleDown(j);

        System.out.println("Heap:  ");
        theHeap.displayArray();
        //        theHeap.displayHeap();

        //biggestNode返回最大值的节点,插入到堆的末尾,方便输出
        for(j = size - 1; j >= 0; j--)
        {
            Node biggestNode = theHeap.remove();
            theHeap.insertAt(j, biggestNode);
        }
        System.out.println("Sorted: ");
        theHeap.displayArray();

    }

    public static String getString() throws IOException
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String s = br.readLine();
        return s;
    }

    public static int getInt() throws IOException
    {
        String s = getString();
        return Integer.parseInt(s);
    }
}

class Node
{

    private int iData;

    public Node(int key)
    {
        iData = key;
    }

    public int getKey()
    {
        return iData;
    }
}

class Heap
{

    private Node[] heapArray;
    private int    maxSize;
    private int    currentSize;

    public Heap(int mx)
    {
        maxSize = mx;
        currentSize = 0;
        heapArray = new Node[maxSize];
    }

    /**
     * 删除并返回根节点,删除后,将最后一个节点放置根节点,再进行向下筛选
     * 
     * @return
     */
    public Node remove()
    {
        Node root = heapArray[0];
        heapArray[0] = heapArray[--currentSize];
        trickleDown(0);
        return root;
    }

    /**
     * 向下筛选,从该节点往下搜索,直到找到最大节点,替换该节点
     * 
     * @param index
     */
    public void trickleDown(int index)
    {
        int largerChild;
        Node top = heapArray[index];
        while (index < currentSize / 2)
        {
            int leftChild = 2 * index + 1;
            int rightChild = leftChild + 1;

            if(rightChild < currentSize && heapArray[leftChild].getKey() < heapArray[rightChild].getKey())
                largerChild = rightChild;
            else
                largerChild = leftChild;
            if(top.getKey() >= heapArray[largerChild].getKey())
                break;
            heapArray[index] = heapArray[largerChild];
            index = largerChild;
        }
        heapArray[index] = top;
    }

    public void displayHeap()
    {
        int nBlanks = 32;
        int itemsPerRow = 1;
        int column = 0;
        int j = 0;
        String dots = "......................";
        System.out.println(dots + dots);

        while (currentSize > 0)
        {
            if(column == 0)
                for(int k = 0; k < nBlanks; k++)
                    System.out.println(" ");

            System.out.println(heapArray[j].getKey());

            if(++j == currentSize)
                break;

            if(++column == itemsPerRow)
            {
                nBlanks /= 2;
                itemsPerRow *= 2;
                column = 0;
                System.out.println();
            }
            else
            {
                for(int k = 0; k < nBlanks * 2 - 2; k++)
                    System.out.println(" ");
            }
        }

        System.out.println("\n" + dots + dots);
    }

    public void displayArray()
    {
        for(int j = 0; j < maxSize; j++)
            System.out.println(heapArray[j].getKey() + " ");
        System.out.println("");
    }

    public void insertAt(int index, Node newNode)
    {
        heapArray[index] = newNode;
    }

    public void incrementSize()
    {
        currentSize++;
    }
}

注:以上代码均出自《Java数据结构和算法中文第二版》
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics