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 months 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

Developers will be able to apply for Vision Pro dev kits
Softwares

Developers will be able to apply for Vision Pro dev kits

by admin
June 6, 2023
High traffic web app optimization – stress testing, refactoring and more
Softwares

High traffic web app optimization – stress testing, refactoring and more

by admin
June 5, 2023
Java Arrays | Developer.com
Softwares

Java Arrays | Developer.com

by admin
June 3, 2023
12 ways to make Vivaldi your custom browser
Softwares

12 ways to make Vivaldi your custom browser

by admin
June 2, 2023
How to Create a Screen Recording with Quicktime
Softwares

How to Create a Screen Recording with Quicktime

by admin
June 1, 2023
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

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Trending
  • Comments
  • Latest
Smart charging can save electric vehicle drivers £110 a year and cut carbon footprint by 20%

Smart charging can save electric vehicle drivers £110 a year and cut carbon footprint by 20%

January 4, 2022
ESPN SHOCKER: Rumors That Jalen Rose’s Wife Molly NOW Dating Stephen A Smith!!

ESPN SHOCKER: Rumors That Jalen Rose’s Wife Molly NOW Dating Stephen A Smith!!

December 10, 2021
Discord Is Inviting an OpenAI-Powered Chatbot to Its Servers

Discord Is Inviting an OpenAI-Powered Chatbot to Its Servers

March 9, 2023
Facebook Eases Social Issues Ads Policy to Allow Product-Focused Ads to Run Without a Disclaimer

Facebook Eases Social Issues Ads Policy to Allow Product-Focused Ads to Run Without a Disclaimer

November 20, 2021
Zelda Williams to make directing debut with ‘bonkers’ zombie comedy Lisa Frankenstein

Zelda Williams to make directing debut with ‘bonkers’ zombie comedy Lisa Frankenstein

June 30, 2022
The Best Linen Shirts For Men: Summer 2021 Edition

The Best Linen Shirts For Men: Summer 2021 Edition

July 13, 2021
Nikki Sixx Recalls Getting Fired From Band By Blackie Lawless

Nikki Sixx Recalls Getting Fired From Band By Blackie Lawless

October 21, 2021
Rest Evercool Cooling Comforter review – making sleeping cool again

Rest Evercool Cooling Comforter review – making sleeping cool again

April 15, 2023
Amazon Orders ‘Angry Birds Mystery Island’ Animated Series – Deadline

Amazon Orders ‘Angry Birds Mystery Island’ Animated Series – Deadline

June 6, 2023
Idaho Murder Prosecutors Issue Telling New Bryan Kohberger Search Warrants!

Idaho Murder Prosecutors Issue Telling New Bryan Kohberger Search Warrants!

June 6, 2023
The Rise of Women in Technology Leadership: Empowering Influence and Driving Progress

The Rise of Women in Technology Leadership: Empowering Influence and Driving Progress

June 6, 2023
Hear Craig Owens transform a chaotic Chiodos song into a ballad

Hear Craig Owens transform a chaotic Chiodos song into a ballad

June 6, 2023
Say Goodbye to Language Barriers with M9 Instant Voice Translator

Say Goodbye to Language Barriers with M9 Instant Voice Translator

June 6, 2023
Developers will be able to apply for Vision Pro dev kits

Developers will be able to apply for Vision Pro dev kits

June 6, 2023
7 Steps to Create Engaging Twitter Ads [Infographic]

7 Steps to Create Engaging Twitter Ads [Infographic]

June 6, 2023
Cardi B Responds to Rumor She Shaded Ice Spice at 2023 Summer Jam

Cardi B Responds to Rumor She Shaded Ice Spice at 2023 Summer Jam

June 5, 2023
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

  • Amazon Orders ‘Angry Birds Mystery Island’ Animated Series – Deadline
  • Idaho Murder Prosecutors Issue Telling New Bryan Kohberger Search Warrants!
  • The Rise of Women in Technology Leadership: Empowering Influence and Driving Progress
  • 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.

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.