# AVL Tree / Red Black Tree Flashcards

1
Q

What is a Binary Search Tree (BST)?

A

A Binary Search Tree is a binary tree in which each node has at most two children, and for every node, the left subtree contains only nodes with keys less than the node’s key, and the right subtree contains only nodes with keys greater than the node’s key.

2
Q

What is the time complexity for searching in a BST?

A

The time complexity for searching in a BST is
O(h), where h is the height of the tree. In the average case, this is
O(logn), and in the worst case (unbalanced tree), this is
O(n).

3
Q

What is the in-order traversal of a BST?

A

The in-order traversal of a BST visits nodes in non-decreasing order of their keys: left subtree, root, right subtree.

4
Q

How do you insert a new node into a BST?

A

To insert a new node into a BST, start at the root and recursively find the correct position in the left or right subtree, then insert the node as a leaf.

5
Q

How do you delete a node from a BST?

A

To delete a node from a BST:

Find the node to be deleted.
If the node has no children, simply remove it.
If the node has one child, replace it with its child.
If the node has two children, find its in-order successor (smallest node in the right subtree) or in-order predecessor (largest node in the left subtree), replace the node’s value with the successor’s value, and delete the successor.

6
Q

What is the height of a BST?

A

The height of a BST is the length of the longest path from the root to a leaf. A BST with n nodes can have a minimum height of
log(n+1) and a maximum height of
n−1.

7
Q

What is a balanced BST?

A

A balanced BST is a BST where the height of the left and right subtrees of any node differ by at most one. Balanced BSTs provide better performance for operations like search, insert, and delete, maintaining O(logn) time complexity.

8
Q

What is an AVL tree?

A

An AVL tree is a self-balancing BST where the difference in heights of left and right subtrees of any node is at most one. If at any time this condition is violated, rotations are performed to restore balance.

9
Q

What is a Red-Black tree?

A

A Red-Black tree is a self-balancing BST with the following properties:

Each node is either red or black.
The root is always black.
Red nodes cannot have red children (no two red nodes can be adjacent).
Every path from a node to its descendant NULL nodes has the same number of black nodes.
These properties ensure the tree remains balanced, with a height of O(logn).

10
Q

What is the purpose of tree rotations in a BST?

A

Tree rotations are used to maintain or restore balance in a BST, particularly in self-balancing trees like AVL and Red-Black trees. Rotations re-arrange the nodes to ensure the height balance property is preserved, improving the efficiency of operations.

11
Q

How do you perform a left rotation in a BST?

A

In a left rotation, a node x and its right child y are rotated such that y takes x’s place, x becomes y’s left child, and y’s left subtree becomes x’s right subtree.

12
Q

How do you perform a right rotation in a BST?

A

In a right rotation, a node y and its left child x are rotated such that x takes y’s place, y becomes x’s right child, and x’s right subtree becomes y’s left subtree.

13
Q

What is the difference between a BST and a binary tree?

A

A BST is a type of binary tree with the additional property that for each node, all elements in the left subtree are less than the node, and all elements in the right subtree are greater. A binary tree does not have this ordering property.

14
Q

How do you check if a given binary tree is a BST?

A

To check if a binary tree is a BST, ensure that for every node, all elements in the left subtree are less than the node, and all elements in the right subtree are greater than the node. This can be done using in-order traversal, checking that the sequence of visited nodes is in ascending order.

15
Q

Balance Factor

A

The difference between the heights of the left and right subtrees of a node.

16
Q

Height-Balanced Tree:

A

A tree where the height difference between left and right subtrees of any node is at most 1.