Sei sulla pagina 1di 70

Binary Search Trees

CS 308 Data Structures

What is a binary tree?


Property1: each node can have up to two
successor nodes (children)
he predecessor node o! a node is ca""ed its parent he #be$innin$# node is ca""ed the root (no parent) % node without children is ca""ed a leaf

&0 &' && &3

A Tree Has a Root Node


ROOT NO !
Manager Brad Owner Jake Chef Carol

Waitress Joyce

Waiter Chris

Cook Max

Helper Len

Leaf nodes have no children


Owner Jake Manager Brad Chef Carol

Waitress Joyce

Waiter Chris

Cook Max

Helper Len

L!"# NO !$ (

What is a binary tree? (cont,)

Property2: a uni)ue path e*ists !ro+ the


root to every other node

So+e ter+ino"o$y

%ncestor o! a node: any node on the path !ro+ the root to that node Descendant o! a node: any node on a path !ro+ the node to the "ast node in the path -eve" (depth) o! a node: nu+ber o! ed$es in the path !ro+ the root to that node .ei$ht o! a tree: nu+ber o! "eve"s (warnin$: so+e boo/s de!ine it as 0"eve"s 1 ')

A Tree Has Levels


L!%!L &
Manager Brad Owner Jake Chef Carol

Waitress Joyce

Waiter Chris

Cook Max

Helper Len

Level One
Owner Jake Manager Brad Chef Carol

L!%!L '
Waitress Joyce

Waiter Chris

Cook Max

Helper Len

Level Two
Owner Jake Manager Brad Chef Carol

L!%!L (

Waitress Joyce

Waiter Chris

Cook Max

Helper Len

A Subtree
Owner Jake Manager Brad Chef Carol

Waitress Joyce

Waiter Chris

Cook Max

Helper Len

L!#T $)BTR!! O# ROOT NO ! '0

Another Subtree
Owner Jake Manager Brad Chef Carol

Waitress Joyce

Waiter Chris

Cook Max

Helper Len

R*+HT $)BTR!! O# ROOT NO ! ''

What is the 0 o! nodes N o! a !u"" tree with hei$ht h?


The max #nodes at level l is &l

N = & + & + ,,, + &


0 '
l=0 l=1

h '

l=h-1

= & '
h i x n ' x '

using the geometric series:

x + x + ,,, + x
0 '

n '

= x =
i =0

n '

What is the hei$ht h o! a !u"" tree with N nodes?


& ' = N
h

& = N +'
h

h = "o$( N + ') O("o$ N )

he +a* hei$ht o! a tree with N nodes is N (sa+e as a "in/ed "ist) he +in hei$ht o! a tree with N nodes is

Searchin$ a binary tree

') (') Start at the root &) (&) Search the tree "eve" by "eve"4 unti" you
!ind the e"e+ent you are searchin$ !or (O(N) ti+e in worst case)

5s this better than searchin$ a "in/ed "ist?


No ---> O(N)

6inary Search rees

Binary Search Tree Property:

he va"ue stored at a node is greater than the va"ue stored at its "e!t chi"d and less than the va"ue stored at its ri$ht chi"d hus4 the va"ue stored at the root o! a subtree is greater than any va"ue in its "e!t subtree and less than any va"ue in its ri$ht subtree77

Searchin$ a binary search tree

') (') Start at the root &) (&) Co+pare the va"ue o! the ite+ you are
searchin$ !or with the va"ue stored at the root 3) (3) 5! the va"ues are e)ua"4 then item found8 otherwise4 i! it is a "ea! node4 then not found

Searchin$ a binary search tree

() (() 5! it is "ess than the va"ue stored at the


root4 then search the "e!t subtree 9) (9) 5! it is $reater than the va"ue stored at the root4 then search the ri$ht subtree :) (:) ;epeat steps &1: !or the root o! the subtree chosen in the previous step ( or 9 5s this better than searchin$ a "in/ed "ist?
Yes ---> O(logN)

(cont,)

ree node structure

tem!late"class #temT$!e> struct TreeNode % #temT$!e in&o' TreeNode( le&t' TreeNode( right' )'

6inary Search ree Speci!ication


#include "&stream*h> tem!late"class #temT$!e> struct TreeNode' enum OrderT$!e %+,-.O,/-,0 #N.O,/-,0 +O1T.O,/-,)' tem!late"class #temT$!e> class TreeT$!e % !u2lic: TreeT$!e()' 3TreeT$!e()' TreeT$!e(const TreeT$!e"#temT$!e>4)' void o!erator=(const TreeT$!e"#temT$!e>4)' void 5a6e-m!t$()' 2ool #s-m!t$() const' 2ool #s7ull() const' int Num2erO&Nodes() const'

(continues)

6inary Search ree Speci!ication


void ,etrieve#tem(#temT$!e40 2ool4 &ound)' void #nsert#tem(#temT$!e)' void /elete#tem(#temT$!e)' void ,esetTree(OrderT$!e)' void 8etNext#tem(#temT$!e40 OrderT$!e0 2ool4)' void +rintTree(o&stream4) const' !rivate: TreeNode"#temT$!e>( root' )' )'

(cont*)

<unction =u+ber>!=odes
;ecursive i+p"e+entation What is the siAe !actor? What is the base case?
he tree is e+pty 0nodes in a tree ? 0nodes in "e!t subtree @ 0nodes in ri$ht subtree @ ' =u+ber o! nodes in the tree we are e*a+inin$

What is the $enera" case?

Count=odes(-e!t(tree)) @ Count=odes(;i$ht(tree)) @ '

<unction =u+ber>!=odes (cont,) tem!late"class #temT$!e>


int TreeT$!e"#temT$!e>::Num2erO&Nodes() const % return 9ountNodes(root)' )

tem!late"class #temT$!e> int 9ountNodes(TreeNode"#temT$!e>( tree) % i& (tree == N:;;) return 0' else return 9ountNodes(tree->le&t) < 9ountNodes(tree->right) < 1' )

;et=s consider the &irst &e> ste!s:

<unction ;etrieve5te+

<unction ;etrieve5te+

What is the siAe o! the prob"e+?


=u+ber o! nodes in the tree we are e*a+inin$

What is the base case(s)?


') When the /ey is !ound &) he tree is e+pty (/ey was not !ound)

What is the $enera" case?


Search in the "e!t or ri$ht subtrees

<unction ;etrieve5te+ (cont,)


tem!late "class #temT$!e> void TreeT$!e"#temT$!e>:: ,etrieve#tem(#temT$!e4 item02ool4 &ound)
% )

,etrieve(root0 item0 &ound)'

tem!late"class #temT$!e> void ,etrieve(TreeNode"#temT$!e>( tree0#temT$!e4 item02ool4 &ound)


%

i& (tree == N:;;) ?? 2ase case @ &ound = &alse' else i&(item " tree->in&o) ,etrieve(tree->le&t0 item0 &ound)' else i&(item > tree->in&o) ,etrieve(tree->right0 item0 &ound)' else % ?? 2ase case 1 item = tree->in&o' &ound = true'
)

<unction 5nsert5te+

Bse the

binary search tree property to insert the new ite+ at the correct p"ace

<unction 5nsert5te+ (cont,)


5+p"e+entin$
#nsert 11

insertion usin$ recursion

<unction 5nsert5te+ (cont,)

What is the siAe o! the prob"e+?


=u+ber o! nodes in the tree we are e*a+inin$

What is the base case(s)?


he tree is e+pty

What is the $enera" case?


Choose the "e!t or ri$ht subtree

<unction 5nsert5te+ (cont,)


tem!late"class #temT$!e> void TreeT$!e"#temT$!e>::#nsert#tem(#temT$!e item) % #nsert(root0 item)' ) tem!late"class #temT$!e> void #nsert(TreeNode"#temT$!e>(4 tree0 #temT$!e item) % i&(tree == N:;;) % ?? 2ase case tree = ne> TreeNode"#temT$!e>' tree->right = N:;;' tree->le&t = N:;;' tree->in&o = item' ) else i&(item " tree->in&o) #nsert(tree->le&t0 item)' else #nsert(tree->right0 item)' )

<unction 5nsert5te+ (cont,)


#nsert 11

Does the order o! insertin$ e"e+ents into a tree +atter?

Ces4 certain orders produce very unba"anced


