How to answer the following question screenshot shows our binary search tree For the binary search trees, the public method, size, calls the private recursive method, recSize, and passes it a reference to the root of the tree, returning to the caller whatever is returned to it by recSize. Critique the following implementation of recSize assuming its parameter variable is node: if (node == null)    return 0; else if ((node.getLeft() == null) && (node.getRight() == null))    return 1; else if ((node.getLeft() == null))    return (1 + recSize(node.getRight())); else    return (1 + recSize(node.getLeft())); A.  The code will throw a "null pointer exception" under certain circumstances. B.  The code will return an incorrect result under certain circumstances. C. The code works correctly but could be simpler. D. The code works correctly.   If an application uses the remove method on an Iterator object returned by our binary search tree: A. a TreeUnderflowException is thrown. B.  an UnsupportedOperationException is thrown. C.  the application will terminate. D.  the most recently visited node of the tree will be r

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

How to answer the following question screenshot shows our binary search tree

For the binary search trees, the public method, size, calls the private recursive method, recSize, and passes it a reference to the root of the tree, returning to the caller whatever is returned to it by recSize. Critique the following implementation of recSize assuming its parameter variable is node:

if (node == null)

   return 0;

else

if ((node.getLeft() == null) && (node.getRight() == null))

   return 1;

else

if ((node.getLeft() == null))

   return (1 + recSize(node.getRight()));

else

   return (1 + recSize(node.getLeft()));

A.  The code will throw a "null pointer exception" under certain circumstances.
B.  The code will return an incorrect result under certain circumstances.
C. The code works correctly but could be simpler.
D. The code works correctly.
 

If an application uses the remove method on an Iterator object returned by our binary search tree:

