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

UNIT 3b Tree

Uploaded by

Nikhil Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

UNIT 3b Tree

Uploaded by

Nikhil Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 77

DATA STRUCTURE

Unit 3: Linked Lists and Trees ( 8 L)


• Linked Lists:
• Introduction to Dynamic Memory Allocation,
• Representation and Implementation of Single, Double, and Circular Linked Lists
• Operations on Linked List: Insert, Delete, Traverse etc.
• Applications of Linked List,
• Linked List representation of Stack and Queue.

• Trees:
• Basic Tree terminologies,

• Types of Trees:

• Binary Tree, Binary Search Tree (BST), AVL Tree, B-Tree, and Heap.

• Representation and Implementations of different types of trees,

• Tree Traversal algorithms,

• Operation on trees: Insert, Delete, etc., Applications of Tress.


Tree
 A tree is a widely used abstract data type that simulates a hierarchical tree
structure, with a root value and subtrees of children with a parent node,
represented as a set of linked nodes.
A tree is a nonlinear data structure, compared to arrays, linked lists, stacks and
queues which are linear data structures.
 Definition:
 A tree data structure can be defined recursively as a collection of nodes
(starting at a root node), where each node is a data structure consisting of a
value, together with a list of references to nodes (the "children"), with the
constraints that no reference is duplicated, and none points to the root.
 A tree can be empty with no nodes or a tree is a structure consisting of one
node called the root and zero or one or more subtrees.
Tree
 Basic Terminologies:
 Path: A path is a sequence of edges between nodes.
 Root: The root is the special node from which all other nodes "descend". Each tree has a
single root node.
 Parent of node n: The parent of node n is the unique node with an edge to node n and which
is the first node on the path from n to the root.
o The root is the only node with no parent.
o Except for the root, each node has one parent.
 Child of node n: A child of node n is a node for which node n is the next node on the path to
the root node.
o More formally, if the last edge on the path from root r of tree T to node x is (y, x) then
node y is the parent of node x and node x is a child of node y.
o Note: Each node may have 0 or more children.
 Number of edges: If there are n nodes, then there would n-1 edges.
Tree
 Basic Terminologies:

 Siblings: Nodes with the same parent are siblings.


 Ancestor of node n: Any node y on the (unique) path from root r to node n is an
ancestor of node n.
o Every node is an ancestor of itself.
 Descendent of n: Any node y for which n is an ancestor of y.
Every node is an descendent of itself.
 Subtrees of node n: Subtrees of node n are the trees whose roots are the children
of n.
 Degree of node n: The degree of node n is the number of children node n has.
 Leaf node: A leaf node is a node with no children.
 External node: An external node is a node with no children (same as a leaf
node).
 Internal node: An internal node is a non-leaf node.
Tree
Depth of node n: The depth of node n is the length of the path from root to node n.
 The root node has depth 0.
 Level: The level of a node is the number of edges along the unique path between it and
the root node.
 Height of tree T: The height of a node is the length of the longest downward path to a
leaf from that node. The height of the root is the height of the tree.
The root node has depth zero, leaf nodes have height zero, and a tree with only a single
node (hence both a root and leaf) has depth and height zero. Conventionally, an empty
tree (tree with no nodes, if such are allowed) has height −1.
o Ex: node 2 has depth zero, node 7 and 5 has depth 1, node 2,10,6,9 has depth 2 .. So
on.
 Ordered tree: An ordered tree is a tree in which the children of each node are ordered.
 Degree of tree: The degree of a tree is the maximum degree of a node in the tree.
Tree
 Types of Trees:
 Binary Tree
 Binary Search Tree
 Heap Tree
 AVL Tree
 B-Teee
Binary Tree
 Definition:

 The Binary tree means that the node can have maximum two
children. Here, binary name itself suggests that 'two'; therefore,
each node can have either 0, 1 or 2 children.
 In the above tree, node 1 contains two pointers, i.e., left and a
right pointer pointing to the left and right node respectively. The
node 2 contains both the nodes (left and right node); therefore, it
has two pointers (left and right). The nodes 3, 5 and 6 are the
leaf nodes, so all these nodes contain NULL pointer on both left
and right parts.
Binary Tree
 Properties of Binary Tree
 At each level of i, the maximum number of nodes is 2^i.
 The height of the tree is defined as the longest path from the root node to
