Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
a23e2a2
added inorder traversal
Anusha-3113 Nov 16, 2023
b0a2555
adding inorder traversal file
Anusha-3113 Nov 16, 2023
54faf55
Update inorder.py
Anusha-3113 Nov 18, 2023
4d1a5b6
Merge pull request #26 from venkys-io/Anusha-3113-patch-1
Anusha-3113 Nov 18, 2023
fbf83de
Delete inorder traversal/BinaryTree.class
Anusha-3113 Nov 18, 2023
e35c1e3
Delete inorder traversal/TreeNode.class
Anusha-3113 Nov 18, 2023
204d5ed
Add files via upload
Anusha-3113 Nov 19, 2023
8a5a628
Merge branch 'main' into Anusha-blog1
Anusha-3113 Nov 19, 2023
22f1358
Delete inorder traversal
Anusha-3113 Nov 19, 2023
cc09779
Delete inorder traversal directory
Anusha-3113 Nov 19, 2023
8e08c33
Add files via upload
Anusha-3113 Nov 19, 2023
3362f38
Delete NQueen.md
Anusha-3113 Dec 4, 2023
5d41ee2
Add files via upload
Anusha-3113 Dec 4, 2023
5a3ca9c
Delete Strings/Medium/palindrompartition
Anusha-3113 Dec 4, 2023
fdc9fad
Delete Strings/Medium/PalindromePartitioning.java,Readme.md,palindrom…
Anusha-3113 Dec 4, 2023
e38410f
Delete Strings/Medium/Readme.md
Anusha-3113 Dec 4, 2023
a3c5e4b
Delete Strings/Medium/palindromepartition.py
Anusha-3113 Dec 4, 2023
7a02415
added palindrome partitioning
Anusha-3113 Dec 4, 2023
1e51215
Update Readme.md
Anusha-3113 Dec 7, 2023
362cbd0
Update Readme.md
Anusha-3113 Dec 7, 2023
4fa2f14
Update Readme.md
Anusha-3113 Dec 7, 2023
17d1f3c
Update Readme.md
Anusha-3113 Dec 7, 2023
cffbff3
added max heap CRUD
Anusha-3113 Dec 11, 2023
61d7f9f
Merge pull request #76 from venkys-io/Anusha-3113-patch-2
Anusha-3113 Dec 11, 2023
9c5deef
Delete Heaps/Hard directory
Anusha-3113 Dec 11, 2023
9b9f2c2
Add files via upload
Anusha-3113 Dec 11, 2023
5cabcd3
Update Readme.md
Anusha-3113 Dec 11, 2023
fe1aa6d
Delete Trees/Easy/Inorder Traversal directory
Anusha-3113 Dec 15, 2023
ae4fd7d
Delete Strings directory
Anusha-3113 Dec 15, 2023
a460e8f
Delete Heaps/Hard directory
Anusha-3113 Dec 15, 2023
0f35d08
add inorder traversal
Anusha-3113 Dec 15, 2023
ff5001d
Add files via upload
Anusha-3113 Dec 16, 2023
80133b0
Add files via upload
Anusha-3113 Dec 16, 2023
57eab02
Update palindromepartition.py
Anusha-3113 Dec 17, 2023
cd2c443
Update Readme.md
Anusha-3113 Dec 17, 2023
65c0dd9
Update Readme.md
Anusha-3113 Dec 17, 2023
22e17eb
Update Readme.md
Anusha-3113 Dec 17, 2023
c9072f4
Update Readme.md
Anusha-3113 Dec 17, 2023
d614702
Update max_heap_CRUD.c++
Anusha-3113 Dec 18, 2023
121ce43
Update palindrompartition.c++
Anusha-3113 Dec 19, 2023
e5c538f
Update Readme.md
Anusha-3113 Dec 19, 2023
c6d277b
Update Readme.md
Anusha-3113 Dec 19, 2023
752afc5
Update BinaryTree.java
Anusha-3113 Dec 30, 2023
9174d4d
Update stringtointeger.py
Anusha-3113 Jan 2, 2024
798f248
Update stringtointeger.cpp
Anusha-3113 Jan 2, 2024
841be23
Update main.java
Anusha-3113 Jan 2, 2024
200c68f
Update Readme.md
Anusha-3113 Jan 2, 2024
1137ffa
Update Readme.md
Anusha-3113 Jan 2, 2024
f64e35b
Update Readme.md
Anusha-3113 Jan 2, 2024
615044a
Update inorder.py
Anusha-3113 Jan 2, 2024
01b5e53
Update inorder.cpp
Anusha-3113 Jan 2, 2024
53a3737
Update max heap CRUD.py
Anusha-3113 Jan 3, 2024
839147c
Update max_heap_CRUD.c++
Anusha-3113 Jan 3, 2024
a98f458
Update MaxHeapCRUD.java
Anusha-3113 Jan 3, 2024
fc766e3
added KMP algorithm
Anusha-3113 Jan 16, 2024
0a122e2
Update Readme.md
vellsneha Mar 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions Heaps/Hard/MaxHeapCRUD.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

