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

Module - 3 Advanced Data Structures: C Manasa Asst. Professor Dept. of CSE, DSCE

Uploaded by

Mohit Nagaraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Module - 3 Advanced Data Structures: C Manasa Asst. Professor Dept. of CSE, DSCE

Uploaded by

Mohit Nagaraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

Module - 3

Advanced Data Structures

C Manasa
Asst. professor
Dept. of CSE,DSCE
Contents
• 2-3 Trees
• B Trees
• B+ Trees
• Heaps
• Skip Lists
2-3 Trees
• 2-3 trees are the data structure same as trees, but it has some different properties like any node can have either
single value or double value. So, there are two types of nodes in 2-3 trees.

• The time complexity of search/insert/delete is O(log N) .

• A 2-3 tree is a B-tree of order 3.

Properties of 2-3 tree:

1. Nodes with two children are called 2-nodes. The 2-nodes have one data value and two children

2. Nodes with three children are called 3-nodes. The 3-nodes have two data values and three children.

3. Data is stored in sorted order.

4. It is a balanced tree.

5. All the leaf nodes are at same level.

6. Each node can either be leaf, 2 node, or 3 node.

7. Always insertion is done at leaf.


2-3 Trees Example
Operations
There are 3 operations: Search, Insert, Delete.

Search: Search is the operation where we are given the root node and target value. If the value is
available in the tree, it returns true; else, it will return false.
• Case 1:

If the current node is single-valued and the value is lesser than the node's value, then call the
recursive function for the left child. Else, call the recursive function for the right child.
• Case 2:
If the current node is double valued, and if the value is lesser than the left value, then call the
recursive function for the left child. If the target element is greater than the current node's left value
and lesser than the current node's right value, then call the recursive function for middle children.
Else, call the recursive for the right child.
Search
Insert
There are 3 possible cases in insertion
Case 1: Insert in a node with only one data element
Insert
Case 2: Insert in a node with two data elements whose parent contains only one
data element.
Insert
Case 3: Insert in a node with two data elements whose parent also contains two data elements.
Insert
1. 50, 60, 70, 40, 30, 20, 10, 80, 90, 100
2. 9, 5, 8, 3, 2, 4, 7
Traversal

Inorder : 2,5,8,10,12,15,16,20,25,30,31,40,60
Delete
In Deletion Process for a specific value:
• To delete a value, it is replaced by its in-order successor and then
removed.
• If a node in left with less than one data value then two nodes must be
merged together.
• If a node becomes empty after deleting a value, it is then merged
with another node.
Delete
Delete a data element from an internal node.
Delete
Delete a data element from leaf node.
Delete
Delete the following values from it: 69,72, 99, 81.
struct Node {
Node Structure
int key1;
int key2;
Node* left;
Node* middle;
Node* right;

Node(int key) {
key1 = key;
key2 = -1;
left = nullptr;
middle = nullptr;
right = nullptr;
}

Node(int key1, int key2, Node* left, Node* middle, Node* right) {
this->key1 = key1;
this->key2 = key2;
this->left = left;
this->middle = middle;
this->right = right;
} };
search()
bool search_2_3_tree(Node* node, int key) {
if (node == nullptr) {
return false;
}

// Traverse through the keys in the node


for (int i = 0; i < node->numKeys; ++i) {
if (key == node->keys[i]) {
// Key is found
return true;
} else if (key < node->keys[i]) {
// Recur on the left child
return search_2_3_tree(node->children[i], key);
}
}
// If the key is greater than all keys in this node, recur on the rightmost child
return search_2_3_tree(node->children[node->numKeys], key);
}
void insert(Node*& node, int key) {
insert()
if (!node) {
node = new Node(key);
} else if (node->key2 == -1) {
if (key < node->key1) {
insert(node->left, key);
} else {
insert(node->right, key);
}
} else {
if (key < node->key1) {
insert(node->left, key);
} else if (key > node->key2) {
insert(node->right, key);
} else {
insert(node->middle, key);
}
}
split(node);
}
Split()
B Trees
B-Tree is a self-balancing search tree. 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.
B Trees
A B tree of order m contains the following properties.

