Transcript
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 1/39
CSG2A3ALGORITMA dan STRUKTUR DATA
Tree Data Structure
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 2/39
Binary Tree Data Structure
2
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 3/39
Binary Tree
Tree Data Structure with maximum child (degree)of 2, which are referred to as the left child and theright child.
3
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 4/39
Properties of Binary Tree
The number of nodes n a full binary tree is
– At least
–At most
– !here h is the height of the tree
"aximum #ode for each le$el % 2n
&
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 5/39
Types of Binary Tree
'ull inary Tree
om*lete inary Tree
S+ewed inary Tree
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 6/39
Full Binary Tree
A tree in which e$ery node other than the lea$eshas two children
– sometimes called *ro*er binary tree or 2-tree
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 7/39
Complete Binary Tree
a binary tree in which e$ery le$el, exce*t *ossiblythe last, is com*letely filled, and all nodes are asfar left as *ossible
/
A A
A
A
D
A
D 0
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 8/39
Skewed Tree
inary Tree with an unbalanced branch betweenleft and right branch
1
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 9/39
ADT Binary Tree
Array e*resentation
in+ed list re*resentation
4
id value
5 A
2
3
& D
0
'
/ 6
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 10/39
Array Representation
if a node has an index i, its children are found atindices
– eft child 2i + 1
– ight child 2i + 2
while its *arent (if any) is found at index
– (assuming the root has index 7ero)
58
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 11/39
Array Representation
9roblem
– !aste s*ace
– :nsertion ; deletion *roblem
Array e*resentation is good forom*lete inary Tree ty*es
– inary <ea* Tree
55
1 2 ! " # $ % & 1#
A - - - - D = 0
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 12/39
'inked 'ist Representation
52
Ty*e infoty*e integerTy*e address *ointer to #ode
Ty*e #ode >info infoty*eleft addressright address
?
left :nfo right
Ty*e inTree address
Dictionaryroot inTree
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 13/39
Binary Tree ( Create )ew )odefunction create#ode( x infoty*e ) address
Algorithm
allocate( # )info( # ) x
left( # ) #ull
right( # ) #ull
#
53
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 14/39
*+ample Application of Binary TreeArithmetic 0x*ression Tree
inary Search Tree
Decision Tree
A@ Tree
9riority ueue
– inary <ea* Tree
5&
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 15/39
Arit,metic *+pression TreeA s*ecific a**lication of a binary tree to e$aluatecertain ex*ressions
0xam*le – (a-b) ; ((cBd) C e)
5
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 16/39
*+ercise - create t,e tree( a B b ) ; ( c d C e ) B f B g C h ; i
(( A B ) C ( B D )) ; ( 0 B ' C < )
( - (52 - (3 B /))) ;((5 B 8) B 2) C(2 C(3 B 5))
5
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 17/39
Binary Searc, TreeErdered ; sorted binary tree
– :nternal nodes each store a +ey
– two distinguished sub-trees
the +ey in each node must be
– greater than all +eys stored in the left sub-tree
– and smaller than all +eys in right sub-tree
5/
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 18/39
Binary Searc, Tree ( .nsert new )ode9rocedure insertST( i x infoty*e, i;o # inTree )Algorithm
if ( # % #il ) then# create#ode( x )
elseif ( info( # ) ? x ) then
insertST( x , left( # ) )else if ( info( # ) > x ) then
insertST( x , right( # ) )else
out*ut(Fdu*licateG)
51
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 19/39
Binary Searc, Tree ( Searc, )ode'unction find#ode( i x infoty*e, i;o # inTree ) addressAlgorithm
if ( info( # ) % x ) or ( # % #il ) then #
elseif ( info( # ) ? x ) then
find#ode( x , left( # ) )else if ( info( # ) > x ) then
find#ode( x , right( # ) )
54
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 20/39
Binary Searc, Tree ( Delete )odeDelete node 9 - ogic
– 9 has no children remo$e 9
– 9 has 5 child re*lace 9 with its child
– 9 has 2 children
'ind where is either
– The leftmost child from the right sub-tree (successor of 9) or
– The rightmost child from the left sub-tree (*redecessor of 9)
e*lace 9 with
28
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 21/39
Binary Searc, Tree ( Delete )ode'unction del"ostight( 9address, address ) address
Algorithmwhile ( right() >? #H ) do
right()info(9) info()left(9) deleteST( left(9), info() ) 9
25
'unction del"osteft( 9address, address ) addressAlgorithm
while ( left() >? #H ) do left()info(9) info()right(9) deleteST( right(9), info() ) 9
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 22/39
Binary Searc, Tree ( Delete )ode'unction deleteST( 9 address, x infoty*e ) -? addressDictionary
'unction del"ostight( 9address, address ) address'unction del"osteft( 9address, address ) address
Algorithm
if( 9 % #H ) then 9
if (x > info(9)) thenleft(9) deleteST( left(9), x )
else if (x ? info(9)) thenright(9) deleteST( right(9), x )
elseif (left(9) >? #H ) then
9 del"ostight( 9 , left(9) )else if( right(9) >?#H ) then
9 del"osteft( 9, right(9) )else
delete 9 #H
9
22
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 23/39
/uestion0
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 24/39
Traversal on Binary TreeD'S tra$ersal
– 9re-order
– :n-order
– 9ost-order
'S tra$ersal
– e$el-order
2&
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 25/39
Preorder TraversalDee* 'irst Search
oot eft ight
– 9refix notation
esult
– 'AD06:<
2
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 26/39
Preorder Traversal9rocedure *reErder( i;o root tree )
Algorithm
if ( root I% null ) then
out*ut( info( root ) )
*reErder( left( root ) )
*reErder( right( root ) )
2
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 27/39
.norder Traversaleft oot ight
– :nfix notation
esult – AD0'6<:
2/
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 28/39
.norder Traversal9rocedure inErder( i;o root tree )
Algorithm
if ( root I% null ) then
inErder( left( root ) )
out*ut( info( root ) )
inErder( right( root ) )
21
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 29/39
Postorder Traversaleft right oot
– 9ostfix notation
esult – A0D<:6'
24
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 30/39
Postorder Traversal9rocedure *ostErder( i;o root tree )
Algorithm
if ( root I% null ) then
*ostErder( left( root ) )
*ostErder( right( root ) )
out*ut( info( root ) )
38
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 31/39
'evelorder Traversalreadth 'irst Search
recursi$ely at each node
esult
– '6AD:0<
35
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 32/39
'evelorder Traversal9rocedure le$elErder( root tree )Dictionary
ueueAlgorithm
enJueue( , root )while ( not is0m*ty() )
n deJueue( )out*ut( n )enJueue( child ( n ) )
32
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 33/39
*+ercise - write t,e traversal 1
33
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 34/39
*+ercise - write t,e traversal 2
3&
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 35/39
*+ercise - write t,e traversal
3
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 36/39
*+ercise - write t,e traversal !
3
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 37/39
*+ercise - Create t,e TreeAssume there is E#0 tree, which if tra$ersed by:norder resulting 0AK'<D6, and whentra$ersed by 9reorder resulting 'A0KD<6
Draw the tree that satisfy the condition abo$e
3/
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 38/39
/uestion0
7/23/2019 11.2 Tree Binary Tree
http://slidepdf.com/reader/full/112-tree-binary-tree 39/39
THANK YOU
34
top related