Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Waitress Joyce
Waiter Chris
Cook Max
Helper Len
Waitress Joyce
Waiter Chris
Cook Max
Helper Len
L!"# NO !$ (
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 ')
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
Another Subtree
Owner Jake Manager Brad Chef Carol
Waitress Joyce
Waiter Chris
Cook Max
Helper Len
h '
l=h-1
= & '
h i x n ' x '
x + x + ,,, + x
0 '
n '
= x =
i =0
n '
& = N +'
h
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
') (') 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)
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
') (') 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
(cont,)
tem!late"class #temT$!e> struct TreeNode % #temT$!e in&o' TreeNode( le&t' TreeNode( right' )'
(continues)
(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$
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' )
<unction ;etrieve5te+
<unction ;etrieve5te+
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 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
#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)'
)
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
5norder raversa"
(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
,.-
Fostorder raversa"
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
,.-
Freorder raversa"
nodes in the "e!t subtree4 then visit the nodes in the ri$ht subtree
ree raversa"s
B/C5D,T
C"ass Constructor
tem!late"class #temT$!e> TreeT$!e"#temT$!e>::TreeT$!e() % root = N:;;' )
C"ass Destructor
/estro$(root)'
Copy Constructor
9o!$Tree(root0 originalTree*root)'
) )
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
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'
) )
See te*tboo/
5nsert5te+ De"ete5te+
G*ercises