二叉堆应该是二叉树还是链表?
我有一个实现二进制堆的作业。但是,我不确定是否应该将二叉堆实现为二叉树数据结构或简单的双链表。
如果我应该实现为二叉树,我应该如何跟踪树的最后一个元素以便插入新元素?在链表中,这会容易得多。
那么,二叉堆一定是二叉树吗?如果是,如何跟踪最后一个元素?
注意:在我的作业中有这样一句话: 但是你不会将二叉堆实现为数组,而是 作为一棵树。
更清楚地说,这是我的节点:
struct Word{
char * word;
int count;
struct Word * parent;
struct Word * left_child;
struct Word * right_child;
}
I have an assignment to implement a binary heap. However, I'm not sure whether I should implement the binary heap as a binary tree data structure or a simple double linked list.
If I should implement as a binary tree, how should I keep track of the last element of the tree in order to insert a new element? In linked list that would be much easier.
So, does binary heap have to be a binary tree? If yes, how to track the last element?
Note: In my assignment there is a statement like this:
But you will implement the binary heap not as an array, but
as a tree.
To be more clear this is my node:
struct Word{
char * word;
int count;
struct Word * parent;
struct Word * left_child;
struct Word * right_child;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
取自问题的解决方案。
作者:@Yunus Eren Güzel
已解决:
经过五个小时的研究,我找到了一种将堆实现为基于指针的树的方法。
插入算法是:
Solution taken from the question.
by @Yunus Eren Güzel
SOLVED:
After five hours of study I have found a way to implement heap as a pointer based tree.
The insertion algorithm is :
根据定义,二叉堆是一棵二叉树。在 C 中实现此目的的一种方法是将树元素存储在数组中,其中数组索引对应于树元素(根节点编号为 0,其左子节点为 1,右子节点为 2,依此类推)。然后,您可以只存储堆的大小(在创建时初始化为 0,并在添加元素时递增)并使用它来查找下一个打开位置。
对于像这样的基本数据结构问题,维基百科是你的朋友。
A binary heap is, by definition, a binary tree. One way of implementing this in C is to store the tree elements in an array where the array index corresponds to the tree element (numbering the root node 0, its left child 1, its right child 2, and so on). You can then just store the size of the heap (initialized to 0 upon creation and incremented whenever an element is added) and use that to find the next open location.
For basic data structures questions like this, Wikipedia is your friend.
您应该将其实现为一棵树。这将是简单而有趣的。堆的唯一属性是,如果它是最大堆,则任何节点的值都小于或等于其父节点。
在数组实现中,我们施加了更多条件。
如果您需要有关具体功能实现的帮助,可以询问。
您需要向下移动以添加新节点
,用 root 调用它,要插入的值
insertBW(p, c) 是一个函数,它在 p 和 c 之间插入包含值 x 的节点
(我没有测试此代码,请检查错误)
You should implement it as a tree. It will be easy and interesting. Heap has only property that any node has value less than or equal to its parent, if it is a max heap.
In array implementation we impose some more conditions.
If you need help about specific function implementation then you can ask it.
You need to travel down to add new node
call it with root, value to be inserted
insertBW(p, c) is a function which insets a node containing value x between p and c
(I didn't tested this code please check for errors)
对我来说,这确实是一个家庭作业问题。看来你在提问之前没有自己做过任何研发(对不起,有点刺耳的话):)
在计算机科学中,堆是一种特殊的基于树的数据结构,满足堆属性:如果 B 是子节点A,则 key(A) ≥ key(B)。
我认为你的老师希望你实现一个优先级队列数据结构,这就是你在同一问题中同时讨论链表和堆的地方。优先级队列可以实现为堆或链表,其中根据优先级提取元素,您必须在链表的情况下维护已排序的元素,其中最大或最小元素位于前面,具体取决于您是否正在实现最大堆或最小堆或优先级队列可以简单地实现为堆数据结构。
来到最后一点,您说“但是您将不是将二进制堆实现为数组,而是实现为树。”似乎非常无关紧要。请再次检查所需内容或重现作业中提出的确切问题。
This to me really seems to be a homework question & it seems you have not done any R&D on your own before asking (sorry for bit harsh words):)
In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if B is a child node of A, then key(A) ≥ key(B).
I think your teacher wants you to implement a priority queue data structure and that's where you are talking about both a Linked List and Heap together in the same question. Priority Queue can be implemented as a Heap or a Linked List where in to extract elements based on priority either you have to maintain elements sorted in case of linked list where say a maximum or minimum element goes at the front based upon whether you are implementing a max heap or a min heap OR priority queue can be implemented simply as a heap data structure.
Coming to the last point where you say "But you will implement the binary heap not as an array, but as a tree.", seems to be very irrelevant. Please do check again as to what is required or reproduce the exact question that has been asked in your assignment.
简而言之,关于你的第一个问题——不。堆可以是任何东西(数组、链表、树,以及当一个人必须临时搭建一个毛茸茸的小猫家族时)。请注意堆的定义:如果“B”是“A”的子级,则 val(A) >= val(B) (或者,在最小堆的情况下,val(A) <= val( B))。
最常见的是将其称为树(也可以这样实现),因为很容易将其视为一棵树。此外,时间复杂度和性能都不错。
关于你的第二个问题,你没有提供任何信息,据我所知,搜索每个节点的解决方案与其他任何解决方案一样好......
为了获得更好的答案,需要更多信息(您有哪些限制,您应该支持哪些操作,等等......)
To put it simply, regarding your first question - no. A heap can be anything (array, linked list, tree, and when one must improvise a family of fluffy kittens). Note the definition of a heap: If "B" is a child of "A" then val(A) >= val(B) (or, in case of a min-heap, val(A) <= val(B)).
It's most common to refer to it as a tree (and also implement it as such) because it's easy to think of it as a tree. Also, the time-complexity & performance are good.
Regarding your second question, you gave no information, so as far as I know a solution that searches every node is as good as any other...
For any better answer, more information is required (what limitations do you have, what operations should you support, etc...)
二叉堆可以是任何东西,即数组、链表、树等。我们只需要保留正确的算法来访问数据。例如,如果您想使其成为左子节点,则可以通过 2N + 1(对于起始索引 0)执行此操作,其中 N 是父索引或右子节点 2N + 2对于最后一个元素,您可以使用变量 count 初始化堆,并在每次插入新元素时将其加 1,这样您就可以跟踪最后一个元素(与删除相同,只需进行一些修改)。关于集合)。
A binary heap can be anything i.e. array, linked list, tree, etc. We just have to keep the right algorithm on how can you can access the data. For example, if you want to make it to the left child you can do this by 2N + 1(For starting index 0) where N is the parent index or the right child by 2N + 2. For the last element, you can initialise the heap with a variable count and increment it by 1 every time you insert a new element, this way you can keep track of the last element (Same for delete, just some modification has to be made on the collection).