优先级队列(Priority Queues)
优先级队列是存储具有自然顺序的元素的集合。
从优先级队列中删除项目总是产生最小的元素。
A priority queue is a collection that stores elements that have a natural order.
Removing an item from a priority queue always yields the least element.
优先级队列的主要操作是
add(E x):向优先级队列添加元素。
E removeMin():删除并返回队列中的最小元素。
The main operations on a priority queue are
add(E x) : adds an element to the priority queue.
E removeMin() : removes and returns the least element in the queue.
方法 | 描述 |
PriorityQueue<E>() | 此构造函数创建一个优先级队列,根据E的自然顺序对元素进行排序。 |
boolean add(E item) | 将项添加到优先级队列并返回true。 |
E poll() | 从优先级队列中删除并返回最小元素。 如果队列为空,则返回null。 |
E peek() | 返回当前位于队列头部的项目,但不删除它。 如果队列为空,则返回null。 |
int size() | 返回当前存储在此优先级队列中的项目数。 |
Method | Description |
PriorityQueue<E>() | This constructor creates a priority queue that orders elements according to the natural order of E. |
boolean add(E item) | Adds the item to the priority queue and returns true. |
E poll() | Removes and returns a minimum element from the priority queue. Returns null if the queue is empty. |
E peek() | Returns the item that is currently at the head of the queue, but does not remove it. Returns null if the queue is empty. |
int size() | Returns the number of items currently stored in this priority queue. |
堆排序
Heapsort是一种基于优先级队列的非常有效的排序方法。
给定列表或元素数组,将它们添加到最初为空的优先级队列。
从优先级队列中删除元素,一次一个。 它们按排序顺序排列。
Heapsort
Heapsort is a very efficient sorting method based on priority queues.
Given a list or array of elements, add them to an initially empty priority queue.
Remove the elements from the priority queue, one at a time. They come out in sorted order.
更多二进制树术语
设T为二叉树。
T中节点N的级别是从根到N的路径长度。
T的深度是T中节点的最大级别:这是从根到叶子的最长路径。
More Binary Tree Terminology
Let T be a binary tree.
The level of a Node N in T is the length of the path from the root to N.
The depth of T is the maximum level of a node in T: this is the longest path from the root to a leaf.
完整的二叉树
高效的添加操作需要二叉树,其深度尽可能小,以便树中的节点数量。
完整的二叉树符合此标准。
Complete Binary Trees
An efficient add operation needs a binary tree with depth as small as possible for the number of nodes in the tree.
A complete binary tree meets this criterion.
完成具有深度D的二叉树T,如果:
T对于每个级别L具有pow(2,L)节点,其中0 <= L <= D-1。 也就是说,除了最后一个级别之外的每个级别都具有可能的最大节点数。
D级的所有叶节点尽可能远离左侧。
A binary tree T with depth D is complete if
T has pow(2, L) nodes for each level L, where 0 <= L <= D-1. That is, each level other than the last has the maximum number of nodes possible.
All leaf nodes at level D are as far to the left as possible.
完整的二叉树( A Complete Binary Tree)
完整二叉树的深度(Depth of Complete Binary Trees)
如果我们忽略最后一级的节点,
然后,完整的二叉树完美平衡,因为在每个节点处,左子树具有与右子树相同的节点数。
If we disregard the nodes at the last level,
then a complete binary tree is perfectly balanced in that at each node, the left subtree has the same number of nodes as the right subtree.
如果树有N个节点,那么沿着从根到叶子的任何路径下降,每次我们下降到一个级别时节点数量减少大约减半。因此,最大级别数最多为log n + 1。
具有N个节点的完整二叉树的深度最多为log n + 1。