Questions and Solutions
1. What is a Red-Black Tree?
- Options:
- A) A binary search tree with additional color properties.
- B) A tree that only allows red nodes.
- C) A tree where all nodes are black.
- D) A tree with no duplicate values.
- Answer: A) A binary search tree with additional color properties.
- Explanation: A Red-Black Tree is a binary search tree that follows specific properties to maintain balance, ensuring efficient operations.
2. What are the two colors used in a Red-Black Tree?
- Options:
- A) Red and Blue
- B) Black and White
- C) Red and Black
- D) Green and Yellow
- Answer: C) Red and Black
- Explanation: Red-Black Trees use two colors (red and black) to maintain balance and ensure that the tree remains approximately balanced.
3. What is the maximum height of a Red-Black Tree with n nodes?
- Options:
- A) O(n)
- B) O(log n)
- C) O(2 log n)
- D) O(sqrt(n))
- Answer: B) O(log n)
- Explanation: The maximum height of a Red-Black Tree is O(log n), ensuring that operations like insertion, deletion, and search remain efficient.
4. Which of the following properties must be true for a Red-Black Tree?
- Options:
- A) The root must be red.
- B) Red nodes cannot have red children.
- C) All leaves are red.
- D) Every path from a node to its leaves must have the same number of red nodes.
- Answer: B) Red nodes cannot have red children.
- Explanation: One of the fundamental properties of Red-Black Trees is that red nodes cannot have red children, which helps maintain balance.
5. What is the role of rotations in Red-Black Trees?
- Options:
- A) To delete nodes.
- B) To maintain the balance of the tree.
- C) To insert nodes.
- D) To traverse the tree.
- Answer: B) To maintain the balance of the tree.
- Explanation: Rotations are used to restore balance in the tree after insertions and deletions, ensuring the Red-Black properties are maintained.
6. When a new node is inserted into a Red-Black Tree, what color is it initially?