Discuss deleting items from binary search trees

Assignment Help Data Structure & Algorithms
Reference no: EM13946101

(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

Reference no: EM13946101

Questions Cloud

Consider the following hypothetical scenario : A hypothetical company has grown substantially using an acquisition strategy. As new companies were acquired, new systems that those companies were using were also acquired.
What probability that customer arrive within next 0.5 minute : On average 2.4 customers per minute arrive at an airline check-in desk during the peak period. Assume that the distribution of arrivals is Poisson. Then T the time between 2 arrivals will be exponential. What is the probability that a customer will..
Surface heat flux for tube diameters : Consider air flowing in a small-diameter steel tube. Graph the Nusselt number associated with fully devel- oped laminar flow with  constant  surface  heat  flux for tube diameters ranging from 1 µ,m D 1 mm.
What are the most important aspects to monitor your servers : We can all agree that it's important to monitor your servers' performance over time. But, what are the most important aspects to monitor and how often? As a network administrator, how do you best use this information for future planning?
Discuss deleting items from binary search trees : 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..
Statement of activities for governmental activities : Prepare in good form a statement of activities for governmental activities
Direction dictates existence of equivalent conditions : A novel scheme for dissipating heat from the chips of a multichip array involves machining coolant channels in  the   ceramic  substrate  to   which   the   chips   are attached.  The  square  chips  (Lc  = 5 mm)  are  aligned above  each  of  the ..
Clearly identify the model of capitalism : Models of Capitalism and the Interdisciplinary Nature of Management Is capitalism good or evil? Does it improve and strengthen a nation, or does the opposite occur? Instead of speaking about capitalism in general terms,

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  Part-11 suppose you want to demonstrate an erd to someone

part-11. suppose you want to demonstrate an erd to someone who has never seen one. provide a scenario from everyday

  Describe ambiguity in proposed algorithm

Describe the distinction between an ambiguity in a proposed algorithm and an ambiguity in the representation of an algorithm. Describe how the use of primitives helps remove ambiguities in an algorithm's representation.

  Data structures used to organize typical file cabinet

Recognize at least two data structures which are used to organize typical file cabinet. Why do you feel it is essential to emulate these types of data structures in computer program?

  Prepare a recursive linear-time algorithm

Prepare a recursive linear-time algorithm that tests whether a binary tree satisfies the search tree order property at every node.

  Discuss infix to postfix conversion

This part will use both a stack and queue in order to convert expressions from infix to postfix notation.

  Linked lists give a program to implement the insert

give a program to implement the insert operation and delete operations on a queue using linked

  Universalist rationality theory

Universalist rationality theory supposes that actors within an institution are rational. They function with their own material interests in mind, maximizing efficiency and resources.

  Identify the dfd elements

Compare the guidelines for preparing flowcharts and DFDs. What general design principles and limitations are common to both documentation techniques?

  Write a programming for sorting

write a programming for sorting

  How many paths are there to the goal

Show your pseudo code for this algorithm. Is this an admissible heuristic function and How many possible states are there? How many paths are there to the goal?

  Server of local hospital to support remote access

Explain the file system that will be installed in the server of your local hospital to support remote access of data through the hospital's doctors while they are using their mobile equipments such as cellular phones or PDAs.

  Which of insertion sort-mergesort and quicksort are stable

A sorting algorithm is described as stable if equal elements are in the same relative order in the sorted sequence as in the original sequence.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd