/* Binary_heap_for_kruskal.c -- binary heap implementation file */<br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include "binary_heap_for_kruskal.h" </P> <p>/* local function declaration */</P> <p> static int percolate_up (const binary_heap * const PBH, const int index); <br/> static int percolate_down (const binary_heap * const PBH, const int index ); </P> <p>/* interface function definition */</P> <p> int initialize_ B (binary_heap * const PBH, const int capacity) <br />{< Br/> * PBH = (struct binary_heap *) malloc (sizeof (struct binary_heap); <br/> If (null = * PBH) <br/> return 0; <br/> (* PBH)-> heap = (edge *) malloc (sizeof (edge) * (capacity + 1 )); <br/> If (null = (* PBH)-> heap) <br/>{< br/> free (* PBH); <br/> return 0; <br/>}< br/> (* PBH)-> heap [0]. weight = negativeinfinity; <br/> (* PBH)-> current = 0; <br/> (* PBH)-> capacity = capacity; </P> <p> retur N 1; <br/>}</P> <p> int insert_ B (const binary_heap * const PBH, const edge * const PD) <br/>{< br/> int current; </P> <p> If (* PBH)-> current = (* PBH)-> capacity) <br/> return 0; <br/> current = ++ (* PBH)-> current; <br/> (* PBH)-> heap [current]. v_hash_value = Pd-> v_hash_value; <br/> (* PBH)-> heap [current]. w_hash_value = Pd-> w_hash_value; <br/> (* PBH)-> heap [current]. weight = Pd-> weight; <Br/> percolate_up (PBH, current); </P> <p> return 1; <br/>}</P> <p> int deletemin_ B (const binary_heap * const PBH, edge * const PD) <br/> {<br/> If (* PBH)-> current <= 0) <br/> return 0; <br/> * Pd = (* PBH) -> heap [1]; <br/> percolate_down (PBH, (* PBH)-> current); <br/> (* PBH)-> current --; </P> <p> return 1; <br/>}</P> <p> void release_ B (const binary_heap * const PBH) <br/> {<br/> free (* PBH)-> heap ); <Br/> free (* PBH ); <br/>}</P> <p>/* local function definition */</P> <p> static int percolate_up (const binary_heap * const PBH, const int index) <br/>{< br/> edge temp = (* PBH)-> heap [Index]; <br/> int I; </P> <p> If (index <= 0 | index> (* PBH)-> current) <br/> return 0; <br/> for (I = index; I/2> 0; I/= 2) <br/> {<br/> If (temp. weight <(* PBH)-> heap [I/2]. weight) <br/> (* PBH)-> heap [I] = (* PBH)-> heap [I/2]; <Br/> else <br/> break; <br/>}< br/> (* PBH)-> heap [I] = temp; </P> <p> return 1; <br/>}</P> <p> static int percolate_down (const binary_heap * const PBH, const int index) <br/>{< br/> int current = (* PBH)-> current; <br/> edge temp = (* PBH)-> heap [current]; <br/> int I, child; </P> <p> If (index <= 0 | index> (* PBH)-> current) <br/> return 0; <br/> for (I = 1; I * 2 <= current; I = Child) <br/> {<B R/> child = I * 2; <br/> If (child! = Current & (* PBH)-> heap [child]. weight> (* PBH)-> heap [Child + 1]. weight) <br/> child ++; <br/> If (temp. weight> (* PBH)-> heap [child]. weight) <br/> (* PBH)-> heap [I] = (* PBH)-> heap [child]; <br/> else <br/> break; <br/>}< br/> (* PBH)-> heap [I] = temp; </P> <p> return 1; <br/>}