Intro to Data Structures
- 83Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What is a data structure?
A way to store and organize data in a computer so that the data can be used efficiently.
Or swipe the card
83 cards
What is a data structure?
A way to store and organize data in a computer so that the data can be used efficiently.
What are the two ways to study data structures?
- As mathematical and logical models (abstract data types). 2. As concrete implementations.
What is an abstract data type (ADT)?
An entity that is a definition of data and operations but does not have implementation details.
Give an example of an ADT.
A list that should store a group of elements of a particular data type, with operations to read and modify elements by position.
What is a static list?
A list that stores a given number of elements of a given data type, where the number of elements does not change and is known before creation.
What operations does a static list provide?
Write or modify element at any position, and read element at a particular position.
What is an array in the context of list implementation?
A data structure that gives concrete implementation for a static list, allowing creation of any data type, reading/writing elements at positions accessed via index.
What is a dynamic list?
A list that can grow as needed, with operations: insert at any position, remove element, count number of elements, read/modify element at a position, and specify data type.
How can a dynamic list be implemented using arrays?
Declare a large array, use a variable 'end' to mark the last element's index, and shift elements for insertions/removals. When full, create a new larger array (typically double size) and copy elements.
What is the time complexity of accessing an element by index in an array-based dynamic list?
O(1) (constant time).
What is the time complexity of inserting an element at a particular position in an array-based list (worst case)?
O(n), where n is the number of elements.
What is the time complexity of removing an element from an array-based list (worst case)?
O(n).
What is the time complexity of inserting an element at the end when the array is not full?
O(1) (constant time).
What is a limitation of using arrays for dynamic lists?
Waste of memory (unused reserved space) and costly resizing (copying elements) when array gets full.
What is a linked list?
A data structure consisting of nodes, each containing data and a link to the next node, stored in non-contiguous memory locations.
How is a linked list node typically defined in C/C++?
A structure with two fields: one for data (e.g., int) and a pointer to the next node (struct node* next).
What is the head of a linked list?
The first node in the linked list; the list is identified by the address of the head node.
What is the time complexity of accessing an element at a given position in a linked list?
O(n), where n is the number of nodes.
What is the time complexity of inserting a node at the beginning of a linked list?
O(1) (constant time).
What is the time complexity of deleting a node at the beginning of a linked list?
O(1).
What is the time complexity of inserting a node at the end of a linked list?
O(n) (if we only have head pointer; O(1) if we maintain a tail pointer).
What is the advantage of a linked list over an array for dynamic lists?
Efficient memory usage (nodes created as needed), no need to pre-specify size, and constant-time insertions/deletions at the beginning.
What is the disadvantage of a linked list compared to an array?
Sequential access (no constant-time random access) and extra memory for pointers.
How is a node inserted at a given position in a linked list?
Traverse to position n−1, set new node's next to the nth node, and set n−1th node's next to the new node.
How is a node deleted at a given position in a linked list?
Traverse to position n−1, set its next to the node after n (skip nth node), and free the nth node.
What is a doubly linked list?
A linked list where each node has two pointers: one to the next node and one to the previous node.
What are the advantages of a doubly linked list over a singly linked list?
Ability to traverse backwards, deletion of a node given only its pointer (no need for previous node), and easier implementation of some operations.
What is the memory overhead of a doubly linked list node compared to a singly linked list node?
Doubly linked list nodes store an extra pointer (for previous), so memory per node is larger.
How do you reverse a linked list iteratively?
Use three pointers: previous (initially NULL), current (head), next. While current != NULL: store next = current->next, set current->next = previous, move previous to current, move current to next. Finally set head = previous.
How do you reverse a linked list using recursion?
Recursively reach the last node, set head to that node. On returning from recursion, set current node's next's next to current node, and set current node's next to NULL.
What is a stack?
A list or collection with the restriction that insertion and deletion are performed only from one end, called the top. It is a Last-In-First-Out (LIFO) structure.
What are the fundamental operations of a stack?
Push (insert an element onto the top) and Pop (remove the top element). Additional operations: Top (return top element without removing) and IsEmpty (check if stack is empty).
How can a stack be implemented using arrays?
Use an array and a variable 'top' initialized to -1. Push: increment top and set array[top] = x. Pop: decrement top. Overflow condition: top == maxSize-1.
How can a stack be implemented using a linked list?
Use a singly linked list with head as top. Push: insert node at beginning. Pop: delete node from beginning. All operations O(1).
What are some applications of stack?
Function call execution (recursion), undo operations in editors, checking balanced parentheses, expression evaluation (postfix/prefix), reversing a list.
How can a stack be used to reverse a string?
Push all characters of the string onto a stack, then pop them one by one and write back to the string. The popped characters appear in reverse order.
How can a stack be used to check balanced parentheses?
Scan the expression left to right: push opening parentheses; on closing parenthesis, check if top of stack matches (same type) and pop; if mismatch or empty, expression is unbalanced. At end, stack should be empty.
What is infix, prefix, and postfix notation?
Infix: operator between operands (e.g., a+b). Prefix (Polish Notation): operator before operands (e.g., +ab). Postfix (Reverse Polish Notation): operator after operands (e.g., ab+).
How do you evaluate a postfix expression using a stack?
Scan tokens left to right. If operand, push onto stack. If operator, pop two operands (op2 then op1), apply operator, push result. At end, the stack contains the result.
How do you evaluate a prefix expression using a stack?
Scan tokens from right to left. If operand, push. If operator, pop two operands (op1 then op2), apply operator, push result. At end, stack has result.
How do you convert an infix expression to postfix using a stack?
Scan infix left to right. Operands: append to output. Operators: while stack not empty and top has higher or equal precedence, pop to output; then push current operator. '(': push; ')': pop until '(' and discard parentheses. At end, pop all remaining operators.
What is a queue?
A list or collection with the restriction that insertion is performed at one end (rear/tail) and deletion at the other end (front/head). It is a First-In-First-Out (FIFO) structure.
What are the fundamental operations of a queue?
Enqueue (insert at rear), Dequeue (remove from front), Front (peek at front element), IsEmpty (check if queue is empty). All should be O(1).
How can a queue be implemented using arrays?
Use a circular array with front and rear indices. Initially both -1. Enqueue: if empty, set front=rear=0; else rear = (rear+1)%N. Dequeue: if only one element, set front=rear=-1; else front = (front+1)%N.
How can a queue be implemented using a linked list?
Use two pointers: front (head) and rear (tail). Enqueue: create new node, set rear->next = new node, update rear. Dequeue: store front, set front = front->next, free old front. Both O(1).
What are some applications of queue?
Printer queue, CPU scheduling (process queue), breadth-first search (BFS) in graphs, simulating waiting lines.
What is a tree data structure?
A collection of entities called nodes linked together to simulate a hierarchy. It is a non-linear data structure with a root node and subtrees.
Define the following tree terms: root, child, parent, sibling, leaf, internal node, ancestor, descendant.
Root: topmost node with no parent. Child: a node directly connected to another node when moving away from root. Parent: the converse of child. Sibling: nodes sharing the same parent. Leaf: node with no children. Internal node: node with at least one child. Ancestor: any node on the path from root to the node (excluding itself). Descendant: any node in the subtree of the node.
What is the depth of a node in a tree?
The number of edges in the path from the root to that node.
What is the height of a node in a tree?
The number of edges in the longest path from that node to a leaf.
What is a binary tree?
A tree in which each node can have at most two children, often called left child and right child.
In a binary tree, what is the maximum number of nodes at level i (root at level 0)?
2i.
What is a complete binary tree?
A binary tree in which all levels are completely filled except possibly the last level, and the last level is filled from left to right.
What is a perfect binary tree?
A binary tree in which all internal nodes have two children and all leaves are at the same level.
What is the maximum number of nodes in a binary tree of height h (height measured as number of edges from root to deepest leaf)?
2h+1−1.
What is the height of a perfect binary tree with n nodes?
h=⌊log2n⌋ (approximate).
What is a balanced binary tree?
A binary tree where for each node, the difference between the heights of its left and right subtrees is at most 1 (or some constant).
How is a binary tree typically implemented in code?
A node structure with three fields: data (e.g., int), pointer to left child, pointer to right child.
What is a binary search tree (BST)?
A binary tree where for each node, all values in the left subtree are lesser (or equal) and all values in the right subtree are greater. This property holds for all nodes.
What is the average-case time complexity for search, insert, and delete in a BST?
O(logn) (if tree is balanced).
What is the worst-case time complexity for BST operations?
O(n), occurring when the tree is skewed (like a linked list).
How do you search for a value in a BST?
Start at root. If value equals node data, return true. If value < node data, search left subtree; else search right subtree. Repeat until found or node becomes NULL.
How do you insert a value into a BST?
Search for the node where the value should be inserted (following BST property). When a NULL child is reached, create a new node and attach it.
How do you find the minimum element in a BST?
Go to the leftmost node by traversing left children until left child is NULL. That node contains the minimum.
How do you find the height (maximum depth) of a binary tree?
Recursively: height(node) = 1 + max(height(left), height(right)). Base case: if node is NULL, return -1 (if counting edges) or 0 (if counting nodes).
What are the three depth-first traversal orders of a binary tree?
Preorder (root -> left -> right), Inorder (left -> root -> right), Postorder (left -> right -> root).
What is level-order traversal of a binary tree?
Breadth-first traversal visiting nodes level by level from left to right.
How do you check if a binary tree is a BST?
Use a recursive function that passes a valid range (min, max) for each node. For root, range is (−∞,∞). For left child, max becomes parent's data; for right child, min becomes parent's data. At each node, data must be within range.
How do you delete a node from a BST?
Search for node. Cases: 1) Leaf: simply delete. 2) One child: replace node with its child. 3) Two children: find inorder successor (minimum in right subtree), copy its value to the node, then delete the successor (which has at most one child).
What is the inorder successor of a node in a BST?
The node that comes immediately after the given node in inorder traversal. If node has right subtree, successor is the leftmost node in right subtree. Otherwise, successor is the nearest ancestor for which the node is in the left subtree.
What is a graph?
An ordered pair G=(V,E) where V is a set of vertices (nodes) and E is a set of edges (connections between vertices). Edges can be directed or undirected.
What is a directed graph (digraph)?
A graph where each edge has a direction, represented as an ordered pair (u, v) meaning an edge from u to v.
What is an undirected graph?
A graph where edges have no direction; each edge is an unordered pair {u, v} representing a bidirectional connection.
What is a weighted graph?
A graph in which each edge has an associated weight or cost.
What is a simple graph?
A graph with no self-loops (edges from a vertex to itself) and no multiple edges between the same pair of vertices.
What is a path in a graph?
A sequence of vertices where each adjacent pair is connected by an edge. A simple path has no repeated vertices.
What does it mean for a graph to be connected?
For an undirected graph: there is a path between any two vertices. For a directed graph: strongly connected if there is a path in both directions between any two vertices; weakly connected if the underlying undirected graph is connected.
What is a cycle in a graph?
A closed walk (starts and ends at same vertex) with no repeated vertices or edges except the start/end vertex.
What is the maximum number of edges in a simple undirected graph with n vertices?
2n(n−1).
What is the maximum number of edges in a simple directed graph with n vertices?
n(n−1).
What are sparse and dense graphs?
A sparse graph has number of edges e much less than n2 (typically e≈n). A dense graph has e close to the maximum possible (n2 for directed, n2/2 for undirected).
What is an adjacency matrix?
A V×V matrix where aij=1 (or weight) if there is an edge from vertex i to vertex j, and 0 (or infinity) otherwise.
What is an adjacency list?
An array of lists, one per vertex, where each list contains the neighbors (and possibly weights) of that vertex. Often implemented using linked lists or dynamic arrays.

Ask anything
We've read this whole study set, so questions about it get the best answers.