Binary heap, heap

Source: Internet
Author: User

Binary heap, heap

Untested:
Public class BinaryHeap {public static final int INIT_CAPACITY = 10; private int [] mArray; private int mLength; public BinaryHeap () {mArray = new int [INIT_CAPACITY + 1]; mLength = 0;} private void expandArray (int length) {int [] arr = new int [length]; System. arraycopy (mArray, 1, arr, 1, mLength); mArray = arr;} public static final int [] buildHeap (int arr [], int length) {if (null = arr | length> = arr. length) {return null;} int [] ret = new int [length + 1]; System. arraycopy (arr, 0, ret, 1, length); // sink the I-th value: for (int I = length/2; I> 0; I --) {int index = I; while (true) {int leftIndex = 2 * index; int rightIndex = leftIndex + 1; int value = ret [index]; if (leftIndex> length) {// The break is exclusive;} else if (rightIndex> length) {// No exclusive if (value> ret [leftIndex] on the left) {ret [index] = ret [leftIndex]; ret [leftIndex] = value;} break;} else {// neither has an exclusive int lv = ret [leftIndex]; int rv = ret [rightIndex]; if (value <= lv & value <= rv) {break;} else if (lv <rv) {ret [index] = ret [lv]; ret [lv] = value; index = lv;} else {ret [index] = ret [rv]; ret [rv] = value; index = rv; }}} return ret;} public void insert (int value) {if (mLength> = mArray. length-1) {expandArray (mArray. length * 2);} mArray [++ mLength] = value; int index = mLength; int parentIndex = index/2; while (parentIndex> 0) {int currentValue = mArray [index]; int parentValue = mArray [parentIndex]; if (currentValue <parentValue) {mArray [parentIndex] = currentValue; mArray [index] = parentValue; index = parentIndex; parentIndex/= 2;} 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 leftIndex = index * 2; int rightIndex = leftIndex + 1; int value = mArray [index]; if (leftIndex> mLength) {// both are out of the break ;} else if (rightIndex> mLength) {// only int leftValue = mArray [leftIndex]; if (value> leftValue) {// change mArray [index] = leftValue; mArray [leftIndex] = value;} break;} else {// neither of them is out of bounds. The int leftValue = mArray [leftIndex] needs to be cyclically executed; int rightValue = mArray [rightIndex]; if (value <= leftValue & value <= rightValue) {// break;} else if (leftValue <rightValue) {// means: value> leftValuemArray [index] = leftValue; mArray [leftIndex] = value; index = leftIndex;} else {// rightValue> = leftValue, meaning: value> rightValuemArray [index] = rightValue; mArray [rightIndex] = value; index = rightIndex ;}}}}}}


Implements the binary heap data structure, including deletion and insertion operations, and graphical binary heap C ++

/* Tree. c -- tree support functions */
# Include <string. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include "tree. h"

/* Local data type */
Typedef struct pair {
Node * parent;
Node * child;
} Pair;

/* Protototypes for local functions */
Static Node * MakeNode (const Item * pi );
Static bool ToLeft (const Item * i1, const Item * i2 );
Static bool ToRight (const Item * i1, const Item * i2 );
Static void AddNode (Node * new_node, Node * root );
Static void InOrder (const Node * root, void (* pfun) (Item item ));
Static Pair SeekItem (const Item * pi, const Tree * ptree );
Static void DeleteNode (Node ** ptr );
Static void DeleteAllNodes (Node * ptr );

/* Function definitions */
Void InitializeTree (Tree * ptree)
{
Ptree-> root = NULL;
Ptree-> size = 0;
}

Bool TreeIsEmpty (const Tree * ptree)
{
If (ptree-> root = NULL)
Return true;
Else
Return false;
}

Bool TreeIsFull (const Tree * ptree)
{
If (ptree-> size = MAXITEMS)
Return true;
Else
Return false;
}

Int TreeItemCount (const Tree * ptree)
{
Return ptree-> size;
}

Bool AddItem (const Item * pi, Tree * ptree)
{
Node * new_node;

If (TreeIsFull (ptree ))
{
Fprintf (stderr, "Tree is full \ n ");
Return false;/* early return */
}
If (SeekItem (pi, ptree). child! = NULL)
{
Fprintf (stderr, "Attempted to add duplicate item & #9 ...... full text>

Implements the binary heap data structure, including deletion and insertion operations, and graphical binary heap C ++

/* Tree. c -- tree support functions */
# Include <string. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include "tree. h"

/* Local data type */
Typedef struct pair {
Node * parent;
Node * child;
} Pair;

/* Protototypes for local functions */
Static Node * MakeNode (const Item * pi );
Static bool ToLeft (const Item * i1, const Item * i2 );
Static bool ToRight (const Item * i1, const Item * i2 );
Static void AddNode (Node * new_node, Node * root );
Static void InOrder (const Node * root, void (* pfun) (Item item ));
Static Pair SeekItem (const Item * pi, const Tree * ptree );
Static void DeleteNode (Node ** ptr );
Static void DeleteAllNodes (Node * ptr );

/* Function definitions */
Void InitializeTree (Tree * ptree)
{
Ptree-> root = NULL;
Ptree-> size = 0;
}

Bool TreeIsEmpty (const Tree * ptree)
{
If (ptree-> root = NULL)
Return true;
Else
Return false;
}

Bool TreeIsFull (const Tree * ptree)
{
If (ptree-> size = MAXITEMS)
Return true;
Else
Return false;
}

Int TreeItemCount (const Tree * ptree)
{
Return ptree-> size;
}

Bool AddItem (const Item * pi, Tree * ptree)
{
Node * new_node;

If (TreeIsFull (ptree ))
{
Fprintf (stderr, "Tree is full \ n ");
Return false;/* early return */
}
If (SeekItem (pi, ptree). child! = NULL)
{
Fprintf (stderr, "Attempted to add duplicate item & #9 ...... full text>

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.