search(S, k)
: return the node x in S with x.key = k; return NIL if there’s no such nodeinsert(S, x)
: add node x to set Sdelete(S, x)
: delete node x from set Sminimum(S)
: returns the node in S containing the minimum keymaximum(S)
: returns the node in S containing the maximum keysuccessor(S, x)
: returns the node in S that immediately follows node x in the inorder traversal of S, or NIL if no such nodepredecessor(S, x)
: returns the node in S that immediately precedes node x in the inorder traversal of S, or NIL if no such nodex
of T has 4 fields:
x.key
contains the key,x.p
references the parent node if any,x.left
references the left child if any, andx.right
references the right child if any.x
does not have a parent, or does not have a left child, or does not have a right child, then x.p
, or x.left
, or x.right
contains the special value NIL
, respectively. A node may have field(s) for satellite data as well.x.key
\(\le\) y.key
\(\le\) z.key
for any nodes x
, y
, z
such that x
is in the tree rooted at y.left
and z
is in the tree rooted at y.right
.Searching for a key: To find a given key k
in the binary search tree T, start at the root, traverse the path down to a node containing k
, or to a leaf if k
is not in T
search(x, k) {
if x = NIL then
return "k is not in T"
else if k < x.key then
return search(x.left, k)
else if k > x.key then
return search(x.right, k)
else
return x
}
x
is called x
’s access path.x
be a node in T that is not leftmost. The predecessor of x
is the rightmost descendant of x.left
if x.left
\(\ne\) NIL
; it is the nearest ancestor of x
having x
in its right subtree if x.left
= NIL
.x
that is not rightmost is similar.k
in binary search tree T, create a new node x
having key k
, find the empty leaf position for x
, and make x
a child of that leaf’s parent.z
from the binary search tree T,z
has no children, replace z
in T by NIL;z
has exactly one child x
, replace z
in T by x
;z
has two children, letting y
be the successor of z
in T, letting \(\alpha\) be the left subtree of z
, and letting \(\beta\) be the right subtree of z
but having the subtree rooted at y
transplanted by the right subtree of y
, transplant the tree rooted at z
by the tree (y
, \(\alpha\), \(\beta\)).