Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Let's start with leaf nodes. When we want to delete a leaf node, what can we do?
We can just remove it, right?
Doesn't it just fall off the tree?
Exactly! A leaf node can simply be deleted since it has no children. Remember, we just ensure that the parent node updates its link to NULL, removing the connection.
So, if I had a tree and I deleted a leaf, the tree structure remains valid?
Yes, the search tree remains intact because we didn’t disturb any of the other nodes. The search properties are still preserved.
To recap this part, leaf node deletion is simple: if it's a leaf, you delete it and adjust the parent's pointer.
Now, what do we do if the node we want to delete has one child?
We promote the child? Like, we just connect the child to the parent instead?
Correct! If a node has only one child, we bypass the node and link the child directly to the node's parent.
Can you give an example, please?
Certainly! If we delete node 74, which has a child 91, we set 52 to point directly to 91 instead of 74.
Remember the mnemonic 'Promote Child' for this scenario. It's straightforward: just promote the one child!
Lastly, what about nodes that have two children? How do we handle those during deletion?
I think we can't just delete it. We need to replace it with something, right?
Yes! We can use the predecessor or successor!
Absolutely! We replace the node with its predecessor or successor to maintain the tree's structure and order—preserving our binary search property.
What happens after we replace it? Do we still need to delete the predecessor?
Correct! After replacing, we need to delete the predecessor, which will be simpler because a predecessor is either a leaf or has one child.
To summarize today's lesson, we talked about deleting leaf nodes, nodes with one child, and two children, focusing on how to maintain the tree's integrity.
Example: Deleting Leaf Node - Given a tree, delete node 28 which is a leaf. The parent node points its left child to NULL.
Example: Deleting Node with One Child - Suppose we delete node 74, which has a child node 91. Node 52 now directly points to 91.
Example: Deleting Node with Two Children - To delete node 37, replace it with its predecessor 28, then delete 28.
To delete a leaf, give it a heave, let it drop and leave.
Imagine a gardener trimming trees. When he finds a leaf, he simply cuts it off and lets it sway away, keeping the tree intact.
Remember 'LCR': Leaf - Cut; Child - Replace; Two kids - Predecessor.
DELETE - Decide, Evaluate, Locate, Execute, Trim, Ensure.
Term
What is meant by a predecessor in a binary search tree?
Click to flip
Term
How do we delete a node with one child?
Click to flip
A node with no children.
The largest node in the left subtree of a node to be deleted.
The smallest node in the right subtree of a node to be deleted.
A tree structure where each node has at most two children, and left children are less than the parent while right children are greater.
This section focuses on the process of deleting nodes from a search tree, discussing various cases that arise during deletion.
The section details the methods for node deletion in a search tree, considering various scenarios: identifying leaf nodes, nodes with a single child, and nodes with two children. It elaborates on the procedures for each case while ensuring the tree maintains its structure and order.
The deletion of a node from a search tree can be performed with several strategies depending on the node's characteristics. This section delves into these methods, ensuring the properties of the binary search tree are preserved. We will address three primary scenarios:
The implications of these operations on the overall tree structure are critical, particularly regarding the height of the tree and the time complexity of the operations.