0% found this document useful (0 votes)
11 views

week 13 algo

algo

Uploaded by

abhinegi063
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

week 13 algo

algo

Uploaded by

abhinegi063
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Q37.

Write an algorithm and a program to implement priority queue (also known


as heap). Priority queue has following properties: a) Each item has a priority
associated with it. b)Element which has heighest priority is dequeued before an
element with low priority. c) If two elements have the same priority, they are
served according to their order in the queue. d)It is always in the form of complete
tree. e) Element which has highest priority should be at root, element which has
priority less than root but more than other elements comes at level 2. Hence as the
level increases, priority of element decreases. The program should implement
following operations:
a) create() - create root node of priority heap. b)insert() - insert an element into the
priority queue. Everytime when a node is inserted, tree should be balanced
according to its priority. c) getHighestPriority() - finds the element which has
highest priority. d)deleteHeighestPriority() - delete the element which has highest
priority.
**Algorithm**
Function definition for main()
Step 1: - Start
Step 2: - Declare PriorityQueue heap and initialize heap.size = 0
Step 3: - Declare int n, arr[n], choice, element
Step 4: - Print "Enter the size of the array:"
Step 5: - Input n
Step 6: - Print "Enter the priorities:"
Step 7: - For i = 0 to n - 1:
Step 8: - Input arr[i]
Step 9: - Call create(&heap, n, arr)
Step 10: - Call display(&heap)
Step 11: - Do:
Step 12: - Print "Menu: Enter your choice:\n1. Insert\n2. Get Highest Priority\n3.
Delete Highest Priority\n4. Display\n5. Exit"
Step 13: - Input choice
Step 14: - Switch choice:
Step 15: - Case 1:
Step 16: - Print "Enter the element to insert:"
Step 17: - Input element
Step 18: - Call insert(&heap, element)

Name: Divyansh Uniyal Roll No.:21 Section: K2


Step 19: - Break
Step 20: - Case 2:
Step 21: - Print "Highest Priority: ", call getHighestPriority(&heap)
Step 22: - Break
Step 23: - Case 3:
Step 24: - Call deleteHighestPriority(&heap)
Step 25: - Break
Step 26: - Case 4:
Step 27: - Call display(&heap)
Step 28: - Break
Step 29: - Case 5:
Step 30: - Print "Exiting..."
Step 31: - Break
Step 32: - Default:
Step 33: - Print "Invalid choice. Try again."
Step 34: - End Switch
Step 35: - While choice != 5
Step 36: - Stop
Function definition for heapify(PriorityQueue *heap, int i)
Step 1: - Start
Step 2: - Set largest = i
Step 3: - Calculate left = 2 * i + 1 and right = 2 * i + 2
Step 4: - If left < heap->size and heap->data[left] > heap->data[largest], set largest
= left
Step 5: - If right < heap->size and heap->data[right] > heap->data[largest], set
largest = right
Step 6: - If largest != i:
Step 7: - Swap heap->data[i] and heap->data[largest]
Step 8: - Call heapify(heap, largest)
Step 9: - Stop
Function definition for create(PriorityQueue *heap, int n, int arr[])
Step 1: - Start
Step 2: - Set heap->size = n
Step 3: - For i = 0 to n - 1:
Step 4: - Set heap->data[i] = arr[i]

Name: Divyansh Uniyal Roll No.:21 Section: K2


Step 5: - For i = (n / 2) - 1 down to 0:
Step 6: - Call heapify(heap, i)
Step 7: - Stop
Function definition for insert(PriorityQueue *heap, int element)
Step 1: - Start
Step 2: - If heap->size == MAX:
Step 3: - Print "Heap is full."
Step 4: - Return
Step 5: - Increment heap->size
Step 6: - Set heap->data[heap->size - 1] = element
Step 7: - Initialize i = heap->size - 1
Step 8: - While i != 0 and heap->data[(i - 1) / 2] < heap->data[i]:
Step 9: - Swap heap->data[i] and heap->data[(i - 1) / 2]
Step 10: - Set i = (i - 1) / 2
Step 11: - Stop
Function definition for getHighestPriority(PriorityQueue *heap)
Step 1: - Start
Step 2: - If heap->size == 0:
Step 3: - Print "Heap is empty."
Step 4: - Return -1
Step 5: - Return heap->data[0]
Step 6: - Stop
Function definition for deleteHighestPriority(PriorityQueue *heap)
Step 1: - Start
Step 2: - If heap->size == 0:
Step 3: - Print "Heap is empty."
Step 4: - Return
Step 5: - Set heap->data[0] = heap->data[heap->size - 1]
Step 6: - Decrement heap->size
Step 7: - Call heapify(heap, 0)
Step 8: - Stop
Function definition for display(PriorityQueue *heap)

Name: Divyansh Uniyal Roll No.:21 Section: K2


Step 1: - Start
Step 2: - If heap->size == 0:
Step 3: - Print "Heap is empty."
Step 4: - Return
Step 5: - Print "Heap elements: "
Step 6: - For i = 0 to heap->size - 1:
Step 7: - Print heap->data[i] followed by a space
Step 8: - Print newline
Step 9: - Stop

Name: Divyansh Uniyal Roll No.:21 Section: K2


