Binary Trees Flashcards

1
Q

What is a binary tree?

A

A binary tree is a tree data structure where each node has at most two children, typically referred to as the left child and the right child.

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

What is a node in a binary tree?

A

A node is a fundamental part of a binary tree that stores data and has pointers to its left and right children (if they exist).

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

What are parent and child nodes in a binary tree?

A

A parent node has references to one or two child nodes, while child nodes are those connected directly below a parent node.

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

What is the maximum height of a binary tree with N nodes?

A

The maximum height occurs when the tree is skewed (like a linked list), and the height is Nāˆ’1.

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

What are the invariants of a binary search tree (BST)?

A

In a BST:

The left subtree contains only nodes with values less than the parent node.
The right subtree contains only nodes with values greater than the parent node.
Both left and right subtrees must also be binary search trees.

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

How many children does a non-leaf node have in a binary tree?

A

A non-leaf node in a binary tree has one or two children.

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

How many parents does a non-root node have in a binary tree?

A

A non-root node has exactly one parent.

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

What makes a binary tree balanced?

A

A binary tree is balanced if the heights of the left and right subtrees of any node differ by at most 1.

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

What makes a binary tree unbalanced?

A

A binary tree is unbalanced if one subtree is significantly deeper than the other, leading to inefficient operations.

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

What is in-order traversal of a binary tree?

A

In-order traversal visits the left subtree, the root, and then the right subtree.

Example: For a BST with nodes 2, 1, 3:

In-order traversal: 1, 2, 3

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

What is pre-order traversal of a binary tree?

A

Pre-order traversal visits the root first, then the left subtree, followed by the right subtree.

Example: For the same BST (2, 1, 3):

Pre-order traversal: 2, 1, 3

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

What is post-order traversal of a binary tree?

A

Post-order traversal visits the left subtree, then the right subtree, and finally the root.

Example: For the BST (2, 1, 3):

Post-order traversal: 1, 3, 2

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

What is an expression tree?

A

An expression tree is a binary tree where the internal nodes represent operators, and the leaf nodes represent operands.

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

What are the properties of an expression tree?

A

Operands are at the leaf nodes.
Operators are at internal nodes.
The tree follows the order of operations.

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

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

A

The worst-case time complexity is
O(N), occurring when the tree becomes unbalanced.

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

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

A

The worst-case time complexity for deletion
O(N), particularly when dealing with unbalanced trees.

17
Q

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

A

The worst-case time complexity for lookup
O(N), occurring in an unbalanced BST.

18
Q

What is the worst-case complexity of dictionary operations (add, search, delete) in a BST?

A

In the worst case (unbalanced tree):

Add: O(N)
Search: O(N)
Delete: O(N)

19
Q

What is the best-case time complexity for searching in a binary search tree with N nodes?

A

The best-case time complexity is
O(logN) when the tree is perfectly balanced.

20
Q

What is the worst-case time complexity for deleting an element in a binary search tree?

A

The worst-case time complexity is
O(N), when the tree is unbalanced and skewed like a linked list.

21
Q

What is TreeSort?

A

TreeSort is a sorting algorithm that inserts elements into a binary search tree and then performs an in-order traversal to produce a sorted sequence.

22
Q

What is the best-case complexity of TreeSort?

A

The best-case complexity of TreeSort is
O(NlogN), occurring when the tree remains balanced.

23
Q

What is the worst-case complexity of TreeSort?

A

The worst-case complexity of TreeSort is
O(N ^2), occurring when the tree becomes unbalanced and degenerate.

24
Q

Is there a way to improve the worst-case complexity of TreeSort?

A

Yes, balancing the tree (e.g., using self-balancing trees like AVL or Red-Black Trees) can improve the worst-case complexity to
O(NlogN).