the leaf node.
 The tree which is shown has a height equal to 2. Therefore, the
maximum number of nodes at height 2 is equal to (1+2+4) = 7.
 In general, the maximum number of nodes possible at height h is (2^0 +
2^1 + 2^2+….+2^h) = 2^(h+1) - 1.
 The minimum number of nodes possible at height h is equal to (h+1).
 If the number of nodes is minimum, then the height of the tree would be
maximum. Conversely, if the number of nodes is maximum, then the
height of the tree would be minimum.
Binary Tree
 Properties of Binary Tree
 The minimum height can be computed as:
o As we know that, for minimum height all level have to full.
o n = 2^(h+1) -1
o n+1 = 2^(h+1)
o Taking log on both the sides,

o = (h+1)
o h= -1
 The maximum height can be computed as:
o As we know that,
o n = h+1
o h= n-1
Binary Tree
Types of Binary Tree

1. Full/Strict Binary Tree: A full binary tree 2. Complete Binary Tree:


is a tree in which every node has either 0 o In a complete binary tree every level, except possibly the last, is
completely filled, and all nodes in the last level are as far left as possible.
or 2 children.
o Some authors use the term complete to refer instead to a perfect binary
• The number of leaf nodes is equal to the
tree, in which case they call this type of tree (with a possibly not filled last
number of internal nodes plus 1. level) an almost complete binary tree or nearly complete binary tree.
Complete binary tree can be efficiently represented using an array.
Binary Tree
Types of Binary Tree

3. Perfect Binary Tree: all the internal nodes


have 2 children, and all the leaf nodes are at
the same level.

Not Perfect BT:


Binary Tree
  
 Binary Tree Prog. 17.   printf("Press 0 to exit");  
18.   printf("\nPress 1 for new node");  
1.#include<stdio.h>   19.   printf("Enter your choice : ");  
2.      struct node   20.   scanf("%d", &choice);   
3.     {   21.   if(choice==0)  
4.          int data;   22.{  
5.          struct node *left, *right;   23.return 0;  
6.      }   24.}  
7.      void main()   25.else  
8.    {   26.{  
9.       struct node *root;   27.   printf("Enter the data:");  
10.       root = create();   28.   scanf("%d", &data);  
11.    }   29.   temp->data = data;  
12. struct node *create()   30.   printf("Enter the left child of %d", data);  
13.{   31.   temp->left = create();  
14.   struct node *temp;   32.printf("Enter the right child of %d", data);  
15.   int data;   33.temp->right = create();  
16.   temp = (struct node *)malloc(sizeof( 34.return temp;   
struct node));   35.}  
36.}  
Tree Traversal
 Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical
way to traverse them, trees can be traversed in different ways. Following are the generally used
ways for traversing trees.
 Example Tree
 Depth First Traversals:
 Inorder (Left, Root, Right) : 4 2 5 1 3
 Preorder (Root, Left, Right) : 1 2 4 5 3
 Postorder (Left, Right, Root) : 4 5 2 3 1
 Breadth First or Level Order Traversal : 1 2 3 4 5
Tree Traversal
 In-order traversal
  Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the node.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)

 Uses of Inorder
 In case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing
order. To get nodes of BST in non-increasing order, a variation of Inorder traversal where
Inorder traversal s reversed can be used.
 Example: Inorder traversal for the above-given figure is 4 2 5 1 3.
Tree Traversal
 Pre-order traversal
  Algorithm Preorder(tree)
1. Visit the node.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)

 Uses of Preorder
 Preorder traversal is used to create a copy of the tree.
 Preorder traversal is also used to get prefix expression on of an expression tree.

 Example: Preorder traversal for the above given figure is 1 2 4 5 3.


Tree Traversal
 Post-order traversal
  Algorithm Post-order(tree)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the node.

 Uses of Postorder

 Postorder traversal is used to delete the tree.


 Postorder traversal is also useful to get the postfix expression of an expression tree.
 Example: Postorder traversal for the above given figure is 4 5 2 3 1.
Tree Traversal
 Level-order traversal
  For each node, first the node is visited and then it’s child nodes are put in a FIFO queue.
printLevelorder(tree)
1) Create an empty queue q
2) temp_node = root /*start from root*/
3) Loop while temp_node is not NULL
a) print temp_node->data.
b) Enqueue temp_node’s children
(first left then right children) to q
c) Dequeue a node from q.
Binary search Tree
 Binary Search Tree is a node-based binary tree data structure which has the following
properties:
 The left subtree of a node contains only nodes with keys lesser[or equal] than the node’s key.
 The right subtree of a node contains only nodes with keys greater than the node’s key.
 The left and right subtree each must also be a binary search tree.
Binary search Tree
 Binary Search Tree is a node-based binary tree data structure which has the following
properties:
 The left subtree of a node contains only nodes with keys lesser[or equal] than the node’s key.
 The right subtree of a node contains only nodes with keys greater than the node’s key.
 The left and right subtree each must also be a binary search tree.
 Advantages of using binary search tree
 Searching become very efficient in a binary search tree.
 The binary search tree is considered as efficient data structure in
compare to arrays and linked lists.
• Searching for an element in a binary search tree takes o(log2n) time.
• In worst case, the time it takes to search an element is 0(height).
Binary search Tree
 Creation of BST
43, 10, 79, 90, 12, 54, 11, 9, 50
 Insert 43 into the tree as the
root of the tree.
 Read the next element, if it is
lesser than the root node
element, insert it as the root of
the left sub-tree.
 Otherwise, insert it as the root of
the right of the right sub-tree.
Binary search Tree
 Searching in BST
 Compare the given element with the root of the tree.
 If the item is matched then return the location of the node.
 Otherwise check if item is less than the element present on root,
 if so then move to the left sub-tree.
 If not, then move to the right sub-tree.
 Repeat this procedure recursively until match found.
 If element is not found then return NULL.
Search (ROOT, ITEM)
Step 1: IF ROOT -> DATA = ITEM OR ROOT = NULL
    Return ROOT
   ELSE
   IF ITEM < ROOT -> DATA
   Return search(ROOT -> LEFT, ITEM)
  ELSE
   Return search(ROOT -> RIGHT,ITEM)
  [END OF IF]
  [END OF IF]
Step 2: END
Binary search Tree
 insertion in BST
 Find the right position to insert the element
then insert it
 Insert (TREE, ITEM)
 Step 1: IF TREE = NULL
 Allocate memory for TREE
 SET TREE -> DATA = ITEM
 SET TREE -> LEFT = TREE -> RIGHT = NULL
 ELSE
 IF ITEM < TREE -> DATA
 Insert(TREE -> LEFT, ITEM)
 ELSE
 Insert(TREE -> RIGHT, ITEM)
 [END OF IF]
 [END OF IF]
 Step 2: END
Binary search Tree
 Deletion in BST
 There are three situations of deleting a node from binary search tree
1. The node to be deleted is leaf node
2. The node to be deleted is having one child
3. The node to be deleted is having two children
Binary search Tree
 Deletion in BST
1. The node to be deleted is leaf node
• replace the leaf node with the NULL and simple free the allocated space.
Binary search Tree
 Deletion in BST
2. The node to be deleted is having one child
• replace the node with its child and delete the child node, which now contains the value which is to
be deleted. Simply replace it with the NULL and free the allocated space..
Binary search Tree
 Deletion in BST
3. The node to be deleted is
having two children
• the node which is to be deleted,
is replaced with its in-order
successor or predecessor.
• After the procedure, replace the
node with NULL and free the
allocated space.
Heap Tree
 Definition
 A Heap is a special Tree-based data structure in which
the tree is a complete binary tree. Generally, Heaps can
be of two types:
 Max-Heap: In a Max-Heap the key present at the
root node must be greatest among the keys present
at all of it’s children. The same property must be
recursively true for all sub-trees in that Binary Tree.
 Min-Heap: In a Min-Heap the key present at the
root node must be minimum among the keys
present at all of it’s children. The same property
must be recursively true for all sub-trees in that
Binary Tree.
Why Heap ?
■ To implement efficiently Priority
Queue. O(n) – find
min
Unsorted & del min

Sequenc O(1) -
e insertion
Priority
Queue
O(1) – find
min & del
Sorted min
Sequenc
e
O(n) -
insertion

08-Jul-18 28
Binary Heap
■ It is a binary tree that stores the priority of element at the node. It has
two properties.
– Structural Property: All levels except last are full. Last level is filled from left.
– Heap Property: Priority of a node is less than or equal to its children.
■ Height of Heap:
– Let heap has n nodes and its height is h.
11
– As for Complete Binary tree of height h
contains 2 ℎ + 1 − 1 nodes.
17 13
– Hence number of nodes in heap can be:
• 2 ℎ − 1 < 𝑛≤ 2 ℎ + 1 − 1
18 21 19 17
ℎ = log 2 𝑛
43 23 26 29 31
Implementation of Heap
11
Parent
(i)
retur (𝑖−1) /
2
n 17 13
Left (i)
return
2i+1 18 21 19 17
Right (i)
return
A[PARENT(i)]
2i+2 <= A[i] 43 23 26 29 31

■ Finding Minimum
–Element
The minimum element always be at the
root of the Heap. Arra
– if it is somewhere else, then it violates 0y 1 2 3 4 5 6 7 8 9 10 11 12

the Heap property. 11 17 13 18 21 19 17 43 23 26 29 31


– Hence minimum element can be find in
O(1) time.
30
Insertion in Heap
■ Insert 8 11

17 13

18 21 19 17

43 23 26 29 31 8

08-Jul-18 31
Insertion in Heap
■ Insert 8 11

17 13

18 21 8 17

43 23 26 29 31 19

08-Jul-18 32
Insertion in Heap
■ Insert 8 11

17 8

18 21 13 17

43 23 26 29 31 19

08-Jul-18 33
Insertion in Heap
■ Insert 8 8

■ It might be to go the element


all the way to the root. 17 11

■ Hence, to insert an element


in heap is O(h) i.e. O( log 2 𝑛) 18 21 13 17
steps are required in
worst case. 43 23 26 29 31 19

08-Jul-18 34
Delete-min in Heap
■ The minimum element is always be 8
on the root of heap.
■ We can delete this & move one of its 10 11
children up to fill the space. But we
might end up at any position on last
level, so resulting tree would not be 18 21 13 17
left filled that violet the structural
property of heap. 43 23 26 29 31 19
■ So we follow the following procedure.
– Exchange the root element
with last element of the heap.
– Then delete the element.

08-Jul-18 35
Delete-min in Heap
■ After deleting the element, it 19
violates the heap property.
■ So we apply heapify procedure: 10 11
– The method heapify makes binary
tree rooted at 𝑖 a heap by moving
18 21 13 17
A[]
𝑖 down the heap.
– Heap property violated at root node
while the left sub-tree and right 43 23 26 29 31
sub-tree of root node are heap.

– So apply heapify(root-
node)

08-Jul-18 36
Delete-min in Heap
19

10 11

18 21 13 17

43 23 26 29 31

08-Jul-18 37
Delete-min in Heap
10

19 11

18 21 13 17

43 23 26 29 31

08-Jul-18 38
Delete-min in Heap
■ At worst case after deleting an 10
element, we might have to traverse
height of the tree to do heapify.
18 11
■ So, it takes O(h) i.e. O( log2 𝑛) time to
delete minimum element.
19 21 13 17

43 23 26 29 31

08-Jul-18 39
Heap Tree
 Build Heap
 Build Heap is a process of building a Heap from a given element generally in an array format. The
resulting heap will be either max-heap or a min-heap.
 The most important function in converting an array into Binary heap is Heapify function.
Build-heap(arr, n)
{
//you can start with the last element of array but as discussed
// the node floor(size/2) is the last non-leaf node
last_parent = floor(n/2);
for(i= last_parent; i>=0; i--)
max_heapify(arr, i);
}
 Heapify
 Heapify is used to convert a Binary tree to a heap data structure.
 Heapify procedure when applied to a node arranges the node and its subtree(children) in such a
way that they follow Heap property.
Heap Tree
 Heapify Algorithm
 Let the current node be i, start by finding the left and
right child of the current node. left-child = heap[2*i]
and right-child = heap[2*i + 1] and assign a variable
largest with the index i, of current node.
 If left-child exists and is less than the current node, take
assign the variable largest the index of left-child.
Use Heapify to convert Step 1: 9 < 27, hence swap 9 & 27, also since
 Else if the right-child exists and is less than the current
above tree into a Max-Heap swapping has happend call heapify again
node, take assign the variable largest the index of right-
child.
 If largest != i swap the values at the index i and the
index largest i.e. swap(heap[i], swap[largest]), and
again call the Max-Heapify procedure with largest as the
new index and repeat the step 1.
 Else Stop. The tree is now following Max-Heap property.
 The time complexity of performing Max/Min Heapify is Step 2: 9 < 18, hence swap 9 & 18, also
Step 1: 9 doesn't have any left since swapping has happend call heapify
O(log N) child so stop again
Complexity Analysis
O(1) – find
min
Binary
Heap O( log 2 𝑛) – del min
& insertion

O(n) – find min &


del min
Unsorted Sequence

O(1) -
insertion
Priority Queue

O(1) – find min &


del min
Sorted Sequence

O(n) -
insertion
08-Jul-18 42
Application of Heap
■ To quickly find the smallest and largest element from a collection of items or
array.
■ In the implementation of Priority queue in graph algorithms like Dijkstra's
algorithm (shortest path), Prim's algorithm (minimum spanning tree) and Huffman
encoding (data compression).
■ In order to overcome the Worst Case Complexity of Quick Sort algorithm from
O(𝑛2) to O(𝑛log 𝑛) in Heap Sort.
■ Systems concerned with security and embedded system such as Linux Kernel
uses Heap Sort because of the O(𝑛log 𝑛).

08-Jul-18 43
AVL Tree
• AVL tree is a self-balancing Binary Search
Tree (BST) where the difference between
heights of left and right subtrees cannot
be more than one for all nodes.
• An Example Tree that is an AVL Tree
• The above tree is AVL because
differences between heights of left and
right subtrees for every node is less than
or equal to 1.
• An Example Tree that is NOT an AVL Tree
• The above tree is not AVL because
differences between heights of left and
right subtrees for 8 and 12 is greater
than 1..
AVL Tree
• Why AVL Trees?
• Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time
where h is the height of the BST.
• The cost of these operations may become O(n) for a skewed Binary tree.
• If we make sure that height of the tree remains O(Logn) after every insertion and deletion,
then we can guarantee an upper bound of O(Logn) for all these operations.
• The height of an AVL tree is always O(Logn) where n is the number of nodes in the tree.
AVL Tree
• Insertion in AVL
• To make sure that the given tree remains AVL
after every insertion, we must augment the
standard BST insert operation to perform some
re-balancing. Following are two basic operations
that can be performed to re-balance a BST
without violating the BST property (keys(left) <
key(root) < keys(right)).
• Left Rotation
• Right Rotation
AVL Tree
• Steps for Insertion
Let the newly inserted node be w
1) Perform standard BST insert for w.
2) Starting from w, travel up and find the first unbalanced node. Let z be the first unbalanced node, y be the
child of z that comes on the path from w to z and w be the grandchild of z that comes on the path from w to z.
3) Re-balance the tree by performing appropriate rotations on the subtree rooted with z. There can be 4
possible cases that needs to be handled as x, y and z can be arranged in 4 ways. Following are the possible 4
arrangements:
a) y is left child of z and x is left child of y (Left Left Case)
b) y is left child of z and x is right child of y (Left Right Case)
c) y is right child of z and x is right child of y (Right Right Case)
d) y is right child of z and x is left child of y (Right Left Case)
In all of the cases, we only need to re-balance the subtree rooted with z and the complete tree becomes
balanced as the height of subtree (After appropriate rotations) rooted with z becomes same as it was before
insertion.
AVL Tree
a) y is left child of z and x is left b) y is left child of z and x is right
child of y (Left Left Case) child of y (Left Right Case)
AVL Tree
c) y is right child of z and x is right d) y is right child of z and x is left
child of y (Right Right Case) child of y (Right Left Case)
AVL Tree
Insertion Examples:
AVL Tree
Insertion Examples:
AVL Tree
Insertion Examples:
AVL Tree
Insertion Examples:
AVL Tree
Insertion Examples:
AVL Tree
• Deletion in AVL Tree
Steps to follow for deletion.
To make sure that the given tree remains AVL after every deletion, we must augment the standard BST delete
operation to perform some re-balancing. Following are two basic operations that can be performed to re-
balance a BST without violating the BST property (keys(left) < key(root) < keys(right)).
1) Left Rotation
2) Right Rotation
AVL Tree
• Deletion in AVL Tree
Steps to follow for deletion.
Let w be the node to be deleted
1) Perform standard BST delete for w.
2) Starting from w, travel up and find the first unbalanced node. Let z be the first unbalanced node, y be the larger
height child of z, and x be the larger height child of y. Note that the definitions of x and y are different from
insertion here.
3) Re-balance the tree by performing appropriate rotations on the subtree rooted with z. There can be 4 possible
cases that needs to be handled as x, y and z can be arranged in 4 ways. Following are the possible 4 arrangements:
a) y is left child of z and x is left child of y (Left Left Case)
b) y is left child of z and x is right child of y (Left Right Case)
c) y is right child of z and x is right child of y (Right Right Case)
d) y is right child of z and x is left child of y (Right Left Case)
Like insertion, following are the operations to be performed in above mentioned 4 cases. Note that, unlike insertion,
fixing the node z won’t fix the complete AVL tree. After fixing z, we may have to fix ancestors of z as well
AVL Tree
a) y is left child of z and x is left b) y is left child of z and x is right
child of y (Left Left Case) child of y (Left Right Case)
AVL Tree
c) y is right child of z and x is right d) y is right child of z and x is left
child of y (Right Right Case) child of y (Right Left Case)

