Recursion, Stack, Queue, and Tree Data Structures in Computer Science

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/47

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

48 Terms

1
New cards

Base Case

Stops recursion

2
New cards

Recursive Case

Smaller problem call

3
New cards

Call Stack

Each call is pushed onto the call stack until the base case is reached, then unwinds (LIFO order)

4
New cards

Factorial Definition

`if (n <= 1) return 1; else return n * factorial(n-1);`

5
New cards

Sum of Array Recursive Function

`if (n == 0) return 0; else return a[n-1] + sumArray(a, n-1);`

6
New cards

Time Complexity of Factorial

O(n)

7
New cards

Time Complexity of Fibonacci Recursion

O(2ⁿ)

8
New cards

Tail Recursion

Performs the recursive call as the last operation

9
New cards

Non-Tail Recursion

Does extra work after the call

10
New cards

Stack Data Structure

Linear structure; LIFO (Last In First Out)

11
New cards

Core Stack Operations

`push()`, `pop()`, `top()` - all O(1)

12
New cards

C++ STL Syntax for Stack

#include

stack S;

S.push(5);

S.top();

S.pop();

S.empty();

13
New cards

Common Uses of Stacks

Function call tracking, Undo/Redo, Backtracking, DFS, expression evaluation

14
New cards

Queue Data Structure

Linear structure; FIFO (First In First Out)

15
New cards

Core Queue Operations

`enqueue(push)`, `dequeue(pop)`, `front()`, `empty()` - O(1)

16
New cards

C++ STL Syntax for Queue

#include

queue Q;

Q.push(5);

Q.front();

Q.pop();

Q.empty();

17
New cards

Common Applications of Queues

CPU/job scheduling, BFS traversal, printer queues, order processing

18
New cards

Binary Tree

A hierarchical structure where each node has ≤ 2 children (left and right)

19
New cards

Full Binary Tree

0 or 2 children

20
New cards

Complete Binary Tree

Levels full except possibly last filled left→right

21
New cards

Perfect Binary Tree

All levels completely filled

22
New cards

Max Nodes in Height h Binary Tree

2^(h+1) - 1

23
New cards

Tree Height

Max edges root→leaf

24
New cards

Number of Edges in Tree

Nodes - 1

25
New cards

Tree Traversal Orders

Inorder (LNR), Preorder (NLR), Postorder (LRN), Level Order (BFS)

26
New cards

height(Node* r)

Returns the height of a binary tree.

27
New cards

Pseudocode for checking full binary tree

if (!root) return true; if (!root->left && !root->right) return true; if (root->left && root->right) return isFull(l) && isFull(r); return false;

28
New cards

Recursion data structure

The stack.

29
New cards

Space complexity of recursion

O(depth of recursion).

30
New cards

Big-O of Merge Sort

O(n log n).

31
New cards

Big-O of Binary Search

O(log n).

32
New cards

Big-O of Insertion Sort

Best O(n), Average/Worst O(n²).

33
New cards

Big-O of Selection Sort

O(n²).

34
New cards

Average time for tree traversal

O(n) - each node visited once.

35
New cards

Examples where a queue is faster than a stack

Breadth-First Search, task scheduling, buffer management.

36
New cards

Recursion vs iteration speed

Extra stack frames and function call overhead.

37
New cards

Choosing iterative vs recursive solutions

Use recursion for naturally divisible problems (trees, divide & conquer); iteration for simple loops.

38
New cards

C++ STL library header for stack and queue

#include and #include

39
New cards

Difference between front and top

Front = queue's first element; Top = stack's last inserted element.

40
New cards

Purpose of recursion in Merge Sort

Divide array into smaller parts, sort each recursively, then merge.

41
New cards

BFS traversal data structure

Queue.

42
New cards

DFS traversal data structure

Stack (or recursion stack).

43
New cards

Pros and cons of linked-list implementation of stack/queue

Pros: Dynamic size; Cons: Extra pointer memory and allocations.

44
New cards

Big-O notation O(1)

Constant time — execution time independent of input size.

45
New cards

Concept of O(log n)

Work reduces by half each step (e.g. binary search).

46
New cards

Typical use-cases of recursion on trees

Traversal, searching, computing height, sum, mirror, or leaf count.

47
New cards

Base case in recursive function

To prevent infinite recursion and stack overflow.

48
New cards

Steps to Implement Recursion

Step1 - Define a base case: Identify the simplest (or base) case for which the solution is known or trivial. This is the stopping condition for the recursion, as it prevents the function from infinitely calling itself.

Step2 - Define a recursive case: Define the problem in terms of smaller subproblems. Break the problem down into smaller versions of itself, and call the function recursively to solve each subproblem.

Step3 - Ensure the recursion terminates: Make sure that the recursive function eventually reaches the base case, and does not enter an infinite loop.

Step4 - Combine the solutions: Combine the solutions of the subproblems to solve the original problem.

Explore top flashcards