2-3 Tree PDF
2-3 Tree PDF
2-3 Tree PDF
DEFINATION
2-3 tree is a tree data structure in which every internal node (non-leaf node) has either one data
element and two children or two data elements and three children. If a node contains one data
element leftVal, it has two subtrees (children) namely left and middle. Whereas if a node
contains two data elements leftVal and rightVal, it has three subtrees
The main advantage with 2-3 trees is that it is balanced in nature as opposed to a binary search
tree whose height in the worst case can be O(n). Due to this, the worst case time-complexity of
operations such as search, insertion and deletion is 0(log(n)) as the height of a 2-3 tree
is 0(log(n)) .
SEARCH OPERATION
Base cases:
1. If T is empty, return False (key cannot be found in the tree).
3. If we reach the leaf-node and it doesn’t contain the required key value K, return False.
Recursive Calls:
Example:
Step1: We start with root node. So root node become current node . we search the element 5 in
root node
Step II Since root node does not contain element 5 and its lie between 2 and 9 so current node
INSERTION OPERATION
An insertion begins by performing a search to determine where the item would be located (if it
were present in the tree). The item is inserted as a leaf at this location. The new leaf’s parent p
may have either one or two data elements. If it has one data elements, we moves the middle
element up to its parent p and we are done. Otherwise, we replace p by two nodes p1,p2, where
the two leftmost children of p are placed under p1 and the two rightmost children are placed
under p2. Of course, the left to right order of the children is maintained. This operation is called
a node partition. This process is then repeated at each successively higher level of the tree
along the path from the inserted item to the root, as required to remove nodes with four
children. A special case arises if the root is replaced by two nodes; then a new root node is
created; its children are the two nodes newly formed from the old root.
I. Splitting a Leaf
II. Splitting an Internal Node
How do we insert32?
A deletion has a similar flavor. Again, a search is performed to find the item (stored at a leaf).
This leaf is deleted. Now the parent p has either one or two children. If it has two children, we
are done. Otherwise, if p has only one child, p’s siblings are checked; if an adjacent siblings has
three children’s gives pone of its children; if not s and p are merged into a single node. This
process is repeated at each successively higher level of the tree along the path from the deleted
item to the root, as required to remove nodes with one child. If the root ends up with one child,
the root is simply removed and its sole child becomes the new root.
Deleting Root