Docsity

Trees - Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Trees - Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

2-3 trees

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.

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.

3 : x 7 : x 13 : 27

Example of a 2-3 tree:

Each fork contains the following indices:

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

3 : x 7 : 8 13 : 27

Same tree after adding the element: 8

Fork

Leaf

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- 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.

3 : x 7 : x 13 : 27

9 : x

Insertion continued…

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- 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.

3 : x 7 : x 13 : 27

10 : x

9 : x

Insertion completed.

4 : x

8 : x

Complexity:

1+log 3 n <= height(n) <= 1+log 2 n

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