由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - (CS) heapify a binary tree
相关主题
备考google onsite, 讨论堆排序的时间复杂度问个题
再上到题Ask a google interview question
请教几个面试问题问一道数组题
Bloomberg面经T家电面一般有几轮? [UPDATE面经]
a very difficult interview question在版上看到的G题
n个点,找出离原点最近的100个点吐槽一个面试
问两道google面试题今天面试问题:有一个整数数组,如何find kth smallest element,如果数据是dynamic
问个google面试题给一个最大堆,求最大的K个数,O(K) 算法?
相关话题的讨论汇总
话题: node话题: value话题: heapify话题: max话题: left
进入JobHunting版参与讨论
1 (共1页)
l********r
发帖数: 140
1
Given the root of a binary tree (not balanced), heapify it.
Am I right we msut need the parent pointer for each node to be more
efficient?
l********r
发帖数: 140
2
By the way, my algorithm is like (assume max heap):
for the current node, if left or right is bigger than the current, switch
the biggest value as the current value, and try to move it all the way up to
its parent chain if that is bigger than the parent's value as well.
Then do the same for its left and right children.
l********r
发帖数: 140
3
Any comments?
s*****n
发帖数: 994
4
It's still not balanced by your algorithm?
l********r
发帖数: 140
5
Why not?
It starts with root, it guarantee the current node and its two children
satisfy the condition. Then it back check (heapify) all its parents, and
guarantee that. Then move down to its children. I just don't know whether it
is efficient or not.

【在 s*****n 的大作中提到】
: It's still not balanced by your algorithm?
s*****n
发帖数: 994
6
左右子节点都是balanced并不保证current node是balanced
比如左深度3 右深度100
c**y
发帖数: 172
7
Does Post-Order travel work here?
void heapify(binaryTree *node)
{
if (!node) return;
if (node->left)
heapify(node->left);
if (node->right)
heapify(node->right);
int max = node->value;
if (node->left && node->left->value > max) {
max = node->left->value;
}
if (node->right && node->right->value > max) {
max = mode->right->value;
}
if (max == node->value) {
return;
} else if (node->left && max == node->left->value) {
int tmp = node->value;
node->value = node->left->value;
node->left->value = tmp;
} else {
int tmp = node->value;
node->value = node->right->value;
node->right->value = tmp;
}
return;
}
l********r
发帖数: 140
8
"Given the root of a binary tree (not balanced), heapify it."
It asks to heapify, it does not ask to balance the tree.

【在 s*****n 的大作中提到】
: 左右子节点都是balanced并不保证current node是balanced
: 比如左深度3 右深度100

s**x
发帖数: 7506
9

I think you need push all the way down in the last swap step, thinking the
case root is the smallest value.

