1,392 views
0 votes
0 votes

Hello,

If i have to insert an element after checking it's not in tree into binary heap then what would be the procedure ?
What i know is heap is an abstract type which mean you can't traverse the nodes to find if the node already exists. Then how to do that ?

 

1 Answer

Best answer
1 votes
1 votes

Theoretically, every data structure is abstract, and is a black box. It's just that the DS provides you with certain functions that let you interact with it. An array provides you with functions to change data. A stack provides you a push and pop. A heap lets you peek minimum, remove minimum, and add a new value

Theoretically, you cannot find the minimum element of a stack without popping from the stack, because you aren't allowed to peep the contents of the stack. But practically, you do it all the time, and it would be inefficient not to.

Similarly, in a practical scenario, a heap is just a collection of memory locations that you handle in a particular way. You can use it in a strict theoretical sense if you wish, but if you need to you can always traverse the nodes.

Coming back to the question,

Pure theoretical way, in which you aren't allowed to traverse the heap:

n = Number of Elements in the Heap   
// the variable 'key' holds the value you wish to find in the heap.    
temp_list = []  
exists_inHeap = False
for index from 1 to n:
    element = Heap.removeMin() // get the minimum element from the heap 
    if element == key:
        exists_inHeap = True
        break
    end if
    temp_list[index] = element //Put the element into the temp list,
    // because we will need to restore the
    // heap at the end.
end for
//Restore the heap    
for each element in temp_list:
    Heap.insert(element)
end for    //Now we know if the element exists in the heap or not.
//If it doesn't exist in the heap already, we insert it.
if not exists_inHeap:      
    Heap.insert(key)
end if    
//Done!

Practical way

// Don't delete and reinsert elements to find  
// if our 'key' exists in the heap.  
// Simply traverse through the heap and look for  
// the 'key' in the heap.    
exists_inHeap = False    
for each elemenet in Heap:
    if element == 'key':
        exists_inHeap = True
        break
    end if
end for
//Now we know if the element exists in the heap or not.  
//If it doesn't exist in the heap already, we insert it.    
if not exists_inHeap:      
    Heap.insert(key)  
end if    
//Done!

Related questions

2 votes
2 votes
1 answer
1
radha gogia asked Jul 14, 2015
5,541 views
1. 2, 252, 401, 398, 330, 344, 350, 3602. 924, 220, 911, 244, 898, 258, 362, 3603. 925, 202, 911, 240, 950, 245, 3604. 2, 399, 387, 219, 266, 382, 381, 278, 360
2 votes
2 votes
1 answer
2
radha gogia asked Dec 10, 2015
1,651 views
If I have n elements in the heap then why does the index of leaf nodes ranges from index ⌊n/2⌋+1 to index n ?
0 votes
0 votes
0 answers
3
0 votes
0 votes
3 answers
4
anurag_am asked Jun 18, 2015
915 views
however number of comparisions in binary search=O(loglogn) and in linear search it is O(logn),time complexity in case of binary search- O(logn) and in case of linear it i...