5 BINARY SEARCH TREES Flashcards

1
Q

What is a binary search tree?

A

A dynamic data structure that supports efficient addition, removal, and searching of elements.

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

What are the key advantages of binary search trees over sorted arrays?

A

Efficient addition and removal of elements in addition to searches.

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

What is the structure of a binary search tree?

A

A hierarchical data structure composed of nodes, where each node has a value and up to two pointers to child nodes.

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

What are internal nodes and leaf nodes in a binary search tree?

A

Internal nodes have at least one child, while leaf nodes do not have any children.

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

What minimal information does a binary search tree node contain?

A

A value (or key), pointers to two child nodes, and an optional pointer to the parent node.

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

How is the binary search tree property defined?

A

For any node N, the value of any node in N’s left subtree is less than N’s value, and the value of any node in N’s right subtree is greater than N’s value.

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

What is the implication of the binary search tree property on node values?

A

It restricts the binary search tree to contain unique values.

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

True or False: Binary search trees can be defined to allow duplicate values.

A

True.

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

What is the role of pointers in a binary search tree?

A

Pointers link nodes to their children and parents, allowing traversal through the tree structure.

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

How do we search for a value in a binary search tree?

A

By walking down from the root node and comparing the current node’s value with the target value.

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

What happens if the target value is less than the current node’s value during a search?

A

The search progresses to the left subtree.

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

What happens if the target value is greater than the current node’s value during a search?

A

The search progresses to the right subtree.

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

What are the two approaches to implement a search in a binary search tree?

A

Iterative and recursive approaches.

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

What does the recursive search function return if the target value is not found?

A

Null.

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

In the context of a binary search tree, what does a dead end indicate?

A

A node that does not match the target value and does not have a child in the correct direction.

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

Describe the iterative search function for a binary search tree.

A

It uses a WHILE loop to traverse down the tree, checking values and reassigning the current node until the target is found or a dead end is reached.

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

What is the computational cost of searching in a binary search tree?

A

Proportional to the depth of the target value in the tree.

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

How does the structure of a binary search tree affect search efficiency?

A

Minimizing the tree’s depth increases search efficiency.

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

What is the primary concern when comparing binary search trees to sorted arrays?

A

Whether the added complexity and overhead of pointers are worth it compared to a single binary search on a sorted array.

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

Fill in the blank: A binary search tree starts at a single _______ node.

A

root

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

What is the purpose of auxiliary data in a binary search tree node?

A

To store additional information related to the node’s value, enhancing the data structure’s utility.

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

Why might a sorted array be preferable over a tree for a single search?

A

Building a tree is more expensive than a single linear scan.

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

What is a key advantage of using binary search trees in dynamic data environments?

A

Binary search trees allow efficient addition and removal of data points.

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

What is the first step when searching for a node in a binary search tree?

A

Start at the root node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What happens when inserting the first node into a binary search tree?
That node becomes the new root.
26
What does the wrapper data structure for a binary search tree simplify?
Handling of the root node and provides an easy-to-use interface.
27
What is the purpose of checking if the tree is empty at the start of a search?
To immediately return null if the search fails.
28
How do we add a value to a binary search tree?
Start at the root, progress down the tree, and create a new node at a dead end.
29
What do we do if we encounter a duplicate value when inserting into a binary search tree?
Update the node as needed or keep going to insert a new copy.
30
What is the cost of inserting a new node into a binary search tree proportional to?
The depth of the branch along which we insert the new node.
31
What are the three cases of node removal in a binary search tree?
* Removing a leaf node * Removing an internal node with a single child * Removing an internal node with two children
32
What happens when we remove a leaf node from a binary search tree?
We delete the node and update its parent's child pointer.
33
How do we remove an internal node with a single child?
Promote the single child to be the child of the deleted node's parent.
34
What is the process for removing an internal node with two children?
Swap it with its successor in the tree.
35
How do we find the successor of a node in a binary search tree?
It is the minimum node in the right-hand subtree.
36
What is a critical advantage when finding a node's successor?
The successor will have at most one right-hand child.
37
What does the pseudocode in Listing 5-1 illustrate?
The removal of three types of nodes from a binary search tree.
38
What must we check before removing a node from a binary search tree?
Whether the tree is empty and if the node is valid (not null).
39
In the case of removing a leaf node, what does the code check first?
If the node to be deleted has a parent node.
40
True or False: When removing a node with two children, we can simply delete the node.
False.
41
Fill in the blank: To remove a node with two children, we swap it with its _______.
successor.
42
What is the result of removing a leaf node in terms of the parent node's structure?
The parent node may become a leaf.
43
What programming structure can simplify the management of a binary search tree's root node?
A thin wrapper data structure.
44
What function is used to remove a node from a binary search tree?
RemoveTreeNode(tree, FindTreeNode(tree, target))
45
What does FindTreeNode return if the node is not found?
null
46
In case A of node removal, what happens when removing a leaf node?
Change the correct child pointer of the removed node’s parent to null
47
What should be checked first when removing a leaf node?
Whether the node to be deleted has a parent node
48
What modification is made if the removed node is the root?
Modify the root node pointer to null
49
In case B, what is the first step when removing a node with a single child?
Identify which of the node’s two child pointers links to the child
50
What does the code do after identifying the child node in case B?
Fixes the pointers between the newly promoted node and its new parent
51
How does the code handle the case of a node with two children?
Identifies the successor node and removes that from the tree
52
What is the worst-case runtime of deletion in a binary search tree?
Proportional to the depth of the tree
53
What is a perfectly balanced binary search tree?
A tree where the right subtree contains the same number of nodes as the left subtree
54
What is the worst-case performance of operations in a balanced tree?
Proportional to log2(N)
55
What happens to a binary search tree when it becomes highly unbalanced?
Its depth could grow linearly with the number of elements
56
What is a potential result of inserting values in sorted order into a binary search tree?
Creation of a sorted linked list
57
What can be the time complexity of operations on an unbalanced tree with N nodes?
Time linearly proportional to N
58
Name three types of trees that can help keep binary search trees balanced.
* Red-black trees * 2-3 trees * B-trees
59
What is the bulk construction method for creating balanced binary search trees?
Recursively dividing elements into smaller subsets
60
How do we select the node value during bulk construction from a sorted array?
Choose the middle value to be the node at that level
61
What do we use to track the current range of the array during bulk construction?
Indices of the highest and lowest values
62
What happens when only a single value is left in the range during construction?
Create a new leaf node with that value and no children
63
Why are binary search trees important in data structures?
They maintain ordering information and facilitate efficient searches
64
What concept will be introduced in the next chapter that extends binary search tree concepts?
The trie