• Unlike insertion, in deletion, after we perform a rotation at z, we may have to perform a rotation
at ancestors of z. Thus, we must continue to trace the path until we reach the root.
AVL Tree
Deletion Examples:
• A node with value 32 is being deleted. After deleting 32, we travel up and find the first unbalanaced node which is 44. We
mark it as z, its higher height child as y which is 62, and y’s higher height child as x which could be either 78 or 50 as both
are of same height. We have considered 78. Now the case is Right Right, so we perform left rotation.
B – Tree
B - Tree
• Introduction
• B-Tree is a self-balancing search tree.
• In other self-balancing search trees (like AVL), it is assumed that everything is in main memory.
• To understand the use of B-Trees, we must think of the huge amount of data that cannot fit in
main memory.
• When the number of keys is high, the data is read from disk in the form of blocks.
• Disk access time is very high compared to the main memory access time.
• The main idea of using B-Trees is to reduce the number of disk accesses.
• Most of the tree operations (search, insert, delete, max, min, etc ) require O(h) disk accesses
where h is the height of the tree.
• The height of B-Trees is kept low by putting maximum possible keys in a B-Tree node.
• Generally, the B-Tree node size is kept equal to the disk block size.
• Since the height of the B-tree is low so total disk accesses for most of the operations are reduced
significantly compared to balanced Binary Search Trees like AVL Tree, Red-Black Tree, ..etc.
B - Tree
• Time Complexity
Sr. No. Algorithm Time Complexity
1. Search O(log n)
2. Insert O(log n)
3. Delete O(log n)

“n” is the total number of node in tree


B - Tree
• Properties of B-Tree
• All leaves are at the same level.
• A B-Tree is defined by the term minimum degree ‘t’. The
value of t depends upon disk block size.
• Every node except root must contain at least (ceiling)([t-
1]/2) keys. The root may contain minimum 1 key.
• All nodes (including root) may contain at most t – 1 keys.
• Number of children of a node is equal to the number of
keys in it plus 1.
• All keys of a node are sorted in increasing order. The child
between two keys k1 and k2 contains all keys in the range
from k1 and k2.
• B-Tree grows and shrinks from the root which is unlike
Binary Search Tree. Binary Search Trees grow downward
and also shrink from downward.
B - Tree
• Traversal:
• Traversal is also similar to In-order traversal of Binary Tree.
• We start from the leftmost child, recursively print the leftmost child, then repeat the same process
for remaining children and keys. In the end, recursively print the rightmost child.
• Ex: Traversal is: 10 20 35 40 50 65 70 80 90 100 … so on
B - Tree
• Searching:
• Let the key to be searched be k.
• We start from the root and recursively traverse down.
• For every visited non-leaf node, if the node has the key, we simply return the node.
• Otherwise, we recur down to the appropriate child (The child which is just before the first greater
key) of the node. If we reach a leaf node and don’t find k in the leaf node, we return NULL.
• Logic:
• Searching a B-Tree is similar to searching a binary tree.
• The algorithm is similar and goes with recursion.
• At each level, the search is optimized as if the key value is not present in the range of parent then
the key is present in another branch.
• As these values limit the search they are also known as limiting value or separation value. If we
reach a leaf node and don’t find the desired key then it will display NULL.
B - Tree
• Example: Searching 120 in the given B-Tree.
B - Tree
• Example: Searching 120 in the given B-Tree.
B - Tree
• Example: Searching 120 in the given B-Tree.
B - Tree
• Insertion:
• A new key is always inserted at the leaf node.
• Let the key to be inserted be k. Like BST, we start from the root and traverse down till we reach a leaf
node.
• Once we reach a leaf node, we insert the key in that leaf node.
• Unlike BSTs, we have a predefined range on the number of keys that a node can contain. So before
inserting a key to the node, we make sure that the node has extra space.
B - Tree
• Insertion:
• How to make sure that a node has space available for a key before the key is inserted?
• We use an operation called splitChild() that is used to split a child of a node.
• See the following diagram to understand split.
• In the following diagram, child y of x is being split into two nodes y and z.
• Note that the splitChild operation moves a key up and this is the reason B-Trees grow up,
unlike BSTs which grow down.
B - Tree
• Insertion:
• As discussed previous, to insert a new key, we go down from root to leaf. Before traversing
down to a node, we first check if the node is full. If the node is full, we split it to create space.
Following is the complete algorithm.
1) Initialize x as root.
2) While x is not leaf, do following
..a) Find the child of x that is going to be traversed next. Let the child be y.
..b) If y is not full, change x to point to y.
..c) If y is full, split it and change x to point to one of the two parts of y. If k is smaller
than mid key in y, then set x as the first part of y. Else second part of y. When we split y,
we move a key from y to its parent x.
3) The loop in step 2 stops when x is leaf. x must have space for 1 extra key as we have
been splitting all nodes in advance. So simply insert k to x.
B - Tree
• Insertion:
• Let us understand the algorithm with an example tree of minimum degree ‘t’ as 3 and a
sequence of integers 10, 20, 30, 40, 50, 60, 70, 80 and 90 in an initially empty B-Tree.
• Step 1:
• Initially root is NULL. Let us first insert 10.
B - Tree
• Insertion:
• Let us understand the algorithm with an example tree of minimum degree ‘t’ as 3 and a
sequence of integers 10, 20, 30, 40, 50, 60, 70, 80 and 90 in an initially empty B-Tree.
• Step 2:
• Let us now insert 20, 30, 40 and 50. They all will be inserted in root because the maximum number of keys a node
can accommodate is 2*t – 1 which is 5.
B - Tree
• Insertion:
• Let us understand the algorithm with an example tree of minimum degree ‘t’ as 3 and a
sequence of integers 10, 20, 30, 40, 50, 60, 70, 80 and 90 in an initially empty B-Tree.
• Step 3:
• Let us now insert 60. Since root node is full, it will first split into two, then 60 will be inserted into the appropriate
child.
B - Tree
• Insertion:
• Let us understand the algorithm with an example tree of minimum degree ‘t’ as 3 and a
sequence of integers 10, 20, 30, 40, 50, 60, 70, 80 and 90 in an initially empty B-Tree.
• Step 4:
• Let us now insert 70 and 80. These new keys will be inserted into the appropriate leaf without any split.
B - Tree
• Insertion:
• Let us understand the algorithm with an example tree of minimum degree ‘t’ as 3 and a
sequence of integers 10, 20, 30, 40, 50, 60, 70, 80 and 90 in an initially empty B-Tree.
• Step 5:
• Let us now insert 90. This insertion will cause a split. The middle key will go up to the parent.
REFERENCES
1. https://www.geeksforgeeks.org/
2. Thareja, R. (2014). Data structures using C. Oxford University.
3. Lipschutz, S. (2003). Schaum's outline of theory and problems of data structures.
McGraw-Hill.

You might also like