2-3 Tree PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

2-3 TREES

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

namely left, middle and right.

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

1. To search a key K in given 2-3 tree T, we follow the following procedure:

Base cases:
1. If T is empty, return False (key cannot be found in the tree).

2. If current node contains data value which is equal to K, return True.

3. If we reach the leaf-node and it doesn’t contain the required key value K, return False.

Recursive Calls:

2. If K < currentNode.leftVal, we explore the left subtree of the current node.

3. Else if currentNode.leftVal < K < currentNode.rightVal, we explore the middle subtree of

the current node.


4. Else if K > currentNode.rightVal, we explore the right subtree of the current node.

Example:

Consider the following example: Searching 5 in following tree

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

become middle child of root node.


Step III we search element into current node and it does not contain element 5 and it is a leaf

node so we stop searching.

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

III.Splitting the Root


Example:

How do we insert32?

Final Result is:


DELETION OPERATION

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

You might also like