对象已移动

可在此处找到该文档 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

Warp 2.0 evolves its terminal experience into an Agentic Development Environment
Softwares

Warp 2.0 evolves its terminal experience into an Agentic Development Environment

by admin
June 25, 2025
Huawei Cloud rolls out Pangu Models 5.5 to cover more industries
Softwares

Huawei Cloud rolls out Pangu Models 5.5 to cover more industries

by admin
June 24, 2025
Minor update(4) for Vivaldi Android Browser 7.4
Softwares

Minor update(4) for Vivaldi Android Browser 7.4

by admin
June 21, 2025
How AI Medical Coding Software Reduces Errors & Accelerates Billing in 2025
Softwares

How AI Medical Coding Software Reduces Errors & Accelerates Billing in 2025

by admin
June 22, 2025
10+ Best Free Portfolio & Lookbook Templates for InDesign in 2025 — Speckyboy
Softwares

10+ Best Free Portfolio & Lookbook Templates for InDesign in 2025 — Speckyboy

by admin
June 20, 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
Pamela Anderson raves about new natural, makeup-free look: ‘It’s freedom’

Pamela Anderson raves about new natural, makeup-free look: ‘It’s freedom’

October 8, 2023
I Tried Calocurb For 90 Days. Here’s My Review.

I Tried Calocurb For 90 Days. Here’s My Review.

January 8, 2025
Alec Baldwin indicted again for ‘Rust’ shooting that left cinematographer dead – National

Alec Baldwin indicted again for ‘Rust’ shooting that left cinematographer dead – National

January 21, 2024
Bones: All Of Brennan’s Interns, Ranked

Bones: All Of Brennan’s Interns, Ranked

June 15, 2021
A look into CAMPUS, ShopBack’s new Singapore HQ at Pasir Panjang

A look into CAMPUS, ShopBack’s new Singapore HQ at Pasir Panjang

July 2, 2022
Guide for Bagisto Quick Commerce

Guide for Bagisto Quick Commerce

October 16, 2024
User Manual for Odoo Docx Report Builder

User Manual for Odoo Docx Report Builder

November 30, 2024
Aaron Rodgers returns to ‘Pat McAfee Show’ 1 day after being axed by host – National

Aaron Rodgers returns to ‘Pat McAfee Show’ 1 day after being axed by host – National

January 11, 2024
13 Wallflowers Classic Rock Collaborations

13 Wallflowers Classic Rock Collaborations

June 26, 2025
AJ, Britney’s Off-Camera Conversation Revealed

AJ, Britney’s Off-Camera Conversation Revealed

June 26, 2025
Thursday’s Workwear Report: Della Pleated-Waist Shell Top

Thursday’s Workwear Report: Della Pleated-Waist Shell Top

June 26, 2025
Well-Endowed 'Freak Off' Regular Describes How Diddy 'Orchestrated' Everything, The Pay Scheme, & More

Well-Endowed 'Freak Off' Regular Describes How Diddy 'Orchestrated' Everything, The Pay Scheme, & More

June 26, 2025
Meta Says It Fixed an Issue That Led To Erroneous Facebook Group Suspensions

Meta Says It Fixed an Issue That Led To Erroneous Facebook Group Suspensions

June 26, 2025
Bradley Cooper Is Being Teased For These Awkward Photos

Bradley Cooper Is Being Teased For These Awkward Photos

June 26, 2025
Warp 2.0 evolves its terminal experience into an Agentic Development Environment

Warp 2.0 evolves its terminal experience into an Agentic Development Environment

June 25, 2025
Cybersight Guardian HUD Glasses review – wearable tech that helps keep you safe on the road

Cybersight Guardian HUD Glasses review – wearable tech that helps keep you safe on the road

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

  • 13 Wallflowers Classic Rock Collaborations
  • AJ, Britney’s Off-Camera Conversation Revealed
  • Thursday’s Workwear Report: Della Pleated-Waist Shell Top
  • 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