d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Heap
12Next
Add Reply New Topic New Poll
Member
Posts: 19,514
Joined: Feb 21 2011
Gold: 3,877.57
Feb 3 2013 11:02am
Hi, if anyone's an expert on this topic, can you post - thanks
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 3 2013 05:56pm
you should post your question.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 3 2013 09:17pm
in c++ any time the `new` keyword is used it allocates memory on the heap else it's allocated on the stack.

what is your question?
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Feb 3 2013 10:53pm
can someone explain to me the difference between a binary tree and a heap? Lol :p
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 3 2013 11:02pm
Quote (Eep @ Feb 3 2013 11:53pm)
can someone explain to me the difference between a binary tree and a heap? Lol :p


binary tree is simply an acyclic graph where each node has at most two children. no ordering is implied.

a heap is a tree where the relative ordering between parent and child remains constant; eg if the parent >= child, it's a max heap. for one thing, a heap can have more than two children so it doesn't have to be a binary tree at all. a binary heap is a binary tree - like structure that satisfies the heap property i already mentioned.

This post was edited by carteblanche on Feb 3 2013 11:04pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Feb 3 2013 11:15pm
Quote (carteblanche @ Feb 4 2013 12:02am)
binary tree is simply an acyclic graph where each node has at most two children. no ordering is implied.

a heap is a tree where the relative ordering between parent and child remains constant; eg if the parent >= child, it's a max heap. for one thing, a heap can have more than two children so it doesn't have to be a binary tree at all. a binary heap is a binary tree - like structure that satisfies the heap property i already mentioned.


relative ordering between parent and child remains constant.....so in a max heap, the children will ALWAYS be less than/equal to the parent?

This post was edited by Eep on Feb 3 2013 11:15pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 3 2013 11:21pm
Quote (Eep @ Feb 4 2013 12:15am)
relative ordering between parent and child remains constant.....so in a max heap, the children will ALWAYS be less than/equal to the parent?


do you understand how a max heap works? tell me what the performance is for removing/adding and explain how you acheive that performance.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Feb 3 2013 11:31pm
Quote (carteblanche @ Feb 4 2013 12:21am)
do you understand how a max heap works? tell me what the performance is for removing/adding and explain how you acheive that performance.


I know they are built from complete binary trees apparently, then they sift the values down

I know that sifting down uses no more than big O of Log (n)



This post was edited by Eep on Feb 3 2013 11:35pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 3 2013 11:39pm
Quote (Eep @ Feb 4 2013 12:31am)
I know they are built from complete binary trees apparently, then they sift the values down

I know that sifting down uses no more than big O of Log (n)


explain it in more detail until you understand it. how is it that a peak is constant time and insert/delete are logarithmic? then answer the question you asked me.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Feb 3 2013 11:53pm
Quote (carteblanche @ Feb 4 2013 12:39am)
explain it in more detail until you understand it. how is it that a peak is constant time and insert/delete are logarithmic? then answer the question you asked me.


peek just finds the root, so it is O(1)

Insert/delete however are based on the number of levels of the heap, therefore the time complexity is O(Log(n))

for delete you have to take the last element on the last level and replace it with the previous root, then you have to make sure it is in the correct order

This post was edited by Eep on Feb 3 2013 11:55pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll