A binary search tree (BST) is a [[trees#Binary Trees|binary tree]] satisfying the **binary-search-tree property**:
> Let $x$ be a node in a binary search tree. If $y$ is a node in the left subtree of $x$, then $y.key \leq x.key$. If $y$ is a node in the right subtree of $x$, then $y.key \geq x.key$.
In terms of the height $h$ of a BST, what is the runtime of search?
Back: $O(h)$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303021-->
END%%
%%ANKI
Basic
In terms of the height $h$ of a BST, what is the runtime for finding the minimum?
Back: $O(h)$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303023-->
END%%
%%ANKI
Basic
In terms of the height $h$ of a BST, what is the runtime for finding the maximum?
Back: $O(h)$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303025-->
END%%
### Successors
The **successor** of a node in a binary search tree is the node whose value would appear immediately after in an in-order traversal.
%%ANKI
Basic
How do we define the successor of a BST node in terms of in-order traversals?
Back: As the node encountered immediately after in an in-order traversal.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722709623599-->
END%%
%%ANKI
Basic
Which node is the successor of $7$ in the following BST?
![[binary-search-tree.png]]
Back: $8$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722709623601-->
END%%
%%ANKI
Basic
Which node is the successor of $2$ in the following BST?
![[binary-search-tree.png]]
Back: $5$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722709623603-->
END%%
%%ANKI
Basic
Which node is the successor of $5$ in the following BST?
![[binary-search-tree.png]]
Back: Either $5$ or $6$.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722709623604-->
END%%
%%ANKI
Basic
Which node(s) in a BST have no successor?
Back: The maximum.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722709623606-->
END%%
%%ANKI
Basic
What two cases are considered when finding the successor of a BST node?
Back: If the node has a right subtree or not.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722709623607-->
END%%
%%ANKI
Basic
Suppose a BST node has a right subtree. What is its successor?
Back: The minimum of the right subtree.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722709623608-->
END%%
%%ANKI
Basic
Suppose a BST node does not have a right subtree. What is its successor?
Back: The first proper ancestor reached from the LHS.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722709623610-->
END%%
%%ANKI
Cloze
If a BST node has a right subtree, it's successor cannot have a {left} subtree.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303026-->
END%%
%%ANKI
Basic
Suppose a BST node has a right subtree. *Why* can't its successor have a left subtree?
Back: Because then a node in that left subtree would be the actual successor.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303028-->
END%%
%%ANKI
Basic
Suppose a BST node has a right subtree. *Why* can't its successor have a right subtree?
Back: N/A. It can.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303029-->
END%%
%%ANKI
Basic
In terms of the height $h$ of a BST, what is the runtime for finding a node's successor?
Back: $O(h)$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303030-->
END%%
### Predecessors
The **predecessor** of a node in a binary search tree is the node whose value would appear immediately before in an in-order traversal.
%%ANKI
Basic
How do we define the predecessor of a BST node in terms of in-order traversals?
Back: As the node encountered immediately before in an in-order traversal.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722709623611-->
END%%
%%ANKI
Basic
Which node is the predecessor of $7$ in the following BST?
![[binary-search-tree.png]]
Back: $6$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722709623613-->
END%%
%%ANKI
Basic
Which node is the predecessor of $2$ in the following BST?
![[binary-search-tree.png]]
Back: N/A.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722709623614-->
END%%
%%ANKI
Basic
Which node is the predecessor of $5$ in the following BST?
![[binary-search-tree.png]]
Back: Either $2$ or $5$.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722709623615-->
END%%
%%ANKI
Basic
Which node(s) in a BST have no predecessor?
Back: The minimum.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722709623616-->
END%%
%%ANKI
Basic
What two cases are considered when finding the predecessor of a BST node?
Back: If the node has a left subtree or not.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722709623617-->
END%%
%%ANKI
Basic
Suppose a BST node has a left subtree. What is its predecessor?
Back: The maximum of the left subtree.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722709623618-->
END%%
%%ANKI
Basic
Suppose a BST node does not have a left subtree. What is its predecessor?
Back: The first proper ancestor reached from the RHS.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722709623619-->
END%%
%%ANKI
Cloze
If a BST node has a left subtree, it's predecessor cannot have a {right} subtree.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303031-->
END%%
%%ANKI
Basic
Suppose a BST node has a left subtree. *Why* can't its successor have a left subtree?
Back: N/A. It can.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303033-->
END%%
%%ANKI
Basic
Suppose a BST node has a left subtree. *Why* can't its predecessor have a right subtree?
Back: Because then a node in that right subtree would be the actual predecessor.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303034-->
END%%
%%ANKI
Basic
In terms of the height $h$ of a BST, what is the runtime for finding a node's predecessor?
Back: $O(h)$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303035-->
END%%
## Deletions
Consider deleting node $z$ from a BST. There are three conceptual cases to consider corresponding to the number of children $z$ has:
* If $z$ has no children we can just replace $z$ with `NIL`.
* If $z$ has one child, we can replace $z$ with its child.
* If $z$ has two children, we swap $z$ with either its predecessor or successor, updating pointers as necessary to maintain the binary-search-tree property.
%%ANKI
Basic
Insert node $7.5$ into the following BST. Where is the new node located?
![[binary-search-tree.png]]
Back: As $8$'s left child.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303036-->
END%%
%%ANKI
Basic
Insert node $6.5$ into the following BST. Where is the new node located?
![[binary-search-tree.png]]
Back: As $7$'s left child.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303037-->
END%%
%%ANKI
Basic
Insert node $5.5$ into the following BST. Where is the new node located?
![[binary-search-tree.png]]
Back: As the lower $5$'s right child.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303038-->
END%%
%%ANKI
Basic
When does insertion into a BST modify the root node?
Back: When the tree being inserted into is empty.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303039-->
END%%
%%ANKI
Basic
In terms of the height $h$ of a BST, what is the runtime for inserting a node?
Back: $O(h)$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303040-->
END%%
%%ANKI
Basic
How many cases are there to consider when deleting a node from a BST?
Back: Three.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303041-->
END%%
%%ANKI
Basic
What corresponds to the cases to consider when deleting a node from a BST?
Back: The number of children the node in question has.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303042-->
END%%
%%ANKI
Basic
Delete BST node $z$ with no children. Which node is $z$ replaced with?
Back: `NIL`
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303043-->
END%%
%%ANKI
Basic
Delete BST node $z$ with one child. Which node is $z$ replaced with?
Back: Its one child.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303044-->
END%%
%%ANKI
Basic
Delete BST node $z$ with two children. Which node is $z$ replaced with?
Back: Either its successor or predecessor.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303045-->
END%%
%%ANKI
Basic
Delete BST node $z$ with two children. If replacing with its successor, what two subcases need to be considered?
Back: If $z$'s successor is its right child or not.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303046-->
END%%
%%ANKI
Basic
Delete BST node $z$ with two children. If replacing with its predecessor, what two subcases need to be considered?
Back: If $z$'s predessor is its left child or not.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303047-->
END%%
%%ANKI
Basic
In terms of the height $h$ of a BST, what is the runtime for deleting a node?
Back: $O(h)$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303048-->
END%%
%%ANKI
Basic
Delete $z$ from the following BST. What does the resulting tree look like?
![[bst-right-child.png]]
Back:
![[bst-right-child-after.png]]
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303049-->
END%%
%%ANKI
Basic
Delete $z$ from the following BST. What does the resulting tree look like?
![[bst-left-child.png]]
Back:
![[bst-left-child-after.png]]
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303050-->
END%%
%%ANKI
Basic
Delete $z$ from the following BST. What does the resulting tree look like?
![[bst-right-succ.png]]
Back:
![[bst-right-succ-after.png]]
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303051-->
END%%
%%ANKI
Basic
Delete $z$ from the following BST. What does the resulting tree look like?
![[bst-deep-succ.png]]
Back:
![[bst-deep-succ-after.png]]
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1722713303052-->
END%%
%%ANKI
Basic
What arbitrary choice was implied in the deletion algorithm of the following BST?
![[bst-deep-succ.png]]
Back: To replace deleted nodes with their successor instead of predecessor.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).