木 とヒープの基本
1導入
この
2根付 き木
3二分 ヒープ
が
4基本操作 と計算量
最小値参照 は根 を参照 するため時間 である。空 のヒープへの参照 は事前条件違反 とする。挿入 では配列末尾 に要素 を追加 し、親 より鍵 が小 さい間 、親 と交換 する。交換前 に不変条件 を破 り得 る辺 は追加要素 とその親 の間 だけであり、この違反 を根方向 へ移動 させるため、停止時 には不変条件 が回復 する。一段 の交換 を とすれば、最悪時 は時間 である。最小値削除 では、空 でないことを事前条件 とする。 なら根 を削除 して空 のヒープとする。 なら根 を削除 し、末尾要素 を根 へ移動 する。子 が存在 し、かつ現在要素 より小 さい鍵 の子 が存在 する間 、存在 する子 のうち最小鍵 の子 と交換 する。停止時 には現在要素 と子 の間 にも不変条件 が成立 し、交換 しなかった部分 の不変条件 は保存 される。最悪時 は時間 である。
Tree and Heap Basics
data/lecture/information/graph/graph-basics.lecture.n.md data/lecture/information/algorithm/data-structures/data-structures-portal.lecture.n.md1Introduction
This lecture defines parent-child relationships and depth in rooted trees and explains the invariant, basic operations, and complexity of binary heaps.
2Rooted trees
A tree is an undirected graph in which there is exactly one path between every pair of vertices. A rooted tree designates one vertex of a tree as its root. For each non-root vertex , the parent of is the vertex immediately after on the path from to the root. A vertex whose parent is is a child of . The root has no parent.
The depth of a vertex is the number of edges on the path from the root to . The root has depth , and a child has depth one greater than its parent. The height of a tree is the maximum depth of its vertices. A rooted tree in which every vertex has at most two children is a binary tree.
3Binary heaps
A complete binary tree with elements has every level except possibly the last filled, and its last-level vertices occupy consecutive positions from the left. Its height is for . It can be stored in level order in an array indexed from , where the children of index have indices and .
A min-heap has the shape of a complete binary tree and satisfies
for every edge. This heap invariant places an element with a minimum key at the root: applying the inequalities transitively along the path from the root to any vertex shows that the root key is no greater than that vertex's key. The invariant does not order siblings or vertices in different subtrees. A max-heap reverses the inequality.
4Basic operations and complexity
- Finding the minimum examines the root and therefore takes time. Accessing an empty heap violates a precondition.
- Insertion appends an element and swaps it with its parent while its key is smaller. Before a swap, only the edge from the inserted element to its parent can violate the invariant. Each swap moves this possible violation toward the root, so the invariant is restored when the process stops. If one level of swapping takes time, the worst-case time is .
- Deleting the minimum requires a nonempty heap. If , remove the root and leave an empty heap. If , remove the root and move the last element to the root. While a child exists with a key smaller than the current element, swap the current element with the minimum-key child among the children that exist. On termination the invariant also holds between the current element and its children, while all untouched edges have preserved it. The worst-case time is .
A min-heap can implement a priority queue, an abstract data type that supports insertion and access to or deletion of an element with minimum priority. A heap does not maintain all elements in sorted order, so it does not necessarily provide fast search for an arbitrary key.