0
0
DSA Typescriptprogramming~30 mins

Heap Concept Structure and Properties in DSA Typescript - Build from Scratch

Choose your learning style9 modes available
Heap Concept Structure and Properties
📖 Scenario: You are working on a task to organize a list of numbers so that the largest number is always easy to find. This is useful in many real-life situations like managing priorities in a to-do list or scheduling tasks.
🎯 Goal: You will build a simple max heap structure using an array in TypeScript. You will learn how to set up the heap, understand its properties, and see how the numbers are arranged.
📋 What You'll Learn
Create an array called heap with specific numbers
Create a variable heapSize to store the number of elements in the heap
Write a function isMaxHeap to check if the array satisfies max heap properties
Print the heap array and the result of isMaxHeap function
💡 Why This Matters
🌍 Real World
Heaps are used in priority queues, scheduling algorithms, and for efficient sorting methods like heapsort.
💼 Career
Understanding heaps helps in roles involving algorithm design, system optimization, and software development where priority management is needed.
Progress0 / 4 steps
1
Create the initial heap array
Create an array called heap with these exact numbers in this order: 90, 15, 10, 7, 12, 2, 7, 3
DSA Typescript
Hint

Use const heap: number[] = [...] to create the array with the exact numbers.

2
Add heap size variable
Create a variable called heapSize and set it to the length of the heap array
DSA Typescript
Hint

Use const heapSize: number = heap.length; to get the number of elements.

3
Write a function to check max heap property
Write a function called isMaxHeap that takes heap and heapSize as parameters and returns true if every parent node is greater than or equal to its children, otherwise false. Use a for loop with variable i from 0 to Math.floor(heapSize / 2) - 1 to check each parent node.
DSA Typescript
Hint

Check each parent node against its left and right children using their indices.

4
Print the heap and check result
Print the heap array and then print the result of calling isMaxHeap(heap, heapSize)
DSA Typescript
Hint

Use console.log(heap); and console.log(isMaxHeap(heap, heapSize)); to print the results.