构建二进制堆

时间:2014-02-18 19:34:50

标签: java binary-tree heap binary-heap

我试图通过传递一个int数组来构建二进制堆。我想知道我是否应该先构建一个BinaryTree类,然后再创建一个Heap类来实现二进制堆,还是应该构建一个二进制堆类?我糊涂了.. 谢谢!

4 个答案:

答案 0 :(得分:1)

二进制堆是一种特殊的二叉树。应该保持堆属性。

来自维基百科的回顾:  如果A是B的父节点,则节点A的密钥相对于节点B的密钥排序,并且在堆上应用相同的排序。父节点的密钥总是大于或等于子节点的密钥,最高密钥在根节点中(这种堆称为最大堆)或父节点的密钥小于或等于父节点的密钥。子节点和最低键位于根节点(最小堆)中。

根据您的实现,还会有一些关于堆完整性的规则。

二叉树不一定具有二进制搜索树属性。

换句话说,只需将二进制堆实现为具有所讨论的特殊功能的树。

答案 1 :(得分:0)

可以通过简单地使用数组来表示和存储二进制堆。首先不需要构建/实现二进制树。

请查看以下有关如何将二进制堆表示为数组的链接:http://www.cse.hut.fi/en/research/SVG/TRAKLA2/tutorials/heap_tutorial/taulukkona.html

确保理解父(i),左(i)和右(i)的概念。另外,看一下构建堆函数,从未排序的数组构建一个堆:http://en.wikipedia.org/wiki/Binary_heap#Building_a_heap

从树中的第一个非叶子节点开始,一直调用heapify直到根。

答案 2 :(得分:0)

这是python中一个在数组中构建二进制堆的例子。

def add_data(heap, data):
  for i in range(0, len(data)):
    item = data[i]
    insert(heap, item)
  if not is_heap_ordered(heap):
    raise Exception("explode!")

def insert(heap, x):
  heap.append(x)
  swim(heap, len(heap) - 1)

def swim(heap, k):
  parent = k / 2
  while (k > 1) and (heap[parent] < heap[k]):
    exchange(heap, k, parent)
    k = parent
    parent = k / 2

def is_heap_ordered(heap):
  # heap property:  parent is >= children
  limit = len(heap)
  for k in range(1, limit):
    if ((2 * k) > limit - 1) or (((2 * k) + 1) > limit - 1):
      break
    node = heap[k]
    parent = heap[k / 2]
    if k / 2 < 1:  # root node
      parent = node
    childl = heap[2 * k]
    childr = heap[(2 * k) + 1]
    if childl > parent or childr > parent:
      print "heap violated"
      return False
  return True

def exchange(array, i, j):
  temp = array[i]
  array[i] = array[j]
  array[j] = temp

heap = []
input = list("ABCDEF")
random.shuffle(input)
add_data(heap, input)

不使用数组的第一个元素,这使得数学变得更简单。

位置k处的每个节点的子节点位于2k和2k + 1 每个节点的父节点位于k / 2

位置

算法是:将一个元素附加到数组的末尾,然后为该元素调用swim,直到它处于正确的堆位置(堆服从'heap property')。堆属性是每个父级都是&gt; =其子级。

这是算法运作的动画

enter image description here

答案 3 :(得分:0)

#Binary Heap with all the common operations
"""This Code will generate Min Binary Heap with all action that we can perform on Binary Heap. The Formula i have taken is in array 0'th position will be None, Will start from 1'st Position. So root will be at 1 ant left will be at 2x(2) right will be 2x+1(3) Position. """
    class MinHeap():
        """ Min Heap:-Node values will always be lees than the left and right child """
        def __init__(self):
            #Creating a Array to push heap values
            self.heap_list=[None]
            
        def push_element(self,value):
            self.heap_list.append(value)
        #Getting right child from postion 2x
        def get_left_child(self,index):
            for i in range(len(self.heap_list)):
                if i==2*index:
                    return self.heap_list[2*index]
            return 999
        #Getting right child from postion 2x+1
        def get_right_child(self,index):
            for i in range(len(self.heap_list)):
                if i==2*index+1:
                    return self.heap_list[2*index+1]
            return 999
        # I have considered a logic node if node will on x index then left child woul bw at 2*x and right child will be at 2*x+1 postion in array
        def re_construct_heap(self):
            for i in range(1,(len(self.heap_list)//2)+1):
                left_child=self.get_left_child(i)
                right_child=self.get_right_child(i)
                if self.heap_list[i]>left_child :
                    v_parent=self.heap_list[i]
                    self.heap_list[i]=left_child
                    self.heap_list[2*i]=v_parent
                elif self.heap_list[i]>right_child:
                    v_parent=self.heap_list[i]
                    self.heap_list[i]=right_child
                    self.heap_list[2*i+1]=v_parent
        #Re cunstructing heap till the heap property satisfies
        def build_min_heap(self):
          for i in range(len(self.heap_list)//2, 0, -1):
            self.re_construct_heap()
        #Deleing a root node and trying to rebuild Min Heap with heap property
        def delte_node_heap(self,value):
            if self.heap_list[1]==value:
                self.heap_list[1]=self.heap_list[-1]
                self.heap_list.pop(-1)
                self.build_min_heap()
        #Min Value of Heap
        def min_value(self):
            return self.heap_list[1]
        #Max Value of Heap
        def max_value(self):
            return self.heap_list[-1]
        #size of heap
        def size_of_heap(self):
            return len(self.heap_list)
        #Printin Heap Values
        def print_min_heap(self):
            for i in self.heap_list:
                print(i,end='->')
    
    min_heap_obj=MinHeap()
    min_heap_obj.push_element(3)
    min_heap_obj.push_element(5)
    min_heap_obj.push_element(8)
    min_heap_obj.push_element(17)
    min_heap_obj.push_element(19)
    min_heap_obj.push_element(36)
    min_heap_obj.push_element(40)
    min_heap_obj.push_element(25)
    min_heap_obj.push_element(100)
    #insert a node less than root node
    #min_heap_obj.push_element(1)
    #re structuring heap with heap property
    min_heap_obj.build_min_heap()
    #deleting a root node
    min_heap_obj.delte_node_heap(3)
    #re structuring heap with heap property
    min_heap_obj.build_min_heap()
    #printing heap values
    min_heap_obj.print_min_heap()
    print('\n')
    print(min_heap_obj.min_value(),min_heap_obj.max_value(),min_heap_obj.size_of_heap())