コンテンツにスキップ

二分木

出典: フリー百科事典『ウィキペディア(Wikipedia)』
簡単な二分木。大きさ9、深さ3、根は値2を持つ

1: binary tree node22

使


[]


 edge leaf (external node)  (internal node) (depth) root  path-1(level)  (height)  (siblings) pqpq(ancestor)qp(descendant) (size) 

[]


2 (full binary tree)  (perfect binary tree, complete binary tree) 

Complete binary treenn n-1 

(almost complete binary tree) 

[]


 (connected)  (acyclic)  (vertex)  (degree) 3 13222

2 (forest) 

:



 a, b vva, b


[]

Pascal

 nilnull Pascal

type
  pNode = ^Node; {ノード型へのポインタ}
  Node = record
    data : (* 必要なデータをためる部分 *)
    left, parent, right : pNode
    {左の子、親、右の子へのポインタ}
  end;
...
var root, newnode : pNode;
...
begin
  new(root); root^.parent := nil;
  {根を植える。根には親がいない}
  new(newnode); {新しいノードをつくる}
  with newnode^ do begin
    left := nil; right := nil; {子はいない}
    parent := root {rootが親}
  end;
  root^.left := newnode; {左側の子に}
  new(newnode); {新しいノードをつくる}
  with newnode^ do begin
    left := nil; right := nil; {子はいない}
    parent := root {rootが親}
  end;
  root^.right := newnode {右側の子に}
...

0 i2i + 1 2i + 2  floor((i-1)/2) preorder traversal n h2h - n
配列に格納された小規模な完全二分木

ML3-tuplePascalC nil 

[]


調

[]


調調 (preorder)調調 (postorder) 調調調 (in-order) 調

[]


調調

[]



[]

[]


 (binary search tree) 

x

n
x = n  

x > n  調

x < n  調


[]



[]


      

[]




a b + c d - × e f + ÷

(a + b) × (c - d) ÷ (e + f)

÷ × + a b - c d + e f

調

[]

[]


 bit  2bit 

10Pascaldata[1] :
function EncodeSuccinct(node n, bitstring structure, array data) {
    if (n = nil) then
        structure の最後に 0 を追加する
    else         
         structure の最後に1を追加する
        data の最後に n.data を追加する
        EncodeSuccinct(n.left, structure, data)
        EncodeSuccinct(n.right, structure, data)
}

string structure  bit  structurestring:
function DecodeSuccinct(bitstring structure, array data) {
    structure の最初のビットを取り除き、そのビットをbに入れる
   if ( b = 1 ) then
        新しいノードnを作る
        dataの最初の要素を取り除き、その要素を n.data に入れる
        n.left = DecodeSuccinct(structure, data)
        n.right = DecodeSuccinct(structure, data)
        n を返す
    else
        nil を返す
}

便

N[]


 (ordered tree) LISPNnnNNnmNmNm



{B,C,D,E,F,G}6A
N進木の二分木表現
N

LISP

(((M N) H I) C D ((O) (P)) F (L))


参考文献[編集]

  • Donald Knuth. Fundamental Algorithms, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89683-4. Section 2.3, especially subsections 2.3.1–2.3.2 (pp.318–348).

関連項目[編集]