D heap implementation and D heap implementation

Source: Internet
Author: User

D heap implementation and D heap implementation

Implement the last blog (http://blog.csdn.net/buleriver/article/details/38469977) said D heap, if the mD is set to 2, D heap degrades to a binary heap, that is to say, the binary heap is a situation of D heap.

Public class DHeap {public static final int INIT_CAPACITY = 10; private int [] mArray; private int mLength; private final int mD; public DHeap (int d) {mArray = new int [INIT_CAPACITY + 1]; mLength = 0; mD = d;} public int getParentIndex (int index) {return (index-2 + mD)/mD ;} public int getChildIndex (int pIndex, int childIndex) {return mD * (pIndex-1) + 2 + childIndex;} private void expandArray (int length) {int [] arr = new int [length]; System. arraycopy (mArray, 1, arr, 1, mLength); mArray = arr;}/*** put the value to the last one from the bottom up and keep switching with its parent node, find the appropriate location */public void insert (int value) {if (mLength> = mArray. length-1) {expandArray (mArray. length * 2);} mArray [++ mLength] = value; int index = mLength; int parentIndex = getParentIndex (index); while (parentIndex> 0) {int currentValue = mArray [index]; int parentValue = mArray [parentIndex]; if (currentValue <parentValue) {mArray [parentIndex] = currentValue; mArray [index] = parentValue; index = parentIndex; parentIndex = getParentIndex (index);} else {break ;}} public void deleteMin () {if (mLength <= 0) {return ;} else if (mLength = 1) {mLength --;} else {mArray [1] = mArray [mLength]; int index = 1; mLength --; while (true) {int value = mArray [index]; int minIndex =-1; // minimum child array index boolean lastLevel = false; // whether the underlying int firstChildIndex = getChildIndex (index, 0); int lastChildIndex = firstChildIndex + mD; for (int childIndex = firstChildIndex; childIndex <lastChildIndex; childIndex ++) {// find the youngest child if (childIndex> mLength) {// The Last lastLevel = true; break;} int childValue = mArray [childIndex]; if (value> childValue) {value = childValue; minIndex = childIndex; }}if (minIndex <0) {// it already meets the nature of the d heap and does not need to replace break ;} else {// replace mArray [minIndex] = mArray [index]; mArray [index] = value; index = minIndex;} if (lastLevel) {// The Bottom-layer break has been reached ;}}}}}



For the inbound stack sequence as A, B, C, D, output all its available outbound stack sequences (implemented using stacks)

A, B, c, d
D, c, B,
B, c, d,
C, d, B,
A, d, c, B
A, c, B, d
A, B, d, c
C, B, d,
C, B, a, d
B, a, c, d
B, a, d, c
 
Implementation of heap Sorting Algorithm

# Include <stdio. h>
# Include <malloc. h>
# Include <time. h>
# Define list size 100
# Define MORESIZE 100
# Define overflow-1
Typedef struct
{
Int data;
Int fre;
} Cell;
Typedef struct {
Cell * elem;
Long int length;
Unsigned long int count1;
Unsigned long int count2;
Long int listsize;
} SqList;
SqList L1;
Clock_t start, end;
FILE * p, * w;
Int main (void)
{
Void assign (Cell * a, Cell * B );
Int LT (int a, int B );
Void HeapSort (SqList & H );
Void HeapAdjust (SqList & H, int s, int m );
Void exchange (Cell * a, Cell * B );

// Read

Int time = 0;
While (time <4)
{
Switch (time)
{
Case 0:
P = fopen ("data01.txt", "r ");
W = fopen ("sorted01.txt", "w ");
Break;
Case 1:
P = fopen ("data02.txt", "r ");
W = fopen ("sorted02.txt", "w ");
Break;
Case 2:
P = fopen ("data03.txt", "r ");
W = fopen ("sorted03.txt", "w ");
Break;
Case 3:
P = fopen ("data04.txt", "r ");
W = fopen ("sorted04.txt", "w ");
Break;
}

L1.count1 = 0;
L1.count2 = 0;

Time ++;
L1.elem = (Cell *) malloc (LISTSIZE + 1) * sizeof (Cell ));
L1.listsize = LISTSIZE;
L1.length = 1;
Cell * newbase;
While (! Feof (p ))
{

If (L1.length> L1.listsize ){
Newbase = (Cell *) realloc (L1.elem, (L1.listsize + MORESIZE + 1) * sizeof (Cell ));
If (! Newbase)
Return overflow;
L1.elem = newbase;
L1.listsize + = MORESIZE ;}
Fscanf (p, "% d (% d) \ n & qu ...... the remaining full text>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.