Data Structures Quiz
48 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What does the pop algorithm do when the stack is empty?

  • Returns an error message
  • Stores the value of stack[top]
  • Decrements top
  • Returns nothing (correct)

The size of an array in C can be altered after its declaration.

False (B)

What is the base value when calculating the position of elements in an array?

0

A static array allocates memory at __________ time.

<p>compile</p> Signup and view all the answers

Match the following stack operations with their description:

<p>pop = Removes the top element from the stack Top = Returns the top element of the stack isEmpty = Checks if the stack has any elements increment = Increases the top index of the stack</p> Signup and view all the answers

Which of the following statements is true regarding static arrays?

<p>They are allocated memory at compile time (B)</p> Signup and view all the answers

In the algorithm for Top, if there are elements in the stack, it returns the element at the index specified by top.

<p>True (A)</p> Signup and view all the answers

What does the isEmpty function return if the top index is less than 1?

<p>true</p> Signup and view all the answers

What does the last node in a linked list point to?

<p>It points to None. (C)</p> Signup and view all the answers

Reversing a linked list is possible for cycled linked lists.

<p>False (B)</p> Signup and view all the answers

What is the time complexity of reversing a linked list using a stack?

<p>O(N)</p> Signup and view all the answers

The factorial of 6 is equal to ____.

<p>720</p> Signup and view all the answers

Which of the following is a characteristic of a stack?

<p>Last-In-First-Out (LIFO) (A)</p> Signup and view all the answers

Match the following descriptions with their terms:

<p>Stack = Last-In-First-Out data structure Factorial = Multiplication of all positive integers up to a number Linked List = Data structure consisting of nodes None = Indicates the end of a linked list</p> Signup and view all the answers

What is the maximum number of children a node in a binary tree can have?

<p>Two (D)</p> Signup and view all the answers

An empty linked list can be reversed.

<p>False (B)</p> Signup and view all the answers

In a full binary tree, all internal nodes must have either one child or two children.

<p>False (B)</p> Signup and view all the answers

What is the base case for the Tower of Hanoi algorithm?

<p>Moving one disk directly to the destination peg (C)</p> Signup and view all the answers

What formula gives the number of internal nodes in a full binary tree with n total nodes?

<p>(n-1)/2</p> Signup and view all the answers

What is the initial action taken when reversing a linked list?

<p>Check if the linked list is empty or has a single node.</p> Signup and view all the answers

The minimum number of nodes possible at the height h of a binary tree is given by ____.

<p>h + 1</p> Signup and view all the answers

The nth disk is always moved before the n-1 disks in the Tower of Hanoi algorithm.

<p>True (A)</p> Signup and view all the answers

What is the purpose of the auxiliary peg in the Tower of Hanoi problem?

<p>To temporarily hold disks while moving the other disks between the source and destination pegs.</p> Signup and view all the answers

Which of the following is true about the height of a binary tree with L leaf nodes?

<p>Height is L + 1 (A)</p> Signup and view all the answers

Match the types of binary trees with their descriptions:

<p>Full Binary Tree = Each internal node has zero or two children Complete Binary Tree = Elements arranged without missing any sequence Perfect Binary Tree = All levels are completely filled Binary Search Tree = Left child nodes are smaller than the parent node</p> Signup and view all the answers

The algorithm requires moving disks from the source peg to the destination peg using the __________ peg as a temporary holding area.

<p>auxiliary</p> Signup and view all the answers

In a binary tree, the number of nodes at each level doubles as you move down the tree.

<p>True (A)</p> Signup and view all the answers

Match the following steps of the Tower of Hanoi algorithm to their descriptions:

<p>Base Case = When only one disk needs moving Recursive Case Step 1 = Moving n-1 disks to the auxiliary peg Moving nth Disk = Moving the largest disk to its destination Recursive Case Step 2 = Moving the n-1 disks to the destination peg</p> Signup and view all the answers

What is the relationship between the number of leaf nodes (L) in a full binary tree with respect to its height?

<p>Height is L + 1</p> Signup and view all the answers

In the pseudocode, what occurs when the function TowerOfHanoi is called with n=1?

<p>A disk is printed to be moved. (C)</p> Signup and view all the answers

The Tower of Hanoi algorithm requires that no disk is placed on top of a smaller disk.

<p>True (A)</p> Signup and view all the answers

Describe the first action taken by the Tower of Hanoi algorithm when n > 1.

<p>Move the top n-1 disks from the source peg to the auxiliary peg.</p> Signup and view all the answers