1. Every node in a B-Tree contains at most m children.

2. Every node in a B-Tree except the root node and the leaf node contain at least m/2 children.

3. The root nodes must have at least 2 nodes.

4. All leaf nodes must be at the same level.

5. The time complexity to search, insert, and delete is O(log n).

It is not necessary that, all the nodes contain the same number of children but, each node must have
m/2 number of nodes.
B Trees Example

B-Tree of Order 4 contains a maximum of 3 key values in a node and maximum of 4 children for a
node.
Operations on a B-Tree
The following operations are performed on a B-Tree...

• Search

• Insertion

• Deletion
Insert()
• 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. Operation splitChild() is called, that
is used to split a child of a node

Insert the node 8 into the B Tree of order 5 shown in the following image.
Insert()
Example:
8 will be inserted to the right of 5, therefore insert 8.
Insertion and splitting
Right-bias: The node is split such that its right subtree has more keys than the left subtree.

Insert elements 5,3,21,9

•Left-bias: The node is split such that its left subtree has more keys than the right subtree.

Insert elements 5,3,21,9


Insert()
Construct a Btree of order 4 with following set of data:
5, 3, 21, 9, 1, 13, 2,7, 10, 12, 4, 8
Search()
Searching in B Trees is similar to that in Binary search tree. For example, if we search for an item 49 in the
following B Tree. The process will something like following :

• Compare item 49 with root node 78. since 49 < 78 hence, move to its left sub-tree.

• Since, 40<49<56, traverse right sub-tree of 40.

• 49>45, move to right. Compare 49.

• match found, return.


Delete()
Deletion is also performed at the leaf nodes. The node which is to be deleted can either be a leaf node or an internal node.

• Locate the leaf node.

• If there are more than m/2 keys in the leaf node then delete the desired key from the node.

• If the leaf node doesn't contain m/2 keys then complete the keys by taking the element from right or left sibling.

• If the left sibling contains more than m/2 elements then push its largest element up to its parent and move the intervening element
down to the node where the key is deleted.

• If the right sibling contains more than m/2 elements then push its smallest element up to the parent and move intervening element
down to the node where the key is deleted.

• If neither of the sibling contain more than m/2 elements then create a new leaf node by joining two leaf nodes and the intervening element

of the parent node.

• If parent is left with less than m/2 nodes then, apply the above process on the parent too.

• If the node which is to be deleted is an internal node, then replace the node with its in-order successor or predecessor. Since, successor or

predecessor will always be on the leaf node hence, the process will be similar as the node is being deleted from the leaf node.
Delete()
Delete the node 53 from the B Tree of order 5 shown in the following figure.

Now, 57 is the only element which is left in the node, the minimum number of elements that must be present in a B
tree of order 5, is 2. it is less than that, the elements in its left and right sub-tree are also not sufficient therefore,
merge it with the left sibling and intervening element of parent i.e. 49.
Algorithms()
B+ Tree
B+ Tree is an extension of B Tree which allows efficient insertion, deletion and search operations.

In B Tree, Keys and records both can be stored in the internal as well as leaf nodes. Whereas, in B+ tree,
records (data) can only be stored on the leaf nodes while internal nodes can only store the key values.

The leaf nodes of a B+ tree are linked together in the form of a singly linked lists to make the search queries
more efficient.

B+ Tree are used to store the large amount of data which can not be stored in the main memory. Due to the
fact that, size of main memory is always limited, the internal nodes (keys to access records) of the B+ tree
are stored in the main memory whereas, leaf nodes are stored in the secondary memory.
B+ Tree
The internal nodes of B+ tree are often called index nodes. A B+ tree of order 3 is shown in the following
figure.
B Tree VS B+ Tree
SN B Tree B+ Tree

1 Search keys cannot be repeatedly stored. Redundant search keys can be present.