A. a TreeUnderflowException is thrown.
B.  an UnsupportedOperationException is thrown.
C.  the application will terminate.
D.  the most recently visited node of the tree will be removed.
//Page1
import jayautil.*; // Iterator, Comparator
public class BinaryRarchTcee<T> implements BSTpterfacesT>
{
protected BSTNodę<T> rogtin // reference to the root of this BST
protected Comparator<T> gompin/ used for all comparisons
//page2
BSTNede<T> node = root;
while (nodesetRightl) != null)
node = nede getRiebt();
return node getlnfel);
//page3
private T resGet(T target, BSTNedesT> node)
// Returns info i from the subtree rooted at node such that
// comBSAVORAKeltarget, i) == 0; if no such info exists, returns null.
{
if (node == null)
return nyllin
else if (comBSORare(target, node setlotel) < 0)
return resGet(target, nede-setleftl));
protected boolean found/ used by remove
private int recSizeBSTNedesT> node)
// Returns the number of elements in subtree rooted at node.
// target is not found
public BipatxSearchtreel)
// Precondition: Timplements Comparable
// Creates an empty BST object - uses the natural order of elements.
{
root = null;
comp = new Comparator<T)
{
public int comparekT element1, T element2)
{
return ((Comparable)element1),comparetelelement2);
{
// get from left subtree
if (node == nullI)
return 0;
else
if (CAMDSOVORAKA(target, odesetlofel)) > 0)
return resfet(target, podegetRiebt();
else
else
// get from right subtree
return 1+ ressizeloode.getleft() + tesSizeloode.getBight());
}
return pode getlnfo(); // target is found
}
public int sized
// Returns the number of elements in this BST.
{
return recSize(root);
}
public T get(T target)
// Returns info i from node of this BST where comp.coRare(target, i == 0;
// if no such node exists, returns null.
{
};
return recGet(target, root);
}
public int size2()
// Returns the number of elements in this BST.
{
int count = 0;
if (roote null)
{
LiokedStack<BSTNode<T>> nodeStack = new LipkedStack<BSTNode<T>>);
BSTNode<T> GUTNode:
Dodestack Rusb(root);
while (!podeStackisEmotx()
{
EUTNode= vedestacktop);
vedestask pop);
count++;
if (gyrrNode.getlet(Q4= null)
odestaskRusblouNodegetlet();
if (gurONode BRtBigbt()A= null)
oodestackpusbleurTNode.getBight();
public BinatxSearctree(Comparator<T> comp)
// Creates an empty BST object - uses Comparator comp for order
// of elements.
{
root = null;
this cema = comp;
}
private BSTNedesT> recaddU element, BSTNede<T> node)
// Adds element to tree rooted at node; tree retains its BST property.
{
if (node == null)
// Addition place found
node = new BSTNode<T>(element);
else if (comasoraKe(element, nodesetlotel)) <= 0)
DodesetlefticesAddlelement, pode getleft()); // Add in left subtree
public booleanisEUUO
// Returns false: this link-based BST
s never full.
{
return false;
}
else
vodesetRiebtlcesAdd(element, nodegetRiebt()); // Add in right subtree
return node;
}
public booleaoisEMrty)
// Returns true if this BST is empty; otherwise, returns false.
{
return (root == null);
}
public booleao add (T element)
// Adds element to this BST. The tree retains its BST property.
{
root = (RCAddlelement, root);
}
return true;
}
public T minl)
// If this BST is empty, returns null;
// otherwise returns the smallest element of the tree.
return count;
}
/*
public booleao add (T element)
// Adds element to this BST. The tree retains its BST property.
{
private booleanrecCootaios(T target, BSTNodesT> node)
// Returns true if the subtree rooted at node contains info i such that
// CAMBSOVORare(target, i) == 0; otherwise, returns false.
{
if (node == null)
return falsen // target is not found
else if (comeSoaRara(target, nodesetlotel)) < 0)
return resContains(target, pode getleft(); // Search left subtree
else if (comeSomRare(target, node setlotel) > 0)
return resContains(target, node getRight()); // Search right subtree
if (işEmptv)
return null;
else
BSTNodesT> newNode= new BSTNede<T>(element);
BSTNedesT> RKEK= null, cu = null;
{
BSTNedesT> node = root;
while (node getletl) != null)
node = nede seteft();
return node getlofel);
if (root == null)
root = newdode:
else
{
GUK = root;
while (gyTJ= null)
}
else
return tryen // target is found
}
public T max()
// If this BST is empty, returns null;
// otherwise returns the largest element of the tree.
{
if (iŞEmpty)
return null;
if (comB.SoMparalelement, çyrr,getlnfo()) <= 0)
{
public boolean.contains (T target)
// Returns true if this BST contains a node with info i such that
// comBsoORAe(target, i) == 0; otherwise, returns false.
{
return resContains(target, root);
}
DAX = CU
EUTK= CUEstleft();
}
else
else
{
{
Transcribed Image Text://Page1 import jayautil.*; // Iterator, Comparator public class BinaryRarchTcee<T> implements BSTpterfacesT> { protected BSTNodę<T> rogtin // reference to the root of this BST protected Comparator<T> gompin/ used for all comparisons //page2 BSTNede<T> node = root; while (nodesetRightl) != null) node = nede getRiebt(); return node getlnfel); //page3 private T resGet(T target, BSTNedesT> node) // Returns info i from the subtree rooted at node such that // comBSAVORAKeltarget, i) == 0; if no such info exists, returns null. { if (node == null) return nyllin else if (comBSORare(target, node setlotel) < 0) return resGet(target, nede-setleftl)); protected boolean found/ used by remove private int recSizeBSTNedesT> node) // Returns the number of elements in subtree rooted at node. // target is not found public BipatxSearchtreel) // Precondition: Timplements Comparable // Creates an empty BST object - uses the natural order of elements. { root = null; comp = new Comparator<T) { public int comparekT element1, T element2) { return ((Comparable)element1),comparetelelement2); { // get from left subtree if (node == nullI) return 0; else if (CAMDSOVORAKA(target, odesetlofel)) > 0) return resfet(target, podegetRiebt(); else else // get from right subtree return 1+ ressizeloode.getleft() + tesSizeloode.getBight()); } return pode getlnfo(); // target is found } public int sized // Returns the number of elements in this BST. { return recSize(root); } public T get(T target) // Returns info i from node of this BST where comp.coRare(target, i == 0; // if no such node exists, returns null. { }; return recGet(target, root); } public int size2() // Returns the number of elements in this BST. { int count = 0; if (roote null) { LiokedStack<BSTNode<T>> nodeStack = new LipkedStack<BSTNode<T>>); BSTNode<T> GUTNode: Dodestack Rusb(root); while (!podeStackisEmotx() { EUTNode= vedestacktop); vedestask pop); count++; if (gyrrNode.getlet(Q4= null) odestaskRusblouNodegetlet(); if (gurONode BRtBigbt()A= null) oodestackpusbleurTNode.getBight(); public BinatxSearctree(Comparator<T> comp) // Creates an empty BST object - uses Comparator comp for order // of elements. { root = null; this cema = comp; } private BSTNedesT> recaddU element, BSTNede<T> node) // Adds element to tree rooted at node; tree retains its BST property. { if (node == null) // Addition place found node = new BSTNode<T>(element); else if (comasoraKe(element, nodesetlotel)) <= 0) DodesetlefticesAddlelement, pode getleft()); // Add in left subtree public booleanisEUUO // Returns false: this link-based BST s never full. { return false; } else vodesetRiebtlcesAdd(element, nodegetRiebt()); // Add in right subtree return node; } public booleaoisEMrty) // Returns true if this BST is empty; otherwise, returns false. { return (root == null); } public booleao add (T element) // Adds element to this BST. The tree retains its BST property. { root = (RCAddlelement, root); } return true; } public T minl) // If this BST is empty, returns null; // otherwise returns the smallest element of the tree. return count; } /* public booleao add (T element) // Adds element to this BST. The tree retains its BST property. { private booleanrecCootaios(T target, BSTNodesT> node) // Returns true if the subtree rooted at node contains info i such that // CAMBSOVORare(target, i) == 0; otherwise, returns false. { if (node == null) return falsen // target is not found else if (comeSoaRara(target, nodesetlotel)) < 0) return resContains(target, pode getleft(); // Search left subtree else if (comeSomRare(target, node setlotel) > 0) return resContains(target, node getRight()); // Search right subtree if (işEmptv) return null; else BSTNodesT> newNode= new BSTNede<T>(element); BSTNedesT> RKEK= null, cu = null; { BSTNedesT> node = root; while (node getletl) != null) node = nede seteft(); return node getlofel); if (root == null) root = newdode: else { GUK = root; while (gyTJ= null) } else return tryen // target is found } public T max() // If this BST is empty, returns null; // otherwise returns the largest element of the tree. { if (iŞEmpty) return null; if (comB.SoMparalelement, çyrr,getlnfo()) <= 0) { public boolean.contains (T target) // Returns true if this BST contains a node with info i such that // comBsoORAe(target, i) == 0; otherwise, returns false. { return resContains(target, root); } DAX = CU EUTK= CUEstleft(); } else else { {
//page4
//page6|
//page5
public Iterator<T> getiterator(BSTloterfaceTraversal exderTvre)
// Creates and returns an Iterator providing a traversal of a "snapshot"
// of the current tree in the order indicated by the argument.
// Supports Preorder, Pestorder, and pordettraversal.
{
final LipkedQueue<T> infoQueue= new LinkedQueue<T);
if (erderTvre == BSToterfaceTraversalLRreorder)
preerdertroot, infeQueve);
else
prAK = CU
GUK= CUEStBight();
}
}
if (comP.CADDarelelement, preygetuata() <= 0)
pKAKSRtLetlvewNode);
private void postordedBSTNode<T> node, LiokedQueue<T> q)
// Enqueues the elements from the subtree rooted at node into q in postorder,
if (nodel null)
{
9ostorderloode getleft(), q);
9ostorderloede setRiebt(), q);
GenaVRuelvedeeetlofe();
}
else
prAKSRtLetlvewdode);
}
return true;
if (exderTure == BSTIoterface-TcaversalhJoerder)
inArdertroot, infoQuevel;
else
}
public Iterator<T> iterator)
// norder is the default, "natural" order.
if (orderTure== BSToterfaceTraversal Rosterderd
9ostorderdroot, inteQueue);
private T getPredecessed(BSTNede<T> subtree)
// Returns the information held in the rightmost node of subtree
{
BSTNedesT> temp = subtree;
while (teroB getBight() != null)
temp = tempgetBiebt();
return terpgetlatel);
}
return getiterator(ESToterfaceTraversalloerder);
}
return new Iterator<T)
{
public boolean.basNext)
// Returns true if the iteration has more elements; otherwise returns false.
{
cetuadinfoQuevejsEmotul);
private BSTNodesT> removeNedelBSTANede<T> node)
// Removes the information at node from the tree.
{
T data;
if (vode setleft) == null)
return pode getBight();
else if (nedesetRiebt() == null)
return node getleft();
public T next)
// Returns the next element in the iteration.
// Throws NoSycbElementExcention - if the iteration has no more elements
{
if (UbasNext())
throw new ndexQutOfBoundsExcention("illegal invocation of next "+
else
'in BinarySearcbTeR iterator.\n");
return infoQueuesdeaURuel);
}
{
data = getRredeceSSoulvede-getleftl));
Dodesetloteldata);
oodesetleftltecRemeveldata, pode getleft());
return node;
public void remove)
// Throws UpsuppetedOrerationException.
// Not supported. Removal from snapshot iteration is meaningless.
}
{
throw new UpsuRroctedOperatienExsention("Unsupported remove attempted on "
private BSTNodesT> recBemexe(T target, BSTNodesT> node)
// Removes element with info i from tree rooted at node such that
+ "BinanSearsbTceR iterator.\n");
}
};
}
// CROBSOVORAKe(target, i == 0 and returns true;
// if no such node exists, returns false.
{
if (node == null)
found = false;
private void preQrder(BSTNode<T> node, LipkedQueue<T> q)
// Enqueues the elements from the subtree rooted at node into q in preorder,
{
if (vedel null)
{
GenaUeuelvodesetlofel);
prerderlvede getleft(), q);
preederlvede.getRigbt(), q);
}
}
else if (compSomRare(target, node setlotel) < 0)
DodesetleftlterRemeveltarget, pode getleft());
else if (comp.compare(target, nodegetlofel) > 0)
vodesetRiebtlcesRemeve(target, node gstBight());
else
{
node = removeNode(node);
found = true;
}
return node;
}
private void inOrder(BSTNedesT> node, linkedQueue<T> q)
// Enqueues the elements from the subtree rooted at node into q in inorder,
{
if (nodel null)
{
inOrderloode geteft(), q);
gAnavevelvedegetlofel);
inorderloodegetRiebt(), g);
public booleaoremove (T target)
// Removes a node with info i from tree such that compOparaltargeti == 0
// and returns true; if no such node exists, returns false.
{
root = recBemeveltarget, root);
return found;
}
Transcribed Image Text://page4 //page6| //page5 public Iterator<T> getiterator(BSTloterfaceTraversal exderTvre) // Creates and returns an Iterator providing a traversal of a "snapshot" // of the current tree in the order indicated by the argument. // Supports Preorder, Pestorder, and pordettraversal. { final LipkedQueue<T> infoQueue= new LinkedQueue<T); if (erderTvre == BSToterfaceTraversalLRreorder) preerdertroot, infeQueve); else prAK = CU GUK= CUEStBight(); } } if (comP.CADDarelelement, preygetuata() <= 0) pKAKSRtLetlvewNode); private void postordedBSTNode<T> node, LiokedQueue<T> q) // Enqueues the elements from the subtree rooted at node into q in postorder, if (nodel null) { 9ostorderloode getleft(), q); 9ostorderloede setRiebt(), q); GenaVRuelvedeeetlofe(); } else prAKSRtLetlvewdode); } return true; if (exderTure == BSTIoterface-TcaversalhJoerder) inArdertroot, infoQuevel; else } public Iterator<T> iterator) // norder is the default, "natural" order. if (orderTure== BSToterfaceTraversal Rosterderd 9ostorderdroot, inteQueue); private T getPredecessed(BSTNede<T> subtree) // Returns the information held in the rightmost node of subtree { BSTNedesT> temp = subtree; while (teroB getBight() != null) temp = tempgetBiebt(); return terpgetlatel); } return getiterator(ESToterfaceTraversalloerder); } return new Iterator<T) { public boolean.basNext) // Returns true if the iteration has more elements; otherwise returns false. { cetuadinfoQuevejsEmotul); private BSTNodesT> removeNedelBSTANede<T> node) // Removes the information at node from the tree. { T data; if (vode setleft) == null) return pode getBight(); else if (nedesetRiebt() == null) return node getleft(); public T next) // Returns the next element in the iteration. // Throws NoSycbElementExcention - if the iteration has no more elements { if (UbasNext()) throw new ndexQutOfBoundsExcention("illegal invocation of next "+ else 'in BinarySearcbTeR iterator.\n"); return infoQueuesdeaURuel); } { data = getRredeceSSoulvede-getleftl)); Dodesetloteldata); oodesetleftltecRemeveldata, pode getleft()); return node; public void remove) // Throws UpsuppetedOrerationException. // Not supported. Removal from snapshot iteration is meaningless. } { throw new UpsuRroctedOperatienExsention("Unsupported remove attempted on " private BSTNodesT> recBemexe(T target, BSTNodesT> node) // Removes element with info i from tree rooted at node such that + "BinanSearsbTceR iterator.\n"); } }; } // CROBSOVORAKe(target, i == 0 and returns true; // if no such node exists, returns false. { if (node == null) found = false; private void preQrder(BSTNode<T> node, LipkedQueue<T> q) // Enqueues the elements from the subtree rooted at node into q in preorder, { if (vedel null) { GenaUeuelvodesetlofel); prerderlvede getleft(), q); preederlvede.getRigbt(), q); } } else if (compSomRare(target, node setlotel) < 0) DodesetleftlterRemeveltarget, pode getleft()); else if (comp.compare(target, nodegetlofel) > 0) vodesetRiebtlcesRemeve(target, node gstBight()); else { node = removeNode(node); found = true; } return node; } private void inOrder(BSTNedesT> node, linkedQueue<T> q) // Enqueues the elements from the subtree rooted at node into q in inorder, { if (nodel null) { inOrderloode geteft(), q); gAnavevelvedegetlofel); inorderloodegetRiebt(), g); public booleaoremove (T target) // Removes a node with info i from tree such that compOparaltargeti == 0 // and returns true; if no such node exists, returns false. { root = recBemeveltarget, root); return found; }
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Mergesort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education