What is the primary function of the Bubble Sort algorithm?

<p>To sort a list in ascending order (B)</p> Signup and view all the answers

Bubble Sort requires additional memory space for sorting.

<p>False (B)</p> Signup and view all the answers

What is the time complexity of Bubble Sort?

<p>O(N^2)</p> Signup and view all the answers

In Bubble Sort, if the current element is greater than the next element, we __________.

<p>swap both elements</p> Signup and view all the answers

Match the following characteristics with their respective sorting algorithm:

<p>Bubble Sort = Stable sorting algorithm Insertion Sort = Simulates sorting playing cards Selection Sort = Not covered in this content Quick Sort = Efficient for large datasets</p> Signup and view all the answers

How many iterations are needed to sort a list of size n using Bubble Sort?

<p>n - 1 (D)</p> Signup and view all the answers

In Bubble Sort, the smallest element gets bubbled to the top in every iteration.

<p>False (B)</p> Signup and view all the answers

What happens to elements with the same key value during Bubble Sort?

<p>They maintain their relative order.</p> Signup and view all the answers

What action is taken when deleting the first node of a circular linked list with multiple nodes?

<p>The last-&gt;next pointer is updated to skip the head node. (D)</p> Signup and view all the answers

In a circular linked list, if the list is empty, we can still delete a node.

<p>False (B)</p> Signup and view all the answers

What should be done if the key matches the single node in a circular linked list?

<p>Delete that node and set last to nullptr.</p> Signup and view all the answers

To delete the last node in a circular linked list, we must first find the __________ node.

<p>second last</p> Signup and view all the answers

Match the deletion tasks with their corresponding actions:

<p>Delete the first node = Update last-&gt;next pointer to skip head Delete a specific node = Use curr and prev pointers Delete the last node = Find the second last node Searching in the list = Traverse until the value is found or return to start</p> Signup and view all the answers

What is returned after deleting a node from a circular linked list?

<p>The updated last pointer. (B)</p> Signup and view all the answers

If a circular linked list has only one node and that node is deleted, the last pointer is set to a valid node.

<p>False (B)</p> Signup and view all the answers

Searching in a circular linked list involves starting at a given node and __________ until the target value is found.

<p>traversing the list</p> Signup and view all the answers

Flashcards

Array

A data structure that stores a collection of elements of the same data type in contiguous memory locations.

Static Array

An array where the size is fixed and cannot be changed after creation.

Stack

A data structure that follows the Last-In-First-Out (LIFO) principle, where the last element added is the first one removed.

pop (Stack)

An operation that removes and returns the top element from a stack.

Signup and view all the flashcards

Top (Stack)

A function that returns the value of the element at the top of the stack.

Signup and view all the flashcards

isEmpty (Stack)

A function that checks if a stack is empty.

Signup and view all the flashcards

push (Stack)

An operation that adds an element to the top of a stack.

Signup and view all the flashcards

top (Stack index)

The index of the topmost element in a stack.

Signup and view all the flashcards

Binary Tree

A data structure where each node has at most two children, referred to as the left and right children.

Signup and view all the flashcards

Depth of a Binary Tree

The number of edges from a node to the root node.

Signup and view all the flashcards

Height of a Binary Tree

The number of edges from the deepest node to the root node.

Signup and view all the flashcards

Full Binary Tree

A binary tree where each internal node has either zero or two children. No nodes have only one child.

Signup and view all the flashcards

Internal Nodes in Full Binary Tree

The number of internal nodes in a full binary tree is given by (n-1)/2, where n is the total number of nodes.

Signup and view all the flashcards

Leaf Nodes in Full Binary Tree

The number of leaf nodes in a full binary tree is given by (n+1)/2, where n is the total number of nodes.

Signup and view all the flashcards

Complete Binary Tree

A binary tree where all levels are filled with nodes from left to right, except possibly the last level.

Signup and view all the flashcards

Perfect Binary Tree

A complete binary tree where all the levels are completely filled and the last level is also filled from left to right.

Signup and view all the flashcards

Linked List

A data structure with a series of nodes, each containing data and a link to the next node. The last node points to None, indicating the end.

Signup and view all the flashcards

Reversing a Linked List

Reversing a linked list means changing the order of nodes so the last one becomes the first, the second-to-last becomes the second, and so on.

Signup and view all the flashcards

Stack in Linked List Reversal

A data structure used for reversing a linked list by storing the values in reverse order. The algorithm iterates through the linked list, pushing each node's value onto the stack. Then, it pops the elements from the stack and updates the linked list nodes.

Signup and view all the flashcards

Factorial

The product of all positive integers from 1 to a given non-negative integer, denoted by '!' (e.g., 5! = 54321 = 120).

Signup and view all the flashcards

Stack in Factorial Calculation

A method of calculating factorial using a stack. It involves taking user input for the number, initializing a stack and a variable ( 'top' ) to track the stack's top, and then pushing elements onto the stack.

Signup and view all the flashcards

Time Complexity: O(N)

The time complexity of an algorithm is a measure of the amount of time it takes to run as the input size grows. O(N) represents linear time complexity, meaning the time grows proportionally to the input size.

Signup and view all the flashcards

Base Case (Tower of Hanoi)

The simplest case in a recursive algorithm where the solution is directly obtainable without further recursion. In the Tower of Hanoi, it's moving the single remaining disk to the destination peg.

Signup and view all the flashcards

Recursive Case (Tower of Hanoi)

The part of a recursive algorithm that breaks down the problem into smaller, similar subproblems that are solved recursively until the base case is reached. In the Tower of Hanoi, it's moving a stack of disks by recursively moving smaller stacks.

Signup and view all the flashcards

Move n-1 Disks (Tower of Hanoi)

Moving a set of n-1 disks from one peg to another using a temporary holding peg. This involves moving disks recursively until the largest disk is free to move. It's a crucial part of the recursive solution to the Tower of Hanoi.

Signup and view all the flashcards

Destination Peg

In the Tower of Hanoi problem, it's the peg that holds the target location for all disks. After the recursive steps are complete, all disks are moved to this peg according to the puzzle's rules.

Signup and view all the flashcards

Auxiliary Peg

The peg used as a temporary holding area in the Tower of Hanoi puzzle while moving disks from the source peg to the destination peg. It allows for the strategic movement of smaller disks when the largest disk is blocked.

Signup and view all the flashcards

Source Peg

The peg where the disks start in the Tower of Hanoi puzzle. The goal is to move these disks, one at a time, following the specific rules to the destination peg.

Signup and view all the flashcards

Recursion

The ability to break down a problem into smaller, identical versions of itself, each solved by a recursive call until a base case is reached. It's a powerful technique for solving complex problems.

Signup and view all the flashcards

Divide and Conquer

A technique used to find solutions to problems by breaking them into subproblems and recursively solving them. It's often used in algorithms like the Tower of Hanoi.

Signup and view all the flashcards

Bubble Sort

A sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order, moving the larger elements towards the end of the list. The algorithm continues until the list is sorted.

Signup and view all the flashcards

Passes in Bubble Sort

The number of times the algorithm iterates through the list. For a list of 'n' elements, Bubble Sort needs 'n-1' passes to sort the list completely.

Signup and view all the flashcards

Swap Operation

The process of exchanging the positions of two elements in the array.

Signup and view all the flashcards

Bubble Sort's Behavior

The largest element is moved to its correct position in each pass, ensuring that the largest elements are gradually placed at the rightmost positions of the list.

Signup and view all the flashcards

Time Complexity of Bubble Sort

The algorithm's efficiency depends on the size of the input data. Time complexity refers to how long the algorithm takes to complete its task. Bubble Sort has a time complexity of O(N2), meaning that its execution time increases proportionally to the square of the number of elements in the list, making it inefficient for larger datasets.

Signup and view all the flashcards

Memory Space Complexity of Bubble Sort

The algorithm only uses the memory space needed to store the original list. It does not require extra memory to perform the sorting operation.

Signup and view all the flashcards

Stability of Bubble Sort

Maintaining the relative order of elements that have the same value in the initial list. This means elements with the same value will be placed in the sorted list in the same order they were in the original list.

Signup and view all the flashcards

Insertion Sort

An algorithm that divides the input list into a sorted and unsorted part. The algorithm iterates through the unsorted part, taking the current element and comparing it to the sorted elements until it finds its appropriate position and inserts it in the sorted part. The process is repeated until the entire list is sorted.

Signup and view all the flashcards

Deleting the First Node in a Circular Linked List

The process of removing the first node from a circular linked list. If the list is empty, do nothing. If it has one element, delete the node and set the last pointer to null. Otherwise, update the last node's next pointer to skip the head, delete the head, and return the updated last pointer.

Signup and view all the flashcards

Recursive Function to Count Nodes in a Linked List

A recursive function that counts the number of nodes in a linked list. It checks if the given node is null. If it is, it returns 0. Otherwise, it recursively calls itself with the next node and adds 1 to the result.

Signup and view all the flashcards

Circular Linked List

A circularly linked list where the last node's next pointer points back to the head node. This creates a continuous loop.

Signup and view all the flashcards

Deleting a Specific Node in a Circular Linked List

The process of removing a specific node from a circular linked list. If the list is empty, do nothing. If it has one element and it's the target, delete it and set the last pointer to null. If the target is the head, update the last node's next pointer to skip the head and delete the head. For other nodes, traverse the list and update the previous node's next pointer to skip the target node. If the target is the last node, update the last pointer accordingly. If the target isn't found, do nothing.

Signup and view all the flashcards

Deleting the Last Node in a Circular Linked List

The process of removing the last node from a circular linked list. If the list is empty, do nothing. If it has one element, delete the node and set the last pointer to null. Otherwise, traverse the list to find the second last node, update its next pointer back to the head, delete the last node, and return the updated last pointer.

Signup and view all the flashcards

Searching in a Circular Linked List

The process of searching for a specific node in a circular linked list. You start at a given node and traverse the list until you find the target value or return to the starting node.

Signup and view all the flashcards

Head Node in a Linked List

This is the node where the traversal of the linked list begins. Often, it's also the node pointed to by the 'head' pointer.

Signup and view all the flashcards

Tail Node in a Linked List

The end of the linked list. It is often marked with a null pointer to indicate that there are no more nodes following it.

Signup and view all the flashcards

Study Notes

Data Structure - "Stack"

  • A data structure is a way of arranging data on a computer for efficient access and updating.
  • Linear Data Structure: Data elements arranged sequentially, with each connected to its previous and next elements.
  • Examples: Array, stack, queue, linked list.
  • Static Data Structure: Fixed memory size. Easier to access elements.
  • Example: Array
  • Dynamic Data Structure: Size not fixed. Can be updated randomly during runtime. Efficient memory usage.
  • Examples: Queue, stack
  • Non-linear Data Structure: Data elements not arranged sequentially. Not traversed in a single run.
  • Examples: Trees, graphs
  • Stack: Linear data structure where insertion and removal of elements happen at the same end (top of the stack).
  • Follows LIFO (Last-In, First-Out) principle.

Introduction to Stack

  • The element inserted last is the first one to be removed.
  • To implement a stack, maintain a pointer to the top of the stack.
  • Key Differences between Arrays and Stacks
  • Stacks are limited-access data structures.
  • Arrays don't have the LIFO principle.

Stack Operations

  • push(): Adds an element to the top of the stack.
  • pop(): Removes an element from the top of the stack.
  • top(): Returns the top element of the stack without removing it.
  • isEmpty(): Checks if the stack is empty.
  • size(): Returns the number of elements in the stack.

Classification of Data Structures

  • Static
  • Dynamic
  • Linear
  • Non-linear

Algorithm for Push Operation

  • Check if the stack is full.
  • If it is full, then it's a stack overflow.
  • Otherwise, increment top(top = top + 1) and insert the new value at the top position.
  • Continue pushing elements until the stack capacity is reached.

Algorithm for Pop Operation

  • Check if the stack is empty.
  • If it's empty, then it's a stack underflow.
  • Otherwise, take the value at the top.
  • Decrement the top (top = top - 1).
  • Return the value that was stored at the top.

Types of Stacks

  • Fixed Size Stack
  • Dynamic Size Stack

Algorithm for isFull Operation

  • Check if the top equals capacity - 1
  • If true, return true, otherwise, return false

Algorithm for isEmpty Operation

  • Check if top equals -1
  • If true, return true, otherwise, return false

Algorithm for Top Operation

  • Check if the stack is empty.
  • If it is empty, print "Stack is empty".
  • Otherwise, return the element stored at index top.

Array

  • Collection of elements of the same data type stored at contiguous memory locations.
  • Position of each element calculated with an offset to a base value.
  • Base value is index 0.
  • Size is fixed in C language - cannot be changed after creation. (Static Array)

Types of Arrays

  • Static Array
  • Dynamic Array

Implementation of Stack Using Array

  • Initialize an array.
  • Treat one end of the array as the top of the stack.
  • Implement push, pop, peer operations.

Advantages of Array Implementation

  • Easy to implement
  • Memory is saved

Disadvantages of Array Implementation

  • Not dynamic - size is fixed at compile time.

Trees

  • Hierarchical data structure composed of nodes.
  • Topmost node is the root.
  • Nodes below are child nodes.
  • Child nodes can have multiple child nodes, forming a recursive structure.

Technical Definition of a Tree

  • A connected acyclic undirected graph.
  • One unique path between any node pairs.
  • Number of vertices (n) - 1 = number of edges

Properties of a Tree

  • Number of edges: A tree with N nodes has N - 1 edges.
  • Depth of a node: The number of edges in the path from the root to the node.
  • Height of a node: The length of the longest path from the node to a leaf node in the tree.
  • Height of the Tree: Length of the longest path from the root to a leaf.
  • Degree of a node: The number of children of a node. A leaf node has a degree of 0.

Types of Trees

  • Binary Tree
  • Ternary Tree
  • N-ary Tree (Generic Tree)

Properties of Binary Tree

  • Each node can have a maximum of two children.
  • Full binary tree has either 0 or 2 children for each internal node
  • Complete binary tree has elements in a sequential manner.
  • Perfect Binary Tree: All internal nodes have exactly two children; all leaf nodes are at the same level.
  • Degenerate Binary tree each node can have a maximum of one child.

Balanced Binary Tree

  • Height of left and right subtree differ by 0 or 1

Application of Tree Data Structure

  • File Systems
  • Data Compression
  • Compiler Design
  • Database Indexing

Graph

  • Non-linear data structure consisting of vertices and edges.
    • Vertices (nodes)
    • Edges (line/arcs) connecting vertices

Graph Representations

  • Adjacency Matrix
  • Adjacency List

Adjacency Matrix

  • 2D matrix (n x n)
  • 1 indicates an edge, 0 if no edge.

Adjacency List

  • Array of lists.
  • Each index represents a vertex
  • Contains a list of adjacent vertices for that vertex.

Breadth-First Search (BFS)

  • Traverses a graph level-by-level.
  • Uses a queue data structure.
  • Processes all the nodes at a level before moving to the next level.
  • Ensures that each vertex is visited once.

Depth First Search (DFS)

  • Explores the graph as deeply as possible along each branch before backtracking to other branches.
  • Uses a stack data structure.
    • Nodes are processed in a LIFO (last-in-first-out) manner.

Applications of BFS

  • Finding Shortest Path
  • Peer-to-peer networks
  • Web Crawling
  • Garbage collection
  • Computing maximum flow

Applications of DFS

  • Topological sorting
  • Cycle detection
  • Solving puzzles
  • Bipartite check

Priority Queue

  • A data structure that maintains elements in a way that the element with the highest priority is removed first.
  • Useful in cases where elements need to be processed based on their priority.

Types of Priority Queue

  • Ascending Order Priority Queue
  • Descending Order Priority Queue

Operations of Priority Queue

  • enQueue: Inserting an element into a priority queue.
  • deQueue: Removing the highest priority element from a priority queue.
  • peek: Getting the highest priority element without removing it.

Definitions

  • Graph: A graph is a data structure that consists of vertices (or nodes) and edges connecting pairs of vertices. Graphs can be used to model many real world problems (e.g. social networks, road maps, etc.)
  • Vertex: A vertex, also known as a node, is a fundamental unit of a graph. It is often represented as a point, circle or a labeled entity.
  • Edge: An edge is a connection/link between two vertices
  • Directed Graph: In a directed graph, the edges are oriented; they specify a direction of traverse between the vertices.
  • Undirected Graph: In an undirected graph edges are not oriented, there is no direction of traverse.

Dijkstra's Algorithm

  • Finds the shortest path between a source vertex and all other vertices in a graph.

Kruskal's Algorithm

  • Finds the minimum spanning tree (MST).

Hashing

  • Process of generating a value from text using a hash function.
  • Maps a significant number or string to a small integer.
  • The hash value can be used to locate data quickly in a hash table.

Types of Hash Function

  • Division
  • Mid-square

Collision Resolution Techniques

  • Separate Chaining (Open Hashing)
  • Open Addressing (Closed Hashing): Linear probing, Quadratic probing, and Double hashing.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

DSA Notes III Sem -1 PDF

Description

Test your knowledge on fundamental concepts of data structures with this quiz. It covers topics like stacks, linked lists, and binary trees, providing a mix of theoretical and practical questions. Challenge yourself and see how well you understand these crucial programming concepts!

More Like This

Use Quizgecko on...
Browser
Browser