All Topics  
2-3-4 tree

 

   Email Print
   Bookmark   Link






 

2-3-4 tree



 
 
A 2-3-4 tree (also called a 2-4 tree), in computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, is a self-balancing data structure
Data structure

A data structure in computer science is a way of storing data in a computer so that it can be used efficiently. It is an organization of mathematical and logical concepts of data....
 that is commonly used to implement dictionaries
Associative array

An associative array is an abstract data type composed of a Collection of unique keys and a collection of values, where each key is associated with one value ....
. 2-3-4 trees are B-tree
B-tree

In computer science, a B-tree is a tree data structure that keeps data sorted and allows searches, insertions, and deletions in logarithmic Amortized analysis....
s of order 4; like B-trees in general, they can search, insert and delete in O(log n) time. One property of a (2, 4) tree is that all external nodes are at the same depth.
h child (p, q, r and s in the diagrams) is a possibly empty 2-3-4 subtree.






Discussion
Ask a question about '2-3-4 tree'
Start a new discussion about '2-3-4 tree'
Answer questions from other users
Full Discussion Forum



Encyclopedia


A 2-3-4 tree (also called a 2-4 tree), in computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, is a self-balancing data structure
Data structure

A data structure in computer science is a way of storing data in a computer so that it can be used efficiently. It is an organization of mathematical and logical concepts of data....
 that is commonly used to implement dictionaries
Associative array

An associative array is an abstract data type composed of a Collection of unique keys and a collection of values, where each key is associated with one value ....
. 2-3-4 trees are B-tree
B-tree

In computer science, a B-tree is a tree data structure that keeps data sorted and allows searches, insertions, and deletions in logarithmic Amortized analysis....
s of order 4; like B-trees in general, they can search, insert and delete in O(log n) time. One property of a (2, 4) tree is that all external nodes are at the same depth.

Background


Each datum in a 2-3-4 tree is called an element. These are grouped into nodes, which may be:
  • A 2-node containing 1 element and 2 children, or
    * A 3-node containing 2 elements and 3 children, or
    * A 4-node containing 3 elements and 4 children
Each child (p, q, r and s in the diagrams) is a possibly empty 2-3-4 subtree. The root node is the topmost node with no parent; it serves as a starting point when walking through the tree because every other node can be reached from it. A leaf node is a node with no children.

As with B-tree
B-tree

In computer science, a B-tree is a tree data structure that keeps data sorted and allows searches, insertions, and deletions in logarithmic Amortized analysis....
s, 2-3-4 trees are ordered: each element must be greater than or equal to any others to its left and in its left subtree. Each child then becomes an interval
Interval (mathematics)

In mathematics, a interval is a set of real numbers with the property that any number that lies between two numbers in the set is also included in the set....
 bracketed by the elements to its left and right. (p, q, r and s in the 4-node diagram above would have intervals (-8, a), (a, b), (b, c) and (c, 8).)

2-3-4 trees are an isometry
Isometry

In mathematics, an isometry, isometric isomorphism or congruence mapping is a distance-preserving isomorphism between metric spaces....
 of red-black trees, meaning that they are equivalent data structures. In other words, for every 2-3-4 tree, there exists at least one red-black tree with data elements in the same order. Moreover, insertion and deletion operations on 2-3-4 trees that cause node expansions, splits and merges are equivalent to the color-flipping and rotations in red-black trees. Introductions to red-black trees usually introduce 2-3-4 trees first, because they are conceptually simpler. 2-3-4 trees, however, can be difficult to implement in most programming languages because of the large number of special cases involved in operations on the tree. Red-black tree
Red-black tree

A red-black tree is a type of self-balancing binary search tree, a data structure used in computer science, typically used to implement associative arrays....
s are simpler to implement, so tend to be used instead.

Magic of the Lower Left Leaf. If you are a bit careful when doing fusions, the Lower Left Leaf is always the same node, from begin to end, when working with the tree. So the minimum element can be found in constant time O(1). By using in-order retrieval from that point of p elements the p lowest elements can be found in O(p log(n)) time.

Insertion


To insert a value, we start at the root of the 2-3-4 tree:
  1. If the current node is a 4-node:
    • Push the middle element of the 4-node up into the parent, leaving a 3-node.
    • Split the remaining 3-node up into a pair of 2-nodes.
    • 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.
  2. Find the child whose interval contains the value to be inserted.
  3. If the child is empty, insert the value into current node and finish.
    • Otherwise, descend into the child and repeat from step 1.


Example


To insert the value "25" into this 2-3-4 tree:
  • Begin at the root (10, 20) and descend towards the rightmost child (22, 24, 29). (Its interval (20, 8) contains 25.)


  • Node (22, 24, 29) is a 4-node, so its middle element 24 is pushed up into the parent node.
  • The remaining 3-node (22, 29) is split into a pair of 2-nodes (22) and (29). Ascend back into the new parent (10, 20, 24).


  • Descend towards the rightmost child (29). (Its interval (24, 8) contains 25.)
  • Node (29) has no rightmost child. (The child for interval (29, 8) is empty.) Stop here and insert value 25 into this node.

Deletion


Deletion is the more complex operation and involves many special cases.

First the element to be deleted needs to be found. The element must be in a node at the bottom of the tree; otherwise, it must be swapped with another element which precedes it in in-order traversal (which must be in a bottom node) and that element removed instead.

If the element is to be removed from a 2-node, then a node with no elements would result. This is called underflow. To solve underflow, an element is pulled from the parent node into the node where the element is being removed, and the vacancy created in the parent node is replaced with an element from a sibling node. (Sibling nodes are those which share the same parent node.) This is called transfer.

If the siblings are 2-nodes themselves, underflow still occurs, because now the sibling has no elements. To solve this, two sibling nodes are fused together (after pulling element from the parent node).

If the parent is a 2-node, underflow will occur on the parent node. This is solved by using the methods above. This may cause different parent node to sustain underflow as deletions and replacements are being made, referred to as underflow cascading.

Deletion in a 2-3-4 tree is O(log n), while transfer and fusion constant time, O(1).

See also


  • B tree
  • 2-3 tree
    2-3 tree

    A 2-3 tree in computer science is a type of data structure, a B-tree where every Node with children has either two children and one data element or three children and two data elements ....
  • Red-black tree
    Red-black tree

    A red-black tree is a type of self-balancing binary search tree, a data structure used in computer science, typically used to implement associative arrays....


External links