ADS & A Unit-3 Study Material
ADS & A Unit-3 Study Material
ADS & A Unit-3 Study Material
Trees-II
Red - Black Tree is another variant of Binary Search Tree in which every node is colored either RED or
Red Black Tree is a Binary Search Tree in which every node is colored either RED or BLACK.
In Red Black Tree, the color of a node is decided based on the properties of Red-Black Tree. Every Red
Property #3: The children of Red colored node must be colored BLACK. (There should not be two
Property #4: In all the paths of the tree, there should be same number of BLACK colored nodes.
Property #5: Every new node must be inserted with RED color.
Property #6: Every leaf (e.i. NULL node) must be colored BLACK.
Example
The above tree is a Red-Black tree where every node is satisfying all the properties of Red-Black Tree.
Every Red Black Tree is a binary search tree but every Binary Search Tree need not be Red Black tree.
Insertion into RED BLACK Tree
In a Red-Black Tree, every new node must be inserted with the color RED. The insertion operation in Red
Black Tree is similar to insertion operation in Binary Search Tree. But it is inserted with a color property.
After every insertion operation, we need to check all the properties of Red-Black Tree. If all the properties
are satisfied then we go to next operation otherwise we perform the following operation to make it Red
Black Tree.
1. Recolor
2. Rotation
The insertion operation in Red Black tree is performed using the following steps...
Step 2 - If tree is Empty then insert the newNode as Root node with color Black and exit from the
operation.
Step 3 - If tree is not Empty then insert the newNode as leaf node with color Red.
Step 4 - If the parent of newNode is Black then exit from the operation.
Step 5 - If the parent of newNode is Red then check the color of parentnode's sibling of newNode.
Step 6 - If it is colored Black or NULL then make suitable Rotation and Recolor it.
Step 7 - If it is colored Red then perform Recolor. Repeat the same until tree becomes Red Black
Tree.
Example
Deletion Operation in Red Black Tree
The deletion operation in Red-Black Tree is similar to deletion operation in BST. But after every deletion
operation, we need to check with the Red-Black Tree properties. If any of the properties are violated then
make suitable operations like Recolor, Rotation and Rotation followed by Recolor to make it Red-Black
Tree.
Splay Tree
Splay tree is another variant of a binary search tree. In a splay tree, recently accessed element is placed at
Splay Tree is a self - adjusted Binary Search Tree in which every operation on element rearranges the
tree so that the element is placed at the root position of the tree.
In a splay tree, every operation is performed at the root of the tree. All the operations in splay tree are
involved with a common operation called "Splaying".
Splaying an element, is the process of bringing it to the root position by performing suitable rotation
operations.
In a splay tree, splaying an element rearranges all the elements in the tree so that splayed element is
placed at the root of the tree.
By splaying elements we bring more frequently used elements closer to the root of the tree so that any
operation on those elements is performed quickly. That means the splaying operation automatically brings
more frequently used elements closer to the root of the tree.
Every operation on splay tree performs the splaying operation. For example, the insertion operation first
inserts the new element using the binary search tree insertion process, then the newly inserted element is
splayed so that it is placed at the root of the tree. The search operation in a splay tree is nothing but
searching the element using binary search process and then splaying that searched element so that it is
placed at the root of the tree.
In splay tree, to splay any element we use the following rotation operations...
1. Zig Rotation
2. Zag Rotation
3. Zig - Zig Rotation
4. Zag - Zag Rotation
5. Zig - Zag Rotation
6. Zag - Zig Rotation
Example
Zig Rotation
The Zig Rotation in splay tree is similar to the single right rotation in AVL Tree rotations. In zig rotation,
every node moves one position to the right from its current position. Consider the following example...
Zag Rotation
The Zag Rotation in splay tree is similar to the single left rotation in AVL Tree rotations. In zag rotation,
every node moves one position to the left from its current position. Consider the following example...
Zig-Zig Rotation
The Zig-Zig Rotation in splay tree is a double zig rotation. In zig-zig rotation, every node moves two
positions to the right from its current position. Consider the following example...
Zag-Zag Rotation
The Zag-Zag Rotation in splay tree is a double zag rotation. In zag-zag rotation, every node moves two
positions to the left from its current position. Consider the following example...
Zig-Zag Rotation
The Zig-Zag Rotation in splay tree is a sequence of zig rotation followed by zag rotation. In zig-zag rotation,
every node moves one position to the right followed by one position to the left from its current position.
Consider the following example...
Zag-Zig Rotation
The Zag-Zig Rotation in splay tree is a sequence of zag rotation followed by zig rotation. In zag-zig rotation,
every node moves one position to the left followed by one position to the right from its current position.
Consider the following example...
Every Splay tree must be a binary search tree but it is need not to be balanced tree.
In the insertion operation, we first insert the element in the tree and then perform the splaying operation
on the inserted element.
Step 1: First, we insert node 15 in the tree. After insertion, we need to perform splaying. As 15 is a root
node, so we do not need to perform splaying.
Step 2: The next element is 10. As 10 is less than 15, so node 10 will be the left child of node 15, as shown
below:
Now, we perform splaying. To make 10 as a root node, we will perform the right rotation, as shown below:
Step 3: The next element is 17. As 17 is greater than 10 and 15 so it will become the right child of node 15.
Now, we will perform splaying. As 17 is having a parent as well as a grandparent so we will perform zig zig
rotations.
In the above figure, we can observe that 17 becomes the root node of the tree; therefore, the insertion is
completed.
Step 4: The next element is 7. As 7 is less than 17, 15, and 10, so node 7 will be left child of 10.
Now, we have to splay the tree. As 7 is having a parent as well as a grandparent so we will perform two
right rotations as shown below:
Still the node 7 is not a root node, it is a left child of the root node, i.e., 17. So, we need to perform one
more right rotation to make node 7 as a root node as shown below:
Deletion Operation in Splay Tree
The deletion operation in splay tree is similar to deletion operation in Binary Search Tree. But before
deleting the element, we first need to splay that element and then delete it from the root position. Finally
join the remaining tree using binary search tree logic.
Types of Deletions:
1. Bottom-up splaying
2. Top-down splaying
Bottom-up splaying
In bottom-up splaying, first we delete the element from the tree and then we perform the splaying on the
deleted node.
o First, we simply perform the standard BST deletion operation to delete 12 element. As 12 is a leaf
node, so we simply delete the node from the tree.
The deletion is still not completed. We need to splay the parent of the deleted node, i.e., 10. We have to
perform Splay(10) on the tree. As we can observe in the above tree that 10 is at the right of node 7, and
node 7 is at the left of node 13. So, first, we perform the left rotation on node 7 and then we perform the
right rotation on node 13, as shown below:
Still, node 10 is not a root node; node 10 is the left child of the root node. So, we need to perform the right
rotation on the root node, i.e., 14 to make node 10 a root node as shown below:
o Now, we have to delete the 14 element from the tree, which is shown below:
As we know that we cannot simply delete the internal node. We will replace the value of the node either
using inorder predecessor or inorder successor. Suppose we use inorder successor in which we replace the
value with the lowest value that exist in the right subtree. The lowest value in the right subtree of node 14
is 15, so we replace the value 14 with 15. Since node 14 becomes the leaf node, so we can simply delete it
as shown below:
Still, the deletion is not completed. We need to perform one more operation, i.e., splaying in which we
need to make the parent of the deleted node as the root node. Before deletion, the parent of node 14 was
the root node, i.e., 10, so we do need to perform any splaying in this case.
Top-down splaying
In top-down splaying, we first perform the splaying on which the deletion is to be performed and then
delete the node from the tree. Once the element is deleted, we will perform the join operation.
Step 1: In top-down splaying, first we perform splaying on the node 16. The node 16 has both parent as
well as grandparent. The node 16 is at the right of its parent and the parent node is also at the right of its
parent, so this is a zag zag situation. In this case, first, we will perform the left rotation on node 13 and
then 14 as shown below:
The node 16 is still not a root node, and it is a right child of the root node, so we need to perform left
rotation on the node 12 to make node 16 as a root node.
Once the node 16 becomes a root node, we will delete the node 16 and we will get two different trees, i.e.,
left subtree and right subtree as shown below:
As we know that the values of the left subtree are always lesser than the values of the right subtree. The
root of the left subtree is 12 and the root of the right subtree is 17. The first step is to find the maximum
element in the left subtree. In the left subtree, the maximum element is 15, and then we need to perform
splaying operation on 15.
As we can observe in the above tree that the element 15 is having a parent as well as a grandparent. A
node is right of its parent, and the parent node is also right of its parent, so we need to perform two left
rotations to make node 15 a root node as shown below:
After performing two rotations on the tree, node 15 becomes the root node. As we can see, the right child
of the 15 is NULL, so we attach node 17 at the right part of the 15 as shown below, and this operation is
known as a join operation.
Hash Table
Hashing : Hashing is a technique that is used to uniquely identify a specific object from a group of similar
objects. Some examples of how hashing is used in our lives include:
In universities, each student is assigned a unique roll number that can be used to retrieve
information about them.
In libraries, each book is assigned a unique number that can be used to determine information
about the book, such as its exact position in the library or the users it has been issued to etc.
In both these examples the students and books were hashed to a unique number.
Assume that you have an object and you want to assign a key to it to make searching easy. To store the
key/value pair, you can use a simple array like a data structure where keys (integers) can be used directly
as an index to store values. However, in cases where the keys are large and cannot be used directly as an
Hash Tables:
Hash Table is a data structure which stores data in an associative manner. In a hash table, data is stored in
an array format, where each data value has its own unique index value. Access of data becomes very fast
if we know the index of the desired data.
Thus, it becomes a data structure in which insertion and search operations are very fast irrespective of the
size of the data. Hash Table uses an array as a storage medium and uses hash technique to generate an
index where an element is to be inserted or is to be located from.
The idea of hashing is to distribute entries (key/value pairs) uniformly across an array. Each element is
assigned a key (converted key). By using that key you can access the element in O(1) time. Using the key,
the algorithm (hash function) computes an index that suggests where an entry can be found or inserted.
1. An element is converted into an integer by using a hash function. This element can be used as an
index to store the original element, which falls into the hash table.
2. The element is stored in the hash table where it can be quickly retrieved using hashed key.
hash = hashfunc(key)
index = hash % array_size
In this method, the hash is independent of the array size and it is then reduced to an index (a number
between 0 and array_size − 1) by using the modulo operator (%).
Hash Function Definition: A hash function is a function that takes a set of inputs of any arbitrary size and
fits them into a table or other data structure that contains fixed-size elements.
Hashes Definition: A hash is a value in the table or data structure generated by the hash function used to
generate that particular table or data structure. The table or data structure
generated is usually called a hash table. It is also generally assumed that the time
complexity of accessing data in a hash table is O(1), or constant.
Our hash function here is to take each value mod 10. The table to the right shows the resulting hash table.
We hash a series of values as we get them, so the first value we hash is the first value in the string of
values, and the last value we hash is the last value in the string of values.
Collisions
Definition: A collision occurs when more than one value to be hashed by a particular hash function hash to
the same slot in the table or data structure (hash table) being generated by the hash function.
Let’s take the exact same hash function from before: take the value to
be hashed mod 10, and place it in that slot in the hash table.
1) Chaining:
The idea is to make each cell of hash table point to a linked list of records that have same hash function
value.
Let us consider a simple hash function as “key mod 7” and sequence of keys as 50, 700, 76, 85, 92, 73,
101.
Chaining Advantages:
1) Simple to implement.
2) Hash table never fills up, we can always add more elements to the chain.
3) Less sensitive to the hash function or load factors.
4) It is mostly used when it is unknown how many and how frequently keys may be inserted or deleted.
Chaining Disadvantages:
1) Cache performance of chaining is not good as keys are stored using a linked list. Open addressing
provides better cache performance as everything is stored in the same table.
2) Wastage of Space (Some Parts of hash table are never used)
3) If the chain becomes long, then search time can become O(n) in the worst case.
4) Uses extra space for links.
2) Linear Probing:
In linear probing, we linearly probe for next slot. For example, the typical gap between two
probes is 1 as seen in the example below.
Let us consider a simple hash function as “key mod 7” and a sequence of keys as 50, 700, 76, 85, 92, 73,
101.
Challenges in Linear Probing :
1. Primary Clustering: One of the problems with linear probing is Primary clustering, many consecutive
elements form groups and it starts taking time to find a free slot or to search for an element.
2. Secondary Clustering: Secondary clustering is less severe, two records only have the same collision
chain (Probe Sequence) if their initial position is the same.
In this section we will see what is quadratic probing technique in open addressing scheme. There is an
ordinary hash function h’(x) : U → {0, 1, . . ., m – 1}. In open addressing scheme, the actual hash function
h(x) is taking the ordinary hash function h’(x) and attach some another part with it to make one quadratic
equation.
h´ = (𝑥) = 𝑥𝑚𝑜𝑑𝑚
ℎ(𝑥, 𝑖) = (ℎ´(𝑥) + 𝑖2)𝑚𝑜𝑑𝑚
We can put some other quadratic equations also using some constants
The value of i = 0, 1, . . ., m – 1. So we start from i = 0, and increase this until we get one free space. So
initially when i = 0, then the h(x, i) is same as h´(x).
Quadratic Probing Example:
we have a list of size 20 (m = 20). We want to put some elements in linear probing fashion. The elements
are {96, 48, 63, 29, 87, 77, 48, 65, 69, 94, 61}
Hash Table
4. Double Hashing
Double hashing is a collision resolving technique in Open Addressed Hash tables. Double hashing uses
the idea of applying a second hash function to key when a collision occurs.
Advantages of Double hashing
The advantage of Double hashing is that it is one of the best form of probing, producing a uniform
distribution of records throughout a hash table.
This technique does not yield any clusters.
It is one of effective method for resolving collisions.
Output:
Difference between separate chaining and open addressing:
Chaining is Less sensitive to the hash function or Open addressing requires extra care to
3. load factors. avoid clustering and load factor.
Wastage of Space (Some Parts of hash table in In Open addressing, a slot can be used even
6. chaining are never used). if an input doesn’t map to it.
2-mark questions:
1. What are red-black trees? What problem do they solve
Binary search trees work well when they are ordered and balanced. But when new items are inserted into
an ordered binary search tree, the binary search tree becomes unbalanced. With unbalanced trees the
efficiency of searches degrades.
Red-black trees are specialized binary search trees with additional features which ensures that the trees
are balanced.
Red-black trees are balanced during insertion and deletion od items to the tree. The insertion and deletion
routines of the red-black tree check that certain characteristics of the tree are not violated. If these
characteristics are violated, the routines re-structure the tree so that the tree remains in a balanced state.
4. What are the rules that have to be followed when an item is inserted or deleted from a tree?
Followed are the rules that must be followed when a node is inserted or deleted from a red-black tree.
1. Every node is either a red node or a black node
2. The root node of the red-black tree is always black.
3. If a node is red then is child nodes must be black. The converse is not true. I.e. if a node is black then its
child nodes can either be red or black.
4. Every path from the root node to a leaf node, or to a null node, must contain the same number of black
nodes.
5. How do you fix rule violations when a node is inserted or deleted from a red-black tree?
6. What is the time efficiency of insertion, deletion and search operations on red-black trees?
Insertions, deletions and searches are the common operations performed on red-black trees.
The time efficiency of these operation on binary search trees is O(logN).
Bits
1. What is the special property of red-black trees and what root should always be?
a) a color which is either red or black and root should always be black color only
b) height of the tree
c) pointer to next node
d) a color which is either green or black
2. Why do we impose restrictions like
.root property is black
. every leaf is black
. children of red node are black
. all leaves have same black
a) to get logarithm time complexity
b) to get linear time complexity
c) to get exponential time complexity
d) to get constant time complexity
3. Which of the following is an application of Red-black trees and why?
a) used to store strings efficiently
b) used to store integers efficiently
c) can be used in process schedulers, maps, sets
d) for efficient sorting
4. Why Red-black trees are preferred over hash tables though hash tables have constant time
complexity?
a) no they are not preferred
b) because of resizing issues of hash table and better ordering in redblack trees
c) because they can be implemented using trees
d) because they are balanced
5. When to choose Red-Black tree, AVL tree and B-trees?
a) many inserts, many searches and when managing more items respectively
b) many searches, when managing more items respectively and many inserts respectively
c) sorting, sorting and retrieval respectively
d) retrieval, sorting and retrieval respectively
6. What are splay trees?
a) self adjusting binary search trees
b) self adjusting binary trees
c) a tree with strings
d) a tree with probability distributions
7. Which of the following property of splay tree is correct?
a) it holds probability usage of the respective sub trees
b) any sequence of j operations starting from an empty tree with h nodes atmost, takes
O(jlogh) time complexity
c) sequence of operations with h nodes can take O(logh) time complexity
d) splay trees are unstable trees
8. Why to prefer splay trees?
a) easier to program
b) space efficiency
c) easier to program and faster access to recently accessed items
d) quick searching
9. Which of the following options is an application of splay trees?
a) cache Implementation
b) networks
c) send values
d) receive values
10. When we have red-black trees and AVL trees that can perform most of operations in logarithmic
times, then what is the need for splay trees?
a) no there is no special usage
b) In real time it is estimated that 80% access is only to 20% data, hence most used ones
must be easily available
c) redblack and avl are not upto mark
d) they are just another type of self balancing binary search trees
11. What is the disadvantage of using splay trees?
a) height of a splay tree can be linear when accessing elements in non decreasing order.
b) splay operations are difficult
c) no significant disadvantage
d) splay tree performs unnecessary splay when a node is only being read
12. What is a hash table?
a) A structure that maps values to keys
b) A structure that maps keys to values
c) A structure used for storage
d) A structure used to implement stack and queue
13. If several elements are competing for the same bucket in the hash table, what is it called?
a) Diffusion
b) Replication
c) Collision
d) Duplication
14. What is direct addressing?
a) Distinct array position for every possible key
b) Fewer array positions than keys
c) Fewer keys than array positions
d) Same array position for all keys
15. What is the search complexity in direct addressing?
a) O(n)
b) O(logn)
c) O(nlogn)
d) O(1)
16. What is a hash function?
a) A function has allocated memory to keys
b) A function that computes the location of the key in the array
c) A function that creates an array
d) A function that computes the location of the values in the array
17. Which of the following is not a technique to avoid a collision?
a) Make the hash function appear random
b) Use the chaining method
c) Use uniform hashing
d) Increasing hash table size
18. What is simple uniform hashing?
a) Every element has equal probability of hashing into any of the slots
b) A weighted probabilistic method is used to hash elements into the slots
c) Elements has Random probability of hashing into array slots
d) Elements are hashed based on priority
19. In simple chaining, what data structure is appropriate?
a) Singly linked list
b) Doubly linked list
c) Circular linked list
d) Binary trees
20. In simple uniform hashing, what is the search complexity?
a) O(n)
b) O(logn)
c) O(nlogn)
d) O(1)