2-3 trees Damien Filiatrault 03-23-2004

10
2-3 trees Damien Filiatrault 03-23-2004

description

2-3 trees Damien Filiatrault 03-23-2004. B-tree (data structure) - PowerPoint PPT Presentation

Transcript of 2-3 trees Damien Filiatrault 03-23-2004

Page 1: 2-3 trees Damien Filiatrault 03-23-2004

2-3 treesDamien Filiatrault

03-23-2004

Page 2: 2-3 trees Damien Filiatrault 03-23-2004

B-tree

(data structure)

Definition: A balanced search tree in which every node has between m/2 and m children, where m>1 is a fixed integer. m is the order. The root may have as few as 2 children. This is a good structure if much of the tree is in slow memory (disk), since the height, and hence the number of accesses, can be kept small, say one or two, by picking a large m.

source: http://www.nist.gov/dads/HTML/btree.html

Page 3: 2-3 trees Damien Filiatrault 03-23-2004

1,000,000,000 = instructions per second (1gigahertz)

7200 = hard disk rpm

120 = hard disk rps

0.00833333 = revolution time (seconds)

0.00416667 = seek time (1/2 revolution avg)

4,166,667 = cost of a seek in instructions

Why it is useful to use high-order b-trees saving application data on hard disk rather than in main memory:

Page 4: 2-3 trees Damien Filiatrault 03-23-2004

2-3 trees are a data structure commonly used to implement ordered lists of records. A 2-3 tree is a tree satisfying the following requirements:

- All internal nodes in the tree have either two or three children.

- All leaves of the tree are at the same level.

1 3 4 7 10 13 27

3 : x 7 : x 13 : 27

4 : 10

Example of a 2-3 tree:

Page 5: 2-3 trees Damien Filiatrault 03-23-2004

Each fork contains the following indices:

- the least element in child 2.

- the least element in child 3 (if not empty).

1 3 4 7 10 13 27

3 : x 7 : 8 13 : 27

4 : 10

8

Same tree after adding the element: 8

Fork

Leaf

Page 6: 2-3 trees Damien Filiatrault 03-23-2004

To insert a new leaf l in a 2-3 tree, locate the position where the new leaf should be inserted and add the new leaf to the tree. Call p the parent of the newly inserted leaf. If the number of children of p has increased from two to three, we still have a 2-3 tree and no further change is needed. If the number of children of p has increased from three to four, split p into two nodes with two children each, incrementing the number of children of p's parent. Proceed recursively up to the root of the tree, and, if needed, add a new root to augment the height of the tree by one.

1 3 4 7 10 13 27

3 : x 7 : 8 13 : 27

4 : 10

8

Same tree after adding the element: 9

source: http://theory.lcs.mit.edu/~miccianc/inccrypto/23trees.html

9

Page 7: 2-3 trees Damien Filiatrault 03-23-2004

To insert a new leaf l in a 2-3 tree, locate the position where the new leaf should be inserted and add the new leaf to the tree. Call p the parent of the newly inserted leaf. If the number of children of p has increased from two to three, we still have a 2-3 tree and no further change is needed. If the number of children of p has increased from three to four, split p into two nodes with two children each, incrementing the number of children of p's parent. Proceed recursively up to the root of the tree, and, if needed, add a new root to augment the height of the tree by one.

1 3 4 7 10 13 27

3 : x 7 : x 13 : 27

4 : 8

8

source: http://theory.lcs.mit.edu/~miccianc/inccrypto/23trees.html

9

9 : x

Insertion continued…

Page 8: 2-3 trees Damien Filiatrault 03-23-2004

To insert a new leaf l in a 2-3 tree, locate the position where the new leaf should be inserted and add the new leaf to the tree. Call p the parent of the newly inserted leaf. If the number of children of p has increased from two to three, we still have a 2-3 tree and no further change is needed. If the number of children of p has increased from three to four, split p into two nodes with two children each, incrementing the number of children of p's parent. Proceed recursively up to the root of the tree, and, if needed, add a new root to augment the height of the tree by one.

1 3 4 7 10 13 27

3 : x 7 : x 13 : 27

10 : x

8

source: http://theory.lcs.mit.edu/~miccianc/inccrypto/23trees.html

9

9 : x

Insertion completed.

4 : x

8 : x

Page 9: 2-3 trees Damien Filiatrault 03-23-2004

source: http://theory.lcs.mit.edu/~miccianc/inccrypto/23trees.html

Deletion:

The deletion of a leaf from a 2-3 tree is performed analogously. First the leaf is located and removed. Call p the parent of the removed leaf. If the number of children of p has decreased from three to two, the tree is still 2-3. If the number of children of p has decreased from two to one, merge p with one of its siblings, possibly reducing the number of children of p's parent. Proceed recursively, up to the root of the tree. If at the end the root has degree one, romove it and make its child become root.

Page 10: 2-3 trees Damien Filiatrault 03-23-2004

Complexity:

1+log3n <= height(n) <= 1+log2n

The time to search, insert or delete is O(log n).

source: http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Tree/23tree/