对象已移动

可在此处找到该文档 Max Heap in JavaScript – GeeksforGeeks – New Self New Life
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

Google to mandate verification for all Android app developers
Softwares

Google to mandate verification for all Android app developers

by admin
August 26, 2025
Further Tab Button work and Chromium 140 – Vivaldi Browser snapshot 3787.3
Softwares

Further Tab Button work and Chromium 140 – Vivaldi Browser snapshot 3787.3

by admin
August 23, 2025
Find a Software Development Company in Europe
Softwares

Find a Software Development Company in Europe

by admin
August 24, 2025
Xero Salesforce Integration – The Definitive Guide
Softwares

Xero Salesforce Integration – The Definitive Guide

by admin
August 20, 2025
BrowserStack launches Chrome extension that bundles 10+ manual web testing tools
Softwares

BrowserStack launches Chrome extension that bundles 10+ manual web testing tools

by admin
August 18, 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
10 Best Netflix Original Thriller Shows, Ranked

10 Best Netflix Original Thriller Shows, Ranked

June 22, 2025
Lil Nas X hospitalized in Los Angeles for ‘possible overdose,’ say reports – National

Lil Nas X hospitalized in Los Angeles for ‘possible overdose,’ say reports – National

August 22, 2025
Taylor Swift, Travis Kelce engaged as pop star reveals 1st glimpse of ring – National

Taylor Swift, Travis Kelce engaged as pop star reveals 1st glimpse of ring – National

August 27, 2025
Best AI Webcams For Video Calls In 2025

Best AI Webcams For Video Calls In 2025

August 21, 2025
Itch.io starts reindexing free NSFW content

Itch.io starts reindexing free NSFW content

August 1, 2025
Google to mandate verification for all Android app developers

Google to mandate verification for all Android app developers

August 26, 2025
20+ Best Titles Templates for DaVinci Resolve in 2025 — Speckyboy

20+ Best Titles Templates for DaVinci Resolve in 2025 — Speckyboy

August 22, 2025
Offset Blames Cooking and More for Failed Marriage to Cardi B

Offset Blames Cooking and More for Failed Marriage to Cardi B

August 23, 2025
Kiwi Ears Altruva Hi-Fi 50mm Dynamic Driver Headphones review

Kiwi Ears Altruva Hi-Fi 50mm Dynamic Driver Headphones review

August 27, 2025
Taylor Swift’s £18,000 engagement jewellery you might have missed

Taylor Swift’s £18,000 engagement jewellery you might have missed

August 27, 2025
All The Details On Taylor Swift’s Engagement Ring!!!

All The Details On Taylor Swift’s Engagement Ring!!!

August 27, 2025
Reddit Adds Trend Insights on Mobile, Guidebook for Brands

Reddit Adds Trend Insights on Mobile, Guidebook for Brands

August 27, 2025
Taylor Swift, Travis Kelce engaged as pop star reveals 1st glimpse of ring – National

Taylor Swift, Travis Kelce engaged as pop star reveals 1st glimpse of ring – National

August 27, 2025
Morgan Wallen Fined After Gillette Stadium Show Breaks Curfew

Morgan Wallen Fined After Gillette Stadium Show Breaks Curfew

August 26, 2025
Tatiana Zagorovski Sounds the Alarm with a New Book About How to Avoid Real Estate Scams

Tatiana Zagorovski Sounds the Alarm with a New Book About How to Avoid Real Estate Scams

August 26, 2025
Google to mandate verification for all Android app developers

Google to mandate verification for all Android app developers

August 26, 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

  • Kiwi Ears Altruva Hi-Fi 50mm Dynamic Driver Headphones review
  • Taylor Swift’s £18,000 engagement jewellery you might have missed
  • All The Details On Taylor Swift’s Engagement Ring!!!
  • 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.

New Self New Life