trees77 Bnba"anced trees are not desirab"e because search ti+e increases77 here are advanced tree structures (e,$,4#red1b"ac/ trees#) which $uarantee ba"anced trees

Does the order o! insertin$ e"e+ents into a tree +atter? (cont,)

<unction De"ete5te+

<irst4 !ind the ite+8 then4 de"ete it 5+portant: binary search tree property +ust
be preserved77 We need to consider three di!!erent cases: (') De"etin$ a "ea! (&) De"etin$ a node with on"y one chi"d (3) De"etin$ a node with two chi"dren

(') De"etin$ a "ea!

(&) De"etin$ a node with on"y one chi"d

(3) De"etin$ a node with two chi"dren

(3) De"etin$ a node with two chi"dren (cont,)

<ind predecessor (it is the ri$ht+ost node in


the "e!t subtree) ;ep"ace the data o! the node to be de"eted with predecessorDs data De"ete predecessor node

<unction De"ete5te+ (cont,)

What is the siAe o! the prob"e+?


=u+ber o! nodes in the tree we are e*a+inin$

What is the base case(s)?


Eey to be de"eted was !ound

What is the $enera" case?


Choose the "e!t or ri$ht subtree

<unction De"ete5te+ (cont,)


tem!late"class #temT$!e> void TreeT$!e"#tmeT$!e>::/elete#tem(#temT$!e item) % /elete(root0 item)' ) tem!late"class #temT$!e> void /elete(TreeNode"#temT$!e>(4 tree0 #temT$!e item) % i&(item " tree->in&o) /elete(tree->le&t0 item)' else i&(item > tree->in&o) /elete(tree->right0 item)' else /eleteNode(tree)' )

<unction De"ete5te+ (cont,)


tem!late "class #temT$!e> void /eleteNode(TreeNode"#temT$!e>(4 tree)
%

#temT$!e data' TreeNode"#temT$!e>( tem!+tr' tem!+tr = tree' i&(tree->le&t == N:;;) % ??right child tree = tree->right' 0 or 1 child delete tem!+tr' else i&(tree->right == N:;;) % ?? le&t child tree = tree->le&t' 0 or 1 child delete tem!+tr' else % 8et+redecessor(tree->le&t0 data)' tree->in&o = data' @ children /elete(tree->le&t0 data)'
)

<unction De"ete5te+ (cont,)


tem!late"class #temT$!e> void 8et+redecessor(TreeNode"#temT$!e>( tree0 #temT$!e4 data) % >hile(tree->right = N:;;) tree = tree->right' data = tree->in&o' )

ree raversa"s
here are +ain"y three ways to traverse a tree: ') 5norder raversa" &) Fostorder raversa" 3) Freorder raversa"

5norder raversa": % G . H I
tree ,J,!,",H,M,T,.%isit second

%isit left s/0tree first

%isit right s/0tree last (:

5norder raversa"

Jisit the nodes in the "e!t subtree4 then visit


the root o! the tree4 then visit the nodes in the ri$ht subtree
#norder(tree) #& tree is not N:;; #norder(;e&t(tree)) Aisit #n&o(tree) #norder(,ight(tree))

(Warnin$: Warnin$ #visit# +eans that the a"$orith+ does so+ethin$ with the va"ues in the node4 e,$,4 print the va"ue)

Postorder Traversal: A H
tree ,J,!,",H,M,T-

!"T#

%isit last

,.-

%isit left s/0tree first

%isit right s/0tree second (8

Fostorder raversa"

Jisit the nodes in the "e!t subtree !irst4 then


+ostorder(tree) #& tree is not N:;; +ostorder(;e&t(tree)) +ostorder(,ight(tree)) Aisit #n&o(tree)

visit the nodes in the ri$ht subtree4 then visit the root o! the tree

Freorder raversa": H G % .
tree ,J,!,",H,M,T%isit first

IC

,.-

%isit left s/0tree second

%isit right s/0tree last 90

Freorder raversa"

Jisit the root o! the tree !irst4 then visit the


+reorder(tree) #& tree is not N:;; Aisit #n&o(tree) +reorder(;e&t(tree)) +reorder(,ight(tree))

nodes in the "e!t subtree4 then visit the nodes in the ri$ht subtree

ree raversa"s

<unction Frint ree


We use #inorder# to print out the node va"ues Why?? (/eys are printed out in ascendin$ order77) Hint: use binary search trees !or sortin$ 77

B/C5D,T

<unction Frint ree (cont,)


void TreeT$!e::+rintTree(o&stream4 out7ile) % +rint(root0 out7ile)' ) tem!late"class #temT$!e> void +rint(TreeNode"#temT$!e>( tree0 o&stream4 out7ile) % i&(tree = N:;;) % +rint(tree->le&t0 out7ile)' out7ile "" tree->in&o' +rint(tree->right0 out7ile)' ) )

(see te*tboo/ !or over"oadin$ KK and LL)

C"ass Constructor
tem!late"class #temT$!e> TreeT$!e"#temT$!e>::TreeT$!e() % root = N:;;' )

C"ass Destructor

Eo> should >e delete the nodes o& a treeF

C"ass Destructor (contMd)


De"ete the tree in a #botto+1up# !ashion Postorder traversal is appropriate !or this 77
TreeT$!e::3TreeT$!e()
% ) %

/estro$(root)'

void /estro$(TreeNode"#temT$!e>(4 tree) i&(tree = N:;;) % /estro$(tree->le&t)' /estro$(tree->right)' delete tree'


) )

Copy Constructor

Eo> should >e create a co!$ o& a treeF

Copy Constructor (contMd)


tem!late"class #temT$!e> TreeT$!e"#temT$!e>::TreeT$!e(const TreeT$!e"#temT$!e>4 originalTree)
% )

9o!$Tree(root0 originalTree*root)'

tem!late"class #temT$!e) void 9o!$Tree(TreeNode"#temT$!e>(4 co!$0 TreeNode"#temT$!e>( originalTree)


%

) )