2 Data can be stored in leaf nodes as well as Data can only be stored on the leaf nodes.
internal nodes

3 Searching for some data is a slower Searching is comparatively faster as data can
process since data can be found on internal only be found on the leaf nodes.
nodes as well as on the leaf nodes.

4 Deletion of internal nodes is so Deletion will never be a complex process since


complicated and time consuming. element will always be deleted from the leaf
nodes.

5 Leaf nodes cannot be linked together. Leaf nodes are linked together to make the search
operations more efficient.
Insertion in B+ Tree
Step 1: Insert the new node as a leaf node

Step 2: If the leaf doesn't have required space, split the node and copy the middle node to the next index node.

Step 3: If the index node doesn't have required space, split the node and copy the middle element to the next
index page.

Example :

• Insert the value 195 into the B+ tree of order 5 shown in the following figure.
Insertion in B+ Tree
195 will be inserted in the right sub-tree of 120 after 190. Insert it at the desired position.

The node contains greater than the maximum number of elements i.e. 4, therefore split it and place
the median node up to the parent.
Insertion in B+ Tree
Now, the index node contains 6 children and 5 keys which violates the B+ tree properties, therefore we
need to split it, shown as follows.
Deletion in B+ Tree
Step 1: Delete the key and data from the leaves.

Step 2: if the leaf node contains less than minimum number of elements, merge down the node with its sibling
and delete the key in between them.

Step 3: if the index node contains less than minimum number of elements, merge the node with the sibling and
move down the key in between them.
Deletion in B+ Tree
Example: Delete the key 200 from the B+ Tree shown in the following figure.

200 is present in the right sub-tree of 190, after 195. delete it.
Deletion in B+ Tree
Merge the two nodes by using 195, 190, 154 and 129.

Now, element 120 is the single element present in the node which is violating the B+ Tree properties.
Therefore, we need to merge it by using 60, 78, 108 and 120.
Now, the height of B+ tree will be decreased by 1.
Deletion in B+ Tree
Heap
Heap is a special tree-based data structure. A binary tree is said to follow a heap data structure if

1. it is a complete binary tree

2. All nodes in the tree follow the property that they are greater than their children i.e. the largest element is at
the root and both its children and smaller than the root and so on. Such a heap is called a max-heap. If
instead, all nodes are smaller than their children, it is called a min-heap
Working of Heap sort Algorithm
• Build a heap from the given input array.

• Repeat the following steps until the heap contains only one element:

• Swap the root element of the heap (which is the largest element) with the last
element of the heap.

• Remove the last element of the heap (which is now in the correct position).

• Heapify the remaining elements of the heap.

• The sorted array is obtained by reversing the order of the elements in the input
array.
Example
Consider the array: arr[] = {4, 10, 3, 5, 1}.
Example
Now remove the root (i.e. 3) again and perform heapify.
TRY
81,89,9,11,14, 76,54,22

10,3,76, 34,23,32
Insertion in heap
Given a Binary Heap and a new element to be added to this Heap. The task is to insert the new element to
the Heap maintaining the properties of Heap.
1. First increase the heap size by 1, so that it can store the new element.
2. Insert the new element at the end of the Heap.
3. This newly inserted element may distort the properties of Heap for its parents. So, in order to keep the
properties of Heap, heapify this newly inserted element following a bottom-up approach.
Insertion in heap
Insert()
Deletion in heap
The standard deletion operation on Heap is to delete the element present at the root node of the Heap. That
is if it is a Max Heap, the standard deletion operation will delete the maximum element and if it is a Min
heap, it will delete the minimum element.
Process of Deletion:
Since deleting an element at any intermediary position in the heap can be costly, so we can simply replace
the element to be deleted by the last element and delete the last element of the Heap.
1. Replace the root or element to be deleted by the last element.
2. Delete the last element from the Heap.
3. Since, the last element is now placed at the position of the root node. So, it may not follow the heap
property. Therefore, heapify the last node placed at the position of root.
Deletion in heap
Delete()
Heapsort()
minheap

You might also like