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....
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...
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*...
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:...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
Java Program: You will be inserting values into a generic tree, then printing the values inorder,...
Java Program: You will be inserting values into a generic tree, then printing the values inorder, as well as printing the minimum and maximum values in the tree. Given main(), write the methods in the 'BSTree' class specified by the // TODO: sections. There are 5 TODOs in all to complete. Ex: If the input is like ferment bought tasty can making apples super improving juice wine -1 the output should be: Enter the words on separate lines to insert...
This is in java and you are not allowed to use Java API classes for queues,...
This is in java and you are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own implementations for them. You should construct a BST by inserting node values starting with a null tree. You can re-use the code for the insert method given in the sample code from the textbook. -insert method is provided below Your code should have a menu driven user interface at the command line with...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT