New Self New Life
No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
New Self New Life
No Result
View All Result
Home Softwares

Max Heap in JavaScript – GeeksforGeeks

by admin
2 years ago
in Softwares
Best Coding Practices For Rest API Design
Share on FacebookShare on Twitter


class MaxHeap {

 constructor() {

     this.heap = [];

 }

  

 

 getLeftChildIndex(parentIndex) { return 2 * parentIndex + 1; }

 getRightChildIndex(parentIndex) { return 2 * parentIndex + 2; }

  

 getParentIndex(childIndex) {

     return Math.ground((childIndex - 1) / 2);

 }

  

 hasLeftChild(index) {

     return this.getLeftChildIndex(index) < this.heap.size;

 }

  

 hasRightChild(index) {

     return this.getRightChildIndex(index) < this.heap.size;

 }

  

 hasParent(index) {

     return this.getParentIndex(index) >= 0;

 }

  

 leftChild(index) {

     return this.heap[this.getLeftChildIndex(index)];

 }

  

 rightChild(index) {

     return this.heap[this.getRightChildIndex(index)];

 }

  

 mum or dad(index) {

     return this.heap[this.getParentIndex(index)];

 }

  

 swap(indexOne, indexTwo) {

     const temp = this.heap[indexOne];

     this.heap[indexOne] = this.heap[indexTwo];

     this.heap[indexTwo] = temp;

 }

  

 peek() {

     if (this.heap.size === 0) {

         return null;

     }

     return this.heap[0];

 }

   

 

 

 

 take away() {

     if (this.heap.size === 0) {

         return null;

     }

     const merchandise = this.heap[0];

     this.heap[0] = this.heap[this.heap.length - 1];

     this.heap.pop();

     this.heapifyDown();

     return merchandise;

 }

  

 add(merchandise) {

     this.heap.push(merchandise);

     this.heapifyUp();

 }

  

 heapifyUp() {

     let index = this.heap.size - 1;

     whereas (this.hasParent(index) && this.mum or dad(index) < this.heap[index]) {

         this.swap(this.getParentIndex(index), index);

         index = this.getParentIndex(index);

     }

 }

  

 heapifyDown() {

     let index = 0;

     whereas (this.hasLeftChild(index)) {

         let largerChildIndex = this.getLeftChildIndex(index);

         if (this.hasRightChild(index) && this.rightChild(index) > this.leftChild(index)) {

             largerChildIndex = this.getRightChildIndex(index);

         }

         if (this.heap[index] > this.heap[largerChildIndex]) {

             break;

         } else {

             this.swap(index, largerChildIndex);

         }

         index = largerChildIndex;

     }

 }

   

 printHeap() {

     var heap =` ${this.heap[0]} `

     for(var i = 1; i<this.heap.size;i++) {

         heap += ` ${this.heap[i]} `;

     }

     console.log(heap);

 }

}

  

var heap = new MaxHeap();

  

heap.add(10);

heap.add(15);

heap.add(30);

heap.add(40);

heap.add(50);

heap.add(100);

heap.add(40);

  

heap.printHeap();

  

console.log(heap.peek());

console.log(heap.take away());

  

heap.printHeap();



Source link

Tags: GeeksforGeeksHeapJavaScriptMax
Previous Post

S’pore startup Take App builds app to simplify WhatsApp ordering

Next Post

What Is a Divorce From Bed and Board?

Related Posts

Unlocking the Future of Finance
Softwares

Unlocking the Future of Finance

by admin
May 8, 2025
Address bar tweaks – Vivaldi Browser snapshot 3683.4
Softwares

Address bar tweaks – Vivaldi Browser snapshot 3683.4

by admin
May 7, 2025
A faster, sleeker JavaScript experience
Softwares

A faster, sleeker JavaScript experience

by admin
May 10, 2025
How WordPress Agencies Can Improve Site Building Efficiency — Speckyboy
Softwares

