Advanced Data Structures
Advanced Data Structures
Unit-1
applications.
2.Min-Max Heaps
Min-max Heap Min-max heap is a heap with the additional property that the levels are
alternately labeled with MIN and MAX. In particular, the root is at min level and its children are
at max level, further its children are at min level and so on. Also, an element v in a max level has
the property that v is the largest of all the elements in the subtree rooted at v. Similarly, if v is in
min level, then v is the smallest of all the elements in the subtree rooted at v. These two
properties we refer to as max, and min property, respectively. An example is illustrated in Figure
1. Creation of min-max heap: a top-down approach This is similar to creation of min-heap in top-
down fashion. In top-down approach each new element is inserted at the last level respecting
nearly complete binary property and the min-max heapify procedure bubbles up the element to
its correct position by recursively checking the min or max property starting from its parent or
grand parent and this check is done recursively till the parent or grand parent is the root node.
For creation of an n-element min-max heap we need
Figures 1 − 18 illustrate the creation of a min-max heap. Min property violation of Figure 2 is set
right in Figure 3. Similarly, max property violation occurs in Figures 4, 6, 8, 11, 16, 17 and min
property is violated in Figure 14. Insertion in a min-max heap Insertion of an element in a min-
max heap is similar to a heap where we insert an element as a left child of the first leaf node
(indexing nodes from top to bottom and left to right) or right child of the last non-leaf. After
insertion we set right the min-max heap by recursively checking the min-max heap property
starting from the inserted element till the root. Depending on the level in which the new element
is inserted, it can violate either min or max property. For example, if we are inserting an element
80 in Figure 2, then it will be first inserted as the left of the element 63. Note that 80 is now in
min level. To set right the heap, we first observe that the max property at the parent element 63 is
violated, and we exchange 63 and 80 (See Figure 2 (a)). Note that there is no need to check the
min property violation with respect to any min elements above 63 (starting from 42). We further
check max property of grand parent of 80, which is 79, see Figure 2 (b). Max property at 79 is
violated, and therefore, exchange 79 and 80. This completes the insertion process and we finally
obtain a min-max heap with 80 inserted as shown in Figure 2 (c). Note that in the insertion,
either the min property is violated or the max property but not both and therefore we may need
to check one among them recursively till the root. Moreover, we need O(log n) effort for
inserting an element in a min-max heap. Extract Min and Max in a min-max heap For extract
min in a min-max heap we replace the root element with the last leaf (the last element in the last
level), henceforth referred to as key. Note that this replace operation ensures heap property is
maintained, however, there may be violation of min-max property. Further we may need to set
right the min-max heap property. Depending on whether the key is in min-level or max-level, or
left subtree or right subtree of the root, to bring back the min-max heap property, we recursively
do the following checks. 1. Compare the key with second minimum which is one of the four
grand children. If key is larger than the second min, swap key and second min. 2. While
swapping key and second min, violation may happen with respect to max property. i.e., key is
greater than its parent which is at the max level. If so, swap key and its parent after performing
the above swap. 3. Recursively do these checks until no longer possible.
In Figure 3, for extract min operation, the element 10 in the root is replaced with 33. Then the
root is exchanged with 20, the second minimum which is shown in Figure 3 (c). Now since there
is no max violation at the element 40. As part of recursive check, 33 is swapped with its second
minimum grand child 25. Now, we notice a max violation and hence swap 33 and 30 and this
completes the extract min procedure. Similar approach works for extract max. Extract min (or
max) need O(1) time for replacing the min or max element with the last leaf element and at most
l 2 + 1 exchanges, where l is the number of levels in the n element min-max heap. Since l = θ(log
n), extract min (or max) can be performed in θ(log n) time. We shall present another data
structure Deaps which also performs extract min and extract max in θ(log n) time.
Binomial Heap
In this article, we will discuss the binomial heap. Before start discussing the topic, we should
first understand some basic terms such as heap, min-heap, max-heap, and binomial tree.
What is a heap?
A heap is a complete binary tree, and the binary tree is a tree in which the node can have utmost
two children. There are two types of heap that are defined as follows -
o Min Heap: The value of the parent node should be less than or equal to either of its
children.
Mathematically, the min-heap can be defined as -
A[Parent(i)] <= A[i]
o Max Heap: The value of the parent node is greater than or equal to its children.
Mathematically, the max-heap can be defined as -
A[Parent(i)] >= A[i]
What is a Binomial tree?
A Binomial tree Bk is an ordered tree defined recursively, where k is defined as the order of the
binomial tree.
o If the binomial tree is represented as B0, then the tree consists of a single node.
o In general terms, Bk consists of two binomial trees, i.e., B k-1 and Bk-1 that are linked
together in which one tree becomes the left subtree of another binomial tree.
We can understand it with the example given below.
If B0, where k is 0, there would exist only one node in the tree.
If B1, where k is 1. Therefore, there would be two binomial trees of B 0 in which one B0 becomes
the left subtree of another B0.
If B2, where k is 2. Therefore, there would be two binomial trees of B 1 in which one B1 becomes
the left subtree of another B1.
If B3, where k is 3. Therefore, there would be two binomial trees of B 2 in which one B2 becomes
the left subtree of another B2.
A binomial heap can be defined as the collection of binomial trees that satisfies the heap
properties, i.e., min-heap. The min-heap is a heap in which each node has a value lesser than the
value of its child nodes. Mainly, Binomial heap is used to implement a priority queue. It is an
extension of binary heap that gives faster merge or union operations along with other operations
provided by binary heap.
o Every binomial tree in the heap must follow the min-heap property, i.e., the key of a
node is greater than or equal to the key of its parent.
o For any non-negative integer k, there should be atleast one binomial tree in a heap where
root has degree k.
The first property of the heap ensures that the min-heap property is hold throughout the heap.
Whereas the second property listed above ensures that a binary tree with n nodes should have at
most 1 + log2 n binomial trees, here log2 is the binary logarithm.
We can understand the properties listed above with the help of an example -
The above figure has three binomial trees, i.e., B 0, B2, and B3. The above all three binomial trees
satisfy the min heap's property as all the nodes have a smaller value than the child nodes.
The above figure also satisfies the second property of the binomial heap. For example, if we
consider the value of k as 3, we can observe in the above figure that the binomial tree of degree 3
exists in a heap.
Suppose we want to create the binomial heap of 'n' nodes that can be simply defined by the
binary number of 'n'. For example: if we want to create the binomial heap of 13 nodes; the binary
form of 13 is 1101, so if we start the numbering from the rightmost digit, then we can observe
that 1 is available at the 0, 2, and 3 positions; therefore, the binomial heap with 13 nodes will
have B0, B2, and B3 binomial trees.
We can use another example to understand it more clearly, suppose we have to create the
binomial heap with 9 nodes. The binary representation of 9 is 1001. So, in the binary
representation of 9, digit 1 is occurring at 0 and 3 positions, therefore, the binomial heap will
contain the binomial trees of 0 and 3 degrees.
When we create a new binomial heap, it simply takes O(1) time because creating a heap will
create the head of the heap in which no elements are attached.
1. function merge(a,b)
2. if a.root.key ? b.root.key
3. return a.add(b)
4. else
5. return b.add(a)
To perform the union of two binomial heaps, we have to consider the below cases -
Case 1: If degree[x] is not equal to degree[next x], then move pointer ahead.
and key[x] < key[next x] then remove [next x] from root and attached to x.
and key[x] > key[next x] then remove x from root and attached to [next x].
Now, let's understand the merging or union of two binomial heaps with the help of an example.
Consider two binomial heaps -
We can see that there are two binomial heaps, so, first, we have to combine both heaps. To
combine the heaps, first, we need to arrange their binomial trees in increasing order.
In the above heap first, the pointer x points to the node 12 with degree B 0, and the pointer next[x]
points the node 18 with degree B 0. Node 7 with degree B1 is the sibling of 18, therefore, it is
represented as sibling[next[x]].
Now, first apply Case1 that says 'if degree[x] ≠ degree[next x] then move pointer ahead' but
in the above example, the degree[x] = degree[next[x]], so this case is not valid.
Now, apply Case2 that says 'if degree[x] = degree[next x] = degree[sibling(next x)] then
Move pointer ahead'. So, this case is also not applied in the above heap.
Now, apply Case3 that says ' If degree[x] = degree[next x] ≠ degree[sibling[next x]] and
key[x] < key[next x] then remove [next x] from root and attached to x'. We will apply this
case because the above heap follows the conditions of case 3 -
degree[x] = degree[next x] ≠ degree[sibling[next x]] {as, B 0 = B0¬ ≠ B1} and key[x] < key[next
x] {as 12 < 18}.
Now we will reapply the cases in the above binomial heap. First, we will apply case 1. Since x is
pointing to node 12 and next[x] is pointing to node 7, the degree of x is equal to the degree of
next x; therefore, case 1 is not valid.
Here, case 2 is valid as the degree of x, next[x], and sibling[next[x]] is equal. So, according to
the case, we have to move the pointer ahead.
Now, let's try to apply case 3, here, first condition of case3 is satisfied as degree[x] =
degree[next[x]] ≠ degree[sibling[next[x]]], but second condition (key[x] < key[next x]) of case 3
is not satisfied.
Now, let's try to apply case 4. So, first condition of case4 is satisfied and second condition
(key[x] > key[next x]) is also satisfied. Therefore, remove x from the root and attach it to
[next[x]].
Now, the pointer x points to node 3, next[x] points to node 15, and sibling[next[x]] points to the
node 6. Since, the degree of x is equal to the degree of next[x] but not equal to the
degree[sibling[next[x]]], and the key value of x is less than the key value of next[x], so we have
to remove next[x] and attach it to x as shown below -
Now, x represents to the node 3, and next[x] points to node 6. Since, the degree of x and next[x]
is not equal, so case1 is valid. Therefore, move the pointer ahead. Now, the pointer x points the
node 6.
The B4 is the last binomial tree in a heap, so it leads to the termination of the loop. The above
tree is the final tree after the union of two binomial heaps.
In the above heap, there are three binomial trees of degrees 0, 1, and 2 are given where B0 is
attached to the head of the heap.
Suppose we have to insert node 15 in the above heap.
First, we have to combine both of the heaps. As both node 12 and node 15 are of degree 0, so
node 15 is attached to node 12 as shown below -
Now, assign x to B0 with value 12, next(x) to B0 with value 15, and assign sibling(next(x)) to
B1 with value 7. As the degree of x and next(x) is equal. The key value of x is smaller than the
key value of next(x), so next(x) is removed and attached to the x. It is shown in the below image
-
Now, x points to node 12 with degree B 1, next(x) to node 7 with degree B 1, and sibling(next(x))
points to node 15 with degree B2. The degree of x is equal to the degree of next(x) but not equal
to the degree of sibling(next(x)). The key value of x is greater than the key value of next(x);
therefore, x is removed and attached to the next(x) as shown in the below image -
Now, x points to node 7, and next(x) points to node 15. The degree of both x and next(x) is B 2,
and the key value of x is less than the key value of next(x), so next(x) will be removed and
attached to x as shown in the below image -
Now, the degree of the above heap is B3, and it is the final binomial heap after inserting node 15.
Now, compare the key values of the root node of the binomial trees in the above heap. So, 12, 7,
and 15 are the key values of the root node in the above heap in which 7 is minimum; therefore,
remove node 7 from the tree as shown in the below image -
Advertisement
Now, the degree of node 12 and node 25 is B 0, and the degree of node 15 is B2. Pointer x points
to the node 12, next(x) points to the node 25, and sibling(next(x)) points to the node 15. Since
the degree of x is equal to the degree of next(x) but not equal to the degree of sibling(next(x)).
Value of pointer x is less than the pointer next(x), so node 25 will be removed and attached to
node 12 as shown in the below image -
Now, the degree of node 12 is changed to B 1. The above heap is the final heap after extracting
the minimum key.
Decreasing a key
Now, let's move forward to another operation to be performed on binomial heap. Once the value
of the key is decreased, it might be smaller than its parent's key that results in the violation of
min-heap property. If such case occurs after decreasing the key, then exchange the element with
its parent, grandparent, and so on until the min-heap property is satisfied.
Let's understand the process of decreasing a key in a binomial heap using an example. Consider a
heap given below -
Decrease the key 45 by 7 of the above heap. After decreasing 45 by 7, the heap will be -
After decreasing the key, the min-heap property of the above heap is violated. Now, compare 7
wits its parent 30, as it is lesser than the parent, swap 7 with 30, and after swapping, the heap will
be -
Again compare the element 7 with its parent 8, again it is lesser than the parent, so swap the
element 7 with its parent 8, after swapping the heap will be -
Now, the min-heap property of the above heap is satisfied. So, the above heap is the final heap
after decreasing a key.
First, replace the node with negative infinity (or -∞) as shown below -
Now, swap the negative infinity with its root node in order to maintain the min-heap property.
Now, again swap the negative infinity with its root node.
The next step is to extract the minimum key from the heap. Since the minimum key in the above
heap is -infinity so we will extract this key, and the heap would be:
The above is the final heap after deleting the node 41.
Time Complexity
Time complexity
The time complexity of finding the minimum element from the heap can be reduced to O(1). It
can be done by using an additional pointer to the minimum element.
Space Complexity
Fibonacci Heap maintains a pointer to the minimum value (which is the root of a tree). All tree
roots are connected using a circular doubly linked list, so all of them can be accessed using a
single ‘min’ pointer.
The main idea is to execute operations in a “lazy” way. For example merge operation simply
links two heaps, insert operation simply adds a new tree with a single node. The operation extract
minimum is the most complicated operation. It does delay the work of consolidating trees. This
makes delete also complicated as delete first decreases the key to minus infinite, then calls
extract minimum.
Below are some interesting facts about Fibonacci Heap
1. The reduced time complexity of Decrease-Key has importance in Dijkstra and Prim
algorithms. With Binary Heap, the time complexity of these algorithms is O(VLogV +
ELogV). If Fibonacci Heap is used, then time complexity is improved to O(VLogV + E)
2. Although Fibonacci Heap looks promising time complexity-wise, it has been found slow in
practice as hidden constants are high (Source Wiki).
3. Fibonacci heaps is mainly called so because Fibonacci numbers are used in the running time
analysis. Also, every node in Fibonacci Heap has a degree at most O(log n) and the size of a
subtree rooted in a node of degree k is at least Fk+2, where Fk is the kth Fibonacci number.
Advantages of Fibonacci Heap:
1. Fast amortized running time: The running time of operations such as insert, extract-min and
merge in a Fibonacci heap is O(1) for insert, O(log n) for extract-min and O(1) amortized for
merge, making it one of the most efficient data structures for these operations.
2. Lazy consolidation: The use of lazy consolidation allows for the merging of trees to be
performed more efficiently in batches, rather than one at a time, improving the efficiency of
the merge operation.
3. Efficient memory usage: Fibonacci heaps have a relatively small constant factor compared to
other data structures, making them a more memory-efficient choice in some applications.
Disadvantages of Fibonacci Heap:
1. Increased complexity: The structure and operations of a Fibonacci heap are more complex
than those of a binary or binomial heap, making it a less intuitive data structure for some
users.
2. Less well-known: Compared to other data structures, Fibonacci heaps are less well-known
and widely used, making it more difficult to find resources and support for implementation
and optimization.
UNIT-2
What is Hash Table?
A Hash table is defined as a data structure used to insert, look up, and remove key-value pairs
quickly. It operates on the hashing concept, where each key is translated by a hash function
into a distinct index in an array. The index functions as a storage location for the matching
value. In simple words, it maps the keys with the value.
in various applications such as data storage, retrieval, and cryptography. In data structures and
algorithms (DSA), hash functions are primarily used in hash tables, which are essential for
efficient data management. This article delves into the intricacies of hash functions, their
properties, and the different types of hash functions used in DSA.
What is a Hash Function?
A hash function is a function that takes an input (or ‘message’) and returns a fixed-size string of
bytes. The output, typically a number, is called the hash code or hash value. The main purpose
of a hash function is to efficiently map data of arbitrary size to fixed-size values, which are often
used as indexes in hash tables.
Advantages:
Works well when 𝑚m is a prime number.
Simple to implement.
In the multiplication method, a constant 𝐴A (0 < A < 1) is used to multiply the key. The
2. Multiplication Method
fractional part of the product is then multiplied by 𝑚m to get the hash value.
h(k)=⌊m(kAmod1)⌋
Where ⌊ ⌋ denotes the floor function.
Disadvantages:
More complex than the division method.
3. Mid-Square Method
In the mid-square method, the key is squared, and the middle digits of the result are taken as the
hash value.
Steps:
1. Square the key.
2. Extract the middle digits of the squared value.
Advantages:
Produces a good distribution of hash values.
Disadvantages:
May require more computational effort.
4. Folding Method
Steps:
1. Divide the key into parts.
Advantages:
Simple and easy to implement.
Disadvantages:
Depends on the choice of partitioning scheme.
1) Separate Chaining
The idea behind Separate Chaining is to make each cell of the hash table point to a linked list
of records that have the same hash function value. Chaining is simple but requires additional
memory outside the table.
Example: We have given a hash function and we have to insert some elements in the hash
table using a separate chaining method for collision resolution technique.
Hash function = key % 5,
Elements = 12, 15, 22, 25 and 37.
Let’s see step by step approach to how to solve the above problem:
Hence In this way, the separate chaining method is used as the collision resolution technique.
2) Open Addressing
In open addressing, all elements are stored in the hash table itself. Each table entry contains
either a record or NIL. When searching for an element, we examine the table slots one by one
until the desired element is found or it is clear that the element is not in the table.
The first hash function is h1(k) which takes the key and gives out a location on the hash table.
But if the new location is not occupied or empty then we can easily place our key.
But in case the location is occupied (collision) we will use secondary hash-function h2(k) in
combination with the first hash-function h1(k) to find the new location on the hash table.
This combination of hash functions is of the form
where
As we know that in binary search tree, the nodes in the left subtree have lesser value than the
root node and the nodes in the right subtree have greater value than the root node.
We know the key values of each node in the tree, and we also know the frequencies of each node
in terms of searching means how much time is required to search a node. The frequency and key-
value determine the overall cost of searching a node. The cost of searching is a very important
factor in various applications. The overall cost of searching a node should be less. The time
required to search a node in BST is more than the balanced binary search tree as a balanced
binary search tree contains a lesser number of levels than the BST. There is one way that can
reduce the cost of a binary search tree is known as an optimal binary search tree.
In the above tree, all the nodes on the left subtree are smaller than the value of the root node, and
all the nodes on the right subtree are larger than the value of the root node. The maximum time
required to search a node is equal to the minimum height of the tree, equal to logn.
Now we will see how many binary search trees can be made from the given number of keys.
For example: 10, 20, 30 are the keys, and the following are the binary search trees that can be
made out from these keys.
When we use the above formula, then it is found that total 5 number of trees can be created.
The cost required for searching an element depends on the comparisons to be made to search an
element. Now, we will calculate the average cost of time of the above binary search trees.
In the above tree, total number of 3 comparisons can be made. The average number of
comparisons can be made as:
In the above tree, the average number of comparisons that can be made as:
In the above tree, the average number of comparisons that can be made as:
In the above tree, the total number of comparisons can be made as 3. Therefore, the average
number of comparisons that can be made as:
In the above tree, the total number of comparisons can be made as 3. Therefore, the average
number of comparisons that can be made as:
In the third case, the number of comparisons is less because the height of the tree is less, so it's a
balanced binary search tree.
Till now, we read about the height-balanced binary search tree. To find the optimal binary search
tree, we will determine the frequency of searching a key.
Let's assume that frequencies associated with the keys 10, 20, 30 are 3, 2, 5.
The above trees have different frequencies. The tree with the lowest frequency would be
considered the optimal binary search tree. The tree with the frequency 17 is the lowest, so it
would be considered as the optimal binary search tree.
Dynamic Approach
Consider the below table, which contains the keys and frequencies.
First, we will calculate the values where j-i is equal to zero.
Now to calculate the cost, we will consider only the jth value.
The cost of c[0,1] is 4 (The key is 10, and the cost corresponding to key 10 is 4).
The cost of c[1,2] is 2 (The key is 20, and the cost corresponding to key 20 is 2).
The cost of c[2,3] is 6 (The key is 30, and the cost corresponding to key 30 is 6)
The cost of c[3,4] is 3 (The key is 40, and the cost corresponding to key 40 is 3)
Now we will calculate the values where j-i = 2
o When i=0 and j=2, then keys 10 and 20. There are two possible trees that can be made
out from these two keys shown below:
o When i=1 and j=3, then keys 20 and 30. There are two possible trees that can be made
out from these two keys shown below:
In the first binary tree, cost would be: 1*2 + 2*6 = 14
In the second binary tree, cost would be: 1*6 + 2*2 = 10
o When i=2 and j=4, we will consider the keys at 3 and 4, i.e., 30 and 40. There are two
possible trees that can be made out from these two keys shown as below:
In the first binary tree, cost would be: 1*6 + 2*3 = 12
o When i=0, j=3 then we will consider three keys, i.e., 10, 20, and 30.
The following are the trees that can be made if 10 is considered as a root node.
Advertisement
In the above tree, 10 is the root node, 20 is the right child of node 10, and 30 is the right child of
node 20.
In the above tree, 20 is the root node, 30 is the right child of node 20, and 10 is the left child of
node 20.
The following are the trees that can be created if 30 is considered as the root node.
In the above tree, 30 is the root node, 20 is the left child of node 30, and 10 is the left child of
node 20.
In the above tree, 30 is the root node, 10 is the left child of node 30 and 20 is the right child of
node 10.
Therefore, the minimum cost is 20 which is the 3rd root. So, c[0,3] is equal to 20.
o When i=1 and j=4 then we will consider the keys 20, 30, 40
c[1,4] = min{ c[1,1] + c[2,4], c[1,2] + c[3,4], c[1,3] + c[4,4] } + 11
= min{0+12, 2+3, 10+0}+ 11
= min{12, 5, 10} + 11
In this case, we will consider four keys, i.e., 10, 20, 30 and 40. The frequencies of 10, 20, 30 and
40 are 4, 2, 6 and 3 respectively.
w[0, 4] = 4 + 2 + 6 + 3 = 15
= min{4 + 12} + 15
= 16 + 15 = 31
= min {8 + 3} + 15
= 26
If we consider 40 as the root node then,
= min{20 + 0} + 15
= 35
In the above cases, we have observed that 26 is the minimum cost; therefore, c[0,4] is equal to
26.
In binary search trees we have seen the average-case time for operations like
search/insert/delete is O(log N) and the worst-case time is O(N) where N is the number of
nodes in the tree.
Like other Trees include AVL trees, Red Black Tree, B tree, 2-3 Tree is also a height
balanced tree.
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:
Nodes with two children are called 2-nodes. The 2-nodes have one data value and two
children
Nodes with three children are called 3-nodes. The 3-nodes have two data values and three
children.
Data is stored in sorted order.
It is a balanced tree.
All the leaf nodes are at same level.
Each node can either be leaf, 2 node, or 3 node.
Always insertion is done at leaf.
Search: 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:
1. If K < currentNode.leftVal, we explore the left subtree of the current node.
2. Else if currentNode.leftVal < K < currentNode.rightVal, we explore the middle subtree of
the current node.
3. Else if K > currentNode.rightVal, we explore the right subtree of the current node.
Consider the following example:
Insertion: There are 3 possible cases in insertion which have been discussed below:
Case 1: Insert in a node with only one data element
. Case 2: Insert in a node with two data elements whose parent contains only one data
element.
Case 3: Insert in a node with two data elements whose parent also contains two data
elements.
After deletion 69
To delete 72, 72 is an internal node. To delete this value swap 72 with its in-order successor
81 so that 72 now becomes a leaf node. Remove the value 72 from the leaf node.
After deletion 72
Now there is a leaf node that has less than 1 data value thereby violating the property of a 2-3
tree. So the node must be merged.
To merge the node, pull down the lowest data value in the parent’s node and merge it with its
left sibling.
To delete 99, 99 is present in a leaf node, so the data value can be easily removed.
After deletion 99
Now there is a leaf node that has less than 1 data value, thereby violating the property of a 2-3
tree.
So the node must be merged. To merge the node, pull down the lowest data value in the
parent’s node and merge it with its left sibling.
To delete 81, 81 is an internal node. To delete this value swap 81 with its in-order successor
90 so that 81 now becomes a leaf node. Remove the value 81 from the leaf node.
After deletion 81
Now there is a leaf node that has less than 1 data value, thereby violating the property of a 2-3
tree. So the node must be merged. To merge the node, pull down the lowest data value in the
parent’s node and merge it with its left sibling.
As internal node cannot be empty. So now pull down the lowest data value from the parent’s
node and merge the empty node with its left sibling
Rebalancing to Satisfy 2-3 Tree Property
NOTE: In a 2-3 tree, each interior node has either two or three children. This means that a 2-3
tree is not a binary tree.
Unit-4
Multiway Trees in Data Structures
Introduction:
In computer science and information technology, multiway trees-also referred to as multi-ary
trees or generic trees-are a fundamental type of data structure. They offer a flexible approach to
describe hierarchical structures and are used in a variety of contexts, including file systems,
databases, and parser trees. We shall examine the idea of multiway trees, their attributes, types,
operations, and importance in data structures in this article.
o Node Structure: Each node in a multiway tree has numerous pointers pointing to the
nodes that are its children. From node to node, the number of pointers may differ.
o Root Node: The root node, from which all other nodes can be accessed, is the node that
acts as the tree's origin.
o Leaf Nodes: Nodes with no children are known as leaf nodes. Leaf nodes in a multiway
tree can have any number of children, even zero.
o Height and Depth: Multiway trees have height (the maximum depth of the tree) and
depth (the distance from the root to a node). The depth and height of the tree might vary
substantially due to the different number of children.
Types of Multiway Trees:
Multiway trees come in a variety of forms, each with particular properties and uses. The most
typical varieties include:
o General Multiway Trees: Each node in these trees is allowed to have an unlimited
number of offspring. They are utilized in a broad variety of applications, such as
company organizational hierarchies and directory structures in file systems.
o B-trees: In file systems and databases, B-trees are a sort of self-balancing multiway tree.
They are effective for insertions, deletions, and searches because they are made to
preserve a balanced structure.
o Ternary Trees: A particular variety of multiway trees in which each node has precisely
three children is known as a ternary tree. They are frequently incorporated into
optimization and language processing methods.
o Quad Trees and Oct Trees: These are two examples of specialized multiway trees used
in geographic information systems and computer graphics for spatial segmentation and
indexing. Oct trees have eight children per node, compared to quad trees' four.
o Insertion: Adding a new node to the tree while making sure it preserves the structure and
characteristics of the tree.
o Deletion: A node is deleted from the tree while still preserving its integrity.
o Search: Finding a particular node or value within the tree using search.
o Traversal: Traversal is the process of going through every node in the tree in a particular
order, such as pre-order, in-order, or post-order.
o Balancing (for B-trees): To maintain quick search and retrieval times, balancing (for B-
trees) involves making sure the tree maintains its balance after insertions and deletions.
o Versatility: Multiway trees are useful for a wide range of applications because they offer
a versatile way to describe hierarchical relationships.
o Efficiency: For managing and organizing massive datasets in databases and file systems,
certain varieties of multiway trees, such as B-trees, are built for efficiency.
o Parsing and Syntax Trees: Multiway trees are crucial for compilers and interpreters
because they are utilized in the parsing and syntax analysis of programming languages.
o Spatial Data Structures: Multiway trees, such as quadtrees and oct trees, allow for
effective spatial indexing and partitioning in geographic information systems and
computer graphics.