Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6993065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:42:59+00:00 2026-05-27T19:42:59+00:00

Just wonder is there a way to implement heapify operation in a functional style

  • 0

Just wonder is there a way to implement heapify operation in a functional style ?

Suppose the data type is :

type 'a heap = Empty | Node of  'a * 'a heap * 'a heap
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:43:00+00:00Added an answer on May 27, 2026 at 7:43 pm

    Say your type, in Haskell, is

    data Heap a = Empty | Node a (Heap a) (Heap a)
    

    Let’s say we want a max heap. Let’s start with a function moveDown which repairs an almost-heap which might have an incorrect root.

    moveDown :: (Ord a) => Heap a -> Heap a
    moveDown Empty = Empty
    moveDown h@(Node x Empty Empty) = h
    moveDown (Node x (Node y Empty Empty) Empty) = Node larger (Node smaller Empty Empty) Empty
    where
        (larger, smaller) = if x >= y then (x,y) else (y,x)
    moveDown h@(Node x le@(Node y p q) ri@(Node z r s) )
        | (x >= y) && (x >= z) = h
        | (y >= x) && (y >= z) = Node y (moveDown (Node x p q)) ri
        | (z >= x) && (z >= y) = Node z le (moveDown (Node x r s))
    

    Note that because of the structure of a heap, if a node has a left child but no right child, then the left child has no children. Also, it is not possible for a node to have a right child but no left child.

    Now heapify is easy:

    heapify :: (Ord a) => Heap a -> Heap a
    heapify Empty = Empty
    heapify (Node x p q) = moveDown (Node x (heapify p) (heapify q))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just wonder there a way to display the files in document directory with
I just wonder if there is some convenient way to detect if overflow happens
I just wonder whether there will be any conflict if two service intents are
I just wonder what options there are to properly measure/profile/optimize ASP.net 2.0 Web Parts,
I just wonder about an easy way to make i18n inside Smarty templates. Something
I wonder if there is any way to generate culture neutral CSV file or
Is there a way to have an empty Project in Visual Studio 2008 or
I am using django soring application: https://github.com/directeur/django-sorting I just wonder if there is a
Are there any implementations of a purely functional standard binary heap? I know there
I wonder if there is a way to give a suggestion from the sphinx

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.