Remove
COMP 103
2
!
how would you remove...
count
! a
leaf node?
! a node with one child?
! a node with two children?
root
O
Binary Search Trees (Part II)
G
BST Remove
V
T
4
Three Cases:
1.
Node has no children
! just remove it
Node has one child
! pull up the child into node’s place
Remove:
another way to think of the
"leftmost node" is that it is
the next node in an
in-order traversal
root
Jack
Joseph
Jasmine
Jason
Jeremy
Jade
Node has two children:
! Put leftmost node of the right subtree in place of the node.
! simple case: right child is the leftmost (it has no left child)
! tricky case: right child has a left subtree
Extra tricky if the root node is being replaced!
Or rightmost node
in left subtree
Joshua
John
Jason
Jack
"
Q
M
BST Remove – 0 or 1 child
3
3.
I
E
X
S
K
C
A
2.
U
Jasmine
Jessica
James
Joseph
Joe
Jim
Julia
Jules
Justi
BST Remove – 2 children
Algorithm for Remove
5
!
6
Remove Joshua (what do we put in its place?)
! Replace node by leftmost
node in right subtree
! leftmost node has no children
root
Jeremy
!
!
Remove Jade
Jade
! leftmost node in right
subtree is the right child:
Jack
James
= James
! James has right but no left child:
Jasmine
! pull up
Remove Jeremy
! replace by Jessica
! leftmost node only has right child
! pull child up.
! Note: changes the root!!
Joshua
John
Jessica
Jason
Joseph
Julia
Jules
Justin
Joe
Jim
public boolean remove (E value) {
special case of
if value is null or root is null return false
root
if value is in root
set root to removeRootOf(root), decrement count, return true
set currentNode to root; // look for parent of node containing value
while true
if value is less than item in currentNode
value not in set
if left child is null
return false
if left child matches value
found it !
replace
set left child of currentNode to removeRootOf(left child)
left child
decrement count, return true
else set currentNode to left child
else // value is greater than item in currentNode
value not in set
if right child is null
return false
if right child matches value
found it !
replace
set right child of currentNode to removeRootOf(right child)
right child
decrement count, return true
else set currentNode to right child
Algorithm for removeRoot
Given a subtree, this returns a new subtree with the root node of the
subtree removed:
private BSTNode removeRootOf(BSTNode subroot) {
Cases
1&2
Case 3
(simple)
Case 3
(tricky)
if subroot has no children, return null
if subroot has only one child, return that child
set rightChild to be right child of subroot
if rightChild has no left subtree
set left child of rightChild to be left child of subroot
return the child
// find parent of leftmost node in right subtree
set parent to rightChild
while parent has a left-left grandchild { set parent to its left child }
// parent’s left child is now the leftmost node in right subtree
set leftmostNode to left child of parent
set parent’s left node to right child of leftmostNode
set right child of leftmostNode to rightChild
set left child of leftmostNode to left child of subroot;
return leftmostNode;
COMP 103
7
Binary Search Trees (III)
use of LinkedNodes to implement a List
9
cf: use of TreeNodes to implement a Set
10
TreeNode
LinkedNode
! can
do a pre-order traversal recursively (although not breadth-1
! or... the calling class can do same thing iteratively, with a Sta
! can
step along recursively, or...
! the calling class can do the same iteratively, with a reference
(e.g. we called this "rest")
BSTSet which could be an O(log n) implementation of Set
! not
much more than ref to a TreeNode (& now empty Set ok !
! a Set needs an iterator, that keeps track of where it is up to
! Set needs to implement contains, add, remove and so on:
! these can be done iteratively by BSTSet, or...
! do it by calling the TreeNode class's methods (recursive)
! these must take care to keep the BST ordering constraint inta
LinkedList could be an implementation of List
! not
much more than ref to a LinkedNode (now empty List ok !)
! a List needs an iterator, that keeps track of where it is up to
! "
! List
step along the list, with a ref to "next" (exactly like "rest")...
needs to implement contains, add and remove:
! these
can be done iteratively by LinkedList, or...
! do it by calling the LinkedNode class's method (which is recursive)
recall: Depth-1st traversal, iteratively with Stack
11
An iterator() method for BST Set
12
private void DepthFirstTraversal(TreeNode root) {
Stack<TreeNode> todo = new Stack<TreeNode>();
todo.push(root);
pre-order
while (!todo.isEmpty()) {
TreeNode node = todo.pop();
System.out.println(node.employee());
for (TreeNode child : node.getChildren())
todo.push(child);
}
Justine
}
Jeremy
John
Jules
Julia
! Returns
an iterator that steps through all items of the set
! Could easily do this breadth-first or pre-order depth-first..
! But for a BSTSet, it would be best to do an in-order traversal
The Iterator has to store the state of the iteration, which means
mimicking the behaviour of an iterative, in-order traversal
! Use a stack
Initialise by putting root on stack, and all its left descendants
! Each call to next() should do one iteration of the loop:
Remove top node from stack
Put right child of node onto stack, and all its left descendants
Return the item
Required for an
Jordan
Jada
Jack
Jenna
Jake James Jackie Jacob
!
Let's see how this works...
in-order traversal
(cf. pre-order, which puts both
children on stack)
Binary Tree Iterator (In Order)
Binary Tree Iterator (In Order)
13
14
Need to also push all left hand descendants
when add node to stack:
Need to push all left hand children
when add node to stack:
Need to add right child to stack
when process a node:
root
Jack
root
Jade
5
Jeremy
5
Jeremy
Jeremy
3
1
Jack
Jade
John
4 Jasmine
3
Joshua
7
9
Justin
Jack
1
Jade
4 Jasmine
6
2
Joshua
7
John
9
8
Julia
Justin
6
Julia
8
Jacob
2
Jacob
BSTSet: Iterator
Binary Tree Iterator (In Order)
15
(private class inside BSTSet clas
16
Need to push all left hand children
when add node to stack:
private class BSTSetIterator implements Iterator<E> {
Stack<BSTNode> stack = new Stack<BSTNode>();
public BSTSetIterator (BSTNode root) {
Need to add right child to stack
when process a node:
for (BSTNode nd = root; nd != null; nd = nd.left)
stack.push(nd);
}
public boolean hasNext() {
Jasmine
root
return !stack.isEmpty();
Jade
5
Jeremy
}
public E next() {
Jacob
Jeremy
3
1
Jack
Jade
4 Jasmine
Jack
Joshua
7
John
9
8
Julia
if (stack.isEmpty()) throw new NoSuchElementException();
BSTNode node = stack.pop();
for (BSTNode nd = node.right; nd != null; nd = nd.left)
stack.push(nd);
return node.value;
Justin
6
2
Jacob
}
}
© Copyright 2025 Paperzz