binary heap as tree structure searching algorithm


so i guess you are all fimilliar with a binary heap data structure if not.. Brilliant. org say

i.e. a binary tree which obeys the property that the root of any tree is greater than or equal to (or smaller than or equal to) all its children (heap property). The primary use of such a data structure is to implement a priority queue.

will one of the properties of a binary heap is that it must be filled from top to bottom (from root) and from right to left

I coded this algorithm to find the next available spot to insert the next number I add (I hard coded the first nodes so I can track more further down the tree

this search method is inspired by BFS(Breadth First Search) algorithm

note that in this code I only care about finding the next empty node without the need to keep the heap property

I tested the code but I don’t think I tested it enough so if you spot problems, bugs or suggest any ideas, every comment is welcomed

    def insert(self, data):
        if == None:
   = data

    def search(self):
        queue = [self.root]

        while queue:
            curr = queue.pop(0)
            if curr.right_child == None:
                print('made it')

h = Min_heap(10)
h.root.left_child = Node(3)
h.root.right_child = Node(5)
h.root.left_child.left_child = Node(8)
h.root.left_child.right_child = Node(7)
h.root.right_child.left_child = Node(9)

# The tree I am building...
#                       __2__
#                      /     
#                     3       5
#                    /      / 
#                   8   7   9   ⨂
#                               ↑
#                            what am
#                          looking for

there is another way to figuring this out which is basically translating the tree into an array/list using special formulas and then we just assume that the next data we want to insert is the last element in the previous array and then work back through the same formulas but I already know that algorithm and I thought why not trying to solve it as a graph soooo…

Asked By: ii3Boodi



If you want to guarantee balanced, just add to each node how many items are there or below. Maintain that with the heap. And when placing an element, always go to where there are the fewest things.

If you just want a simple way to place, just randomly place it. You don’t have to be perfect. You will still on average be O(log(n)) levels, just with a worse constant.

(Of course your constants are better with the array approach, but you say you know that one and are deliberately not implementing it.)

Answered By: btilly

You should better implement a binary heap as a list (array). But if you want to do it with node objects that have left/right attributes, then the position for the next node can be derived from the size of the tree.

So if you enrich your heap class instances with a size attribute and maintain that attribute to reflect the current number of nodes in the tree, then the following method will tell you where the next insertion point is, in O(logn) time:

Take the binary representation of the current size plus 1. So if the tree currently has 4 nodes, take the binary representation of 5, i.e. 101. Then drop the leftmost (most significant) bit. The bits that then remain are an encoding of the path towards the new spot: 0 means "left", 1 means "right".

Here is an implementation of a method that will return the parent node of where the new insertion spot is, and whether it would become the "left" or the "right" child of it:

    def next_spot(self):
        if not self.root:
            raise ValueError("empty tree")
        node = self.root
        path = self.size + 1
        sides = bin(path)[3:-1]  # skip "0b1" and final bit
        for side in sides:
            if side == "0":
                node = node.left
                node = node.right
        # use final bit for saying "left" or "right"
        return node, ("left", "right")[path % 2]
Answered By: trincot