Question

do a code trace on how a key gets deleted package interview; import java.util.*; public class...

do a code trace on how a key gets deleted

package interview;

import java.util.*;

public class binarySearchTree {

Node root;

void insert(int key) {

root = insertRec(root,key);

}

void delete(int key) {

root = deleteRec(root,key);

}

Node insertRec(Node root, int key) {

if(root == null) {

root = new Node(key);

return root;

}

if(key < root.key) {

root.leftChild = insertRec(root.leftChild,key);

}

else if(key > root.key){

root.rightChild = insertRec(root.rightChild,key);

}

return root;

}

Node deleteRec(Node root, int key) {

if(root == null) {

return root;

}

if(key < root.key) {

root.leftChild = deleteRec(root.leftChild,key);

}

else if(key > root.key) {

root.rightChild = deleteRec(root.rightChild,key);

}

else {

if(root.leftChild == null) {

return root.rightChild;

}

else if(root.rightChild == null) {

return root.leftChild;

}

root.key = minValue(root.rightChild);

root.rightChild = deleteRec(root.rightChild, root.key);

}

return root;

}

int minValue(Node root){

int minV = root.key;

while(root.leftChild != null) {

minV = root.leftChild.key;

root = root.leftChild;

}

return minV;

}

void inorder() {

inorderRec(root);

}

void inorderRec(Node root) {

if(root != null) {

inorderRec(root.leftChild);

System.out.println(root.key);

inorderRec(root.rightChild);

}

}

public static void main(String[] args) {

binarySearchTree tree1 = new binarySearchTree();

tree1.insert(43);

tree1.insert(22);

tree1.insert(39);

tree1.insert(17);

tree1.insert(10);

tree1.insert(28);

System.out.println("In order traversal of the binary search tree: ");

tree1.inorder();

System.out.println("\n");

System.out.println("Delete 39 from the tree!");

System.out.println("In order traversal after deletion of the tree: ");

tree1.delete(22);

tree1.inorder();

}

class Node{

int key;

Node rightChild;

Node leftChild;

Node(int key){

this.key = key;

rightChild = null;

leftChild = null;

}

}

}

Homework Answers

Answer #1

Basically this a binary search tree. The function responsible for deletion are delete and deleteRec

///////////////////////////////////

void delete(int key) {

root = deleteRec(root,key);

}

The above first is just calling deleteRec with root and node value to delete

//////////////////////////////////////////////

Node deleteRec(Node root, int key) {

if(root == null) {

return root;

}

if(key < root.key) {

root.leftChild = deleteRec(root.leftChild,key);

}

else if(key > root.key) {

root.rightChild = deleteRec(root.rightChild,key);

}

else {

if(root.leftChild == null) {

return root.rightChild;

}

else if(root.rightChild == null) {

return root.leftChild;

}

root.key = minValue(root.rightChild);

root.rightChild = deleteRec(root.rightChild, root.key);

}

return root;

}

To delete 39 was just printed in the statement System.out.println("Delete 39 from the tree!");

but actually delete is called for 22

tree1.delete(22);

Here is how the deleteRec is taking steps to delete

BST after insertion:

43

22   

17 39

10    28

A empty box represents now children in that place

so delete(22) calls

deleteRec(43,22)

if(root == null) False

if(key < root.key) True

{

root.leftChild = deleteRec(root.leftChild,key);

}

so, deleteRec(22,22) will be called

if(root == null) False

if(key < root.key) False

else if(key > root.key) False

else { True

if(root.leftChild == null) False

else if(root.rightChild == null) False

root.key = minValue(root.rightChild);   

right child of 22 is 39

so minValue(39)

so minvalue returns 28

root.key = 28 //chaning the value of root from 22 to 28

root.rightChild = deleteRec(root.rightChild, root.key); ---------------------- point reference 1

}

Tree becomes

43

28   

17 39

10    28

so deleteRec(root.rightChild, root.key);

so deleteRec(28, 28);

if(root == null) False

if(key < root.key) False

else if(key > root.key) False

else {

if(root.leftChild == null) True {

return root.rightChild;

}

Null will be returned here to the call deleteRec(28, 28);

so,   point reference 1

root.rightChild = Null

So Tree becomes

43

28   

17 39

10    Null

// The new tree is

43

28   

17 39

10      

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Here is a modification of the BST program that includes a recursive find method: BinarySearchTree2C.java (posted...
Here is a modification of the BST program that includes a recursive find method: BinarySearchTree2C.java (posted below) Implement the following methods using recursion: int depth() // returns the length of the deepest path from root to any leaf int node_count() // returns the number of nodes in the tree void insert(int n) // inserts value n into the tree BinarySearchTree2C clone() // returns a clone (deep copy) of the tree Add code to the main method to test these methods....
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
You will be traversing through an integer tree to print the data. Given main(), write the...
You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 - 85 -...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the in-order traversal as the default.             This means, in Tester the following code should work for an instance of this class             called bst that is storing Student objects for the data:                 BinarySearchTree_Lab08<String> bst = new BinarySearchTree_Lab08<String>();                 bst.add("Man");       bst.add("Soda");   bst.add("Flag");                 bst.add("Home");   bst.add("Today");   bst.add("Jack");                ...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class is empty. Complete the destructor so the memory allocated for each node in the BST is freed. Make a couple of different trees in your main method or in a function to test the destructor (the program should not crash upon exiting). bst.zip (includes the following files below in c++): bst.h: #pragma once #include #include "node.cpp" using namespace std; template class BST { public:...
what output is produced by the following code and explain how it works. public class A...
what output is produced by the following code and explain how it works. public class A { int a = 1; int b = 2; public int getSum(int a, int b) {     this.a+=a;     this.b+=b;     return this.a + this.b; } } public class B extends A { int a = 3; int b = 4; public int getSum(int a, int b) {     this.b=a;     super.b=b+b;     return super.a+this.b; } } public class q2 { public static void...
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...