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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
Say your type, in Haskell, is
Let’s say we want a max heap. Let’s start with a function
moveDownwhich repairs an almost-heap which might have an incorrect root.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
heapifyis easy: