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
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private...
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private boolean isBalancedByNodeCount() { /**************************************************************************** Implement this method and replace the return statement below with your code. * Definition of Balance tree (On page 372 of book): * An unbalanced tree is created when most of the nodes are on one side of the root or the other. ****************************************************************************/    return false; }       public static void main(String args[]) { int[] values1 = {50,...
Copy the file SimplerBST.java from the week 1 examples package and rename the file and class...
Copy the file SimplerBST.java from the week 1 examples package and rename the file and class A3BST. Add two public methods: one named countTwins that takes no parameters and that, when called, traverses every node of the tree to count the number of nodes having two children. one named lessThanValueCount that takes a paremeter of generic type V, and that, when called, traverses every node of the tree to count the number of nodes whose value (not its key) is...
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....
This is my current code and im having trouble inserting the findMin, findMax, and Search functions...
This is my current code and im having trouble inserting the findMin, findMax, and Search functions in main to output them. #include<iostream> using namespace std; /*Define the class for BST*/ class BST_TREE {    /*Structure of the tree*/    struct tree    {        int value;        tree* left;        tree* right;    };    tree* root;    /*Method to clear the tree*/    tree* Empty(tree* t)    {        if (t == NULL)       ...
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 -...
Java Program: You will be traversing through an integer tree to print the data. Given main(),...
Java Program: 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 -...
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...
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
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");                ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT