SlideShare a Scribd company logo
There is BinarySearchTree class. When removing a node from a BST, we can replace it with
either its predecessor or its successor. Complete the predecessor() method
so that it returns the node that comes immediately before e. Hint: the work needed to find the
predecessor is a mirror image of what is required to find a node's
successor, so use successor() as a resource.
BinarySearchTree class:
Solution
import java.util.AbstractSet;
import java.util.Iterator;
public class BinarySearchTree> extends AbstractSet {
protected BTNode 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;
}
protected BTNode copy(BTNode p, BTNode parent) {
if (p != null) {
BTNode q = new BTNode(p.element, parent);
q.left = copy(p.left, q);
q.right = copy(p.right, q);
return q;
}
return null;
} // method copy
/**
* Finds the predecessor of a specified BTNode object in this BinarySearchTree. The
worstTime(n) is O(n) and
* averageTime(n) is constant.
*
* @param e - the BTNode object whose successor is to be found.
* @return the successor of e, if e has a successor; otherwise, return null.
*/
protected BTNode predecessor(BTNode e) {
if (e == null) {
return null;
} else if (e.left != null) {
// successor is leftmost BTNode in right subtree of e
BTNode p = e.left;
while (p.right != null) {
p = p.right;
}
return p;
} // e has a right child
else {
// go up the tree to the right as far as possible, then go up
// to the left.
BTNode p = e.parent;
BTNode ch = e;
while ((p != null) && (ch == p.left)) {
ch = p;
p = p.parent;
} // while
return p;
} // e has no left child
// method successor
}
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object obj) {
if (!(obj instanceof BinarySearchTree)) {
return false;
}
return equals(root, ((BinarySearchTree) obj).root);
} // method 1-parameter equals
public boolean equals(BTNode p, BTNode 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() {
// Skipped in preparation for next week
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 getBTNode(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 BTNode(element, null);
size++ ;
return true;
}
else {
BTNode 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 BTNode(element, temp);
size++ ;
return true;
} // temp.left == null
} else if (temp.right != null) {
temp = temp.right;
} else {
temp.right = new BTNode(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) {
BTNode e = getBTNode(obj);
if (e == null) {
return false;
}
deleteBTNode(e);
return true;
}
/**
* Finds the BTNode object that houses a specified element, if there is such an BTNode. The
worstTime(n) is O(n), and
* averageTime(n) is O(log n).
*
* @param obj - the element whose BTNode is sought.
* @return the BTNode object that houses obj - if there is such an BTNode; 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 BTNode getBTNode(Object obj) {
int comp;
if (obj == null) {
throw new NullPointerException();
}
if (!(obj instanceof Comparable)) {
return null;
}
Comparable compObj = (Comparable) obj;
BTNode 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 BTNode object from this BinarySearchTree.
*
* @param p - the BTNode object whose element is to be deleted from this BinarySearchTree
object.
* @return the BTNode object that was actually deleted from this BinarySearchTree object.
*/
protected BTNode deleteBTNode(BTNode 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)) {
BTNode s = successor(p);
p.element = s.element;
p = s;
} // p had two children
// At this point, p has either no children or one child.
BTNode 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 deleteBTNode
/**
* Finds the successor of a specified BTNode object in this BinarySearchTree. The
worstTime(n) is O(n) and
* averageTime(n) is constant.
*
* @param e - the BTNode object whose successor is to be found.
* @return the successor of e, if e has a successor; otherwise, return null.
*/
protected BTNode successor(BTNode e) {
if (e == null) {
return null;
} else if (e.right != null) {
// successor is leftmost BTNode in right subtree of e
BTNode 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.
BTNode p = e.parent;
BTNode ch = e;
while ((p != null) && (ch == p.right)) {
ch = p;
p = p.parent;
} // while
return p;
} // e has no right child
} // method successor
protected static class BTNode {
protected E element;
protected BTNode left = null, right = null, parent;
/**
* Initializes this BTNode object. This default constructor is defined for the sake of subclasses
of the
* BinarySearchTree class.
*/
public BTNode() {}
/**
* Initializes this BTNode object from element and parent.
*/
public BTNode(E element, BTNode parent) {
this.element = element;
this.parent = parent;
} // constructor
} // class BTNode
} // class BinarySearchTree

More Related Content

PDF
For the code below complete the preOrder() method so that it perform.pdf
PDF
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
PDF
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
DOCX
Required to augment the authors Binary Search Tree (BST) code to .docx
PDF
Create a class BinarySearchTree- A class that implements the ADT binar.pdf
PDF
To create a node for a binary search tree (BTNode, ) and to create a .pdf
PDF
Given the following codepackage data1;import java.util.;p.pdf
DOCX
Once you have all the structures working as intended- it is time to co.docx
For the code below complete the preOrder() method so that it perform.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
Required to augment the authors Binary Search Tree (BST) code to .docx
Create a class BinarySearchTree- A class that implements the ADT binar.pdf
To create a node for a binary search tree (BTNode, ) and to create a .pdf
Given the following codepackage data1;import java.util.;p.pdf
Once you have all the structures working as intended- it is time to co.docx

Similar to There is BinarySearchTree class. When removing a node from a BST, we.pdf (20)

PDF
A perfect left-sided binary tree is a binary tree where every intern.pdf
PDF
In the class LinkedBinarySearchTree implement only the following metho.pdf
PDF
How to do the main method for this programBinaryNode.javapublic.pdf
PDF
BSTNode.Java Node class used for implementing the BST. .pdf
PPT
Unit14_BinarySearchTree.ppt
PDF
CS253: Binary Search Trees (2019)
PDF
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
PDF
4. The size of instructions can be fixed or variable. What are advant.pdf
PDF
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
DOCX
For this project, write a program that stores integers in a binary.docx
PDF
JavaCreate a java application using the code example in the follo.pdf
PPT
s11_bin_search_trees.ppt
PDF
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
PDF
import javautilQueue import javautilLinkedList import .pdf
PDF
Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdf
PDF
On the code which has a class in which I implementing a binary tree .pdf
PDF
8 chapter4 trees_bst
PDF
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
PPTX
Binary Search Tree
A perfect left-sided binary tree is a binary tree where every intern.pdf
In the class LinkedBinarySearchTree implement only the following metho.pdf
How to do the main method for this programBinaryNode.javapublic.pdf
BSTNode.Java Node class used for implementing the BST. .pdf
Unit14_BinarySearchTree.ppt
CS253: Binary Search Trees (2019)
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
For this project, write a program that stores integers in a binary.docx
JavaCreate a java application using the code example in the follo.pdf
s11_bin_search_trees.ppt
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
import javautilQueue import javautilLinkedList import .pdf
Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdf
On the code which has a class in which I implementing a binary tree .pdf
8 chapter4 trees_bst
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
Binary Search Tree

More from Dhanrajsolanki2091 (20)

PDF
Please I need help with this....Please also provide comments in each.pdf
PDF
Please give me the origin and history on Hassall’s corpusclesSol.pdf
PDF
OBJ. 2 PE 12-2A Dividing partnership net income Han Lee and Monica An.pdf
PDF
O’Connor says that Aristotelian justice “overcomes misorientation to.pdf
PDF
Let T R^5 rightarrow R^3 be a linear transformation whose null space.pdf
PDF
Make drawings of both organisms stained in each staining procedure. L.pdf
PDF
Link to TextThese selected condensed data are taken from a recent ba.pdf
PDF
Java Database Connect Write a program that views, inserts, and u.pdf
PDF
JUST DO QUESTION 32) You have discussed with the human resource ma.pdf
PDF
Ins struction Use the information that is associated with your name .pdf
PDF
I how does double fertilization aid in the survival of land plants o.pdf
PDF
how does malpractice affect quality of healthcare service What are .pdf
PDF
How can critical researchers claim to represent groups to which they.pdf
PDF
Discuss Allport’s concept of psychological healthy personality.Des.pdf
PDF
Earning revenue journal entry is recorded asA Increases assets.pdf
PDF
Discuss why is it important to use logical and physical data flow di.pdf
PDF
Did HBGary’s Aaron Barr have the legal and ethical right to pursue A.pdf
PDF
Define each of the following terms. Be clear whether it is at develop.pdf
PDF
Could anyone please explain how CPS1 and CPS2 are used for reliabili.pdf
PDF
Complete the following code by deallocating the memory. Int ptr1; .pdf
Please I need help with this....Please also provide comments in each.pdf
Please give me the origin and history on Hassall’s corpusclesSol.pdf
OBJ. 2 PE 12-2A Dividing partnership net income Han Lee and Monica An.pdf
O’Connor says that Aristotelian justice “overcomes misorientation to.pdf
Let T R^5 rightarrow R^3 be a linear transformation whose null space.pdf
Make drawings of both organisms stained in each staining procedure. L.pdf
Link to TextThese selected condensed data are taken from a recent ba.pdf
Java Database Connect Write a program that views, inserts, and u.pdf
JUST DO QUESTION 32) You have discussed with the human resource ma.pdf
Ins struction Use the information that is associated with your name .pdf
I how does double fertilization aid in the survival of land plants o.pdf
how does malpractice affect quality of healthcare service What are .pdf
How can critical researchers claim to represent groups to which they.pdf
Discuss Allport’s concept of psychological healthy personality.Des.pdf
Earning revenue journal entry is recorded asA Increases assets.pdf
Discuss why is it important to use logical and physical data flow di.pdf
Did HBGary’s Aaron Barr have the legal and ethical right to pursue A.pdf
Define each of the following terms. Be clear whether it is at develop.pdf
Could anyone please explain how CPS1 and CPS2 are used for reliabili.pdf
Complete the following code by deallocating the memory. Int ptr1; .pdf

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Sports Quiz easy sports quiz sports quiz
PDF
RMMM.pdf make it easy to upload and study
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Classroom Observation Tools for Teachers
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Lesson notes of climatology university.
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
VCE English Exam - Section C Student Revision Booklet
human mycosis Human fungal infections are called human mycosis..pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Module 4: Burden of Disease Tutorial Slides S2 2025
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Sports Quiz easy sports quiz sports quiz
RMMM.pdf make it easy to upload and study
O7-L3 Supply Chain Operations - ICLT Program
Supply Chain Operations Speaking Notes -ICLT Program
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Classroom Observation Tools for Teachers
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Computing-Curriculum for Schools in Ghana
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Lesson notes of climatology university.
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Insiders guide to clinical Medicine.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
VCE English Exam - Section C Student Revision Booklet

There is BinarySearchTree class. When removing a node from a BST, we.pdf

  • 1. There is BinarySearchTree class. When removing a node from a BST, we can replace it with either its predecessor or its successor. Complete the predecessor() method so that it returns the node that comes immediately before e. Hint: the work needed to find the predecessor is a mirror image of what is required to find a node's successor, so use successor() as a resource. BinarySearchTree class: Solution import java.util.AbstractSet; import java.util.Iterator; public class BinarySearchTree> extends AbstractSet { protected BTNode 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; }
  • 2. protected BTNode copy(BTNode p, BTNode parent) { if (p != null) { BTNode q = new BTNode(p.element, parent); q.left = copy(p.left, q); q.right = copy(p.right, q); return q; } return null; } // method copy /** * Finds the predecessor of a specified BTNode object in this BinarySearchTree. The worstTime(n) is O(n) and * averageTime(n) is constant. * * @param e - the BTNode object whose successor is to be found. * @return the successor of e, if e has a successor; otherwise, return null. */ protected BTNode predecessor(BTNode e) { if (e == null) { return null; } else if (e.left != null) { // successor is leftmost BTNode in right subtree of e BTNode p = e.left; while (p.right != null) { p = p.right; } return p; } // e has a right child else { // go up the tree to the right as far as possible, then go up // to the left. BTNode p = e.parent; BTNode ch = e; while ((p != null) && (ch == p.left)) { ch = p; p = p.parent;
  • 3. } // while return p; } // e has no left child // method successor } @SuppressWarnings("unchecked") @Override public boolean equals(Object obj) { if (!(obj instanceof BinarySearchTree)) { return false; } return equals(root, ((BinarySearchTree) obj).root); } // method 1-parameter equals public boolean equals(BTNode p, BTNode 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
  • 4. * * @return an iterator positioned at the smallest element in this * BinarySearchTree object. */ @Override public Iterator iterator() { // Skipped in preparation for next week 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 getBTNode(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.
  • 5. * @throws NullPointerException - if element is null. */ @Override public boolean add(E element) { if (root == null) { if (element == null) { throw new NullPointerException(); } root = new BTNode(element, null); size++ ; return true; } else { BTNode 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 BTNode(element, temp); size++ ; return true; } // temp.left == null } else if (temp.right != null) { temp = temp.right; } else { temp.right = new BTNode(element, temp); size++ ; return true; } // temp.right == null } // while
  • 6. } // 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) { BTNode e = getBTNode(obj); if (e == null) { return false; } deleteBTNode(e); return true; } /** * Finds the BTNode object that houses a specified element, if there is such an BTNode. The worstTime(n) is O(n), and * averageTime(n) is O(log n). * * @param obj - the element whose BTNode is sought. * @return the BTNode object that houses obj - if there is such an BTNode; otherwise, return null. * @throws ClassCastException - if obj is not comparable to the elements already in this BinarySearchTree object. * @throws NullPointerException - if obj is null. */
  • 7. @SuppressWarnings("unchecked") protected BTNode getBTNode(Object obj) { int comp; if (obj == null) { throw new NullPointerException(); } if (!(obj instanceof Comparable)) { return null; } Comparable compObj = (Comparable) obj; BTNode 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 BTNode object from this BinarySearchTree. * * @param p - the BTNode object whose element is to be deleted from this BinarySearchTree object. * @return the BTNode object that was actually deleted from this BinarySearchTree object. */ protected BTNode deleteBTNode(BTNode 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)) { BTNode s = successor(p);
  • 8. p.element = s.element; p = s; } // p had two children // At this point, p has either no children or one child. BTNode 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 deleteBTNode /** * Finds the successor of a specified BTNode object in this BinarySearchTree. The worstTime(n) is O(n) and * averageTime(n) is constant.
  • 9. * * @param e - the BTNode object whose successor is to be found. * @return the successor of e, if e has a successor; otherwise, return null. */ protected BTNode successor(BTNode e) { if (e == null) { return null; } else if (e.right != null) { // successor is leftmost BTNode in right subtree of e BTNode 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. BTNode p = e.parent; BTNode ch = e; while ((p != null) && (ch == p.right)) { ch = p; p = p.parent; } // while return p; } // e has no right child } // method successor protected static class BTNode { protected E element; protected BTNode left = null, right = null, parent; /** * Initializes this BTNode object. This default constructor is defined for the sake of subclasses of the * BinarySearchTree class. */ public BTNode() {}
  • 10. /** * Initializes this BTNode object from element and parent. */ public BTNode(E element, BTNode parent) { this.element = element; this.parent = parent; } // constructor } // class BTNode } // class BinarySearchTree