Binary heap implementation file C language with the Kruskal Algorithm

Source: Internet
Author: User

/* 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/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.