0% found this document useful (0 votes)
27 views

Unit-4 Tree Notes

Uploaded by

sarvesh.for.gdsc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Unit-4 Tree Notes

Uploaded by

sarvesh.for.gdsc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

Unit 5 Tree

General Tree
Tree Traversal
1) Pre-Order Traversal.
2) In Order Traversal
3) Post Order Traversal
child with min value of the given search interval. If greater, then repeat these steps for left child
otherwise, for right child.
Complexity O(logn)

10.18 B-Tree

A B-Tree is a self-balancing tree that maintains sorted data and allows sequential access,
insertion, deletion in logarithmic time.B-Tree is commonly used in database and file systems.

10.18.1 Need of Self Balancing Tree


BST is not a self-balancing tree, and if there exists a special case where nodes are only inserted
on right side or at left side, then it is known as a skewed tree. The complexity in such a scenario
for insertion, search, and deletionwill be O(n), where n is the total nodes in the tree. In order to
reduce the complexity of the above operations, self-balancing trees are required—for example,
AVL tree, B-Tree, Red Black Tree and many more. Here we are going to discuss B-Tree.

Skewed BST (Worst case complexity of operations) O(n)

B-Tree (Balanced m way tree)


B-Tree is a generalization of BST. Like BST the data is sorted in B-Tree but in B-Tree we can have
more than two children. Nodes in the B-Tree can have more than one key value (Restricted by
the parameter degree or order).

10.18.2 Properties of B-Tree


A B-Tree is defined by its Order (The maximum children a node can have). Usually, the order is
denoted by the term m.
1. Each node (in a B-Tree of order m) has atmost m children.
2. Each node (in a B-Tree of order m) has at most m-1keys.
3. A node with k children has k-1 keys. (1<=k<=m)
4. All leaf nodes are at the same level
5. Every node (except root node)has a restriction of containing at least (m-1)/2 keys.
6. Root node can have number of keys less than (m-1)/2 but at least one key.
7. Root has at least has two children (if it is not leaf).
8. Keys in the nodes are arranged in non-descending order (k1<=K2<=K3<= … <=Km)

For a B-Tree for order- m


1) Minimum children :
Root :2
Internal node :⌈𝑚/2⌉
2) Maximum children : m
3) Maximum keys : m-1
4) Minimum keys :
Root node :1
All others :⌈𝑚/2⌉ − 1
B-Tree can be useful when there would be large size of data that needs to be read or written.
For example, when we are working on databases, then there, we can make use of B-Tree.

10.18.3 Basic Operations on B-Tree

10.18.3.1 Insertion
All insertions take place in the leaf nodes. The search is followed starting from root node that
directs us to reach the target leaf node (same as in BST). If the target leaf node contains the
maximum keys, the key is virtually inserted in that node, and the node splits in two parts while
sending the median key up to the parent node. In this process, if the parent node also contains
the maximum key, it also splits in the same manner and the median key is sent up. The process
continues until we reach the parent that contains the keys less than maximum or to the root
node. If the root also contains the maximum keys, it also splits into two parts and the median
key is sent up. A new root node is created with this median key.

Example 1: Let us insert keys 1 to 10 in an empty B-Tree(Order 3).


Maximum children :3
Maximum keys :2
Insert 1

Insert 2

Insert 3
(Splitting after virtually inserting 3)

Insert 4
Insert 5
(Splitting after virtually inserting 5)

Insert 6,7
(Splitting after virtually inserting 7)

Insert 8
Insert 9

Insert 10

Example 2: Insert the following keys in an empty B-Tree of order 4.


1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
Maximum children in B-Tree of order 4 can be 4.
Maximum keys in one node can be 3.
Minimum keys can be 1.
Insertion of elements in B-Tree follows BST order properties. The key smaller than the given key
will go on the left side and the key greater will go on the right side. In the order 4 B-Tree, A
node can contain a maximum of three keys. After reaching to a maximum of three keys, while
insertion of 4th key, the tree splits and grows upwards.
Step 1
Insert 1: - first key 1 is inserted at root node.

Step 2
Insert 2: - The second key 2 is greater than 1 hence inserted next to 2.