Q38. Given a binary tree, design an algorithm and write a program to check
whether this tree is heap or not. Tree should satisfy following heap properties: a)
Tree should be complete binary tree b)Every nodes value should be greater than or
equal to its child node (incase of max heap)
**Algorithm**
Function definition for main()
Step 1: - Start
Step 2: - Declare int T, n, arr[n]
Step 3: - Print "Enter the number of test cases: "
Step 4: - Input T
Step 5: - While T--:
Step 6: - Print "Enter the size of the tree: "
Step 7: - Input n
Step 8: - Declare an array arr[n]
Step 9: - Print "Enter the elements of the tree: "
Step 10: - For i = 0 to n - 1:
Step 11: - Input arr[i]
Step 12: - Call isCompleteBinaryTree(arr, n) and isHeap(arr, n)
Step 13: - If both return 1, print "YES"
Step 14: - Else, print "NO"
Step 15: - End While
Step 16: - Stop
Function definition for isCompleteBinaryTree(int arr[], int n)
Step 1: - Start
Step 2: - For i = 0 to n - 1:
Step 3: - If 2 * i + 1 < n and arr[i] < arr[2 * i + 1], return 0
Step 4: - If 2 * i + 2 < n and arr[i] < arr[2 * i + 2], return 0
Step 5: - End For
Step 6: - Return 1
Step 7: - Stop
Function definition for isHeap(int arr[], int n)
Step 1: - Start
Step 2: - For i = 0 to n - 1:
Step 3: - If 2 * i + 1 < n and arr[i] < arr[2 * i + 1], return 0

Name: Divyansh Uniyal Roll No.:21 Section: K2


Step 4: - If 2 * i + 2 < n and arr[i] < arr[2 * i + 2], return 0
Step 5: - End For
Step 6: - Return 1
Step 7: - Stop

Name: Divyansh Uniyal Roll No.:21 Section: K2


Q39. Given a complete binary tree, design an algorithm and write a program to
find Kth largest element in it. (Hint: Max heap)

**Algorithm**

Function definition for main()

Step 1: - Start
Step 2: - Declare int n, choice, k, arr[n]
Step 3: - Print "Enter the size of the tree: "
Step 4: - Input n
Step 5: - Print "Enter the elements of the tree: "
Step 6: - For i = 0 to n - 1:
Step 7: - Input arr[i]
Step 8: - Call buildMaxHeap(arr, n)
Step 9: - Do:
Step 10: - Print "Menu:\n1. Display Heap\n2. Find Kth Largest Element\n3.
Extract Maximum Element\n4. Exit"
Step 11: - Input choice
Step 12: - Switch choice:
Step 13: - Case 1:
Step 14: - Call displayHeap(arr, n)
Step 15: - Break
Step 16: - Case 2:
Step 17: - Print "Enter the value of K: "
Step 18: - Input k

Name: Divyansh Uniyal Roll No.:21 Section: K2


Step 19: - If k > n or k <= 0:
Step 20: - Print "Invalid value of K. Please try again."
Step 21: - Break
Step 22: - Declare int tempSize = n and kthLargest = -1
Step 23: - For i = 0 to k - 1:
Step 24: - Call extractMax(arr, &tempSize) and assign the result to kthLargest
Step 25: - Print "The Kth largest element is ", followed by kthLargest
Step 26: - Break
Step 27: - Case 3:
Step 28: - If n == 0:
Step 29: - Print "Heap is empty. Nothing to extract."
Step 30: - Else:
Step 31: - Call extractMax(arr, &n) and assign the result to maxElement
Step 32: - Print "Extracted maximum element: ", followed by maxElement
Step 33: - Break
Step 34: - Case 4:
Step 35: - Print "Exiting program."
Step 36: - Exit Loop
Step 37: - Default:
Step 38: - Print "Invalid choice. Please try again."
Step 39: - End Switch
Step 40: - While 1
Step 41: - Stop

Function definition for heapify(int arr[], int n, int i)

Name: Divyansh Uniyal Roll No.:21 Section: K2


Step 1: - Start
Step 2: - Set largest = i
Step 3: - Calculate left = 2 * i + 1 and right = 2 * i + 2
Step 4: - If left < n and arr[left] > arr[largest], set largest = left
Step 5: - If right < n and arr[right] > arr[largest], set largest = right
Step 6: - If largest != i:
Step 7: - Swap arr[i] and arr[largest]
Step 8: - Call heapify(arr, n, largest)
Step 9: - Stop

Function definition for buildMaxHeap(int arr[], int n)

Step 1: - Start
Step 2: - For i = (n / 2) - 1 down to 0:
Step 3: - Call heapify(arr, n, i)
Step 4: - Stop

Function definition for extractMax(int arr[], int *n)

Step 1: - Start
Step 2: - If *n <= 0, return -1
Step 3: - Set maxElement = arr[0]
Step 4: - Set arr[0] = arr[*n - 1]
Step 5: - Decrement *n

Name: Divyansh Uniyal Roll No.:21 Section: K2


Step 6: - Call heapify(arr, *n, 0)
Step 7: - Return maxElement
Step 8: - Stop

Function definition for displayHeap(int arr[], int n)

Step 1: - Start
Step 2: - Print "Heap elements: "
Step 3: - For i = 0 to n - 1:
Step 4: - Print arr[i] followed by a space
Step 5: - Print newline
Step 6: - Stop

Name: Divyansh Uniyal Roll No.:21 Section: K2

You might also like