HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, a 2–3 tree is a
tree data structure In computer science, a tree is a widely used abstract data type that represents a hierarchical tree structure with a set of connected nodes. Each node in the tree can be connected to many children (depending on the type of tree), but must be conn ...
, where every
node In general, a node is a localized swelling (a "knot") or a point of intersection (a vertex). Node may refer to: In mathematics *Vertex (graph theory), a vertex in a mathematical graph *Vertex (geometry), a point where two or more curves, lines, ...
with children (
internal node In computer science, a tree is a widely used abstract data type that represents a hierarchical tree structure with a set of connected nodes. Each node in the tree can be connected to many children (depending on the type of tree), but must be conn ...
) has either two children (2-node) and one
data element In metadata, the term data element is an atomic unit of data that has precise meaning or precise semantics. A data element has: # An identification such as a data element name # A clear data element definition # One or more representation terms # ...
or three children (3-nodes) and two data elements. A 2–3 tree is a
B-tree In computer science, a B-tree is a self-balancing tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time. The B-tree generalizes the binary search tree, allowing for n ...
of order 3. Nodes on the outside of the tree (
leaf node In computer science, a tree is a widely used abstract data type that represents a hierarchical tree structure with a set of connected nodes. Each node in the tree can be connected to many children (depending on the type of tree), but must be con ...
s) have no children and one or two data elements. 2–3 trees were invented by
John Hopcroft John Edward Hopcroft (born October 7, 1939) is an American theoretical computer scientist. His textbooks on theory of computation (also known as the Cinderella book) and data structures are regarded as standards in their fields. He is the IBM Pro ...
in 1970. 2–3 trees are required to be balanced, meaning that each leaf is at the same level. It follows that each right, center, and left subtree of a node contains the same or close to the same amount of data.


Definitions

We say that an internal node is a 2-node if it has ''one'' data element and ''two'' children. We say that an internal node is a 3-node if it has ''two'' data elements and ''three'' children. A 4-node, with three data elements, may be temporarily created during manipulation of the tree but is never persistently stored in the tree. Image:2-3-4 tree 2-node.svg, 2 node Image:2-3-4-tree 3-node.svg, 3 node We say that is a 2–3 tree if and only if one of the following statements hold: * is empty. In other words, does not have any nodes. * is a 2-node with data element . If has left child and right child , then ** and are 2–3 trees of the same
height Height is measure of vertical distance, either vertical extent (how "tall" something or someone is) or vertical position (how "high" a point is). For example, "The height of that building is 50 m" or "The height of an airplane in-flight is abou ...
; ** is greater than each element in ; and ** is less than each data element in . * is a 3-node with data elements and , where . If has left child , middle child , and right child , then ** , , and are 2–3 trees of equal height; ** is greater than each data element in and less than each data element in ; and ** is greater than each data element in and less than each data element in .


Properties

* Every internal node is a 2-node or a 3-node. * All leaves are at the same level. * All data is kept in sorted order.


Operations


Searching

Searching for an item in a 2–3 tree is similar to searching for an item in a
binary search tree In computer science, a binary search tree (BST), also called an ordered or sorted binary tree, is a rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left subtree and ...
. Since the data elements in each node are ordered, a search function will be directed to the correct subtree and eventually to the correct node which contains the item. # Let be a 2–3 tree and be the data element we want to find. If is empty, then is not in and we're done. # Let be the root of . # Suppose is a leaf. #* If is not in , then is not in . Otherwise, is in . We need no further steps and we're done. # Suppose is a 2-node with left child and right child . Let be the data element in . There are three cases: #* If is equal to , then we've found in and we're done. #* If d < a, then set to , which by definition is a 2–3 tree, and go back to step 2. #* If d > a, then set to and go back to step 2. # Suppose is a 3-node with left child , middle child , and right child . Let and be the two data elements of , where a < b. There are four cases: #* If is equal to or , then is in and we're done. #* If d < a, then set to and go back to step 2. #* If a < d < b, then set to and go back to step 2. #* If d > b, then set to and go back to step 2.


Insertion

Insertion maintains the balanced property of the tree. To insert into a 2-node, the new key is added to the 2-node in the appropriate order. To insert into a 3-node, more work may be required depending on the location of the 3-node. If the tree consists only of a 3-node, the node is split into three 2-nodes with the appropriate keys and children. If the target node is a 3-node whose parent is a 2-node, the key is inserted into the 3-node to create a temporary 4-node. In the illustration, the key 10 is inserted into the 2-node with 6 and 9. The middle key is 9, and is promoted to the parent 2-node. This leaves a 3-node of 6 and 10, which is split to be two 2-nodes held as children of the parent 3-node. If the target node is a 3-node and the parent is a 3-node, a temporary 4-node is created then split as above. This process continues up the tree to the root. If the root must be split, then the process of a single 3-node is followed: a temporary 4-node root is split into three 2-nodes, one of which is considered to be the root. This operation grows the height of the tree by one.


Parallel operations

Since 2–3 trees are similar in structure to
red–black tree In computer science, a red–black tree is a kind of self-balancing binary search tree. Each node stores an extra bit representing "color" ("red" or "black"), used to ensure that the tree remains balanced during insertions and deletions. When the ...
s, parallel algorithms for red–black trees can be applied to 2–3 trees as well.


See also

*
2–3–4 tree In computer science, a 2–3–4 tree (also called a 2–4 tree) is a self-balancing data structure that can be used to implement dictionaries. The numbers mean a tree where every node with children (internal node) has either two, three, or fou ...
* 2–3 heap *
AA tree An AA tree in computer science is a form of balanced tree used for storing and retrieving ordered data efficiently. AA trees are named after Arne Andersson, the one who theorized them. AA trees are a variation of the red–black tree, a form of ...
*
B-tree In computer science, a B-tree is a self-balancing tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time. The B-tree generalizes the binary search tree, allowing for n ...
* (a,b)-tree * Finger tree


References


External links


2–3 Tree In-depth description
{{DEFAULTSORT:2-3 tree B-tree