SlideShare a Scribd company logo
For the code below complete the preOrder() method so that it performs a preOrder traversal of
the tree. For each node it traverses, it should call sb.append() to add a "[" the results of
traversing the subtree rooted at that node, and then a "]". You should add a space before the
results of the left and right child traversals, but only if the node exists.
code:
Solution
In preorder Nodes visited are in the order of
code:
package edu.buffalo.cse116;
import java.util.AbstractSet;
import java.util.Iterator;
public class BinarySearchTree> extends AbstractSet {
protected Entry root;
protected int size;
/**
* Initializes this BinarySearchTree object to be empty, to contain only
* elements of type E, to be ordered by the Comparable interface, and to
* contain no duplicate elements.
*/
public BinarySearchTree() {
root = null;
size = 0;
}
/**
* Initializes this BinarySearchTree object to contain a shallow copy of a
* specified BinarySearchTree object. The worstTime(n) is O(n), where n is the
* number of elements in the specified BinarySearchTree object.
*
* @param otherTree - the specified BinarySearchTree object that this
* BinarySearchTree object will be assigned a shallow copy of.
*/
public BinarySearchTree(BinarySearchTree otherTree) {
root = copy(otherTree.root, null);
size = otherTree.size;
}
/* Method to complete is here! */
public void preOrder(Entry ent, StringBuilder sb) {
preorder(root)
protected Entry copy(Entry p, Entry parent) {
if (p != null) {
Entry q = new Entry(p.element, parent);
q.left = copy(p.left, q);
q.right = copy(p.right, q);
return q;
}
return null;
} // method copy
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object obj) {
if (!(obj instanceof BinarySearchTree)) {
return false;
}
return equals(root, ((BinarySearchTree) obj).root);
}
public boolean equals(Entry p, Entry q) {
if ((p == null) || (q == null)) {
return p == q;
}
if (!p.element.equals(q.element)) {
return false;
}
if (equals(p.left, q.left) && equals(p.right, q.right)) {
return true;
}
return false;
}
/**
* Returns the size of this BinarySearchTree object.
*
* @return the size of this BinarySearchTree object.
*/
@Override
public int size() {
return size;
}
/**
* Iterator method will be implemented for a future
*
* @return an iterator positioned at the smallest element in this
* BinarySearchTree object.
*/
@Override
public Iterator iterator() {
throw new UnsupportedOperationException("Not implemented yet!");
}
/**
* Determines if there is at least one element in this BinarySearchTree object
* that equals a specified element. The worstTime(n) is O(n) and
* averageTime(n) is O(log n).
*
* @param obj - the element sought in this BinarySearchTree object.
* @return true - if there is an element in this BinarySearchTree object that
* equals obj; otherwise, return false.
* @throws ClassCastException - if obj cannot be compared to the elements in
* this BinarySearchTree object.
* @throws NullPointerException - if obj is null.
*/
@Override
public boolean contains(Object obj) {
return getEntry(obj) != null;
}
/**
* Ensures that this BinarySearchTree object contains a specified element. The
* worstTime(n) is O(n) and averageTime(n) is O(log n).
*
* @param element - the element whose presence is ensured in this
* BinarySearchTree object.
* @return true - if this BinarySearchTree object changed as a result of this
* method call (that is, if element was actually inserted); otherwise,
* return false.
* @throws ClassCastException - if element cannot be compared to the elements
* already in this BinarySearchTree object.
* @throws NullPointerException - if element is null.
*/
@Override
public boolean add(E element) {
if (root == null) {
if (element == null) {
throw new NullPointerException();
}
root = new Entry(element, null);
size++ ;
return true;
}
else {
Entry temp = root;
int comp;
while (true) {
comp = element.compareTo(temp.element);
if (comp == 0) {
return false;
}
if (comp < 0) {
if (temp.left != null) {
temp = temp.left;
} else {
temp.left = new Entry(element, temp);
size++ ;
return true;
} // temp.left == null
} else if (temp.right != null) {
temp = temp.right;
} else {
temp.right = new Entry(element, temp);
size++ ;
return true;
} // temp.right == null
} // while
} // root not null
} // method add
/**
* Ensures that this BinarySearchTree object does not contain a specified
* element. The worstTime(n) is O(n) and averageTime(n) is O(log n).
*
* @param obj - the object whose absence is ensured in this BinarySearchTree
* object.
* @return true - if this BinarySearchTree object changed as a result of this
* method call (that is, if obj was actually removed); otherwise,
* return false.
* @throws ClassCastException - if obj cannot be compared to the elements
* already in this BinarySearchTree object.
* @throws NullPointerException - if obj is null.
*/
@Override
public boolean remove(Object obj) {
Entry e = getEntry(obj);
if (e == null) {
return false;
}
deleteEntry(e);
return true;
}
/**
* Finds the Entry object that houses a specified element, if there is such an
* Entry. The worstTime(n) is O(n), and averageTime(n) is O(log n).
*
* @param obj - the element whose Entry is sought.
* @return the Entry object that houses obj - if there is such an Entry;
* otherwise, return null.
* @throws ClassCastException - if obj is not comparable to the elements
* already in this BinarySearchTree object.
* @throws NullPointerException - if obj is null.
*/
@SuppressWarnings("unchecked")
protected Entry getEntry(Object obj) {
int comp;
if (obj == null) {
throw new NullPointerException();
}
if (!(obj instanceof Comparable)) {
return null;
}
Comparable compObj = (Comparable) obj;
Entry e = root;
while (e != null) {
comp = compObj.compareTo(e.element);
if (comp == 0) {
return e;
} else if (comp < 0) {
e = e.left;
} else {
e = e.right;
}
} // while
return null;
}
/**
* Deletes the element in a specified Entry object from this BinarySearchTree.
*
* @param p - the Entry object whose element is to be deleted from this
* BinarySearchTree object.
* @return the Entry object that was actually deleted from this
* BinarySearchTree object.
*/
protected Entry deleteEntry(Entry p) {
size-- ;
// If p has two children, replace p's element with p's successor's
// element, then make p reference that successor.
if ((p.left != null) && (p.right != null)) {
Entry s = successor(p);
p.element = s.element;
p = s;
} // p had two children
// At this point, p has either no children or one child.
Entry replacement;
if (p.left != null) {
replacement = p.left;
} else {
replacement = p.right;
}
// If p has at least one child, link replacement to p.parent.
if (replacement != null) {
replacement.parent = p.parent;
if (p.parent == null) {
root = replacement;
} else if (p == p.parent.left) {
p.parent.left = replacement;
} else {
p.parent.right = replacement;
}
} // p has at least one child
else if (p.parent == null) {
root = null;
} else {
if (p == p.parent.left) {
p.parent.left = null;
} else {
p.parent.right = null;
}
} // p has a parent but no children
return p;
} // method deleteEntry
/**
* Finds the successor of a specified Entry object in this BinarySearchTree.
* The worstTime(n) is O(n) and averageTime(n) is constant.
*
* @param e - the Entry object whose successor is to be found.
* @return the successor of e, if e has a successor; otherwise, return null.
*/
protected Entry successor(Entry e) {
if (e == null) {
return null;
} else if (e.right != null) {
// successor is leftmost Entry in right subtree of e
Entry p = e.right;
while (p.left != null) {
p = p.left;
}
return p;
} // e has a right child
else {
// go up the tree to the left as far as possible, then go up
// to the right.
Entry p = e.parent;
Entry ch = e;
while ((p != null) && (ch == p.right)) {
ch = p;
p = p.parent;
} // while
return p;
} // e has no right child
} // method successor
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
preOrder(root, sb);
return sb.toString();
}
protected static class Entry {
protected E element;
protected Entry left = null, right = null, parent;
/**
* Initializes this Entry object. This default constructor is defined for
* the sake of subclasses of the BinarySearchTree class.
*/
public Entry() {}
/**
* Initializes this Entry object from element and parent.
*/
public Entry(E element, Entry parent) {
this.element = element;
this.parent = parent;
} // constructor
} // class Entry
} // class BinarySearchTree

