markdown
木とヒープの基本md a0aac15
lecture/information/algorithm/data-structures/tree-and-heap-basics.lecture.n.md
Download PDF

とヒープの基本きほん

date2026-07-14document_iddoc_6d4933a65f37e16b776fa98af494e734description根付き木の親子関係と深さを定義し、二分ヒープの不変条件、基本操作、計算量を説明する。prerequisitesグラフの基本 / スタックとキューの基本 / 計算量の基本 / 再帰の基本type講義statusactiverelateddata/lecture/information/algorithm/data-structures/data-structures-portal.lecture.n.md / data/lecture/information/algorithm/foundation/sorting-basics.lecture.n.md
algorithmdata-structuresundergraduatelecture
data/lecture/information/graph/graph-basics.lecture.n.md data/lecture/information/algorithm/data-structures/data-structures-portal.lecture.n.md

1導入どうにゅう

この講義こうぎでは、根付ねつ親子関係おやこかんけいふかさを定義ていぎし、二分にぶんヒープの不変条件ふへんじょうけん基本操作きほんそうさ計算量けいさんりょう説明せつめいする。

2根付ねつ

は、任意にんい二頂点間にちょうてんかんみちがただひと存在そんざいする無向むこうグラフである。根付ねつは、一頂点いちちょうてんrootとして指定していした構造こうぞうである。でない頂点ちょうてん v について、v からへのみちv直後ちょくごにある頂点ちょうてんvおやparentという。おやv である頂点ちょうてんvchildという。おや存在そんざいしない。

頂点ちょうてん vふかdepthは、から v へのみち辺数へんすうである。ふかさは 0 であり、ふかさはおやふかさより 1 おおきい。たかheight頂点ちょうてんふかさの最大値さいだいちである。各頂点かくちょうてん高々たかだか 2 である根付ねつ二分木にぶんぎbinary treeという。

3二分にぶんヒープ

n 要素ようそをもつ完全二分木かんぜんにぶんぎcomplete binary treeは、最下段さいかだんのぞ各段かくだん充足じゅうそくされ、最下段さいかだん頂点ちょうてんひだりから連続れんぞくしている二分木にぶんぎである。この形状けいじょうたかさは log2nn>0)であり、添字そえじ 0 からはじまる配列はいれつ段階順だんかいじゅん格納かくのうできる。添字そえじ i添字そえじ2i+12i+2 である。

最小さいしょうヒープmin-heapは、完全二分木かんぜんにぶんぎ形状けいじょうをもち、各辺かくへんについて

key(parent(v))key(v)

成立せいりつする。このヒープ不変条件ふへんじょうけんheap invariantにより、全要素ぜんようそ最小鍵さいしょうかぎをもつ。実際じっさいから任意にんい頂点ちょうてんへのみち沿って不等式ふとうしき推移的すいいてき適用てきようすると、かぎはその頂点ちょうてんかぎ以下いかである。ただし、兄弟間きょうだいかんことなる部分木間ぶぶんぎかん順序じゅんじょ規定きていされない。最大さいだいヒープは不等号ふとうごうぎゃくにした構造こうぞうである。

4基本操作きほんそうさ計算量けいさんりょう

  • 最小値参照さいしょうちさんしょう参照さんしょうするため Θ(1) 時間じかんである。からのヒープへの参照さんしょう事前条件違反じぜんじょうけんいはんとする。
  • 挿入そうにゅうでは配列末尾はいれつまつび要素ようそ追加ついかし、おやよりかぎちいさいあいだおや交換こうかんする。交換前こうかんまえ不変条件ふへんじょうけんやぶへん追加要素ついかようそとそのおやあいだだけであり、この違反いはん根方向ねほうこう移動いどうさせるため、停止時ていしじには不変条件ふへんじょうけん回復かいふくする。一段いちだん交換こうかんO(1) とすれば、最悪時さいあくじO(logn) 時間じかんである。
  • 最小値削除さいしょうちさくじょでは、からでないことを事前条件じぜんじょうけんとする。n=1 なら削除さくじょしてからのヒープとする。n>1 なら削除さくじょし、末尾要素まつびようそ移動いどうする。存在そんざいし、かつ現在要素げんざいようそよりちいさいかぎ存在そんざいするあいだ存在そんざいするのうち最小鍵さいしょうかぎ交換こうかんする。停止時ていしじには現在要素げんざいようそあいだにも不変条件ふへんじょうけん成立せいりつし、交換こうかんしなかった部分ぶぶん不変条件ふへんじょうけん保存ほぞんされる。最悪時さいあくじO(logn) 時間じかんである。

最小さいしょうヒープは優先度付ゆうせんどつきキューpriority queue実装じっそうできる。優先度付ゆうせんどつきキューは、要素ようそ挿入そうにゅう最小優先度要素さいしょうゆうせんどようそ参照さんしょう削除さくじょ提供ていきょうする抽象ちゅうしょうデータがたである。ヒープは全要素ぜんようそ整列順せいれつじゅん保持ほじしないため、任意鍵にんいかぎ探索たんさく高速こうそくになるとはかぎらない。

Tree and Heap Basics

data/lecture/information/graph/graph-basics.lecture.n.md data/lecture/information/algorithm/data-structures/data-structures-portal.lecture.n.md

1Introduction

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 v, the parent of v is the vertex immediately after v on the path from v to the root. A vertex whose parent is v is a child of v. The root has no parent.

The depth of a vertex v is the number of edges on the path from the root to v. The root has depth 0, 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 n elements has every level except possibly the last filled, and its last-level vertices occupy consecutive positions from the left. Its height is log2n for n>0. It can be stored in level order in an array indexed from 0, where the children of index i have indices 2i+1 and 2i+2.

A min-heap has the shape of a complete binary tree and satisfies

key(parent(v))key(v)

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 Θ(1) 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 O(1) time, the worst-case time is O(logn).
  • Deleting the minimum requires a nonempty heap. If n=1, remove the root and leave an empty heap. If n>1, 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 O(logn).

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.

raw .n.md をコピー
loc をコピー (filepath:line ~ line)
copy share link
copy encoded share link
path をコピー
copy share link
copy encoded share link
copy share link
copy encoded share link
タブを全て閉じる