Question

A heap can be encoded either as an array, or as a full binary tree. For...

A heap can be encoded either as an array, or as a full binary tree. For this question, write a function that takes the array representation of a heap and outputs its binary tree representation. More specifically, you should write a function with the specifications given below.

Specifications for the function:

# def arrayToTree(A, j):

# input: array A representing a heap, an index j in [0:len(A)]

# output: a Node object storing the heap with root j in the array A

Should use a recursive algorithm.

All I want is an explanation! Can you please explain this question really simply so I can understand and maybe explain the algorithm but no code please! Can you explain what this problem is asking and how someone with barely any knowledge of python might do this. Please don't code anything, just give explanations

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

The problem is about converting the given heap in array to heap in full binary tree.

For Example, the heap is [1,2,3,4,5,6,7] in array data structure

And in full binary fill tree it looks as follows:

                            1

                          / \

                       2       3

                    /    \     /    \

                  4      5 6      7

The queston is about converting the heap array from index j into heap fulll binary tree.

For Example, the heap is [1,2,3,4,5,6,7] in array data structure

And let the index be 2, then the heap is [3,4,5,6,7] in array data structure

And in full binary fill tree it looks as follows:

                            3

                          / \

                       4       5

                    /    \

                 6       7

Our target is to achieve this and return the root node of this tree.

Python code for above problem

class Node:
   def __init__(self,data):
       self.data=data
       self.left=None
       self.right=None
  
   def set_left(self,left):
       self.left=left
      
   def set_right(self,right):
       self.right=right
  
def set_pointers(nodes,j,length):
   if(j==length):
       return
   left=2*j+1
   right=2*j+2
   if(left<length):
       nodes[j].set_left(nodes[left])
   if(right<length):
       nodes[j].set_right(nodes[right])
   set_pointers(nodes,j+1,length)
  
def arrayToTree(A,j):
   nodes=[]
   A=A[j:]
   for i in range(len(A)):
       nodes.append(Node(A[i]))
      
   set_pointers(nodes,0,len(A))
   return nodes[0]
  
def inorder(root):
   if root is None:
       return
   print(root.data),
   inorder(root.left)
   inorder(root.right)
  
def test_case_1():
   arr=[1,2,3,4,5,6,7]
   root=arrayToTree(arr,2)
   print("Array:"),
   print(arr[2:])
   print("Heap:"),
   inorder(root)
   print("")
  
def test_case_2():
   arr=[1,2,3,4,5,6,7]
   root=arrayToTree(arr,0)
   print("Array:"),
   print(arr[0:])
   print("Heap:"),
   inorder(root)
   print("")

def main():
   test_case_1()
   test_case_2()
  
main()

Sample output

Array: [3, 4, 5, 6, 7] Heap: 3 4 6 7 5 Array: [1, 2, 3, 4, 5, 6, 7] Heap: 1 2 4 5 3 6 7

Mention in comments if any mistakes or errors are found. Thank you.

Add a comment
Know the answer?
Add Answer to:
A heap can be encoded either as an array, or as a full binary tree. For...
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...

  • [12] 3. a) Draw the binary min-heap after inserting the following values, one after another. 21,...

    [12] 3. a) Draw the binary min-heap after inserting the following values, one after another. 21, 13, 12, 25, 4, 20, 16, 1, 11 You must show each step of building the heap and eventually the final tree. Please, put your final tree inside a box so that it can be easily understood among other intermediate trees. b) A 4-ary max heap is like a binary max heap, but instead of 2 children, nodes have 4 children. A 4-ary heap...

  • 1. Which of the following is a proper array representation a binary min heap?

    1. Which of the following is a proper array representation a binary min heap?2. A heap is implemented using an array. At what index will the right child of node at index i be found? Note, the Oth position of the array is not used.Select one:a. i/2b. 2 i+1c. i-1d. 2 i3. Consider the following array of length 6. Elements from the array are added, in the given order, to a max heap. The heap is initially empty and stored as an array.A={18,5,37,44,27,53}What...

  • QUESTION 16 Show the first pass of sorting the following array-based binary tree max-heap. In other...

    QUESTION 16 Show the first pass of sorting the following array-based binary tree max-heap. In other words, show the first step in sorting, then re-heap the remaining tree into a max-heap. For answers that are not used, put null. You may use scratch paper to draw the trees if you wish. (You will not need all the columns)

  • I need question 9-10 answered. Thank you Question 1 iShow the resulting binary search tree if...

    I need question 9-10 answered. Thank you Question 1 iShow the resulting binary search tree if we are to insert following elements into the tree in given order, [34, 12, 23, 27,31,9,11,45, 20, 37. i) Show the resulting balanced binary search tree if we are to insert following sorted elements into the tree, [9,12,21, 23, 29, 31, 34, 45, 48, 52, 55] iii What is the pre-order traversal of the balanced binary search tree? v) What is the post-order traversal...

  • Implement the class MaxHeapPriorityQueue as a heap with the following operations: • MaxHeapPriorityQueue() creates a new...

    Implement the class MaxHeapPriorityQueue as a heap with the following operations: • MaxHeapPriorityQueue() creates a new heap that is empty. It needs no parameters and returns nothing. Note that as discussed in the video lecture, the index range of the array implementation of a heap is 1:n, NOT 0:n-1 • parent(index) returns the value of the parent of heap[index]. index is between 1 and the size of the heap. If index<=1 or index>size of the heap, it returns None •...

  • 1. In Lab 4, you developed a program to build a Max Heap, and then Heap...

    1. In Lab 4, you developed a program to build a Max Heap, and then Heap Sort. Update the program by adding two additional functions: (a) AddData(A, N, V) where V is the new value added. (b) Delete a data Delete by giving the index of the data position Make sure to display the array after calling each of the function. 2. Write a program to implement Binary Search Algorithm, which will return the index of the data searched (V)....

  • What is the difference between the binary-search-tree property and the min-heap property (see page 129)? Can...

    What is the difference between the binary-search-tree property and the min-heap property (see page 129)? Can the min-heap property be used to print out the keys of an n-node tree in sorted order in O(n) time? Explain how or why not.

  • Instructions Develop a CPP program to test is an array conforms heap ordered binary tree. This...

    Instructions Develop a CPP program to test is an array conforms heap ordered binary tree. This program 7 numbers. Example runs and comments (after // ) are below. Your program does not prin Exp-1: Enter a number: 65 // this is first item (root, [1]) in the heap three. it does not violate a Enter a number: 56 // this is third (3) number, should be less than or equal to its root ([1 Enter a number: 45 // this...

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