12-crs-0106 revised 8 feb 2013 csg2a3 algoritma dan struktur data
TRANSCRIPT
![Page 1: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/1.jpg)
CSG2A3ALGORITMA dan STRUKTUR DATA
Tree Data Structure
![Page 2: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/2.jpg)
2
Binary Tree Data Structure
![Page 3: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/3.jpg)
3
Binary Tree
Tree Data Structure with maximum child (degree) of 2, which are referred to as the left child and the right child.
![Page 4: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/4.jpg)
4
Properties of Binary Tree
The number of nodes n a full binary tree is– At least :
– At most :
– Where h is the height of the tree
Maximum Node for each level = 2n
![Page 5: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/5.jpg)
5
Types of Binary Tree
Full Binary Tree
Complete Binary Tree
Skewed Binary Tree
![Page 6: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/6.jpg)
6
Full Binary Tree
A tree in which every node other than the leaves has two children– sometimes called proper binary tree or 2-tree
![Page 7: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/7.jpg)
7
Complete Binary Tree
a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible
A A
B
A
B C
A
B C
D
A
B C
D E
![Page 8: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/8.jpg)
8
Skewed Tree
Binary Tree with an unbalanced branch between left and right branch
![Page 9: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/9.jpg)
9
ADT Binary Tree
Array Representation
Linked list representation
id value
1 A
2 B
3 C
4 D
5 E
6 F
7 G
![Page 10: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/10.jpg)
10
Array Representation
if a node has an index i, its children are found at indices :– Left child : 2i + 1
– Right child : 2i + 2
while its parent (if any) is found at index – (assuming the root has index zero)
![Page 11: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/11.jpg)
11
Array Representation
Problem :
– Waste space
– Insertion / deletion problem
Array Representation is good for Complete Binary Tree types
– Binary Heap Tree
1 2 3 4 5 6 7 8 … 16
A B - C - - - D … E
![Page 12: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/12.jpg)
12
Linked List Representation
Type infotype : integerType address : pointer to Node
Type Node <info : infotypeleft : addressright : address
>
left Info right
Type BinTree : address
Dictionaryroot : BinTree
![Page 13: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/13.jpg)
13
Binary Tree : Create New Node
function createNode( x : infotype ) : addressAlgorithm
allocate( N )info( N ) xleft( N ) Nullright( N ) Null N
![Page 14: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/14.jpg)
14
Example Application of Binary Tree
Arithmetic Expression Tree
Binary Search Tree
Decision Tree
AVL Tree
Priority Queue– Binary Heap Tree
![Page 15: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/15.jpg)
15
Arithmetic Expression Tree
A specific application of a binary tree to evaluate certain expressions
Example : – (a-b) / ((c+d) * e)
![Page 16: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/16.jpg)
16
Exercise – create the tree
( a + b ) / ( c – d * e ) + f + g * h / i
(( A + B ) * ( C + D )) / ( E + F * H )
(6 - (12 - (3 + 7))) / ((1 + 0) + 2) * (2 *(3 + 1))
![Page 17: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/17.jpg)
17
Binary Search Tree
Ordered / sorted binary tree– Internal nodes each store a key
– two distinguished sub-trees
the key in each node must be:– greater than all keys stored in the left sub-tree
– and smaller than all keys in right sub-tree
![Page 18: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/18.jpg)
18
Binary Search Tree : Insert new NodeProcedure insertBST( i: x: infotype, i/o: N: BinTree )Algorithm
if ( N = Nil ) thenN createNode( x )
elseif ( info( N ) > x ) then
insertBST( x , left( N ) )else if ( info( N ) < x ) then
insertBST( x , right( N ) )else
output(‘duplicate’)
![Page 19: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/19.jpg)
19
Binary Search Tree : Search NodeFunction findNode( i: x: infotype, i/o: N: BinTree ) : addressAlgorithm
if ( info( N ) = x ) or ( N = Nil ) then N
elseif ( info( N ) > x ) then
findNode( x , left( N ) )else if ( info( N ) < x ) then
findNode( x , right( N ) )
![Page 20: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/20.jpg)
20
Binary Search Tree : Delete Node
Delete node P - Logic: – P has no children : remove P
– P has 1 child : replace P with its child
– P has 2 children : Find Q where Q is either:– The leftmost child from the right sub-tree (successor of P) or
– The rightmost child from the left sub-tree (predecessor of P)
Replace P with Q
![Page 21: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/21.jpg)
21
Binary Search Tree : Delete NodeProcedure delMostRight( i/o : P , Q : BinTree )Algorithm
if ( right( Q ) <> Nil ) thendelMostRight( P, right(Q) )if( info( P ) = info( right( Q ) ) )
thenfree( right( Q ) )right( Q ) = Nil
elseinfo( P ) info( Q )deleteBST( Q )
Procedure delMostLeft( i/o : P , Q : BinTree )Algorithm
if ( left( Q ) <> Nil ) thendelMostLeft( P, left(Q) )if( info( P ) = info( left( Q ) ) )
thenfree( left( Q ) )left( Q ) = Nil
elseinfo( P ) info( Q )deleteBST( Q )
![Page 22: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/22.jpg)
22
Binary Search Tree : Delete NodeProcedure deleteBST( i/o : P : BinTree )Dictionary
Q : addressprocedure delMostRight( i/o : P , Q : BinTree )procedure delMostLeft( i/o : P , Q : BinTree )
Algorithmif( left( P ) <> Nil ) then
Q left( P )delMostRight( P , Q )
else Q right( P )delMostLeft( P , Q )
![Page 23: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/23.jpg)
Question?
![Page 24: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/24.jpg)
24
Traversal on Binary Tree
DFS traversal– Pre-order
– In-order
– Post-order
BFS traversal– Level-order
![Page 25: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/25.jpg)
25
Pre-order Traversal
Deep First Search
Root Left Right– Prefix notation
Result : – FBADCEGIH
![Page 26: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/26.jpg)
26
Pre-order TraversalProcedure preOrder( i/o root : tree )
Algorithm
if ( root != null ) then
output( info( root ) )
preOrder( left( root ) )
preOrder( right( root ) )
![Page 27: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/27.jpg)
27
In-order Traversal
Left Root Right– Infix notation
Result : – ABCDEFGHI
![Page 28: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/28.jpg)
28
In-order TraversalProcedure inOrder( i/o root : tree )
Algorithm
if ( root != null ) then
inOrder( left( root ) )
output( info( root ) )
inOrder( right( root ) )
![Page 29: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/29.jpg)
29
Post-order Traversal
Left right Root– Postfix notation
Result : – ACEDBHIGF
![Page 30: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/30.jpg)
30
Post-order TraversalProcedure postOrder( i/o root : tree )
Algorithm
if ( root != null ) then
postOrder( left( root ) )
postOrder( right( root ) )
output( info( root ) )
![Page 31: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/31.jpg)
31
Level-order Traversal
Breadth First Search
recursively at each node
Result : – FBGADICEH
![Page 32: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/32.jpg)
32
Level-order TraversalProcedure levelOrder( root : tree )Dictionary
Q : QueueAlgorithm
enqueue( Q, root )while ( not isEmpty(Q) )
n dequeue( Q )output( n )enqueue( child ( n ) )
![Page 33: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/33.jpg)
33
Exercise – write the traversal - 1
![Page 34: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/34.jpg)
34
Exercise – write the traversal - 2
![Page 35: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/35.jpg)
35
Exercise – write the traversal - 3
![Page 36: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/36.jpg)
36
Exercise – write the traversal - 4
![Page 37: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/37.jpg)
37
Exercise – Create the Tree
Assume there is ONE tree, which if traversed by Inorder resulting : EACKFHDBG, and when traversed by Preorder resulting : FAEKCDHGB
Draw the tree that satisfy the condition above
![Page 38: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/38.jpg)
Question?
![Page 39: 12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA](https://reader038.vdocuments.us/reader038/viewer/2022110101/56649eaa5503460f94bafa01/html5/thumbnails/39.jpg)
THANK YOU39