Heap-Large heap algorithm __ algorithm

Source: Internet
Author: User

The heap is a special two-forked tree that has the following characteristics:
The key value of the parent node is always greater than or equal to (less than or equal to) the key value of any of the child nodes.
And the Saozi right subtree of each node is a binary heap (both the largest heap or the smallest heap).
The maximum heap when the key value of a parent node is always greater than or equal to the key value of any one child node.
The minimum heap when the key value of a parent node is always less than or equal to the key value of any one child node.
The heap is typically stored using arrays.

The advantages of using an array storage heap:

The parent-child relationship is simple and clear:
Storing a[i] is the element of node I.
A[2i+1] and A[2 (i+1)] are the nodes of the left and right children of node I, respectively.
A[I/2] is the element of the parent node of node I.

Large heap algorithm: a schematic of inserting a new node

Large Heap algorithm: Delete the maximum node diagram

Import Java.io.InputStreamReader;
Import Java.io.BufferedReader;

/**
* @Class Priorityqueue
* @Description Heap is actually a binary tree, the condition that needs to be satisfied is that each node's key value is greater than or equal to the node's child node key value
* Its relative binary sort tree is subordinate to the weak sequence. Because the right node key value is not required to be greater than the left key value. But using large stacks to implement priority queues
* Quickly remove the largest keyword and insert a new keyword is convenient. This example is an array based algorithm for large stacks
* @Company OpenData
* @Author chenlly
* @Version 1.0
* @Data 09-04-3
*/

Class Node {
private int idata;

Public Node (int idata) {
This.idata = idata;
}

public int Getidata () {
return idata;
}

public void Setidata (int data) {
idata = data;
}
}

Class Heap {
Private node[] Heapnode;
private int max_size;
private int current_index;

public Heap (int size) {
max_size = size;
Current_index = 0;
Heapnode = new Node[size];
}

The insertion is the process of brushing up the selection. When you insert a node on the last node of the tree, you need to adjust the overall structure of the heap to conform to the conditions of the large heap. Brush up the selection.
public boolean Insert (node node) {
if (Current_index = = max_size) {
return false;
} else {
Heapnode[current_index] = node;
Trickleup (Current_index);
current_index++;
return true;
}
}

The

 //deletion is the process of brushing down the selection. When the maximum key value (root) is removed and the last value is placed in the root position, the entire structure of the heap needs to be adjusted to match the conditions of the large heap. Brush down the selection.
 public Node Remove () {
  node root = heapnode[0];
  if (current_index = 0) {
& Nbsp;  return null;
  }
  //The last node value to the root node
  heapnode[0] = Heapnode[--current_index];
  //brush down
  trickledown (0);
  return Root;
 }

 //modifies the key value of the node, and then adjusts the overall structure of the heap to conform to the conditions of the large heap
 public boolean change (int index, int newvalue) {
   if (Index < 0 | | | index > max_size) {
   return false;
  }
  int Oldva Lue = Heapnode[index].getidata ();
  heapnode[index].setidata (NewValue);
  //adjusts the entire structure of the heap to conform to the basic requirements of a large heap
  if (OldValue < NewValue) {
   trickleup ( index);
  } else {
   trickledown (index);
  
  return true;
 .}

 //displays data in the large heap
 public void Displayheap () {
  system.out.print ("Heaparray:");
  for (int i = 0; i < heapnode.length; i++) {
   if (Heapnode[i]!= null) {
 &nbs P;  system.out.print (Heapnode[i].getidata () + ",");
   } else
    break
  
   System.out.println ("");
 }

Node up brush selection method, index as a "hole" in the process of brush selection
public void Trickleup (int index) {
int parent = (index-1)/2;//formula find the parent node subscript for the current node
Temporary node Bootom Save the node value you want to insert
Node bottom = Heapnode[index];
Continues looping when the root node is not yet present, and the key value of the current node is greater than the key value of the parent node
while (Index > 0 && (Heapnode[parent].getidata () < Bottom.getidata ())) {
Heapnode[index] = heapnode[parent];
index = parent;
Parent = (index-1)/2;
}
Heapnode[index] = bottom;
}

Node downward brush selection method, index as a "hole" in the process of brush selection
public void Trickledown (int index) {
The formula index = CURRENT_INDEX/2 indicates the index to the leaf node.
int largerchild;
Node top = Heapnode[index];
while (Index < CURRENT_INDEX/2) {
int leftchild = 2 * index + 1;
int rightchild = leftchild + 1;
If the right node exists and the node is greater than the left node. The right node is the largest node
if (Rightchild < Current_index
&& heapnode[rightchild].getidata () > Heapnode[leftchild]
. Getidata ()) {
Largerchild = Rightchild;
} else {
Largerchild = Leftchild;
}
Found such a node smaller than the byte
if (Top.getidata () >= heapnode[largerchild].getidata ()) {
Break
} else {
Heapnode[index] = Heapnode[largerchild];
index = Largerchild;
}
}//End While
Heapnode[index] = top;
}

}

public class Priorityqueue {
public static String Readstr () {
InputStreamReader ir = new InputStreamReader (system.in);
BufferedReader br = new BufferedReader (IR);
String str = "";
try {
str = Br.readline ();
catch (Exception ex) {
Ex.printstacktrace ();
}
return str;
}

public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN ("Please select Action sequence");
System.out.println ("1 insert new node into large heap");
System.out.println ("2 removes the largest node (root node) in a large heap");
System.out.println ("3 Change the key value of the specified subscript node");
System.out.println ("4 Show Large Data Items");
System.out.println ("5 exit system");
System.out.println ("Please enter heap size:");
Boolean success = false;
int size = Integer.parseint (Priorityqueue.readstr ());
Heap Heap = new Heap (size);
while (true) {
System.out.println ("Please enter the sequence of operations");
int op = integer.parseint (Priorityqueue.readstr ());
Switch (OP) {
Case 1:
System.out.println ("Enter the data item you want to insert");
int key = Integer.parseint (Priorityqueue.readstr ());
Node node = new node (key);
Success = Heap.insert (node);
if (success) {
System.out.println ("Insert data to heap success!");
} else {
System.out.println ("Index out of");
}
Break
Case 2:
Node Rmnode = Heap.remove ();
if (Rmnode!= null) {
SYSTEM.OUT.PRINTLN ("The Data" + rmnode.getidata ()
+ "is remove");
} else {
System.out.println ("Index out of");
}
Break
Case 3:
System.out.println ("Please enter a large stack of subscript to be modified");
int index = Integer.parseint (Priorityqueue.readstr ());
System.out.println ("Please enter a new data item");
int newvalue = Integer.parseint (Priorityqueue.readstr ());
Success = Heap.change (index, newvalue);
if (success) {
SYSTEM.OUT.PRINTLN ("Change is success");
} else {
System.out.println ("Index out of");
}
Break
Case 4:
Heap.displayheap ();
Break
Case 5:
Default
System.exit (0);
}//End Switch
}//End While
}//End Main
}

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.