ADS&A
ADS&A
Doubly linked list is a data structure that has reference to both the previous and next nodes in
the list. It provides simplicity to traverse, insert and delete the nodes in both directions in a list.
In a doubly linked list, each node contains three data members:
data: The data stored in the node
next: It refers to the reference to the next node
prev: It refers to the reference to the previous node
Creation of Doubly Linked Lists in Java:
To create a doubly linked list, first, we need to define a Node class that has three data members
that will store the data stored in the node, the reference to the next node, and the reference to
the previous node.
Now we need to create a doubly linked list class that will contain two variables head and tail
which will be referenced to the first and last node of the list respectively and also a constructor
which will initialize both head and tail to the null.
Below is the code to insert a node at the front of the linked list:
Below is the code to insert a node at a specific position in the linked list:
Below is the code to insert a node at the end of the linked list:
First, we will check if the list is empty if it is then we return, and if there is only one node in
the list then we will set both the head and tail to null otherwise we will simply set the head
node to the next node of the current head in the list, and set the previous reference of the new
head node to null.
if (pos == 1) {
deleteAtBeginning();
return;
}
Below is the code for the Deletion of the last node in the list:
Java
2. What is a binary tree? Differentiate a binary tree from a binary search tree.
First, we will understand the binary tree and binary search tree separately, and then we will look
at the differences between a binary tree and a binary search tree.
A Binary tree is a non-linear data structure in which a node can have either 0, 1 or maximum 2
nodes. Each node in a binary tree is represented either as a parent node or a child node. There
can be two children of the parent node, i.e., left child and right child.
There is only one way to reach from one node to its next node in a binary tree.
In the above figure, we can observe that each node contains utmost 2 children. If any node does
not contain left or right child then the value of the pointer with respect to that child would be
NULL.
o Root node: The root node is the first or the topmost node in a binary tree.
o Parent node: When a node is connected to another node through edges, then that node is
known as a parent node. In a binary tree, parent node can have a maximum of 2 children.
o Child node: If a node has its predecessor, then that node is known as a child node.
o Leaf node: The node which does not contain any child known as a leaf node.
o Internal node: The node that has atleast 2 children known as an internal node.
o Depth of a node: The distance from the root node to the given node is known as a depth
of a node. We provide labels to all the nodes like root node is labeled with 0 as it has no
depth, children of the root nodes are labeled with 1, children of the root child are labeled
with 2.
o Height: The longest distance from the root node to the leaf node is the height of the
node.
In a binary tree, there is one tree known as a perfect binary tree. It is a tree in which all the
internal nodes must contain two nodes, and all the leaf nodes must be at the same depth. In the
case of a perfect binary tree, the total number of nodes exist in a binary tree can be calculated by
using the following equation:
n = 2m+1-1
A Binary search tree is a tree that follows some order to arrange the elements, whereas the binary
tree does not follow any order. In a Binary search tree, the value of the left node must be smaller
than the parent node, and the value of the right node must be greater than the parent node.
In the above figure, we can observe that the value of the root node is 15, which is greater than the
value of all the nodes in the left subtree. The value of root node is less than the values of all the
nodes in a right-subtree. Now, we move to the left-child of the root node. 10 is greater than 8 and
lesser than 12; it also satisfies the property of the Binary search tree. Now, we move to the right-
child of the root node; the value 20 is greater than 17 and lesser than 25; it also satisfies the
property of binary search tree. Therefore, we can say that the tree shown above is the binary
search tree.
Now, if we change the value of 12 to 16 in the above binary tree, we have to find whether it is
still a binary search tree or not.
The value of the root node is 15 which is greater than 10 but lesser than 16, so it does not satisfy
the property of the Binary search tree. Therefore, it is not a binary search tree.
Suppose we have to search 10 in the above binary tree. To perform the binary search, we will
consider all the integers in a sorted array. First, we create a complete list in a search space, and
all the numbers will exist in the search space. The search space is marked by two pointers, i.e.,
start and end. The array of the above binary tree can be represented as
First, we will calculate the middle element and compare the middle element with the element,
which is to be searched. The middle element is calculated by using n/2. The value of n is 7;
therefore, the middle element is 15. The middle element is not equal to the searched element, i.e.,
10.
Note: If the element is being searched is lesser than the mid element, then the searching will
be performed in the left half; else, searching will be done on the right half. In the case of
equality, the element is found.
As the element to be searched is lesser than the mid element, so searching will be performed on
the left array. Now the search is reduced to half, as shown below:
The mid element in the left array is 10, which is equal to the searched element.
Time complexity
In a binary search, there are n elements. If the middle element is not equal to the searched
element, then the search space is reduced to n/2, and we will keep on reducing the search space
by n/2 until we found the element. In the whole reduction, if we move from n to n/2 to n/4 and so
on, then it will take log2n steps.
Structure The structure of the binary tree is The binary search tree is one of the
that the first node or the topmost types of binary tree that has the value of
node is known as the root node. all the nodes in the left subtree lesser or
Each node in a binary tree contains equal to the root node, and the value of
the left pointer and the right pointer. all the nodes in a right subtree are
The left pointer contains the address greater than or equal to the value of the
of the left subtree, whereas right root node.
pointer contains the address of right
subtree.
Operations The operations that can be Binary search trees are the sorted binary
implemented on a binary tree are trees that provide fast insertion, deletion
insertion, deletion, and traversal. and search. Lookups mainly implement
binary search as all the keys are
arranged in sorted order.
types Four types of binary trees are Full There are different types of binary
Binary Tree, Complete Binary Tree, search trees such as AVL trees, Splay
Perfect Binary Tree, and Extended tree, Tango trees, etc.
Binary Tree.
3. Linear Probing:
Suppose a new record R with key k is to be added to the memory table T but that the memory
locations with the hash address H (k). H is already filled.
Our natural key to resolve the collision is to crossing R to the first available location following T
(h). We assume that the table T with m location is circular, so that T [i] comes after T [m].
Linear probing is simple to implement, but it suffers from an issue known as primary clustering.
Long runs of occupied slots build up, increasing the average search time. Clusters arise because
an empty slot proceeded by i full slots gets filled next with probability (i + 1)/m. Long runs of
occupied slots tend to get longer, and the average search time increases.
Given an ordinary hash function h': U {0, 1...m-1}, the method of linear probing uses the hash
function.
Where 'm' is the size of hash table and h' (k) = k mod m. for i=0, 1....m-1.
Given key k, the first slot is T [h' (k)]. We next slot T [h' (k) +1] and so on up to the slot T [m-1].
Then we wrap around to slots T [0], T [1]....until finally slot T [h' (k)-1]. Since the initial probe
position dispose of the entire probe sequence, only m distinct probe sequences are used with
linear probing.
Example: Consider inserting the keys 24, 36, 58,65,62,86 into a hash table of size m=11 using
linear probing, consider the primary hash function is h' (k) = k mod m.
4. Bubble sort
In this article, we will discuss the Bubble sort Algorithm. The working procedure of bubble sort
is simplest. This article will be very helpful and interesting to students as they might face bubble
sort as a question in their examinations. So, it is important to discuss the topic.
Bubble sort works on the repeatedly swapping of adjacent elements until they are not in the
intended order. It is called bubble sort because the movement of array elements is just like the
movement of air bubbles in the water. Bubbles in water rise up to the surface; similarly, the array
elements in bubble sort move to the end in each iteration.
Although it is simple to use, it is primarily used as an educational tool because the performance
of bubble sort is poor in the real world. It is not suitable for large data sets. The average and
worst-case complexity of Bubble sort is O(n2), where n is a number of items.
Algorithm
In the algorithm given below, suppose arr is an array of n elements. The assumed swap function
in the algorithm will swap the values of given array elements.
1. begin BubbleSort(arr)
2. for all array elements
3. if arr[i] > arr[i+1]
4. swap(arr[i], arr[i+1])
5. end if
6. end for
7. return arr
8. end BubbleSort
To understand the working of bubble sort algorithm, let's take an unsorted array. We are taking a
short and accurate array, as we know the complexity of bubble sort is O(n2).
First Pass
Sorting will start from the initial two elements. Let compare them to check which is greater.
Here, 32 is greater than 13 (32 > 13), so it is already sorted. Now, compare 32 with 26.
Here, 26 is smaller than 36. So, swapping is required. After swapping new array will look like -
Here, 10 is smaller than 35 that are not sorted. So, swapping is required. Now, we reach at the
end of the array. After first pass, the array will be -
Second Pass
Here, 10 is smaller than 32. So, swapping is required. After swapping, the array will be -
Third Pass
Fourth pass