Sei sulla pagina 1di 24

2-3-4 Trees

Extended tree.
Tree in which all empty subtrees are replaced by new nodes that are called external nodes. Original nodes are called internal nodes.

Extended Binary Tree

external node

internal node

2-3-4 Tree Definition


A 2-3-4 tree is a search tree that either is empty or satisfies the following properties: Each internal node is a 2-node, 3-node or a 4-node. A 2-node has one element (key), a 3-node has two elements (keys) and a 4-node has three elements (keys). In case of 2-node: All keys in left subtree are smaller than this key. All keys in right subtree are bigger than this key.

2-3 Tree Definition


A 3-node has 2 keys and 3 children/subtrees; first key is smaller than second key.
All keys in left subtree are smaller than first key. All keys in middle subtree are bigger than first key and smaller than second key. All keys in right subtree are bigger than second key.

2-3 Tree Definition


A 4-node has 3 keys and 4 children/subtrees; first key is smaller than second key and second is smaller than the third.
key1<key2<key3 All keys in left subtree are smaller than key1. All keys in left-middle subtree are bigger than key1 and smaller than key2. All keys in right-mid subtree are bigger than key2 and less than key3. All keys in right tree are greater than key3.

All external nodes are on the same level.

2-3-4 Tree Definition


struct twoThreeNode { int dataL, dataM, dataR; // Three data fields twoThreeNode *left, *left-mid, *right-mid, *right; //the three children };

Example-1

Example-2

A leaf node has no children, but can still contain one, two, or three data items ( 2, 3, or 4 links); cannot be empty. In a 2-3-4 tree, nodes with a single link are NOT permitted; a node with one data item must have two links (unless its a leaf); nodes with two data items must have three children; nodes with three data items must have four children.

More on 2-3-4 Tree Organization


A B C

Points to nodes w/keys < A ; Nodes with key between A and <B

Nodes w/keys between B and < C Nodes w/keys > C

See below: (Equal keys not permitted; leaves all on same level; upper level nodes often not full; tree balanced! Its construction always maintains its balance, even if you add additional data items. (ahead)

50

2-node

30

2-node

60

70

80 4-node

10 20

40

55

62 64 66

75

83 86

Searching a 2-3-4 Tree


A very nice feature of these trees. You have a search key; Go to root. Retrieve node; search data items; If hit:
done.

Else
Select the link that leads to the appropriate subtree with the appropriate range of values. If you dont find your target here, go to next child. (notice data items are sequential.) Data will ultimately be
found or not found.

Try it: search for 64, 40, 65


50 2-node

30

2-node

60

70

80 4-node

10 20

40

55

62 64 66

75

83 86

Note: Nodes serve as holders of data and holders of indexes. Note: can easily have a no hit condition Note: the sequential nature after indexingsequential searching within node.

13

Efficiency of Search
Searching: 2-3-4 Trees: one node must be visited, but
More data per node / level. Searches are fast.
recognize all data items at node must be checked in a 2-3-4 tree, but this is very fast and is done sequentially.

All nodes in the 2-3-4 tree are NOT always full.

Insertion in 2-3-4 Trees


To insert a value, we start at the root of the 234 tree: If the current node is a 4-node:
Remove and save the middle value to get a 3-node. Split the remaining 3-node up into a pair of 2-nodes (the now missing middle value is handled in the next step). If this is the root node (which thus has no parent):
the middle value becomes the new root 2-node and the tree height increases by 1. Ascend into the root.

Otherwise, push the middle value up into the parent node. Ascend into the parent node.

Find the child whose interval contains the value to be inserted. If that child is a leaf, insert the value into the child node and finish.
Otherwise, descend into the child and repeat from step 1

Example of Insertion in a 2-3-4 Tree


B A B C A S T U V S T U V
C A

Another Insertion Example of 2-3-4 Tree

Insert: 2, 15, 12, 4, 8, 10, 25, 35, 55, .11


2 15 2 12 15
4 12 4 12

Insert 2
12

Insert 15
12

Insert 12

15

10

15

Split 4-node (2, 4, 8)


4 12 4 12

Insert 10

15

15

8 10

15 25

8 10

15 25 35

Split 4-node (2, 12, 15)


12

Insert 4

Insert 25
4 12 25

Insert 35

12

25

8 10

15

15

35

8 10

15

35 55

Split 4-node (15, 25, 35)

Insert 55

Example (Cont)
12 4 25

Insert 11, 9, 5
12 4 25

10

15

35

55

10 11

15

35

55

(4, 12, 25) Split 4-node (15, 25, 35)


12 4 10 25 4 10

Insert 11
12 25

11

15

35

55

11

15

35

55

Split 4-node (8, 10, 11)

Insert 9

Example (Cont)
12 4 10 25

Insert 5, 7
12 4 8 10 25

5 8 9

11

15

35

55

11

15

35

55

Insert 5
12 4 8 10

Split 4-node (5, 8, 9)

25

11

15

35

55

2-3-4 Trees: Splitting 4-nodes During Insertion


Figure
Splitting a 4-node whose parent is a 2-node during insertion

2-3-4 Trees: Splitting 4-nodes During Insertion


Figure 13.30
Splitting a 4-node whose parent is a 3-node during insertion

Deleting From 2-3-4 Tree


The deletion algorithm for a 2-3-4 tree
Locate the node n that contains the item theItem Find theItems inorder successor and swap it with theItem (deletion will always be at a leaf) If that leaf is a 3-node or a 4-node, remove theItem To ensure that theItem does not occur in a 2-node
Transform each 2-node encountered into a 3-node or a 4-node

Deleting From 2-3-4 Tree


Suppose the search is presently at node p and will move next to node q. The following cases need to be considered: (1) p is a leaf: The element to be deleted is either in p or not in the tree. (2) q is not a 2-node. In this case, the search moves to q, and no restructuring is needed. (3) q is a 2-node, and its nearest sibling, r, is also a 2-node.
if p is a 2-node, p must be root, and p, q, r are combined by reserving the 4-node being the root splitting transformation. if p is a 3-node or a 4-node, perform, in reverse, the 4-node splitting transformation.

(4) q is a 2-node, and its nearest sibling, r, is a 3-node. (rotation) (5) q is a 2-node and its nearest sibling, r, is a 4-node. (rotation)

Deletion Transformation when Nearest Sibling is 3-Node

Potrebbero piacerti anche