2-3 Trees - WordPress.com

2-3 Trees
 The basic idea behind maintaining a search tree is a make the insertion,
deletion and searching operations efficient.
 In AVL trees the searching operation is efficient.
 However, insertion and deletion involves rotation that makes the
operation complicated.
 To eliminate this complication a data structure called 2-3 tree can
used.
 To build a 2-3 tree there are certain rules that need to be followed.
 These rules are as follow:
 All the non-leaf nodes in a 2-3 tree must always have two or
three non-empty child nodes that are again 2-3 trees.
 The level of all the leaf nodes must always be the same.
 One single node can contain either one or two values.
 If any node has two children (left and right) then the node
contains single data. The data occurring one left sub-tree of that
node is less than the data of the node and the data occurring on
right sub-tree of that node is greater than the data of the node.
 If the node has three children (left,middle and right) then the
node contains two data values. Let say i and j, where i<j. The
data of all the nodes on the left sub-tree are less than i. The data
of all the nodes are the middle sub-tree are greater than i but
less than j and the data of all the nodes on the right sub tree are
greater than j.
20
6 , 15
3,5
9
24
17
23
27,33
Example of 2-3 tree
Operation on 2-3 tree:
 Searching
 Insertion
 Deletion
Prepared By:-SWETA JETHAVA
2-3&b-tree&b+-tree Notes
`
1
Searching Operation:
 The process of searching data in a 2-3 tree starts from the root node of
the tree.
 Suppose we want to search value 17.
 It is first compared with the root node of the tree that is 20.
 And comparison process will process up to searching node either leaf
node of the tree.
Insertion Operation:
 To insert a value in a 2-3 tree we first need to search the position
where the value can be inserted, and then the value and the node in
which the value is to be inserted are adjusted.
 Suppose we want to insert a value 2.
 To insert the value first we need to search the appropriate position for
the value.
 In our case 2 will be insert at the 3, 5 node.
 Here this node contain already two values which is violates the 2-3
tree.
 To gain make it a 2-3 tree, the medium of the three values 2,3 and 5 is
taken the that middle value is shifted to the parent of this node.
 So again the same process of finding the median of the tree values and
shifting that value to the parent nodes is repeated.
 Finally all the nodes satisfy the conditions of a 2-3 tree.
Deletion Operation:
 Deletion of a value from a 2-3 tree is exactly opposite to insertion.
 In case of insertion the node where the data is to be inserted is split if
it already contains maximum number of values(that is two values).
 But in case of deletion two nodes are merged if the node of the value
to be deleted contains minimum number of values(that is only one
value).
 Suppose we want to delete 17 value from tree.
 Here, its parent hold values 6,15 and 15 is predecessor of 17.
 Hence 17 is replaced by 15 ,17 is remove from there and then merged
with its sibling that is with 9.
Prepared By:-SWETA JETHAVA
2-3&b-tree&b+-tree Notes
2
B- Trees
 The number of values that a particular node of a binary search tree or
an AVL tree can hold is only one.
 On the other hand a 2-3 tree can contain at the most two values per
node.
 To improve the efficiency of operations performed on a tree we need
to reduce the height of the tree.
 Another problem arises when the data is stored in secondary storage
medium.
 The time required to access the data from a secondary storage medium
is very high.
 Hence if we access the data less number of times less would be the
time required to perform an operation.
 If a node contains more number of values then at a time more number
of values then at a time more values can be accessed from the
secondary medium.
 To improve the efficiency of tree operations Multi-way search trees
can be used.
What is Multi-Way Search Trees (m-way tree):
 A multi-way tree of order n is a tree in which any node may contain
maximum n-1 values and can have maximum of n children.
 In a multi-way three of order 4(or a 4-way tree) any node can contain
maximum three values in each node and four children.
27,42
2,13,22
32,30
47,51
Example of Multi-way tree of order 4.
Prepared By:-SWETA JETHAVA
2-3&b-tree&b+-tree Notes
3
Definition of B-Tree:
 B-Tree is a multi-way search tree of order n that satisfies the following
conditions:
 All the non-leaf nodes (except the root node) have at least n/2 children
and at the most n children.
 The non-leaf root node may have at the most n non-empty child and at
least two child nodes.
 A B-tree can exist with only one node that is the root node containing
no child.
 If a node has n children then it must have n-1 values. All the values of
a particular node are in increasing order.
 All the values that appear on the left most child of a node are smaller
than the first value of that node. All the values that appear on the right
most child of a node are greater than the last values of the node.
 If x and y are any two i and i+1 values of an node, where x<y then all
the values appearing on the i+1 sub tree of that node are greater than x
and less than y.
 All the leaf nodes should appear on the same level
How to create B-Tree of order 4:
 27,42,22,47,32,2,51,40,13 to create 4-way tree.
27,42
2,13,22
32,30
47,51
B-Tree of order 4
Insertion Operation:
 To insert a value in a B-tree firstly we need to search the position of
the node where the value can be inserted and then the value and the
node are adjusted if required.
 The node where the new value is to be inserted would always be the
leaf node.
 Suppose we want inserted in a B-Tree of order 4.
 There is two possibility of insertion:
(a) The leaf node is not full(doesn’t contain 3 values).
(b) The leaf node is full(contain 3 values).
Prepared By:-SWETA JETHAVA
2-3&b-tree&b+-tree Notes
4
Case (a):
 In above B-tree we want insert 37
 Firstly to find position of new value.
Case (b):
 If leaf node is full then that node is split into two nodes.
 If m is the order of the tree then any node is always split after the
value m/2-1.
 As a result, the first part of the node contains the first m/2-1 values
and m/2 children, if any.
 And the second part of the node contains the last m-m/2 values and mm/2+1 children, if any.
 Then the m/2 values is moved up to the parent node and the new value
is appropriately attached to one of the two split nodes.
 In the parent node is full then the same procedure is repeated.
 In above B-tree we want insert 19.
 First to find position of 19.
 In our case 19 is inserted at 2,13,22 node.
 Here this node contain already maximum values , so there required
split into two nodes.
 Here, after splitting the node is divided into two part, so first part 4/21=1 values that is 2 become first value in this node and 4/2=2 children
if any.
 Second part 4-4/2=2 values that is one is 22 in this node and 44/2+1=3 children if any.
B+ Tree:
 One of the popular techniques for implementation index sequential
organisation is to use a variation on the basic known as B+- Tree.
 In B+ tree all the leaves have been connected to form a linked list of
the keys in sequential order.
 The b+ tree has two parts: the index part is the interior nodes and
sequence set. Interior nodes are the branch nodes from ‘a’ through ‘c’
(form the index part); nodes‘d’ through ‘h’ form the sequence set.
 The linked leaves are an excellent aspect of B+ tree; the keys can be
accessed efficiently both directly & sequentially.
 B+ tree is used to provide indexed sequential file organization, the key
value in the sequence set are the key values in the record collection,
the key values in the index part exist solely for internal purposes of
directing access to the sequence set.
Prepared By:-SWETA JETHAVA
2-3&b-tree&b+-tree Notes
5
a
b
d
c
e
f
g
h
Example of B+ Tree of order 3
B vs. B+ Tree
 B-tree: fulfils the desired requirements of the ideal tree
 Structure:
o Similar structure with the generic search tree
 B+-tree: enhanced B-tree
 Structure:
o Data values and pointers stored in leaf nodes
o Internal nodes contain only some data values for navigating
o Internal nodes at most k-1 data values, & k pointers. This has
the advantage that is all the leaves are linked together
sequentially; the entire tree may be scanned without visiting the
higher nodes at all.
Prepared By:-SWETA JETHAVA
2-3&b-tree&b+-tree Notes
6