Docsity

Deleting Nodes from a Binary Tree: Cases and Auxiliary Functions - Prof. Arup Guha, Study notes of Computer Science

Toggle sidebar
bg1
Deletion from a Binary Tree
Last time we outlined how to delete a node from a binary tree.
We broke up our work into three cases:
1) Deleting a leaf node
2) Deleting a node with one child
3) Deleting a node with two children.
Keep these three in mind, as well as how we determined the
delete would work in these cases when looking at this code.
For a leaf node, our job isn't too hard. The key will be
identifying the parent of the node to delete. (Actually, we'll do
this in the other cases as well.) Once we do this, then we just
have to set the appropriate node to NULL:
parert->left = NULL or
parent->right = NULL
For the second task, we'll need to find the parent node of the
node to be deleted, along with the sole child node of the node to
be deleted and "connect them." Here are the four possibilities:
10 10
/ \ / \
5 15 ====> 1 15 (delete 5)
/
1
pf3
pf4

Partial preview of the text

Download Deleting Nodes from a Binary Tree: Cases and Auxiliary Functions - Prof. Arup Guha and more Study notes Computer Science in PDF only on Docsity!

Deletion from a Binary Tree

Last time we outlined how to delete a node from a binary tree. We broke up our work into three cases:

  1. Deleting a leaf node
  2. Deleting a node with one child
  3. Deleting a node with two children. Keep these three in mind, as well as how we determined the delete would work in these cases when looking at this code. For a leaf node, our job isn't too hard. The key will be identifying the parent of the node to delete. (Actually, we'll do this in the other cases as well.) Once we do this, then we just have to set the appropriate node to NULL: parert->left = NULL or parent->right = NULL For the second task, we'll need to find the parent node of the node to be deleted, along with the sole child node of the node to be deleted and "connect them." Here are the four possibilities: 10 10 / \ /
    5 15 ====> 1 15 (delete 5) / 1

/ \ / \

5 15 ====> 7 15 (delete 7)
7 10 10 / \ /
5 15 ====> 7 12 (delete 12) / 12 10 10 / \ /
5 15 ====> 7 15 (delete 23)
23 In each of these two main cases, I am still glossing over a couple details:

  1. What happens if the node to delete IS the root node of the tree and has no parent?
  2. How do we free memory for the deleted node? We'll deal with both of these issues when we see the code. Finally, we have to deal with the case where a node to be deleted has two children. The nice thing here is that we will not physically do the delete of the chosen node. If we did, deciding what gets to be the root node, and what takes its place etc. could be unusually complicated.

/ \

/ \ / \

Some Auxiliary Functions

In aiding the delete function, I have written several auxiliary functions:

  1. parent - finds the parent of a given node in a given binary tree.
  2. minVal - finds the minimum value in a given binary tree.
  3. maxVal - finds the maximum value in a given binary tree.
  4. isLeaf - determines if a node is a leaf node or not.
  5. hasOnlyLeftChild - determines if a node ONLY has a left child or not.
  6. hasOnlyRightChild - determines if a node ONLY has a right child or not.
  7. findNode - returns a pointer to a node in a given tree that stores a particular value