Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in java 2 1 . 2 2 ( Binary Tree Delete ) In this exercise, we discuss deleting items from binary search trees. The deletion

in java 21.22(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. Three cases are encountered when deleting an itemthe item is contained in a leaf node (i.e., it has no children), or in a node that has one child or 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 embody 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? Its 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. Lets consider the node with the smaller value. In a binary search tree, the largest value less than a parents 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. Were 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:
Store the reference to the node to be deleted in a temporary reference variable.
Set the reference in the parent of the node being deleted to reference the replacement node.
Set the reference in the parent of the replacement node to null.
Set the reference to the right subtree in the replacement node to reference the right subtree of the node to be deleted.
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 nodes position in the tree. If the replacement node is a node with a left child, the steps to perform the deletion are as follows:
Store the reference to the node to be deleted in a temporary reference variable.
Set the reference in the parent of the node being deleted to refer to the replacement node.
Set the reference in the parent of the replacement node to reference the left child of the replacement node.
Set the reference to the right subtree in the replacement node to reference the right subtree of the node to be deleted.
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 delete. Method deleteNode should locate in the tree the node containing the value to delete and use the algorithms discussed here to delete the node. If the value is not found in the tree, the method should display a message saying so. Modify the program of Figs. 21.15 and 21.16 to use this method. After deleting an item, call the methods inorderTraversal, preorderTraversal and postorderTraversal to confirm that the delete operation was performed correctly.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Oracle8I Building Efficient Databases

Authors: Jonathan Lewis

1st Edition

0201715848, 978-0201715842

More Books

Students also viewed these Databases questions