trees Flashcards

(42 cards)

1
Q

What is a graph?

A

A structure of nodes (vertices) connected by edges that model relationships.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the components of a graph?

A

Vertices (nodes) and edges (connections between nodes).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a directed graph?

A

A graph where edges have direction (arrows from one node to another).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an undirected graph?

A

A graph where edges have no direction; connections are bidirectional.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a weighted graph?

A

A graph where edges carry weights, often representing cost or distance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a path in a graph?

A

A sequence of connected edges where each edge leads to the next node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a cycle in a graph?

A

A path that starts and ends at the same node with all edges distinct.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does it mean for a graph to be connected?

A

There is a path between every pair of vertices.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an acyclic graph?

A

A graph with no cycles.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What defines a tree in graph theory?

A

An undirected, connected, and acyclic graph.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How many paths are there between two nodes in a tree?

A

Exactly one path.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a rooted tree?

A

A tree with one node designated as the root; defines levels in the tree.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a level in a tree?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the main difference between BFS and DFS?

A

BFS uses a queue (FIFO) to explore level-by-level; DFS uses a stack (LIFO) to go deep first.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why must you track visited nodes in graph traversal?

A

To avoid revisiting nodes and prevent infinite loops in cyclic graphs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why don’t you need to track visited nodes in trees?

A

Because trees are acyclic and each node is reached only once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is preorder traversal?

A

Visit root, then traverse left subtree, then right subtree.

18
Q

What is postorder traversal?

A

Traverse left subtree, then right subtree, then visit root.

19
Q

What is inorder traversal?

A

Traverse left subtree, visit root, then traverse right subtree (binary trees only).

20
Q

What is level-order traversal?

A

Traverse nodes level by level using a queue — same as BFS.

21
Q

Which traversal method uses a queue?

A

Breadth-first search (BFS) and level-order traversal.

22
Q

Which traversal method uses a stack or recursion?

A

Depth-first search (DFS), preorder, postorder, and inorder traversals.

23
Q

Why might a hash table not be suitable for range queries?

A

Because hash tables do not maintain any ordering of keys.

24
Q

What kind of data structure allows efficient key-based lookup and range queries?

A

A binary search tree (BST).

25
What property defines a binary search tree (BST)?
For each node, all keys in the left subtree are smaller, and all keys in the right subtree are larger.
26
What traversal of a BST gives keys in sorted order?
Inorder traversal.
27
What is the average-case time complexity of search in a BST?
O(log n) if the tree is balanced.
28
What is the worst-case time complexity of search in a BST?
O(n), if the tree is skewed.
29
What causes poor performance in an unbalanced BST?
Insertion of keys in sorted order, creating a degenerate (linear) tree.
30
What is the expected height of a random BST?
O(log n), as shown in a 2003 paper by Bruce Reed.
31
What is the motivation for AVL trees?
To maintain balance and guarantee O(log n) operations even in the worst case.
32
What does AVL stand for?
Adelson-Velsky and Landis — the inventors of AVL trees.
33
What is the balance factor of a node in an AVL tree?
The height of the right subtree minus the height of the left subtree.
34
What balance factors are allowed in an AVL tree?
-1, 0, or 1.
35
What happens when the balance factor goes outside the allowed range?
A rotation (single or double) is performed to restore balance.
36
What is a single rotation in AVL trees?
A restructure where a child node replaces its parent to reduce imbalance (used in LL or RR cases).
37
What is a double rotation in AVL trees?
Two successive rotations used for LR or RL imbalances to restore balance.
38
What is preserved after rotations in AVL trees?
The binary search tree property and the inorder traversal order.
39
What is the time complexity of insert/delete in an AVL tree?
O(log n), due to rebalancing.
40
How is balance restored after insertion or deletion in AVL trees?
By updating balance factors and performing rotations if necessary.
41
What additional information is stored in AVL tree nodes?
Height and balance factor.
42
What is the main advantage of an AVL tree over a regular BST?
AVL trees guarantee balanced height, ensuring efficient operations.