Step 3
Insert 3: - The third key 3 is inserted to right of 2 as 3>2.

Step 4
Insert 4: Now the node has maximum keys so it splits from the middle and grows upwards. Here
2 will be the root node. Element smaller than 2 will be on LHS and greater than 2 will be on
RHS. Note that we have already discussed that root can have 1 key (minimum).

Step 5
Insert 5: - Now 5 will be inserted at leaf node, we search the correct position of 5 from root
node, here 5 is greater than 2 hence will be inserted as a right child of 2. Here 5 is inserted to
the right to key 4 as 5 > 4.
Step 6
Insert 6: - Now the node has maximum keys, so it splits from middle and grows upwards. Here 4
will shift in the root node. Keys less than 4 will be on LHS and greater than 4 will be on RHS.
Key 6 will be inserted as the Right child of node 4 and right to node 5 in leaf node.

Step 7
Insert 7: - Key 7 will be inserted as the Right child of node 4 and right to node 6 in leaf node.

Step 8
Insert 8: - Now the node is having maximum keys, so it splits from the middle and grows
upwards. Here 6 will shift towards the root node. Keys less than 6 will be on LHS and greater
than 6 will be on RHS.
Key 8 will be inserted as the Right child of node 6 and right to key 7 in leaf node.

Step 9
Insert 9: - Key 9 will be inserted as the Right child of node containing 6 and right to key 8 in the
leaf node.
Step 10
Insert 10: - Now the target leaf node is having maximum keys so it splits from the middle and
grows upwards. Here 8 will shift towards the root node. Now root node also contains maximum
keys; hence another splitting will occur and 4 will move upwards as a new root node of the
tree.
Element less than 4 will be on LHS and greater than 4 will be on RHS.
Node 10 will be inserted as the Right child of node containing key 8 and right to key 9 in leaf
node.

10.18.3.2 Deletion in B-Tree


The deletion in B-Tree can be done with the help of the following cases: -
• If node to be deleted is in leaf node.
• if node to be deleted is in internal node
Properties of B-Tree that need to be considered: -
Min keys for m order will be ceiling (m/2) -1. Max keys for m order will be m-1.
Min children for m order will be ceiling (m/2). Max children for m order will be m.

Case 1: If node to be deleted is in leaf node


Consider the following B-Tree of order 5. If we want to delete a node from Leaf node,
Figure: Initial Tree
Case – a: leaf node contains more than minimum numbers of keys.
For example, if we want to delete 23 key in the above tree. For this, we need first to locate 23.
We will start searching this from the root node. As 23 is less than 40, we will search on the left
side as the value is greater than 16 and less than 26 it will be lying on the right side of key 16 or
at left side of key 26.
Since the minimum number of keys in leaf node can be 2 so we deleted 23 that results in the
node having sufficient key.

Figure: Delete 23

Case – b: leaf node contains exactly the minimum number of keys.


In this case, we want to delete key 13. This key can be easily found on the left to left side of
root 40.

Since the leaf node contains the minimum number of keys, the solution to such case regards
the help of the following: -
a. Immediate Left Sibling node of the node containing key to be deleted,
b. Immediate Right Sibling node of the node containing key to be deleted.
c. The parent node of the node containing key to be deleted.
For case a & b, if the immediate sibling contains more than minimum keys, we can transfer keys
from sibling to sibling via parent node.
In case c, if both the immediate left and right sibling nodes contain minimum required keys
then we can merge these nodes with the parent node.
Here in the above case, the immediate right sibling consists of keys that are greater than
minimum required, so we can transfer the key via root node.

Figure: Delete 13
Take another case; if we want to delete the key 16 from the above formed tree, its immediate
right sibling contains the minimum number of keys. In this case, we merge the node which
contains key 16 with its right sibling along with bringing the separator key from the parent to
this node. After deletion, the tree will be like,

Figure: Tree after deleting 16


Case 2: If we want to delete a key from the Internal node.
If we want to delete any key from the internal node then the following cases could arise.
a) If the required node contains more than required number of keys, in such case, it can be
deleted and that key can be replaced with maximum key value from its left child or the
minimum key value of its right child. In this case, precedence will be given to that child whose
keys are greater than the minimum number of keys required.
Figure: Delete 82 from Tree
In the above case, we want to delete 82. We can either take 74key (maximum) from the left
child or 89 key(minimum) from the right child.

