Beruflich Dokumente
Kultur Dokumente
Binary Search
Search Trees
Trees
Binary Trees
Recursive definition
1. An empty tree is a binary tree
2. A node with two child subtrees is a binary tree
56
26
200
28
18
12
btrees - 2
24
27
190
213
btrees - 3
BST Representation
Represented by a linked data structure of nodes.
root(T) points to the root of tree T.
Each node contains fields:
btrees - 4
key
left pointer to left child: root of left subtree.
right pointer to right child : root of right subtree.
p pointer to parent. p[root[T]] = NIL (optional).
26
200
28
18
12
btrees - 5
56
24
27
190
213
Inorder Traversal
The binary-search-tree property allows the keys of a binary search
tree to be printed, in (monotonically increasing) order, recursively.
Inorder-Tree-Walk
Inorder-Tree-Walk (x)
(x)
1.
1. ifif xx NIL
NIL
2.
2. then
then Inorder-Tree-Walk(left[x])
Inorder-Tree-Walk(left[x])
3.
print
3.
print key[x]
key[x]
4.
Inorder-Tree-Walk(right[x])
4.
Inorder-Tree-Walk(right[x])
56
26
28
18
12
200
24
27
190
213
btrees - 7
Tree Search
Tree-Search(x,
Tree-Search(x, k)
k)
1.
1. ifif xx == NIL
NIL or
or kk == key[x]
key[x]
2.
2. then
then return
return xx
3.
3. ifif kk << key[x]
key[x]
4.
4. then
then return
returnTree-Search(left[x],
Tree-Search(left[x], k)
k)
5.
5. else
else return
returnTree-Search(right[x],
Tree-Search(right[x], k)
k)
26
200
28
18
56
12
24
27
190
213
56
26
200
28
18
12
24
190
27
213
Tree-Minimum(x)
Tree-Minimum(x)
1.
1. while
while left[x]
left[x] NIL
NIL
2.
2. do
do xx
left[x]
left[x]
3.
3. return
return xx
Tree-Maximum(x)
Tree-Maximum(x)
1.
1. while
while right[x]
right[x] NIL
NIL
2.
do
2.
do xx
right[x]
right[x]
3.
3. return
return xx
btrees - 12
12
26
200
28
18
56
24
27
190
213
200
28
18
12
btrees - 13
24
27
190
213
Tree-Insert(T,
Tree-Insert(T,z)z)
1.
1. yy
NIL
NIL
2.
2. xx
root[T]
root[T]
3.
3. while
whilexxNIL
NIL
4.
do
4.
doyy
xx
5.
ififkey[z]
5.
key[z]<<key[x]
key[x]
6.
then
6.
thenxx
left[x]
left[x]
7.
else
7.
elsexx
right[x]
right[x]
8.
8. p[z]
p[z]
yy
9.
9. ififyy==NIL
NIL
10.
10. then
thenroot[t]
root[t]
zz
11.
else
11.
elseififkey[z]
key[z]<<key[y]
key[y]
12.
then
12.
then left[y]
left[y]
zz
13.
else
13.
elseright[y]
right[y]
zz
Analysis of Insertion
Initialization: O(1)
While loop in lines 3-7
searches for place to
insert z, maintaining
parent y.
This takes O(h) time.
Lines 8-13 insert the
value: O(1)
Tree-Insert(T,
Tree-Insert(T,z)z)
1.
1. yy
NIL
NIL
2.
2. xx
root[T]
root[T]
3.
3. while
whilexxNIL
NIL
4.
do
4.
doyy
xx
5.
ififkey[z]
5.
key[z]<<key[x]
key[x]
6.
then
6.
thenxx
left[x]
left[x]
7.
else
7.
elsexx
right[x]
right[x]
8.
8. p[z]
p[z]
yy
9.
9. ififyy==NIL
NIL
10.
10. then
thenroot[t]
root[t]
zz
11.
else
11.
elseififkey[z]
key[z]<<key[y]
key[y]
12.
then
12.
then left[y]
left[y]
zz
13.
else
13.
elseright[y]
right[y]
zz
btrees - 15
Tree-Delete (T, x)
if x has no children
case 0
then remove x
if x has one child
case 1
then make p[x] point to child
if x has two children (subtrees) case 2
then swap x with its successor
perform case 0 or case 1 to delete it
TOTAL: O(h) time to delete a node
btrees - 16
Deletion Pseudocode
Tree-Delete(T,
Tree-Delete(T,z)z)
/*
/*Determine
Determinewhich
whichnode
nodeto
tosplice
spliceout:
out:either
eitherzzor
orzs
zssuccessor.
successor.*/
*/
ififleft[z]
left[z]==NIL
NILor
orright[z]
right[z]==NIL
NIL
then
thenyy
zz
else
elseyy
Tree-Successor[z]
Tree-Successor[z]
/*
/*Set
Setxxto
toaanon-NIL
non-NILchild
childof
ofx,x,or
orto
toNIL
NILififyyhas
hasno
nochildren.
children.*/
*/
4.
4. ifif left[y]
left[y]NIL
NIL
5.
then
5.
thenxx
left[y]
left[y]
6.
else
6.
elsexx
right[y]
right[y]
/*
/*yyisisremoved
removedfrom
fromthe
thetree
treeby
bymanipulating
manipulatingpointers
pointersof
of p[y]
p[y]
and
andxx*/
*/
7.
7. ififxxNIL
NIL
8.
then
8.
thenp[x]
p[x]
p[y]
p[y]
/*
/*Continued
Continuedon
onnext
nextslide
slide*/
*/
btrees - 17
Deletion Pseudocode
Tree-Delete(T,
Tree-Delete(T,z)z)(Contd.
(Contd.from
fromprevious
previousslide)
slide)
9.
9. ififp[y]
p[y]==NIL
NIL
10.
then
10.
thenroot[T]
root[T]
xx
11.
else
11.
elseifif yy
left[p[i]]
left[p[i]]
12.
then
12.
thenleft[p[y]]
left[p[y]]
xx
13.
else
13.
elseright[p[y]]
right[p[y]]
xx
/*
/*IfIfzs
zssuccessor
successorwas
wasspliced
splicedout,
out,copy
copyits
itsdata
datainto
intozz*/
*/
14.
14. ififyyzz
15.
then
15.
then key[z]
key[z]
key[y]
key[y]
16.
copy
16.
copyys
yssatellite
satellitedata
datainto
intoz.z.
17.
17. return
returnyy
btrees - 18
Correctness of Tree-Delete
How do we know case 2 should go to case 0 or case
1 instead of back to case 2?
Because when x has 2 children, its successor is the
minimum in its right subtree, and that successor
has no left child (hence 0 or 1 child).
Equivalently, we could swap with predecessor
instead of successor. It might be good to alternate to
avoid creating lopsided tree.
btrees - 19
btrees - 21
Red-black Tree
Binary search tree + 1 bit per node: the attribute
color, which is either red or black.
All other attributes of BSTs are inherited:
key, left, right, and p.
All empty trees (leaves) are colored black.
We use a single sentinel, nil, for all the leaves of
red-black tree T, with color[nil] = black.
The roots parent is also nil[T ].
btrees - 22
17
41
30
47
38
nil[T]
btrees - 23
50
Red-black Properties
1.
2.
3.
4.
btrees - 25
26
Height of a node:
Number of edges in a
longest path to a leaf.
17
h=1
bh=1
Black-height of a node
bh(x) is the number of
black nodes on path from
x to leaf, not counting x.
h=2 30
bh=1
41
h=3
bh=2
h=1
bh=1
38
nil[T]
btrees - 26
h=4
bh=2
47
h=2
bh=1
h=1 50
bh=1
btrees - 27