コンテンツにスキップ

木構造 (データ構造)

出典: フリー百科事典『ウィキペディア(Wikipedia)』

2023年10月17日 (火) 13:38; Glayhours (会話 | 投稿記録) による版(日時は個人設定で未設定ならUTC

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
親子構造

木構造(きこうぞう)とは、グラフ理論におけるに対応づけられるデータ構造である。

用語

[編集]

使

使[ 1]

0 (: child node)  (: parent node)  (: sibling node)  (: descendant node)  (: ancestor node) 1

 (: root node) 11辿

 (: leaf node) 

 (: internal nodeinner node) 

 (: height) 

 (: depth)  0 

 (: subtree)  T T (: proper subtree) 

 (: forest) 

子ノードの順序性

[編集]

 (: ordered tree) 2

実装方法

[編集]











使

走査法

[編集]
前順: F, B, A, D, C, E, G, I, H
間順: A, B, C, D, E, F, G, H, I
後順: A, C, E, D, B, H, I, G, F
レベル順: F, B, G, A, D, I, C, E, H

 (: traverse) 調1

深さ優先探索

[編集]

調調3

 (: pre-order)


(一)調

(二)

(三)

2

 (: in-order)


(一)

(二)調

(三)

2使

 (: post-order)


(一)

(二)

(三)調

幅優先探索

[編集]
レベル順 (: level-order)
幅優先探索は、深さが同じノードを浅い方から順に走査していく。

走査例

[編集]
2分探索木 この2分探索木において、
  • 前順走査での調査順: F, B, A, D, C, E, G, I, H
  • 間順走査での調査順: A, B, C, D, E, F, G, H, I
    • 2分探索木での間順走査は、ソートされた順となる。
  • 後順走査での調査順: A, C, E, D, B, H, I, G, F
  • レベル順走査での調査順: F, B, G, A, D, I, C, E, H

擬似コード

[編集]
前順(n)
    n を処理
    for each (n の子ノード i)
        前順(i)
間順(n)
    if (n に左の子ノードがあれば)
        間順(n の左の子ノード)
    n を処理
    if (n に右の子ノードがあれば)
        間順(n の右の子ノード)
後順(n)
    for each (n の子ノード i)
        後順(i)
    n を処理

使


レベル順(n)
    n をキューに追加
    while (キューに要素を含むなら)
        n ← キューから取り出す
        n を処理
        for each (n の子ノード i)
            i をキューに追加

糸付き2分木

[編集]
糸付き2分木

2 (: threaded binary tree) 使Joseph M. Morris 1979[1]2使使

2
Sub inorder(n∈node)
    Do While hasLeftChild(n)
        Let node ← node.left
    Loop
    Do
        visit(n)
        If hasRightChild(n) Then
            Let n ← n.right
            Do While hasLeftChild(n)
                Let n ← n.left
            Loop
        Else
            Let n ← n.right
        End If
    Loop While n ≠ Null
End Sub

主な操作

[編集]














木構造の種類

[編集]


 - 2
2

 - 3




 - 
2 - 2
AA

AVL22



2

T (T-tree)

 (splay tree)

Treap


B (B-tree)
B+B*

2-32-3-4








 - 




 (Suffix tree)


 (range tree)

 (segment tree)

 (interval tree)

R (Rectangle tree)

kd

コンピュータにみる木構造

[編集]

使

XML DOM

 

使

脚注

[編集]

注釈

[編集]


(一)^ 

出典

[編集]
  1. ^ Morris, Joseph M. (December 1979). “Traversing binary trees simply and cheaply”. Information Processing Letters 9 (5): 197-200. doi:10.1016/0020-0190(79)90068-1. 

関連項目

[編集]

参考文献

[編集]
  • Donald Knuth. The Art of Computer Programming: Fundamental Algorithms, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89683-4 . Section 2.3: Trees, pp.308–423.
  • Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7 . Section 10.4: Representing rooted trees, pp.214–217. Chapters 12–14 (Binary Search Trees, Red-Black Trees, Augmenting Data Structures), pp.253–320.
  • Dale, Nell. Lilly, Susan D. "Pascal Plus Data Structures". D. C. Heath and Company. Lexington, MA. 1995. Fourth Edition.
  • Drozdek, Adam. "Data Structures and Algorithms in C++". Brook/Cole. Pacific Grove, CA. 2001. Second edition.

外部リンク

[編集]