Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

(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

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M91616962
  • Price:- $20

Priced at Now at $20, Verified Solution

Have any Question?


Related Questions in Computer Engineering

Risk management software is a critical application that

Risk management software is a critical application that businesses use to gain insight on how risk drivers can impact a project and business. For this research assignment, you will do an internet search for "risk managem ...

One of the 5 halogens which are diatomic has the fastest

One of the 5 halogens (which are diatomic) has the fastest root mean square velocity at STP. Calculate this velocity in meters per second.

Question some help me to answer this1 discuss the osi

Question: Some help me to answer this 1. Discuss the OSI reference model and TCP/IP model. discuss in details their features, advantages and disadvantages 2. Compare and contrast the two models. The response must be type ...

If the annualized five-year rate of return is 15 and if the

If the annualized five-year rate of return is 15%, and if the first year's rate of return is 20%, and if the returns in all other years are equal, what are they? Provide the answer in % with four decimal places without r ...

Service set identifier ssid also known as the wireless

Service Set Identifier (SSID), also known as the wireless network name, identifies the wireless network. An SSID is configured on the wireless AP (on the access point for the infrastructure mode) or on an initial wireles ...

Item 4 is the tester program for implementing the amusement

Item 4 is the tester program for implementing the Amusement Park. I am looking for an Amusement Park Tester program that is in Java. None of the answers included the tester program! I need to: Design a tester class calle ...

Question suppose user process application p1 of one

Question : Suppose user process (application) P1 of one computer wishes to transfer data (file) to process (application) P2 on another computer in the Internet. What addressing information about P2 is necessary for P1 to ...

A string in c is simply an array of characters with the

A string in C++ is simply an array of characters with the null character(\0) used to mark the end of the string. C++ provides a set of string handling function in as well as I/O functions in . With the addition of the ST ...

Taskwrite a cc program to emulate a simple calculator that

Task Write a C/C++ program to emulate a simple calculator that can calculate and display the results of performing one of the following arithmetic operations between two integer numbers (+, -, *, /). The Calculator progr ...

Question suppose you are designing a database for a library

Question : Suppose you are designing a database for a library, the library system contains information about people who borrow. Each person is uniquely identified. People can search and borrow books. A book has book ID. ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As