How WordPress Agencies Can Improve Site Building Efficiency — Speckyboy

by admin
May 6, 2025
Grand Theft Auto VI delayed again, this time until May 2026
Softwares

Grand Theft Auto VI delayed again, this time until May 2026

by admin
May 5, 2025
Next Post
What Is a Divorce From Bed and Board?

What Is a Divorce From Bed and Board?

Copilot X heralds a new era of AI-powered coding

Copilot X heralds a new era of AI-powered coding

  • Trending
  • Comments
  • Latest
Cameron Monaghan Discusses Erotic Thriller

Cameron Monaghan Discusses Erotic Thriller

January 13, 2022
Doctor Strange: 12 Best Comic Issues Of The 1990s

Doctor Strange: 12 Best Comic Issues Of The 1990s

December 11, 2021
Guide for Odoo Website Razorpay Checkout Payment Acquirer

Guide for Odoo Website Razorpay Checkout Payment Acquirer

January 6, 2023
Phantom Parade Gets Opening Movie, Cast Announced

Phantom Parade Gets Opening Movie, Cast Announced

March 8, 2022
Anant Ambani wedding: Celebs, wealthy elite attend lavish billionaire festivities – National

Anant Ambani wedding: Celebs, wealthy elite attend lavish billionaire festivities – National

March 1, 2024
Adobe commerce module seller invitation

Adobe commerce module seller invitation

April 13, 2022
Best Cryptocurrency Wallets Comparison | SCAND Blog

Best Cryptocurrency Wallets Comparison | SCAND Blog

July 30, 2022
UGREEN 65W USB Charger and Power Adapter review 

UGREEN 65W USB Charger and Power Adapter review 

January 25, 2023
Study Uncovers the One Thing That Cuts Through Climate Apathy: Loss

Study Uncovers the One Thing That Cuts Through Climate Apathy: Loss

May 10, 2025
Millennium Docs Against Gravity Expands Industry Program

Millennium Docs Against Gravity Expands Industry Program

May 10, 2025
Billy Ray Cyrus shares rare photo with daughter Miley amid rumoured family rift

Billy Ray Cyrus shares rare photo with daughter Miley amid rumoured family rift

May 10, 2025
Galantis Is Throwing a Midsommar-Themed Concert at Red Rocks

Galantis Is Throwing a Midsommar-Themed Concert at Red Rocks

May 10, 2025
Startups, Stop Chasing Only Clicks: Why Brand Building (and Out-Of-Home Ads) Deserves Your Attention from Day One

Startups, Stop Chasing Only Clicks: Why Brand Building (and Out-Of-Home Ads) Deserves Your Attention from Day One

May 10, 2025
Bill Gates says he will give away almost all of his money over next 20 years

Bill Gates says he will give away almost all of his money over next 20 years

May 9, 2025
Here’s How You Can Win a $500 Prepaid Visa Gift Card

Here’s How You Can Win a $500 Prepaid Visa Gift Card

May 9, 2025
Ben Affleck Embraces Latina Love Post-Jennifer Lopez Divorce

Ben Affleck Embraces Latina Love Post-Jennifer Lopez Divorce

May 9, 2025
New Self New Life

Your source for entertainment news, celebrities, celebrity news, and Music, Cinema, Digital Lifestyle and Social Media and More !

Categories

  • Celebrity
  • Cinema
  • Devices
  • Digital Lifestyle
  • Entertainment
  • Music
  • Social Media
  • Softwares
  • Uncategorized

Recent Posts

  • Study Uncovers the One Thing That Cuts Through Climate Apathy: Loss
  • Millennium Docs Against Gravity Expands Industry Program
  • Billy Ray Cyrus shares rare photo with daughter Miley amid rumoured family rift
  • Home
  • Disclaimer
  • DMCA
  • Privacy Policy
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites. slotsfree  creator solana token

No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites.

ho yeah slots