Question

Suppose that binary heaps are represented using explicit links. Give a simple algorithm to find the...

Suppose that binary heaps are represented using explicit links. Give a simple algorithm to find the tree node that is at implicit position i.

instructions: provide Java-like pseudocode. The implicit position of a node refers to the index it would have if the heap was stored in the array format reviewed in class (first element at index 1).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Algorithm is very simple. Since the parent index of element at index i is index i/2 , hence to find the tree node at implicit position i, covert the number i into binary format.

Now the let index i in binary format be bkbk-1....b1b0 . So process the binary number bkbk-1....b1b0  from left to right. Starting from bit bk to bit b0 if bit bi is 0 then we will go to left child and if bit bi is 1 then go to right child. If we follow like this then at the end while accessing bit b0 we will reach at element with implicit position i. The reason behind this is that, when we reach at bit bi while processed elements bkbk-1....bi the corresponding index we reached is bk*2(k-i)+ bk-1 * 2(k-i+1)*bk-1+....+bi+1*2 + bi . Hence each subsequent search will multiply the index by 2 if we go to left child and multiply the index by 2 and add 1 if we go to right child and hence finally we reach at index i.

Below is the method:-

node * find( node *root, index i)

{

1. B = binary(i); //B will be array of 0 and 1 storing number i in binary format.

2. node *result = root; //result will store the final node

3. for i = k to 0 //k is the leftmost index of binary number in B

4........if(B[ i ] == 0)

5..............result = result->left; //Go to left child

6.......else

8..............result = result->right; //Go to right child

9........if result==NULL

10...........Return NULL since index i does not exist

11. Return result //This will be final node which will be at implicit position i

}

Now let us convert it into recursive function in java

node * find(node *root, int i)

{

int k=1;

if(i==1)

return(root); //Since the root element of subtree will have index 1

else if(i==2)

return(root->left); //index of root->left is 2

else if(i==3)

return(root->right); //index of root->right is 3

//This is the condition for going to left subtree

//Since now the index of node i will be

  //This is the condition for going to right subtree

//Since now the index of node i will be

}

Please comment for any clarification.

Add a comment
Know the answer?
Add Answer to:
Suppose that binary heaps are represented using explicit links. Give a simple algorithm to find the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In the lectures, we studied binary heaps. A min-Heap can be visualized as a binary tree...

    In the lectures, we studied binary heaps. A min-Heap can be visualized as a binary tree of height with each node having at most two children with the property that value of a node is at most the value of its children. Such heap containing n elements can be represented (stored) as an array with the property Suppose that you would like to construct a & min Heap: each node has at most& children and the value of a node...

  • 5. A three-heap with n elements can be stored in an array A, where A[O] contains...

    5. A three-heap with n elements can be stored in an array A, where A[O] contains the root of the tree. a) Draw the three-heap that results from inserting 5, 2, 8, 3, 6, 4, 9, 7, 1 in that order into an initially empty three-heap. You do not need to show the array representation of the heap. You are only required to show the final tree, although if you draw intermediate trees. b) Assuming that elements are placed in...

  • Consider the following complete binary tree is stored in an array the way we learned during...

    Consider the following complete binary tree is stored in an array the way we learned during heap lecture. The root node is stored at index 1. The last node (47) is stored at index heapsize. If you want to build a heap from the array using the heapify(), what position of the array you start doing percolate Down in heapify()? Answer: What is the run-time to build a heap from an array of size n using heapify() process ? OC...

  • 14 18 11 10 19 16 23 28 15 21 Again, recall that in a Java...

    14 18 11 10 19 16 23 28 15 21 Again, recall that in a Java program, a binary heap is stored as an array with the first element in position [O] unused, in order to simplify the swim() and sink() operations. Let us denote the unused element of the array with the symbol "#". Therefore, the above complete binary tree will be represented by the following array: T[ 0..13 ] = [ #, 5, 6, 7, 14, 18, 11,...

  • SECTION B (4 QUESTIONS, 90 MARKS) Question 4. [32 marks in total] For this question, assume the definition of Java clas...

    SECTION B (4 QUESTIONS, 90 MARKS) Question 4. [32 marks in total] For this question, assume the definition of Java class Heap given in the Appendix. The heaps referred to in this question are all maxHeaps. a. (5 marks) Insert into an empty (binary) heap the values 35, 4, 72, 36, 49, 50. Draw all intermediate steps b. (3 marks) Carry out one deletion operation on the final heap in (a.) above. c. (2 marks) Give the worst-case time complexity...

  • In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the...

    In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the element of the heap with the smallest key is the root of the binary tree. On the other hand, a max-heap has as root the element with the biggest key, and the relationship between the keys of a node and its parent is reversed of that of a min-heap. We also discussed an array-based implementation of heaps. In this assignment, your task is to...

  • Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap...

    Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either > (in a max heap) or s (in a min heap) the key of C. The node at the "top" of the heap (with no parents) is called the root node. In binary-tree based heap, it...

  • You are going to implement Treesort algorithm in C++ to sort string data. Here are the...

    You are going to implement Treesort algorithm in C++ to sort string data. Here are the steps to complete the homework 1) Use the following class definition for binary search tree nodes. Its constructor is incomplete you should first complete the constructor. class TreeNode t public: string data; / this is the string stored in the node TreeNode left: TreeNode right; TreeNode (string element, TreeNode 1t, TreeNode rt //your code here 2) Write a function that will insert a string...

  • Complete HeapPriorityQueue (7 points). In lecture we implemented HeapPriorityQueue using an array-based representation of a heap...

    Complete HeapPriorityQueue (7 points). In lecture we implemented HeapPriorityQueue using an array-based representation of a heap (a complete binary tree whose entries satisfy the heap-order property). For this problem, complete the included HeapPriorityQueue class by using the LinkedBinaryTree class to represent a heap. Hint: the most challenging part of this problem is identifying the last Position in the heap and the next available Position in the heap. It is suggested that you review the array-based heap to better understand how...

  • Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods...

    Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods are meant to determine if an array of objects corresponds to a heap. The methods are generic, and they should work with an array of any type T that implement Comparable<T>. Implement the second method so that it uses recursion to process the complete tree/subtree whose root is at position i in the array arr. The method should return true if that tree/subtree is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT