Trees Flashcards

(49 cards)

1
Q

What is a tree in data structures?

A

A hierarchical data structure consisting of nodes, with one root node and subtrees of children.

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

What is a binary tree?

A

A tree where each node has at most two children.

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

What is a binary search tree (BST)?

A

A binary tree where left child < parent < right child for all nodes.

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

What is a root node?

A

The topmost node in a tree.

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

What is a leaf node?

A

A node with no children.

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

What is an internal node?

A

A node with at least one child.

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

What is the depth of a node?

A

The number of edges from the root to the node.

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

What is the height of a node?

A

The number of edges on the longest downward path to a leaf.

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

What is the height of a tree?

A

The height of the root node.

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

What is a balanced binary tree?

A

A tree where the height difference between left and right subtrees is no more than 1.

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

What is a complete binary tree?

A

All levels are fully filled except possibly the last, which is filled left to right.

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

What is a full binary tree?

A

A binary tree where every node has either 0 or 2 children.

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

What is a perfect binary tree?

A

A complete binary tree in which all leaf nodes are at the same level.

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

What are the types of tree traversal?

A

Preorder, inorder, postorder, and level order.

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

What is preorder traversal?

A

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

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

What is inorder traversal?

A

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

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

What is postorder traversal?

A

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

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

What is level order traversal?

A

Visit nodes level by level from top to bottom.

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

What is the time complexity of traversing a tree?

A

O(n), where n is the number of nodes.

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

What is a degenerate (or pathological) tree?

A

A tree where each parent has only one child, like a linked list.

21
Q

What is the average time complexity for search in a BST?

A

O(log n) for balanced trees.

22
Q

What is the worst-case time complexity for search in a BST?

A

O(n), when the tree is unbalanced.

23
Q

What is a self-balancing binary search tree?

A

A BST that maintains a balanced structure automatically (e.g., AVL, Red-Black Tree).

24
Q

What is an AVL tree?

A

A self-balancing BST where the balance factor of any node is -1, 0, or 1.

25
What is a Red-Black Tree?
A self-balancing BST where each node is red or black, maintaining balance through color rules.
26
What is the benefit of self-balancing trees?
They provide guaranteed O(log n) search, insert, and delete.
27
How do you insert into a binary search tree?
Recursively or iteratively find the correct spot and add the node.
28
How do you delete a node in a BST?
Handle three cases: no child, one child, two children (replace with in-order successor).
29
What is an in-order successor?
The smallest node in the right subtree.
30
What is an in-order predecessor?
The largest node in the left subtree.
31
What is a trie?
A tree used for storing strings where each node represents a prefix.
32
What is a n-ary tree?
A tree where nodes can have up to n children.
33
What is a subtree?
A tree consisting of a node and all its descendants.
34
What is the space complexity of a binary tree with n nodes?
O(n)
35
How do you calculate the number of nodes in a perfect binary tree of height h?
2^(h+1) - 1
36
What is the time complexity for insertion in a BST?
O(log n) for balanced trees, O(n) for unbalanced.
37
What is the benefit of inorder traversal in a BST?
It returns the elements in sorted order.
38
What is a threaded binary tree?
A tree where null pointers are used to point to in-order predecessors or successors.
39
What is a binary expression tree?
A binary tree where leaves are operands and internal nodes are operators.
40
What is the use of a tree in file systems?
Directory structures are often represented as trees.
41
How do you check if two trees are identical?
Compare the data and structure recursively.
42
What is a mirror of a binary tree?
A tree with left and right children swapped at all nodes.
43
What is a segment tree?
A tree used for storing intervals and supporting efficient range queries.
44
What is a binary indexed tree (Fenwick Tree)?
A tree-like structure used to perform prefix sum queries efficiently.
45
What is the best-case time for searching in a balanced BST?
O(log n)
46
What is the worst-case time for inserting into an unbalanced BST?
O(n)
47
How is recursion commonly used with trees?
To traverse or modify tree nodes.
48
What is a B-tree?
A self-balancing search tree optimized for systems that read and write large blocks of data.
49
What is the main advantage of using a B-tree?
Efficient disk access and balanced search times.