<<

Binary Search Trees

I think that I shall never see

a poem as lovelyasatree Po ems

are wrote byfools likemebut only

G can makea

Joyce Kilmer

Binary search trees provide a data which ef

ciently supp orts all six dictionary op erations

A is a ro oted tree where each no de contains

at most two children

Each child can be identied as either a left or right child

parent

left right

A binary tree can be implemented where each no de

has left and right p ointer elds an optional parent

p ointer and a data eld

Binary Search Trees

A binary lab els each no de in a binary tree

with a single key such that foranynode x and no des

in the left subtree of x have keys  x and all no des in

the right subtree of x have keys  x

2

3

7 8

68 7 5

5 6 2 3

Left A Right A but not a

binary search tree

The search tree lab eling enables us to nd where any

key is Start at the ro ot if thatisnottheonewewant

search either left or right dep ending up on whether what

wewant is  or  then ro ot

Searching in a Binary Tree

Dictionary search op erations are easy in binary trees

TREESEARCHx k

if x NILork keyx

then return x

if kkeyx

then return TREESEARCHleftxk

else return TREESEARCHrightxk

The algorithm works b ecause b oth the left and right

subtrees of a binary search tree are binary search trees

recursive structure recursive algorithm

This takes time prop ortional to the height of the tree

O h

Maximum and Minimum

Where are the maximum and minimum elements in a

binary tree

TREEMAXIMUMX

while rightx  NIL

do x rightx

return x

TREEMINIMUMx

while lef tx  NIL

do x leftx

return x

Both take timeprop ortional to the height of the tree

O h

Where is the predecessor

Where is the predecessorofano de in a tree assuming

all keys are distinct

X

PREDECESSOR(X) SUCCESSOR(X)

If X has twochildren its predecessoristhemaximum

value in its left subtree and its successortheminimum value in its right subtree predecessor(x)

X

If it do es not have a left child a no des predecessoris

its rst left ancestor

The pro of of correctness comes from lo oking at the

inorder traversal of the tree H

A F

B G

D

CE

TreeSuccessorx

if rightx  NIL

then return TreeMinimumrightx

y  px

while y  NIL and x righty

do x  y

y  py

return y

Tree predecessorsuccessor b oth run in time prop or

tional to the height of the tree

InorderTreewalkx

if xNIL

then InorderTreeWalklef tx

print keyx

InorderTreewalkrightx ABDEFGH

Tree Insertion

Do a binary search to nd where it should be then

replace the termination NIL p ointer with the new item

LEAF INSERTION FIGURE

TreeinsertT z

y NIL

x r ootT

while x  NIL

do y x

if keyz keyx

then x lef tx

else x rightx

pz  y

if y NIL

then rootT  z

else if keyz keyy

then lef ty  z

else righty  z

y is maintained as the parent of x since x eventually

b ecomes NIL

The nal test establishes whether the NIL was a left

or right turn from y

Insertion takes timeprop ortional to the height of the

tree O h

Tree Deletion

Deletion is somewhat more tricky than insertion be

cause the no de to die maynot b e a leaf and thus eect

other no des

Case a where the no de is a leaf is simple just NIL

out the parents child p ointer

Case b where a no de has one chld the do omed no de

can just b e cut out

Case c relab el the no de as its successorwhich has

at most one child when z has two children and delete

the successor

This implementation of deletion assumes parent p oint

ers to make the co de nicer but if you had to save space

they could be disp ensed with by keeping the p ointers

on the search stored in a stack

TreeDeleteT z

if lef tz NILorrightz NIL

then y  z

else y  TreeSuccessorz

if lef ty  NIL

then x  lef ty

else x  righty

if x  NIL

then px  py

if py NIL

then rootT  x

else if y lef tpy

then lef tpy  x

else rightpy  x

if yz

then keyz  keyy

If y has other elds copy them to o

return y

Lines determine which no de y is physically removed

Lines identify x as the nonnil decendant if any

Lines give x anewparent

Lines mo dify the ro ot no de if necessary

Lines reattach the subtree if necessary

Lines if the removed no de is deleted copy

Conclusion deletion takes time prop ortional to the height of the tree

Balanced Search Trees

All six of our dictionary op erations when implemented

with binary search trees take O h where h is the

height of the tree

The b est height we could hop e to get is lg n if the

tree was p erfectly balanced since

P

blg nc

i

 n

i

But if we get unlucky with our order of insertion or

deletion we could get linearheight

inserta

insertb

insertc

insertd

A

B

C

D

In fact random search trees on average have lg N

height but weare worried ab out worst case height

We cant easily use randomization Why

Perfectly Balanced Trees

Perfectly balanced trees require a lot of work to main tain

9

513

3 7 11 15

2 4 6 8 10 12 14 16

1

If weinsertthekey wemust move every single no de

in the tree to rebalance it taking ntime

Therefore when we talk ab out balanced trees we

mean trees whose height is O lg n so all dictionary

op erations insert delete search minmax succes

sorpredecessor take O lg n time

RedBlack trees are binary search trees where each

no de is assigned a color where the coloring scheme

helps us maintain the height as lg n