More Related Content

Similar to For the code below complete the preOrder() method so that it perform.pdf

Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
Stewart29UReesa
 
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docxJAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
GavinUJtMathist
 
4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf
mumnesh
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
ravikapoorindia
 
Given below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdfGiven below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
info430661
 
Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docx
cgraciela1
 
Web Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for PerformanceWeb Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for Performance
johndaviddalton
 
I need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfI need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdf
sauravmanwanicp
 
Can you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdfCan you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdf
FashionBoutiquedelhi
 
Please review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdfPlease review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdf
fathimafancyjeweller
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docx
debishakespeare
 
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
curwenmichaela
 
Hi, Please find my code.I have correted all of your classes.Plea.pdf
Hi, Please find my code.I have correted all of your classes.Plea.pdfHi, Please find my code.I have correted all of your classes.Plea.pdf
Hi, Please find my code.I have correted all of your classes.Plea.pdf
pritikulkarni20
 
In this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdfIn this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdf
charanjit1717
 
usingpackage util;import java.util.;This class implements.pdf
usingpackage util;import java.util.;This class implements.pdfusingpackage util;import java.util.;This class implements.pdf
usingpackage util;import java.util.;This class implements.pdf
info335653
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
footstatus
 
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdfAssignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Footageetoffe16
 
On the code which has a class in which I implementing a binary tree .pdf
On the code which has a class in which I implementing a binary tree .pdfOn the code which has a class in which I implementing a binary tree .pdf
On the code which has a class in which I implementing a binary tree .pdf
wasemanivytreenrco51
 
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
aptind
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
Stewart29UReesa
 

Similar to For the code below complete the preOrder() method so that it perform.pdf (20)

Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
 
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docxJAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
 
4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
 
Given below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdfGiven below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
 
Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docx
 
Web Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for PerformanceWeb Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for Performance
 
I need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfI need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdf
 
Can you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdfCan you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdf
 
Please review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdfPlease review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdf
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docx
 
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
 
Hi, Please find my code.I have correted all of your classes.Plea.pdf
Hi, Please find my code.I have correted all of your classes.Plea.pdfHi, Please find my code.I have correted all of your classes.Plea.pdf
Hi, Please find my code.I have correted all of your classes.Plea.pdf
 
In this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdfIn this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdf
 
usingpackage util;import java.util.;This class implements.pdf
usingpackage util;import java.util.;This class implements.pdfusingpackage util;import java.util.;This class implements.pdf
usingpackage util;import java.util.;This class implements.pdf
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
 
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdfAssignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
 
On the code which has a class in which I implementing a binary tree .pdf
On the code which has a class in which I implementing a binary tree .pdfOn the code which has a class in which I implementing a binary tree .pdf
On the code which has a class in which I implementing a binary tree .pdf
 
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
 

More from xlynettalampleyxc

According to the IRS, 1.1 of tax returns will be audited in 2011. A.pdf
According to the IRS, 1.1 of tax returns will be audited in 2011. A.pdfAccording to the IRS, 1.1 of tax returns will be audited in 2011. A.pdf
According to the IRS, 1.1 of tax returns will be audited in 2011. A.pdf
xlynettalampleyxc
 
Write a SELECT statement that returns a single value that represents.pdf
Write a SELECT statement that returns a single value that represents.pdfWrite a SELECT statement that returns a single value that represents.pdf
Write a SELECT statement that returns a single value that represents.pdf
xlynettalampleyxc
 
which represents the ground state for the N- ion i. w.ia ere sm.pdf
which represents the ground state for the N- ion  i. w.ia ere sm.pdfwhich represents the ground state for the N- ion  i. w.ia ere sm.pdf
which represents the ground state for the N- ion i. w.ia ere sm.pdf
xlynettalampleyxc
 
what is the prediction equation for the median income and incarcerat.pdf
what is the prediction equation for the median income and incarcerat.pdfwhat is the prediction equation for the median income and incarcerat.pdf
what is the prediction equation for the median income and incarcerat.pdf
xlynettalampleyxc
 
What is the ecological and environmental importance of salinityoxyg.pdf
What is the ecological and environmental importance of salinityoxyg.pdfWhat is the ecological and environmental importance of salinityoxyg.pdf
What is the ecological and environmental importance of salinityoxyg.pdf
xlynettalampleyxc
 
WEP has vulnerabilities. Which of the following is not a reason why .pdf
WEP has vulnerabilities. Which of the following is not a reason why .pdfWEP has vulnerabilities. Which of the following is not a reason why .pdf
WEP has vulnerabilities. Which of the following is not a reason why .pdf
xlynettalampleyxc
 
Two astronauts are 2.40 m apart in their spaceship. One speaks to the.pdf
Two astronauts are 2.40 m apart in their spaceship. One speaks to the.pdfTwo astronauts are 2.40 m apart in their spaceship. One speaks to the.pdf
Two astronauts are 2.40 m apart in their spaceship. One speaks to the.pdf
xlynettalampleyxc
 
The ratio of the probability of disease in an exposed group to the p.pdf
The ratio of the probability of disease in an exposed group to the p.pdfThe ratio of the probability of disease in an exposed group to the p.pdf
The ratio of the probability of disease in an exposed group to the p.pdf
xlynettalampleyxc
 
The output should now look like this Row 1 sum 30 Row 2 sum.pdf
The output should now look like this Row 1 sum 30 Row 2 sum.pdfThe output should now look like this Row 1 sum 30 Row 2 sum.pdf
The output should now look like this Row 1 sum 30 Row 2 sum.pdf
xlynettalampleyxc
 
The end problem in eukaryotic DNA replicationa. is solved by t.pdf
The end problem in eukaryotic DNA replicationa. is solved by t.pdfThe end problem in eukaryotic DNA replicationa. is solved by t.pdf
The end problem in eukaryotic DNA replicationa. is solved by t.pdf
xlynettalampleyxc
 
Suppose that one obtained the following DNA sequence data for the fou.pdf
Suppose that one obtained the following DNA sequence data for the fou.pdfSuppose that one obtained the following DNA sequence data for the fou.pdf
Suppose that one obtained the following DNA sequence data for the fou.pdf
xlynettalampleyxc
 
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdfSet up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
xlynettalampleyxc
 
Sample of DNA isolated from bacterium X contains 17.5 of adenine.pdf
Sample of DNA isolated from bacterium X contains 17.5 of adenine.pdfSample of DNA isolated from bacterium X contains 17.5 of adenine.pdf
Sample of DNA isolated from bacterium X contains 17.5 of adenine.pdf
xlynettalampleyxc
 
Practice using layouts Anderson, Franceschi import javax..pdf
Practice using layouts Anderson, Franceschi import javax..pdfPractice using layouts Anderson, Franceschi import javax..pdf
Practice using layouts Anderson, Franceschi import javax..pdf
xlynettalampleyxc
 
Please, I need a correct answer and clear explanation. open image an.pdf
Please, I need a correct answer and clear explanation. open image an.pdfPlease, I need a correct answer and clear explanation. open image an.pdf
Please, I need a correct answer and clear explanation. open image an.pdf
xlynettalampleyxc
 
Patient 2 Patient #2 Jan Johnson Next, Dr. Gupta sees Jan Johnson. .pdf
Patient 2 Patient #2 Jan Johnson Next, Dr. Gupta sees Jan Johnson. .pdfPatient 2 Patient #2 Jan Johnson Next, Dr. Gupta sees Jan Johnson. .pdf
Patient 2 Patient #2 Jan Johnson Next, Dr. Gupta sees Jan Johnson. .pdf
xlynettalampleyxc
 
Non-math and physics question, but engineering orientated.Identify.pdf
Non-math and physics question, but engineering orientated.Identify.pdfNon-math and physics question, but engineering orientated.Identify.pdf
Non-math and physics question, but engineering orientated.Identify.pdf
xlynettalampleyxc
 
Name three properties of the waveform that can be changed with the m.pdf
Name three properties of the waveform that can be changed with the m.pdfName three properties of the waveform that can be changed with the m.pdf
Name three properties of the waveform that can be changed with the m.pdf
xlynettalampleyxc
 
Inventory Valuation FIFO, LIFO, and Average The company reported the.pdf
Inventory Valuation FIFO, LIFO, and Average The company reported the.pdfInventory Valuation FIFO, LIFO, and Average The company reported the.pdf
Inventory Valuation FIFO, LIFO, and Average The company reported the.pdf
xlynettalampleyxc
 
In December 2009, a 45-year-old female presented to the emergency dep.pdf
In December 2009, a 45-year-old female presented to the emergency dep.pdfIn December 2009, a 45-year-old female presented to the emergency dep.pdf
In December 2009, a 45-year-old female presented to the emergency dep.pdf
xlynettalampleyxc
 

More from xlynettalampleyxc (20)

According to the IRS, 1.1 of tax returns will be audited in 2011. A.pdf
According to the IRS, 1.1 of tax returns will be audited in 2011. A.pdfAccording to the IRS, 1.1 of tax returns will be audited in 2011. A.pdf
According to the IRS, 1.1 of tax returns will be audited in 2011. A.pdf
 
Write a SELECT statement that returns a single value that represents.pdf
Write a SELECT statement that returns a single value that represents.pdfWrite a SELECT statement that returns a single value that represents.pdf
Write a SELECT statement that returns a single value that represents.pdf
 
which represents the ground state for the N- ion i. w.ia ere sm.pdf
which represents the ground state for the N- ion  i. w.ia ere sm.pdfwhich represents the ground state for the N- ion  i. w.ia ere sm.pdf
which represents the ground state for the N- ion i. w.ia ere sm.pdf
 
what is the prediction equation for the median income and incarcerat.pdf
what is the prediction equation for the median income and incarcerat.pdfwhat is the prediction equation for the median income and incarcerat.pdf
what is the prediction equation for the median income and incarcerat.pdf
 
What is the ecological and environmental importance of salinityoxyg.pdf
What is the ecological and environmental importance of salinityoxyg.pdfWhat is the ecological and environmental importance of salinityoxyg.pdf
What is the ecological and environmental importance of salinityoxyg.pdf
 
WEP has vulnerabilities. Which of the following is not a reason why .pdf
WEP has vulnerabilities. Which of the following is not a reason why .pdfWEP has vulnerabilities. Which of the following is not a reason why .pdf
WEP has vulnerabilities. Which of the following is not a reason why .pdf
 
Two astronauts are 2.40 m apart in their spaceship. One speaks to the.pdf
Two astronauts are 2.40 m apart in their spaceship. One speaks to the.pdfTwo astronauts are 2.40 m apart in their spaceship. One speaks to the.pdf
Two astronauts are 2.40 m apart in their spaceship. One speaks to the.pdf
 
The ratio of the probability of disease in an exposed group to the p.pdf
The ratio of the probability of disease in an exposed group to the p.pdfThe ratio of the probability of disease in an exposed group to the p.pdf
The ratio of the probability of disease in an exposed group to the p.pdf
 
The output should now look like this Row 1 sum 30 Row 2 sum.pdf
The output should now look like this Row 1 sum 30 Row 2 sum.pdfThe output should now look like this Row 1 sum 30 Row 2 sum.pdf
The output should now look like this Row 1 sum 30 Row 2 sum.pdf
 
The end problem in eukaryotic DNA replicationa. is solved by t.pdf
The end problem in eukaryotic DNA replicationa. is solved by t.pdfThe end problem in eukaryotic DNA replicationa. is solved by t.pdf
The end problem in eukaryotic DNA replicationa. is solved by t.pdf
 
Suppose that one obtained the following DNA sequence data for the fou.pdf
Suppose that one obtained the following DNA sequence data for the fou.pdfSuppose that one obtained the following DNA sequence data for the fou.pdf
Suppose that one obtained the following DNA sequence data for the fou.pdf
 
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdfSet up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
 
Sample of DNA isolated from bacterium X contains 17.5 of adenine.pdf
Sample of DNA isolated from bacterium X contains 17.5 of adenine.pdfSample of DNA isolated from bacterium X contains 17.5 of adenine.pdf
Sample of DNA isolated from bacterium X contains 17.5 of adenine.pdf
 
Practice using layouts Anderson, Franceschi import javax..pdf
Practice using layouts Anderson, Franceschi import javax..pdfPractice using layouts Anderson, Franceschi import javax..pdf
Practice using layouts Anderson, Franceschi import javax..pdf
 
Please, I need a correct answer and clear explanation. open image an.pdf
Please, I need a correct answer and clear explanation. open image an.pdfPlease, I need a correct answer and clear explanation. open image an.pdf
Please, I need a correct answer and clear explanation. open image an.pdf
 
Patient 2 Patient #2 Jan Johnson Next, Dr. Gupta sees Jan Johnson. .pdf
Patient 2 Patient #2 Jan Johnson Next, Dr. Gupta sees Jan Johnson. .pdfPatient 2 Patient #2 Jan Johnson Next, Dr. Gupta sees Jan Johnson. .pdf
Patient 2 Patient #2 Jan Johnson Next, Dr. Gupta sees Jan Johnson. .pdf
 
Non-math and physics question, but engineering orientated.Identify.pdf
Non-math and physics question, but engineering orientated.Identify.pdfNon-math and physics question, but engineering orientated.Identify.pdf
Non-math and physics question, but engineering orientated.Identify.pdf
 
Name three properties of the waveform that can be changed with the m.pdf
Name three properties of the waveform that can be changed with the m.pdfName three properties of the waveform that can be changed with the m.pdf
Name three properties of the waveform that can be changed with the m.pdf
 
Inventory Valuation FIFO, LIFO, and Average The company reported the.pdf
Inventory Valuation FIFO, LIFO, and Average The company reported the.pdfInventory Valuation FIFO, LIFO, and Average The company reported the.pdf
Inventory Valuation FIFO, LIFO, and Average The company reported the.pdf
 
In December 2009, a 45-year-old female presented to the emergency dep.pdf
In December 2009, a 45-year-old female presented to the emergency dep.pdfIn December 2009, a 45-year-old female presented to the emergency dep.pdf
In December 2009, a 45-year-old female presented to the emergency dep.pdf
 

Recently uploaded

678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 

Recently uploaded (20)

678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 

For the code below complete the preOrder() method so that it perform.pdf

  • 1. For the code below complete the preOrder() method so that it performs a preOrder traversal of the tree. For each node it traverses, it should call sb.append() to add a "[" the results of traversing the subtree rooted at that node, and then a "]". You should add a space before the results of the left and right child traversals, but only if the node exists. code: Solution In preorder Nodes visited are in the order of code: package edu.buffalo.cse116; import java.util.AbstractSet; import java.util.Iterator; public class BinarySearchTree> extends AbstractSet { protected Entry root; protected int size; /** * Initializes this BinarySearchTree object to be empty, to contain only * elements of type E, to be ordered by the Comparable interface, and to * contain no duplicate elements. */ public BinarySearchTree() { root = null; size = 0; } /** * Initializes this BinarySearchTree object to contain a shallow copy of a * specified BinarySearchTree object. The worstTime(n) is O(n), where n is the * number of elements in the specified BinarySearchTree object. * * @param otherTree - the specified BinarySearchTree object that this * BinarySearchTree object will be assigned a shallow copy of. */ public BinarySearchTree(BinarySearchTree otherTree) { root = copy(otherTree.root, null);
  • 2. size = otherTree.size; } /* Method to complete is here! */ public void preOrder(Entry ent, StringBuilder sb) { preorder(root) protected Entry copy(Entry p, Entry parent) { if (p != null) { Entry q = new Entry(p.element, parent); q.left = copy(p.left, q); q.right = copy(p.right, q); return q; } return null; } // method copy @SuppressWarnings("unchecked") @Override public boolean equals(Object obj) { if (!(obj instanceof BinarySearchTree)) { return false; } return equals(root, ((BinarySearchTree) obj).root); } public boolean equals(Entry p, Entry q) { if ((p == null) || (q == null)) { return p == q; } if (!p.element.equals(q.element)) { return false; } if (equals(p.left, q.left) && equals(p.right, q.right)) { return true; } return false; } /** * Returns the size of this BinarySearchTree object.
  • 3. * * @return the size of this BinarySearchTree object. */ @Override public int size() { return size; } /** * Iterator method will be implemented for a future * * @return an iterator positioned at the smallest element in this * BinarySearchTree object. */ @Override public Iterator iterator() { throw new UnsupportedOperationException("Not implemented yet!"); } /** * Determines if there is at least one element in this BinarySearchTree object * that equals a specified element. The worstTime(n) is O(n) and * averageTime(n) is O(log n). * * @param obj - the element sought in this BinarySearchTree object. * @return true - if there is an element in this BinarySearchTree object that * equals obj; otherwise, return false. * @throws ClassCastException - if obj cannot be compared to the elements in * this BinarySearchTree object. * @throws NullPointerException - if obj is null. */ @Override public boolean contains(Object obj) { return getEntry(obj) != null; } /** * Ensures that this BinarySearchTree object contains a specified element. The * worstTime(n) is O(n) and averageTime(n) is O(log n).
  • 4. * * @param element - the element whose presence is ensured in this * BinarySearchTree object. * @return true - if this BinarySearchTree object changed as a result of this * method call (that is, if element was actually inserted); otherwise, * return false. * @throws ClassCastException - if element cannot be compared to the elements * already in this BinarySearchTree object. * @throws NullPointerException - if element is null. */ @Override public boolean add(E element) { if (root == null) { if (element == null) { throw new NullPointerException(); } root = new Entry(element, null); size++ ; return true; } else { Entry temp = root; int comp; while (true) { comp = element.compareTo(temp.element); if (comp == 0) { return false; } if (comp < 0) { if (temp.left != null) { temp = temp.left; } else { temp.left = new Entry(element, temp); size++ ; return true; } // temp.left == null
  • 5. } else if (temp.right != null) { temp = temp.right; } else { temp.right = new Entry(element, temp); size++ ; return true; } // temp.right == null } // while } // root not null } // method add /** * Ensures that this BinarySearchTree object does not contain a specified * element. The worstTime(n) is O(n) and averageTime(n) is O(log n). * * @param obj - the object whose absence is ensured in this BinarySearchTree * object. * @return true - if this BinarySearchTree object changed as a result of this * method call (that is, if obj was actually removed); otherwise, * return false. * @throws ClassCastException - if obj cannot be compared to the elements * already in this BinarySearchTree object. * @throws NullPointerException - if obj is null. */ @Override public boolean remove(Object obj) { Entry e = getEntry(obj); if (e == null) { return false; } deleteEntry(e); return true; } /** * Finds the Entry object that houses a specified element, if there is such an * Entry. The worstTime(n) is O(n), and averageTime(n) is O(log n). *
  • 6. * @param obj - the element whose Entry is sought. * @return the Entry object that houses obj - if there is such an Entry; * otherwise, return null. * @throws ClassCastException - if obj is not comparable to the elements * already in this BinarySearchTree object. * @throws NullPointerException - if obj is null. */ @SuppressWarnings("unchecked") protected Entry getEntry(Object obj) { int comp; if (obj == null) { throw new NullPointerException(); } if (!(obj instanceof Comparable)) { return null; } Comparable compObj = (Comparable) obj; Entry e = root; while (e != null) { comp = compObj.compareTo(e.element); if (comp == 0) { return e; } else if (comp < 0) { e = e.left; } else { e = e.right; } } // while return null; } /** * Deletes the element in a specified Entry object from this BinarySearchTree. * * @param p - the Entry object whose element is to be deleted from this * BinarySearchTree object. * @return the Entry object that was actually deleted from this
  • 7. * BinarySearchTree object. */ protected Entry deleteEntry(Entry p) { size-- ; // If p has two children, replace p's element with p's successor's // element, then make p reference that successor. if ((p.left != null) && (p.right != null)) { Entry s = successor(p); p.element = s.element; p = s; } // p had two children // At this point, p has either no children or one child. Entry replacement; if (p.left != null) { replacement = p.left; } else { replacement = p.right; } // If p has at least one child, link replacement to p.parent. if (replacement != null) { replacement.parent = p.parent; if (p.parent == null) { root = replacement; } else if (p == p.parent.left) { p.parent.left = replacement; } else { p.parent.right = replacement; } } // p has at least one child else if (p.parent == null) { root = null; } else { if (p == p.parent.left) { p.parent.left = null; } else { p.parent.right = null;
  • 8. } } // p has a parent but no children return p; } // method deleteEntry /** * Finds the successor of a specified Entry object in this BinarySearchTree. * The worstTime(n) is O(n) and averageTime(n) is constant. * * @param e - the Entry object whose successor is to be found. * @return the successor of e, if e has a successor; otherwise, return null. */ protected Entry successor(Entry e) { if (e == null) { return null; } else if (e.right != null) { // successor is leftmost Entry in right subtree of e Entry p = e.right; while (p.left != null) { p = p.left; } return p; } // e has a right child else { // go up the tree to the left as far as possible, then go up // to the right. Entry p = e.parent; Entry ch = e; while ((p != null) && (ch == p.right)) { ch = p; p = p.parent; } // while return p; } // e has no right child } // method successor @Override public String toString() {
  • 9. StringBuilder sb = new StringBuilder(); preOrder(root, sb); return sb.toString(); } protected static class Entry { protected E element; protected Entry left = null, right = null, parent; /** * Initializes this Entry object. This default constructor is defined for * the sake of subclasses of the BinarySearchTree class. */ public Entry() {} /** * Initializes this Entry object from element and parent. */ public Entry(E element, Entry parent) { this.element = element; this.parent = parent; } // constructor } // class Entry } // class BinarySearchTree