Uploaded by Qu Yuan

lab3-post

advertisement
1. When input a new thing, with the location and place to put, we add it to the
array heap and the whole size of the heap increases one. At the same time,
we need to check the new thing whether the value of the child is bigger than
the values of its parents. If not , change the two place of the two.
Instance Variable: private Decreaser<T>[] array;
private int size;
Help method: void decrease(int loc)
What does it do: help to test whether the value of the child is bigger than or
equal to the values of its parent. If not, change the place of these two.
2. If there is only one element. There is no need to compare. So, return. When
there are more than one element in the array, we have test whether the new
element satisfy the requirement bigger or equal to value of the parent. If not,
we change the child with his parent. And continue to assess the parent of
element bigger than his parent of element.
Instance Variable: array[]
Help method: decrease()
What does it do: we need to test the parent of the new element satisfy his
parent smaller than it. We keep on recall itself to make sure all the parents
smaller then their children.
3. We extract the root or say the first element of the array because it is the
smallest element in this heap. And there is a hole. And size subtracts one
because the smallest already be extracted. We put the last element on the
root or say the place of the first element. And make the last element of the
heap is null. And call heapify() to check from the start point whether the
value of his child is bigger than the parent. If not, switch it.
Instance variable: array,size
Help method: heapify()
What does it do: looks at a parent and its two children, imposing the heap
property on them by perhaps swapping the parent with the lesser of the two
children.
4.first make sure, the parent have children. If the parent have no children.
Return. compare the value of the parent whether bigger than the value of
left child or value of right child. If yes, compare the values of the left or
right to find the smaller element and swap with the parent. Test until the end
of the heap.
instance variable: array[],size
help method: heapify() recall itself children to make sure their children is
smaller then their children
5.
when we compare the heapsort.ticks graph with the nlogn graph. We can
find that it is the same! so it is the nlogn curve.
6. when we use extraMin(), we have to use heapify method to make sure
the new root satisfy the heap property. So, we have to keep on call on itself
to make sure the parent is smaller than their children. One time call the
extraMin,
the
time
complexity
is
logn,
n
times:
klog1+klog2…+klogn<klogn+klogn+klogn+..klogn=knlogn= nlogn
Depth
Problem Size
#Nodes
Local Work
0
n
n/2
1
cn
2
Cn
2^2
Cn
2^i
Cn
2^log2n=n
dn
1
2
i
Log2n
N/4
n/2^i
1
𝑙𝑜𝑔𝑛−1
𝑑𝑛 + ∑ 𝑐𝑛 = 𝑑𝑛 + 𝑐𝑛𝑙𝑜𝑔𝑛 = 𝜃(𝑛𝑙𝑜𝑔𝑛)
𝑖=0
7. the heap property is a special relationship between each parent and its
children. It applies between every node and its children, This time we think
the heap property is the value of parent must be smaller than the value of
its left and right child.
In the insert method. We insert one element in to the end of the heap, we
use decrease method to check whether the heap property: the parent is
smaller than its children from the end to the top, if it is not satisfied, we swap
their place.
In the extractMin method, we extract the smallest element in the heap which
is the root of the heap. When we extract it, there is a hole. We move the last
value to the root. We use the heapify method to check whether the heap
property is satisfied. It not, the child and parent swap.
8. definite not! a min-heap is a binary tree. If we need to insert new thing.
Nodes are always added top-to-bottom and left-to-right. In this case, if there
is an empty space on the left, the new element will be fill on left and then fill
on the right. So the maximum height of any node in the left subtree of a minheap every different from the max height of any node in the right subtree no
more than 1.
9.advantage: the time complexity of extract Min is logn which is quicker than
unordered list n.
disadvantage. The time complexity of insert is logn which is slower then
unordered list 1
Download