i&(originalTree == N:;;) co!$ = N:;;' else % co!$ = ne> TreeNode"#temT$!e>' co!$->in&o = originalTree->in&o' 9o!$Tree(co!$->le&t0 originalTree->le&t)' 9o!$Tree(co!$->right0 originalTree->right)'

!reorder

;eset ree and Net=e*t5te+



he user is a""owed to speci!y the tree traversa" order <or e!!iciency4 Reset ree stores in a )ueue the resu"ts o! the speci!ied tree traversa" hen4 !etNext"tem4 de)ueues the node va"ues !ro+ the )ueue

;eset ree and Net=e*t5te+ (cont,)


(speci!ication !i"e)
enum OrderT$!e %+,-.O,/-,0 #N.O,/-,0 +O1T.O,/-,)' tem!late"class #temT$!e> class TreeT$!e % !u2lic: ?? same as 2e&ore !rivate: TreeNode"#temT$!e>( root' DueT$!e"#temT$!e> !reDue' DueT$!e"#temT$!e> inDue' DueT$!e"#temT$!e> !ostDue' )'

ne> !rivate data

;eset ree and Net=e*t5te+ (cont,)


tem!late"class #temT$!e> void +reOrder(TreeNode"#temT$!e>(0 DueT$!e"#temT$!e>4)' tem!late"class #temT$!e> void #nOrder(TreeNode"#temT$!e>(0 DueT$!e"#temT$!e>4)' tem!late"class #temT$!e> void +ostOrder(TreeNode"#temT$!e>(0 DueT$!e"#temT$!e>4)'

;eset ree and Net=e*t5te+ (cont,)


tem!late"class #temT$!e> void +reOrder(TreeNode"#temT$!e>tree0 DueT$!e"#temT$!e>4 !reDue) % i&(tree = N:;;) % !reDue*-nGueue(tree->in&o)' +reOrder(tree->le&t0 !reDue)' +reOrder(tree->right0 !reDue)' ) )

;eset ree and Net=e*t5te+ (cont,)


tem!late"class #temT$!e> void #nOrder(TreeNode"#temT$!e>tree0 DueT$!e"#temT$!e>4 inDue) % i&(tree = N:;;) % #nOrder(tree->le&t0 inDue)' inDue*-nGueue(tree->in&o)' #nOrder(tree->right0 inDue)' ) )

;eset ree and Net=e*t5te+ (cont,)


tem!late"class #temT$!e> void +ostOrder(TreeNode"#temT$!e>tree0 DueT$!e"#temT$!e>4 !ostDue) % i&(tree = N:;;) % +ostOrder(tree->le&t0 !ostDue)' +ostOrder(tree->right0 !ostDue)' !ostDue*-nGueue(tree->in&o)' ) )

he !unction Reset ree


tem!late"class #temT$!e> void TreeT$!e"#temT$!e>::,esetTree(OrderT$!e order) % s>itch(order) % case +,-.O,/-,: +reOrder(root0 !reDue)' 2rea6' case #N.O,/-,: #nOrder(root0 inDue)' 2rea6' case +O1T.O,/-,: +ostOrder(root0 !ostDue)' 2rea6' ) )

he !unction !etNext"tem
tem!late"class #temT$!e> void TreeT$!e"#temT$!e>::8etNext#tem(#temT$!e4 item0 OrderT$!e order0 2ool4 &inished)
%

&inished = &alse' s>itch(order) % case +,-.O,/-,: !reDue*/eGueue(item)' i&(!reDue*#s-m!t$()) &inished = true' 2rea6' case #N.O,/-,: inDue*/eGueue(item)' i&(inDue*#s-m!t$()) &inished = true' 2rea6' case +O1T.O,/-,: !ostDue*/eGueue(item)' i&(!ostDue*#s-m!t$()) &inished = true' 2rea6'

) )

5terative 5nsertion and De"etion

See te*tboo/

Co+parin$ 6inary Search rees to -inear -ists


6i$1> Co+parison 6inary %rray1 >peration Search ree based -ist Constructor >(') >(') Destructor 5s<u"" 5sG+pty
;etrieve5te+

-in/ed -ist >(') >(=) >(') >(') >(=) >(=) >(=)

>(=) >(') >(') >("o$=) >("o$=) >("o$=)

>(') >(') >(') >("o$=) >(=) >(=)

5nsert5te+ De"ete5te+

G*ercises

'134 81'84 &'4 &&4 &313&

Potrebbero piacerti anche