The JavaScript implementation method of the detailed heap _javascript skills

Source: Internet
Author: User
Tags data structures see definition javascript array

Definition of a heap

The maximum (minimum) heap is a tree of key values for each node whose key value is not less than (greater than) its child (if present). The Big top pile is a completely binary tree, but also a tree. The small top heap is a completely binary tree, and it is also a minimum tree.

In addition, remembering these two concepts is too important to write code:

1. Relationship between parent node and child node: see definition

2, complete binary tree: reference [2]

Basic operations

1, build (building heap)

2, insert (inserted)

3, delete (deletion: the smallest or the largest)

Code implementation

First, there are two very important points before you write code:

1, with an array can be used as a heap of storage structure, very simple and easy to operate;

2, and also because the array as the storage structure, so the relationship between the parent-child node can be easily indexed to find each other.

For JavaScript to start with 0 as an array index, the relationship is as follows:

Nleftindex = 2 * (nfatherindex+1)-1;
Nrightindex = 2* (nfatherindex+1);

The previous mention of two concepts is helpful to understand:

1, because it is an array, so the relationship between the parent-child node does not need a special structure to maintain, the index can be calculated between the results, save a lot of trouble. If it is a linked list structure, it will be much more complicated;

2, the concept of a complete binary tree can refer to [2], requiring the leaf node to fill from left to right to start filling the next layer, which ensures that no large array of the group to move the whole. This is also a random storage structure (array) of the short board: After the deletion of an element, the overall move forward is more time-consuming. This feature also causes the heap to add the last leaf node to the root node when the element is deleted.

Code implementation:

/****************************************************** * File: Heap * Author: "Page" * Time: "2016/11/02" **************

/function Heap () {this.data = [];}

Heap.prototype.print = function () {Console.log ("Heap:" + this.data);}
 Heap.prototype.build = function (data) {//initialization this.data = [];

 if (!data instanceof Array) return false;
 Into the heap for (var i = 0; i < data.length ++i) {This.insert (data[i));
return true;
 } Heap.prototype.insert = function (nvalue) {if (!this.data instanceof Array) {this.data = [];
 } this.data.push (Nvalue);
 Update new node var nindex = this.data.length-1;
 var nfatherindex = Math.floor ((nIndex-1)/2);
 while (Nfatherindex > 0) {if (This.data[nindex] < This.data[nfatherindex]) {var temp = This.data[nindex];
 This.data[nindex] = This.data[nfatherindex];
 This.data[nfatherindex] = temp;
 } nindex = Nfatherindex;
 Nfatherindex = Math.floor ((nIndex-1)/2); }} Heap.prototype.delete = FunctiOn () {if (!this.data instanceof Array) {return null;
 } var nindex = 0;
 var nvalue = This.data[nindex];
 var nmaxindex = this.data.length-1;
 Update new node var nleaf = This.data.pop ();

 This.data[nindex] = nleaf;
 while (Nindex < Nmaxindex) {var nleftindex = 2 * (nindex+1)-1;

 var nrightindex = 2 * (nindex+1);
 Find the smallest child node (Nleftindex < Nrightindex) var nselectindex = Nleftindex; if (Nrightindex < Nmaxindex) {Nselectindex = (This.data[nleftindex] > This.data[nrightindex])? nrightindex:nlef
 Tindex; } if (Nselectindex < Nmaxindex && This.data[nindex] > This.data[nselectindex]) {var temp = This.data[nin
 DEX];
 This.data[nindex] = This.data[nselectindex];
 This.data[nselectindex] = temp;
 } nindex = Nselectindex;
return nvalue;
//Test var heap = new Heap ();
Heap.build ([1, 3, 5, 11, 4, 6, 7, 12, 15, 10, 9, 8]);
Heap.print ();
Insert Heap.insert (2);
Heap.print ();
Delete Heap.delete (); Heap.print ();

A few summary of JavaScript

Here is the adoption of object-oriented approach, not feeling too elegant, do not know whether there is a better way to express and writing;

I learned several uses of arrays: Push and pop are easy to use;

The way to judge the array is also temporarily from the Internet search (instanceof), the impression is not deep, do not use the next estimate or may be forgotten.

Reference

[1] Data structure and algorithm analysis: C language Description

[2] Graphic data structure (8)--Two fork heap

[3]> Data structure: Heap

Summarize

The JavaScript array implements push and pop operations, and many other languages provide similar data structures and operations (such as the vector of C + +) while also supporting random operations. So, I began to think that if these simple structure and the concept of automatic sorting, then a heap easy to do, and then see the C + + STL Make_heap know that they know too little, but also happy their way of thinking is right. JavaScript is not to check, I would like to have or realize it is very easy;
After the realization of their own, found that the structure is also very simple, as long as you are willing to close contact with it once you can;

The details of JavaScript are still not well understood, such as the use of the array of data to be used again to use, for the soul of JavaScript or not touch, the essence of the need for continuous learning and practice;

This code, as long as you go to understand the concept, understand the basis of programming, you can write out. However, the code can also write more concise, such as the delete function to find the smallest child nodes, the left and right node index does not need to be compared, is definitely the left side of the small. Part of the code feels like it can continue to be optimized and streamlined.

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, if there is doubt you can message exchange, thank you for the cloud Habitat Community support.

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.