// MaxHeapCRUD class representing a max heap with CRUD operations
public class MaxHeapCRUD {
private List<Integer> heap;

// Constructor initializes an empty list for the heap
public MaxHeapCRUD() {
this.heap = new ArrayList<>();
}

// Move the element up the heap until the heap property is restored
private void percolateUp(int index) {
while (index > 0) {
int parent = (index - 1) / 2;
if (heap.get(parent) >= heap.get(index)) {
break;
}
// Swap the current element with its parent
int temp = heap.get(parent);
heap.set(parent, heap.get(index));
heap.set(index, temp);
index = parent;
}
}

// Ensure the heap property is maintained starting from the given index
private void maxHeapify(int index) {
int left = 2 * index + 1;
int right = 2 * index + 2;
int largest = index;

// Check if the left child is larger than the current largest element
if (left < heap.size() && heap.get(left) > heap.get(largest)) {
largest = left;
}
// Check if the right child is larger than the current largest element
if (right < heap.size() && heap.get(right) > heap.get(largest)) {
largest = right;
}

// If the largest element is not the current index, swap and continue heapifying
if (largest != index) {
int temp = heap.get(largest);
heap.set(largest, heap.get(index));
heap.set(index, temp);
maxHeapify(largest);
}
}

// Add a new element to the heap and percolate it up to maintain the heap property
public void insert(int val) {
heap.add(val);
percolateUp(heap.size() - 1);
}

// Return the maximum element in the heap (root of the heap)
public int getMax() {
if (heap.isEmpty()) {
throw new RuntimeException("Heap is empty");
}
return heap.get(0);
}

// Delete the element at the specified index from the heap
public void delete(int index) {
if (index < 0 || index >= heap.size()) {
throw new RuntimeException("Invalid index");
}
// Swap the element with the last element
heap.set(index, heap.get(heap.size() - 1));
heap.remove(heap.size() - 1);
// Max heapify to maintain the heap property
maxHeapify(index);
}

// Print all values in the heap
public void printHeap() {
for (int i : heap) {
System.out.print(i + " ");
}
System.out.println();
}

// Main method demonstrating usage of the MaxHeapCRUD class
public static void main(String[] args) {
MaxHeapCRUD heap = new MaxHeapCRUD();

// Insert, delete, fetch, and print elements into/from the heap using user input
try (Scanner scanner = new Scanner(System.in)) {
while (true) {
System.out.println("1. Insert element");
System.out.println("2. Get max element");
System.out.println("3. Delete element at index");
System.out.println("4. Print heap");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");

try {
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter an integer to insert into the heap: ");
heap.insert(scanner.nextInt());
break;
case 2:
try {
System.out.println("Max Value: " + heap.getMax());
} catch (RuntimeException e) {
System.err.println(e.getMessage());
}
break;
case 3:
System.out.print("Enter the index to delete: ");
int deleteIndex = scanner.nextInt();
try {
heap.delete(deleteIndex);
System.out.println("Element at index " + deleteIndex + " deleted");
} catch (RuntimeException e) {
System.err.println(e.getMessage());
}
break;
case 4:
System.out.print("All values in heap: ");
heap.printHeap();
break;
case 5:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid choice. Please enter a number between 1 and 5.");
}
} catch (InputMismatchException e) {
System.err.println("Invalid input. Please enter a number.");
// Clear the scanner buffer to avoid an infinite loop on invalid input
scanner.next();
}
}
}
}
}
Loading