If the item to be deleted is contained in a node with one


(Binary Tree Delete) In this exercise, we discuss deleting items from binary search trees. The deletion algorithm is not as straightforward as the insertion algorithm. There are three cases that are encountered when deleting an item-the item is contained in a leaf node (i.e., it has no children), the item is contained in a node that has one child or the item is contained in a node that has two children.

If the item to be deleted is contained in a leaf node, the node is deleted and the reference in the parent node is set to null.

If the item to be deleted is contained in a node with one child, the reference in the parent node is set to reference the child node and the node containing the data item is deleted. This causes the child node to take the place of the deleted node in the tree.

The last case is the most difficult. When a node with two children is deleted, another node in the tree must take its place. However, the reference in the parent node cannot simply be assigned to reference one of the children of the node to be deleted. In most cases, the resulting binary search tree would not adhere to the following characteristic of binary search trees (with no duplicate values): The values in any left subtree are less than the value in the parent node, and the values in any right subtree are greater than the value in the parent node.

Which node is used as a replacement node to maintain this characteristic? Either the node containing the largest value in the tree less than the value in the node being deleted, or the node containing the smallest value in the tree greater than the value in the node being deleted. Let us consider the node with the smaller value. In a binary search tree, the largest value less than a parent's value is located in the left subtree of the parent node and is guaranteed to be contained in the rightmost node of the subtree.

This node is located by walking down the left subtree to the right until the reference to the right child of the current node is null. We are now referencing the replacement node which is either a leaf node or a node with one child to its left. If the replacement node is a leaf node, the steps to perform the deletion are as follows:

a. Store the reference to the node to be deleted in a temporary reference variable.

b. Set the reference in the parent of the node being deleted to reference the replacement node.

c. Set the reference in the parent of the replacement node to null.

d. Set the reference to the right subtree in the replacement node to reference the right subtree of the node to be deleted.

e. Set the reference to the left subtree in the replacement node to reference the left subtree of the node to be deleted.

The deletion steps for a replacement node with a left child are similar to those for a replacement node with no children, but the algorithm also must move the child into the replacement node's position in the tree. If the replacement node is a node with a left child, the steps to perform the deletion are as follows:

a. Store the reference to the node to be deleted in a temporary reference variable.

b. Set the reference in the parent of the node being deleted to reference the replacement node.

c. Set the reference in the parent of the replacement node reference to the left child of the replacement node.

d. Set the reference to the right subtree in the replacement node reference to the right subtree of the node to be deleted.

e. Set the reference to the left subtree in the replacement node to reference the left subtree of the node to be deleted.

Write method deleteNode, which takes as its argument the value to be deleted. Method deleteNode should locate in the tree the node containing the value to be deleted and use the algorithms discussed here to delete the node. If the value is not found in the tree, the method should print a message that indicates whether or not the value is deleted. Modify the program of

Fig. 17.17 and Fig. 17.18 (Tree.java and TreeTest.java) to use this method. After deleting an item, call the methods inorderTraversal, preorderTraversal and postorderTraversal to confirm that the delete operation was performed correctly.

Attachment:- Tree.zip

Solution Preview :

Prepared by a verified Expert
Data Structure & Algorithms: If the item to be deleted is contained in a node with one
Reference No:- TGS01255400

Now Priced at $20 (50% Discount)

Recommended (98%)

Rated (4.3/5)