【在 c**y 的大作中提到】
: Does Post-Order travel work here?
: void heapify(binaryTree *node)
: {
: if (!node) return;
: if (node->left)
: heapify(node->left);
: if (node->right)
: heapify(node->right);
: int max = node->value;
: if (node->left && node->left->value > max) {

c**y
发帖数: 172
10
Thanks for pointing out the issue. I now know it.

【在 s**x 的大作中提到】
:
: I think you need push all the way down in the last swap step, thinking the
: case root is the smallest value.

相关主题
n个点,找出离原点最近的100个点问个题
问两道google面试题Ask a google interview question
问个google面试题问一道数组题
进入JobHunting版参与讨论
g*********e
发帖数: 14401
11

but heap is a balanced tree

【在 l********r 的大作中提到】
: "Given the root of a binary tree (not balanced), heapify it."
: It asks to heapify, it does not ask to balance the tree.

c**y
发帖数: 172
12
Revised solution. Will try to test it if I get a chance today.
void heapify(binaryTree *node)
{
if (!node) return;
if (node->left)
heapify(node->left);
if (node->right)
heapify(node->right);
int max = node->value;
if (node->left && node->left->value > max) {
max = node->left->value;
}
if (node->right && node->right->value > max) {
max = mode->right->value;
}
if (max == node->value) {
return;
} else if (node->left && max == node->left->value) {
int tmp = node->value;
node->value = node->left->value;
node->left->value = tmp;
heapify(node->left);
} else {
int tmp = node->value;
node->value = node->right->value;
node->right->value = tmp;
heapify(node->right);
}
return;
}
s**x
发帖数: 7506
13

the pushdown (or siftdown) part should not call heapify, it is much simpler
than heapify, just swap the bigger of two children with root if needed, you
need a separate function or a for loop I think.

【在 c**y 的大作中提到】
: Revised solution. Will try to test it if I get a chance today.
: void heapify(binaryTree *node)
: {
: if (!node) return;
: if (node->left)
: heapify(node->left);
: if (node->right)
: heapify(node->right);
: int max = node->value;
: if (node->left && node->left->value > max) {

c**y
发帖数: 172
14
The revised algorithm above follows the approach used in building a heap,
which consists of two parts: BUILD-MAX-HEAP (see page 157 of CLRC, 3rd
edition) and MAX-HEAPIFY (see page 154 of the same book).
The MAX-HEAPIFY algorithm is a recursive solution in that it calls itself
again in the pushdown part. Similarly, heapify is directly called by itself
in the above solution.
BUILD-MAX-HEAP restores the heap property in a bottom-up fashion.
Correspondingly, post-order is used to achieve the same bottom-up effect.

simpler
you

【在 s**x 的大作中提到】
:
: the pushdown (or siftdown) part should not call heapify, it is much simpler
: than heapify, just swap the bigger of two children with root if needed, you
: need a separate function or a for loop I think.

s**x
发帖数: 7506
15
I just reviewed some chapters and searched online as well. the MAX-HEAPITY
is called SiftDown in some other papers, which makes more sense.
MAX-HEAPITY is SiftDown
BUILD-MAX-HEAP is calling SiftDown/MAX-HEAPITY from bottom up.
your solution does not have a SiftDown Call, siftdown only touches one child
, while your heapify always touches both left and right children during a
recursive call.it might work too, but would be expensive.

itself

【在 c**y 的大作中提到】
: The revised algorithm above follows the approach used in building a heap,
: which consists of two parts: BUILD-MAX-HEAP (see page 157 of CLRC, 3rd
: edition) and MAX-HEAPIFY (see page 154 of the same book).
: The MAX-HEAPIFY algorithm is a recursive solution in that it calls itself
: again in the pushdown part. Similarly, heapify is directly called by itself
: in the above solution.
: BUILD-MAX-HEAP restores the heap property in a bottom-up fashion.
: Correspondingly, post-order is used to achieve the same bottom-up effect.
:
: simpler

s**x
发帖数: 7506
16
modifies Caiy's code:
void SiftDown(binaryTree *node)
{
if(!node) return;

binaryTree *largest = node;
if (node->left != NULL && largest->value < node->left->value)
largest = node->left;
if (node->right != NULL && largest->value < node->right->value)
largest = node->right;
if (largest == node) return;

int tmp = node->value;
node->value = largest->value;
largest->value = tmp;
SiftDown(largest);
}
void heapify(binaryTree *node)
{
if (!node) return;
if (node->left)
heapify(node->left);
if (node->right)
heapify(node->right);
SiftDown(node);
}
l********r
发帖数: 140
17
Given the root of a binary tree (not balanced), heapify it.
Am I right we msut need the parent pointer for each node to be more
efficient?
l********r
发帖数: 140
18
By the way, my algorithm is like (assume max heap):
for the current node, if left or right is bigger than the current, switch
the biggest value as the current value, and try to move it all the way up to
its parent chain if that is bigger than the parent's value as well.
Then do the same for its left and right children.
l********r
发帖数: 140
19
Any comments?
s*****n
发帖数: 994
20
It's still not balanced by your algorithm?
相关主题
T家电面一般有几轮? [UPDATE面经]今天面试问题:有一个整数数组,如何find kth smallest element,如果数据是dynamic
在版上看到的G题给一个最大堆,求最大的K个数,O(K) 算法?
吐槽一个面试Top K in N sorted array
进入JobHunting版参与讨论
l********r
发帖数: 140
21
Why not?
It starts with root, it guarantee the current node and its two children
satisfy the condition. Then it back check (heapify) all its parents, and
guarantee that. Then move down to its children. I just don't know whether it
is efficient or not.

【在 s*****n 的大作中提到】
: It's still not balanced by your algorithm?
s*****n
发帖数: 994
22
左右子节点都是balanced并不保证current node是balanced
比如左深度3 右深度100
c**y
发帖数: 172
23
Does Post-Order travel work here?
void heapify(binaryTree *node)
{
if (!node) return;
if (node->left)
heapify(node->left);
if (node->right)
heapify(node->right);
int max = node->value;
if (node->left && node->left->value > max) {
max = node->left->value;
}
if (node->right && node->right->value > max) {
max = mode->right->value;
}
if (max == node->value) {
return;
} else if (node->left && max == node->left->value) {
int tmp = node->value;
node->value = node->left->value;
node->left->value = tmp;
} else {
int tmp = node->value;
node->value = node->right->value;
node->right->value = tmp;
}
return;
}
l********r
发帖数: 140
24
"Given the root of a binary tree (not balanced), heapify it."
It asks to heapify, it does not ask to balance the tree.

【在 s*****n 的大作中提到】
: 左右子节点都是balanced并不保证current node是balanced
: 比如左深度3 右深度100

s**x
发帖数: 7506
25

I think you need push all the way down in the last swap step, thinking the
case root is the smallest value.

【在 c**y 的大作中提到】
: Does Post-Order travel work here?
: void heapify(binaryTree *node)
: {
: if (!node) return;
: if (node->left)
: heapify(node->left);
: if (node->right)
: heapify(node->right);
: int max = node->value;
: if (node->left && node->left->value > max) {

c**y
发帖数: 172
26
Thanks for pointing out the issue. I now know it.

【在 s**x 的大作中提到】
:
: I think you need push all the way down in the last swap step, thinking the
: case root is the smallest value.

g*********e
发帖数: 14401
27

but heap is a balanced tree

【在 l********r 的大作中提到】
: "Given the root of a binary tree (not balanced), heapify it."
: It asks to heapify, it does not ask to balance the tree.

c**y
发帖数: 172
28
Revised solution. Will try to test it if I get a chance today.
void heapify(binaryTree *node)
{
if (!node) return;
if (node->left)
heapify(node->left);
if (node->right)
heapify(node->right);
int max = node->value;
if (node->left && node->left->value > max) {
max = node->left->value;
}
if (node->right && node->right->value > max) {
max = mode->right->value;
}
if (max == node->value) {
return;
} else if (node->left && max == node->left->value) {
int tmp = node->value;
node->value = node->left->value;
node->left->value = tmp;
heapify(node->left);
} else {
int tmp = node->value;
node->value = node->right->value;
node->right->value = tmp;
heapify(node->right);
}
return;
}
s**x
发帖数: 7506
29

the pushdown (or siftdown) part should not call heapify, it is much simpler
than heapify, just swap the bigger of two children with root if needed, you
need a separate function or a for loop I think.

【在 c**y 的大作中提到】
: Revised solution. Will try to test it if I get a chance today.
: void heapify(binaryTree *node)
: {
: if (!node) return;
: if (node->left)
: heapify(node->left);
: if (node->right)
: heapify(node->right);
: int max = node->value;
: if (node->left && node->left->value > max) {

c**y
发帖数: 172
30
The revised algorithm above follows the approach used in building a heap,
which consists of two parts: BUILD-MAX-HEAP (see page 157 of CLRC, 3rd
edition) and MAX-HEAPIFY (see page 154 of the same book).
The MAX-HEAPIFY algorithm is a recursive solution in that it calls itself
again in the pushdown part. Similarly, heapify is directly called by itself
in the above solution.
BUILD-MAX-HEAP restores the heap property in a bottom-up fashion.
Correspondingly, post-order is used to achieve the same bottom-up effect.

simpler
you

【在 s**x 的大作中提到】
:
: the pushdown (or siftdown) part should not call heapify, it is much simpler
: than heapify, just swap the bigger of two children with root if needed, you
: need a separate function or a for loop I think.

相关主题
A家电面面经再上到题
求最快办法在 heap删除最后一个加入元素 然后在加入一个新元素请教几个面试问题
备考google onsite, 讨论堆排序的时间复杂度Bloomberg面经
进入JobHunting版参与讨论
s**x
发帖数: 7506
31
I just reviewed some chapters and searched online as well. the MAX-HEAPITY
is called SiftDown in some other papers, which makes more sense.
MAX-HEAPITY is SiftDown
BUILD-MAX-HEAP is calling SiftDown/MAX-HEAPITY from bottom up.
your solution does not have a SiftDown Call, siftdown only touches one child
, while your heapify always touches both left and right children during a
recursive call.it might work too, but would be expensive.

itself

【在 c**y 的大作中提到】
: The revised algorithm above follows the approach used in building a heap,
: which consists of two parts: BUILD-MAX-HEAP (see page 157 of CLRC, 3rd
: edition) and MAX-HEAPIFY (see page 154 of the same book).
: The MAX-HEAPIFY algorithm is a recursive solution in that it calls itself
: again in the pushdown part. Similarly, heapify is directly called by itself
: in the above solution.
: BUILD-MAX-HEAP restores the heap property in a bottom-up fashion.
: Correspondingly, post-order is used to achieve the same bottom-up effect.
:
: simpler

s**x
发帖数: 7506
32
modifies Caiy's code:
void SiftDown(binaryTree *node)
{
if(!node) return;

binaryTree *largest = node;
if (node->left != NULL && largest->value < node->left->value)
largest = node->left;
if (node->right != NULL && largest->value < node->right->value)
largest = node->right;
if (largest == node) return;

int tmp = node->value;
node->value = largest->value;
largest->value = tmp;
SiftDown(largest);
}
void heapify(binaryTree *node)
{
if (!node) return;
if (node->left)
heapify(node->left);
if (node->right)
heapify(node->right);
SiftDown(node);
}
c********p
发帖数: 1969
33
mark
x*****0
发帖数: 452
34
mark
c********p
发帖数: 1969
35
mark
x*****0
发帖数: 452
36
mark
x*****a
发帖数: 9
37
不太理解这里的heapify
比如说一个heap, 是一个complete binary tree. 所以其实他是balanced的.
如果不需要上面这个条件, 只是让他有heap的特征, 比如说root就是max或者min, post
order traversal就可以, 不需要parent pointer
s**x
发帖数: 7506
38
先读一读几个 heap sort 的算法就明白了。 不同的书用的术语也不一样, 核心是一
样的。 我感觉你取post order 也是一样的。不过没人那么说而已,本质都是先处理子
树,再处理根结点,处理根结点就是siftdown.

post
★ 发自iPhone App: ChineseWeb 8.6

【在 x*****a 的大作中提到】
: 不太理解这里的heapify
: 比如说一个heap, 是一个complete binary tree. 所以其实他是balanced的.
: 如果不需要上面这个条件, 只是让他有heap的特征, 比如说root就是max或者min, post
: order traversal就可以, 不需要parent pointer

1 (共1页)
进入JobHunting版参与讨论
相关主题
给一个最大堆,求最大的K个数,O(K) 算法?a very difficult interview question
Top K in N sorted arrayn个点,找出离原点最近的100个点
A家电面面经问两道google面试题
求最快办法在 heap删除最后一个加入元素 然后在加入一个新元素问个google面试题
备考google onsite, 讨论堆排序的时间复杂度问个题
再上到题Ask a google interview question
请教几个面试问题问一道数组题
Bloomberg面经T家电面一般有几轮? [UPDATE面经]
相关话题的讨论汇总
话题: node话题: value话题: heapify话题: max话题: left