Figure: Tree after deleting key 82


Now, if we want to delete 89 from node, then in this case, 74 will be replaced as its Right child
now contains the minimum number of keys.

Figure: Tree delete 89

Figure: Tree after deleting 89


b) if the required node contains the minimum number of keys.

Figure: Delete key 34


If we want to delete 34 from node then as it does not have any child so it will take help of its
sibling.
The right sibling of this node contains 3 keys, more than 2, that is minimum one. Here 40 that is
root key will be transferred to deleted one i.e. 34 key, and 60 will be the new root.

Figure: Tree after deleting 34

10.18.4 Order Computation of a B-Tree


Consider the structure of a B-Tree Node. Each node has a key and Record address pair < Ki, Ri>
and the address to the child node (Ci). All the nodes in a B-Tree will have the same structure.

For a node having order m,


- There will be at most m Child nodes
- For each key, there will be a record address. Hence there are m-1 Keys and Record
address pairs i.e., < Ki, Ri>.
Let's assume that each key requires x Bytes for storage, each record address be of y Bytes and
each child pointer is of size z Bytes. Every B-Tree node should be adjusted in the Hard Disk
Sectors. As usual, size of each of the sectors in the Hard disk is 512 Bytes; each B-Tree node
should exactly fit into the Hard Disk sectors.
Size of each node = (m-1*)size of key+ (m-1)*size of Record address + (m)*size of Child node
Pointer
i.e. (m-1)*x + (m-1)*y + (m)*z <=512 Bytes
Question: Consider a B-Tree of Order m. Considering each key of 8 Bytes, Record address of 4
Bytes, and Child node Address of 9 Bytes, Compute the order of the B-Tree such that it gets
adjusted to exactly a Hard Disk Sector.
x = 8 Bytes
y= 4 Bytes
z= 9 Bytes
(m-1)*8+(m-1)*4+(m)*9 <= 512
21m – 12 <=512
21m <= 524
m <= 524/21
m<=24.95
As m cannot be a fraction, it should be 24

10.19 B+ Tree

B+ Tree is an m-way search Tree in which internal nodes behave like the indexes and leaf nodes
as data nodes.

10.19.1 Properties of B+Tree


For any order-m B+Tree,
1. Each node has at most m children.
2. Each node has at most m-1keys.
3. A node with k children has k-1 keys. (1<=k<=m)
4. All leaf nodes are at the same level
5. Every node (except root node) has a restriction of containing at least (m-1)/2 keys.
6. The root node can have the number of keys less than (m-1)/2 but at least one key.
7. Root has at least had two children (if it is not leaf).
8. Keys in the nodes are arranged in non-descending order (k1<=K2<=K3<= … <=Km)
9. Leaf nodes are connected with each other.

10.19.2 Difference between B-Tree and B+Tree


In B-Tree data is stored in leaf node as well as in the internal node. In B+ tree data is stored in
leaf node only.
Searching is slower in B-Tree, while searching is faster in B+ tree.
Deletion is complex in B-Tree as compared to deletion performed in B+ tree, which is quite
simple.
In B+ tree all leaf nodes are connected together like a linked list.
B+ Tree – leaf nodes are connected together.

B-Tree- all leaf nodes are not connected.

10.19.3 Insertion in B+Tree


Insertion in B+Treeis exactly like other search tree. For fast accessing, all the data lies in leaf
node. The internal node, on the other hand holds the index that guides to reach to the leaf
nodes that contain the data keys and record addresses.

Let us take an example where we insert the following keys in B+Tree of order 3:-
5, 10, 12, 14, 13, 1, 2, 3, 4.
Minimum children will be 2 Minimum keys will be 1
Maximum children will be 3 Max keys will be 2

Step 1
Insert 5
Since key 5 is the first value to be inserted, it can easily be placed into a node.

Step 2
Insert 10
Now key 10 is being inserted into root node and since maximum keys that can be inserted is 2,
it is inserted in increasing order into root node.

You might also like