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

How to Add Custom Style Variations to WordPress Blocks — Speckyboy
Softwares

How to Add Custom Style Variations to WordPress Blocks — Speckyboy

by admin
June 2, 2025
Smart software replaces expensive sensors for glass wall detection with 96% accuracy
Softwares

Smart software replaces expensive sensors for glass wall detection with 96% accuracy

by admin
June 1, 2025
User Guide For UnoPim PDF Generator
Softwares

User Guide For UnoPim PDF Generator

by admin
May 31, 2025
Infragistics Ultimate 25.1 includes updates across several of its UI toolkit components
Softwares

Infragistics Ultimate 25.1 includes updates across several of its UI toolkit components

by admin
May 29, 2025
Quick exit from Settings – Vivaldi Android Browser snapshot 3708.4
Softwares

Quick exit from Settings – Vivaldi Android Browser snapshot 3708.4

by admin
June 3, 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
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
How to Build a JavaScript Search [Article]

How to Build a JavaScript Search [Article]

August 30, 2022
Product Information Management Trends (PIM)

Product Information Management Trends (PIM)

February 4, 2022
How to Build a DIY Spotify Music Player with Raspberry Pi Pico

How to Build a DIY Spotify Music Player with Raspberry Pi Pico

May 13, 2025
15 Best Movies Like Parasite

15 Best Movies Like Parasite

February 20, 2022
Every Kathryn Hahn Film Performance, Ranked

Every Kathryn Hahn Film Performance, Ranked

December 24, 2022
What is Kubernetes: An Overview

An Introduction to Kubernetes | Developer.com

August 11, 2022
Deployment Diagrams Explained in Detail, With Examples

Deployment Diagrams Explained in Detail, With Examples

August 11, 2021
Major Labels Shift Gears, Explore Licensing Deals With Controversial AI Platforms Suno and Udio

Major Labels Shift Gears, Explore Licensing Deals With Controversial AI Platforms Suno and Udio

June 3, 2025
Samsung Galaxy Z Fold 7 Rumored Features and Design Updates

Samsung Galaxy Z Fold 7 Rumored Features and Design Updates

June 3, 2025
Ellen Pompeo Detained By TSA, Bomb Squad Called In

Ellen Pompeo Detained By TSA, Bomb Squad Called In

June 3, 2025
X Rolls Out Support for 4K Video Uploads

X Continues To Highlight Misleading Claims About Its Popularity

June 3, 2025
May 30-June 1 Box Office Recap – ‘Lilo & Stitch’ crosses $600M worldwide, while ‘Mission: Impossible – The Final Reckoning’ crosses $350M worldwide. ‘Karate Kid: Legends’ and ‘Bring Her Back’ have solid debuts, while ‘The Phoenician Scheme’ opens with the best per-theater average ($93K) of the year.

May 30-June 1 Box Office Recap – ‘Lilo & Stitch’ crosses $600M worldwide, while ‘Mission: Impossible – The Final Reckoning’ crosses $350M worldwide. ‘Karate Kid: Legends’ and ‘Bring Her Back’ have solid debuts, while ‘The Phoenician Scheme’ opens with the best per-theater average ($93K) of the year.

June 3, 2025
Jonathan Joss, ‘King of the Hill’ actor, killed in Texas shooting – National

Jonathan Joss, ‘King of the Hill’ actor, killed in Texas shooting – National

June 3, 2025
What Is the Best Brand of Workout Clothes? (2025 Guide)

What Is the Best Brand of Workout Clothes? (2025 Guide)

June 3, 2025
Samsung may incorporate Perplexity’s AI tech in its phones

Samsung may incorporate Perplexity’s AI tech in its phones

June 2, 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

  • Major Labels Shift Gears, Explore Licensing Deals With Controversial AI Platforms Suno and Udio
  • Samsung Galaxy Z Fold 7 Rumored Features and Design Updates
  • Ellen Pompeo Detained By TSA, Bomb Squad Called In
  • 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.

slot pronunciation