Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary Heaps

Rating
-
Sold
-
Pages
16
Uploaded on
18-08-2023
Written in
2023/2024

Heaps are specialized binary trees used to efficiently manage and retrieve elements with certain properties, often focusing on either the smallest or largest element. They are commonly used in priority queues and provide fast access to the most significant or least significant element based on a comparison criterion

Show more Read less
Institution
Course

Content preview

Heaps

,Heap Implementation :
public class Heap {
int[] arr = new int[100];
int size;

public Heap() {
arr[0] = -1;
size = 0;
}

public boolean isEmpty() {
return size == 0;
}

public int size() {
return size;
}

//---------------------------------------------------------------------

public void insert(int val) {
size = size + 1;
int index = size;
arr[index] = val;

while (index > 1) {
int parent = index / 2;

if (arr[parent] < arr[index]) {
int temp = arr[parent];
arr[parent] = arr[index];
arr[index] = temp;

index = parent;
}

else {
return;
}
}
}

//---------------------------------------------------------------------



Insert : compare child to its parent.
Delete : compare parent to its child.

, public void delete() {
if (size == 0) {
System.out.println("Heap is Empty");
return;
}

//Step 1 : Put last element into first index :
arr[1] = arr[size];

//Step 2 : Remove last element :
size--;

//Step 3 : Take root node to its correct position :
int i = 1;

while (i < size) {
int leftIndex = 2 * i;
int rightIndex = 2 * i + 1;

if (leftIndex < size && arr[i] < arr[leftIndex]) {
int temp = arr[i];
arr[i] = arr[leftIndex];
arr[leftIndex] = temp;
i = leftIndex;
}

else if (rightIndex < size && arr[i] < arr[rightIndex]) {
int temp = arr[i];
arr[i] = arr[rightIndex];
arr[rightIndex] = temp;
i = rightIndex;
}

else {
return;
}
}
}
//---------------------------------------------------------------------

public void printMaxHeap() {
for (int i = 1; i <= size; i++) {
System.out.print(arr[i] + " ");
}
}


--------------------------------------------------------------------------------------------------------------------------------------
Heapify : convert an array into Heap.

Written for

Institution
Course

Document information

Uploaded on
August 18, 2023
Number of pages
16
Written in
2023/2024
Type
SUMMARY

Subjects

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
shyamsundersharma

Get to know the seller

Seller avatar
shyamsundersharma Sanskar public school
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
14
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions