Top 10 ArticlesLS-StudioGayRomeo Justus_Dahinden Mercedes Benz OM601 Diyanet İşleri Başkanlığı Radically 25 Ral color system RTLnow.de New concept Electromagnetic compatibility |
News: |
A 2-3-4 tree (also called a 2-4 tree), in computer science, is a self-balancing data structure that is commonly used to implement dictionaries. 2-3-4 trees are B-trees of order 4; like B-trees in general, they can search, insert and delete in O(log n) time.
Contents |
Each datum in a 2-3-4 tree is called an element. These are grouped into nodes, which may be:
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-trees, 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 bracketed by the elements to its left and right. (p, q, r and s in the 4-node diagram above would have intervals (-∞, a), (a, b), (b, c) and (c, ∞).)
2-3-4 trees are an isometry 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 trees are simpler to implement, so tend to be used instead.
To insert a value, we start at the root of the 2-3-4 tree:
To insert the value "25" into this 2-3-4 tree:
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).[1][3]
|
Custom Search
|
© Copyright 2011 WorldLingo All rights reserved.