Ans:By not allowing it to get bogged down, the AVL Tree regulates the height of the binary search tree. The time required for every operation in the binary height h search tree is O (h). However, if BST is skewed, it may be extended to O(n) (i.e. worst case). If this height is limited to log n, the tree imposes on each operation a higher limit that is O(log n), where n is the number of nodes.
Ans:
// V - total vertices
// visited - boolean array to keep track of visited nodes
// graph - adjacency list.
// Main Topological Sort Function.
void topologicalSort()
{
Stack
// Mark all the vertices as not visited
boolean visited[] = new boolean[V];
for (int j = 0; j < V; j++){
visited[j] = false;
}
// Call the util function starting from all vertices one by one
for (int i = 0; i < V; i++)
if (visited[i] == false)
topologicalSortUtil(i, visited, stack);
// Print contents of stack -> result of topological sort
while (stack.empty() == false)
System.out.print(stack.pop() + " ");
}
// A helper function used by topologicalSort
void topologicalSortUtil(int v, boolean visited[],
Stack
{
// Mark the current node as visited.
visited[v] = true;
Integer i;
// Recur for all the vertices adjacent to the current vertex
Iterator
while (it.hasNext()) {
i = it.next();
if (!visited[i])
topologicalSortUtil(i, visited, stack);
}
// Push current vertex to stack that saves result
stack.push(new Integer(v));
}
Ans:In binary search, comparisons are substantially less than in linear search. On average, a linear search takes O(n) to search a list of n items while O(log n) is used to search for a list of n elements via a binary search.
Ans:You must sequentially search to discover the target key in a linked list. Each node is crossed and compared to the destination key. If different, the link to the next node follows. This crossing goes on to either identify the target key or to reach the final node.
|