プリンキピア

第3章データ構造とアルゴリズム

Tree Data Structures木のデータ構造
A tree is a hierarchical data structure. While there are many kinds of tree data structures, all of them share the same basic organizing structure: a node represents an element in a tree or graph. A node may or may not have a descendant. A child node is a descendant of another node. Often, the primary node is referred to as the “parent node.” Trees maintain a hierarchy through parent-child relationships, which repeat from the root node at the top of the tree down to each leaf node, which is at the bottom of the tree and has no children. The height of a tree corresponds to the depth of the hierarchy of descendants. Figure 3.5 illustrates the structure and elements of a tree.
英語のヒント
木は階層的なデータ構造である。木のデータ構造には多くの種類があるが、どれも同じ基本的な構成を持つ。ノードは、木やグラフの要素を表す。ノードには子孫がある場合もない場合もある。子ノードは、別のノードの子孫である。元のノードは「親ノード」と呼ばれることが多い。木は親子関係によって階層を保つ。この関係が、木の最上部の根ノードから、最下部にあって子を持たない各葉ノードまで繰り返される。木の高さは、子孫の階層の深さに対応する。図3.5に、木の構造と要素を示す。
Figure 3.5

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 3.5 A tree is a hierarchical data structure with nodes where each node can have zero or more descendant child nodes. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図3.5 木はノードを持つ階層的なデータ構造で、各ノードには子孫となる子ノードが0個以上ある。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)
Binary Search Trees
二分探索木
A binary search tree is a kind of tree data structure often used to implement sets and maps with the binary tree property, which requires that each node can have either zero, one, or two children, and the search tree property, which requires that elements in the tree are organized least-to-greatest from left-to-right. In other words, the values of all the elements of the left subtree of a node have a lesser value than that of the node. Similarly, the values of all the elements of the right subtree of a node have a greater value than that of the node. The search tree property suggests that when elements are read left-to-right in a search tree, we will get the elements in sorted order. For numbers, we can compare and sort numbers by their numeric values. For more complex data like words or sentences, we can compare and sort them in dictionary order. Binary search trees use these intrinsic properties of data to organize elements in a searchable hierarchy (Figure 3.6).
英語のヒント
二分探索木は、集合やマップの実装によく使われる、木のデータ構造の一種である。各ノードの子が0個、1個、2個のいずれかであるという二分木の性質と、木の要素を左から右へ小さい順に並べるという探索木の性質を持つ。言い換えると、あるノードの左部分木に含まれるすべての要素の値は、そのノードの値より小さい。同様に、右部分木に含まれるすべての要素の値は、そのノードの値より大きい。探索木の性質から、探索木の要素を左から右へ読めば、整列された順序で要素を取り出せることがわかる。数は数値で比較し、並べ替えられる。単語や文のような複雑なデータは、辞書順で比較し、並べ替えられる。二分探索木は、データに内在するこうした性質を使って、要素を検索可能な階層に整理する(図3.6)。
Figure 3.6

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 3.6 A binary search tree organizes elements least to greatest from left to right. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図3.6 二分探索木は、要素を左から右へ小さいものから大きいものの順に並べる。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)
The tree illustrated satisfies the binary tree property based on the natural alphabetical order between letters since the elements in the tree are organized least to greatest from left to right. In other words, for a given list of letters (A, B, C, D, E, F, G), start at the middle of the list of letters with D (the root node) then pick B as the left sub-node of D which is at the middle of the list of letters (A, B, C) that is on the right of D and pick F as the right sub-node of D, which is at the middle of the list letters (E, F, G) that is on the left of D. Finally, organize the remaining letters under sub-nodes B and F to ensure that they are least-to-greatest from the left to right.
英語のヒント
図の木では、要素が左から右へ小さい順に並んでいるため、文字の自然なアルファベット順に基づく二分木の性質が満たされている。言い換えると、文字のリスト(A、B、C、D、E、F、G)が与えられたとき、まずリストの中央にあるDを根ノードとする。次に、Dの右側にある文字のリスト(A、B、C)の中央のBを、Dの左の子ノードに選ぶ。また、Dの左側にある文字のリスト(E、F、G)の中央のFを、Dの右の子ノードに選ぶ。最後に、残りの文字を子ノードBとFの下に配置し、左から右へ小さい順に並ぶようにする。
The search tree property is responsible for efficiency improvements over linear data structures. By storing elements in a sorted order in the search tree rather than in an indexed order in a list, binary search trees can more efficiently find a given element. Consider how we might look up words in a dictionary. A binary search tree dictionary storing all the terms and their associated definitions can enable efficient search by starting at the middle of the dictionary (the root node) before determining whether to go left or right based on whether we expect our word to appear earlier or later in the dictionary order. If we repeat this process, we can repeatedly rule out half of the remaining elements each time. Searching for a term in a list-based dictionary that is not sorted, on the other hand, would require us to start from the beginning of the list and consider every word until the end of the list since there is no underlying ordering structure to the elements.
英語のヒント
線形データ構造に比べて効率が向上するのは、探索木の性質による。リスト内でインデックス順に要素を保存する代わりに、探索木に整列順で保存することで、二分探索木は、ある要素をより効率よく見つけられる。辞書で単語を調べる場面を考えてみよう。すべての語と、それに対応する定義を保存する二分探索木の辞書では、辞書の中央、すなわち根ノードから始める。探す単語が辞書順で前にあるか後ろにあるかを予想し、それに応じて左へ進むか右へ進むかを決めることで、効率よく探索できる。この処理を繰り返すと、そのたびに残る要素の半分を候補から外せる。これに対し、並べ替えられていないリストによる辞書で語を探すには、リストの最初から最後まで、すべての単語を検討する必要がある。要素の並びの基礎となる順序構造がないからである。
Balanced Binary Search Trees
平衡二分探索木
Binary search trees are not as effective as we have described. The dictionary example represents a best-case scenario for binary search trees. We can only rule out half of the remaining elements each time if the binary search tree is perfectly balanced, which means that for every node in the binary search tree, its left and right subtrees contain the same number of elements. This is a strong requirement, since the order in which elements are added to a binary search tree determines the shape of the tree. In other words, binary search trees can easily become unbalanced. It is possible for a binary search tree to look exactly like a linked list, in which each node contains either zero children or one child, which is no more efficient than a linear data structure.
英語のヒント
二分探索木は、ここまで説明したほど有効とは限らない。辞書の例は、二分探索木にとって最も都合のよい場合を示している。毎回、残る要素の半分を除外できるのは、二分探索木が完全に平衡しているときだけである。これは、二分探索木のどのノードでも、左部分木と右部分木に同じ数の要素が含まれることを意味する。これは厳しい条件である。二分探索木の形は、要素を追加する順序で決まるからである。言い換えると、二分探索木は簡単に不平衡になる。各ノードに子が0個か1個しかない、連結リストとまったく同じ形になることもある。その場合、線形データ構造より効率がよいわけではない。
An AVL tree (named after its inventors, Adelson-Velsky and Landis) is a balanced binary search tree data structure often used to implement sets or maps with one additional tree property: the AVL tree property, which requires the left and right subtrees to be balanced at every node of the tree. AVL trees are just one among many “self-balancing” binary search trees. A balanced binary search tree introduces additional properties that ensure that the tree reorganizes elements to maintain balance (Figure 3.7).
英語のヒント
AVL木(考案者のアデルソン=ヴェルスキーとランディスにちなんで命名)は、集合やマップの実装によく使われる、平衡二分探索木のデータ構造である。AVL木の性質という、木に関する条件を1つ追加している。これは、木のすべてのノードで、左右の部分木が平衡していることを求める条件である。AVL木は、多くの「自己平衡」二分探索木の1つにすぎない。平衡二分探索木では、平衡を維持するように要素を組み替えることを保証する、追加の性質を導入する(図3.7)。
Figure 3.7

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 3.7 An AVL tree rotates nodes in a binary search tree to maintain balance. This sequence of steps illustrates the insertion of numbers 1, 2, 3, 4, 5, 6 into an initially empty AVL tree. (The steps in which rotation occurs are represented by the solid black arrows.) (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図3.7 AVL木は、二分探索木のノードを回転させて平衡を維持する。この一連の手順は、最初は空のAVL木に、数1、2、3、4、5、6を挿入する様子を示す。(回転が起きる手順は、黒い実線の矢印で表している。)(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)
Balanced binary search trees such as AVL trees represent just one approach for ensuring that the tree never enters a worst-case situation. There are many other balanced binary search tree data structures in addition to AVL trees. Balanced binary search trees can also be used to implement the priority queue abstract data type if the elements are ordered according to their priority value. But balanced search trees are not the only way to implement priority queues.
英語のヒント
AVL木などの平衡二分探索木は、木が最悪の状況に決して陥らないようにする方法の1つである。AVL木のほかにも、平衡二分探索木のデータ構造は多数ある。要素を優先度の値に従って並べれば、平衡二分探索木で優先度付きキューの抽象データ型を実装することもできる。しかし、優先度付きキューの実装方法は、平衡探索木だけではない。
Binary Heaps
二分ヒープ
Priority queues focus on retrieving and removing the highest-priority elements first, adding an element to a priority queue also involves specifying an associated priority value that is used to determine which elements are served next. For example, patients in an emergency room might be served according to the severity of their health concerns rather than according to arrival time. A binary heap is a type of binary tree data structure that is also the most common implementation for the priority queue abstract data type (Figure 3.8). A binary heap is not a search tree, but rather a hybrid data structure between a binary tree and an array list. Data is stored as an array list in memory, but the binary heap helps visualize data in the same way that a binary tree does, which makes it easier to understand how data are stored and manipulated. Binary heaps organize elements according to the heap property, which requires that the priority value of each node in the heap is greater than or equal to the priority values of its children. The heap property suggests that the highest-priority element will always be the root node where it is efficient to access.
英語のヒント
優先度付きキューは、優先度の高い要素から順に取得し、削除することを重視する。そのため、優先度付きキューに要素を追加するときは、次にどの要素を処理するかを決めるための、対応する優先度の値も指定する。たとえば救急室では、到着時刻ではなく、健康上の問題の深刻さに応じて患者に対応することがある。二分ヒープは二分木のデータ構造の一種であり、優先度付きキューの抽象データ型を実装する、最も一般的な方法でもある(図3.8)。二分ヒープは探索木ではなく、二分木と配列リストを組み合わせたデータ構造である。データはメモリ内では配列リストとして保存されるが、二分ヒープでは二分木と同じようにデータを視覚化できるため、保存や操作の仕組みを理解しやすくなる。二分ヒープは、ヒープの性質に従って要素を整理する。この性質では、ヒープの各ノードの優先度の値が、その子の優先度の値以上であることを求める。ヒープの性質から、優先度が最も高い要素は、常に効率よくアクセスできる根ノードにあることがわかる。
Figure 3.8

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 3.8 A binary heap is the most common implementation of the priority queue abstract data type. The priority value of each node in the binary heap is greater than or equal to the priority values of the children. Note that the value stored in the root node of the right subtree can be smaller than the value stored in any node in the left subtree, while not violating the heap property. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図3.8 二分ヒープは、優先度付きキューの抽象データ型を実装する最も一般的な方法である。二分ヒープの各ノードの優先度の値は、その子の優先度の値以上である。右部分木の根ノードに保存される値が、左部分木のどのノードの値より小さくても、ヒープの性質には違反しない点に注意しよう。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)