Title: Chapter 4: Tree
1Chapter 4 Tree
2Recursion (section 1.3)
- Recursive function defined in terms of itself
- f(x) 2f(x-1) x2 and f(0) 0
- f(4) 2f(3) 44
- f(3) 2f(2) 33
- f(2) 2f(1) 22
- f(1) 2f(0) 11
- f(0) 0
- f(1) 20 11 1
- f(2) 21 22 6
- f(3) 26 33 21
- f(4) 221 44 58
3Recursion
- public static int f( int x )
-
- if ( x 0 ) / Base case /
- return 0
- else / General case /
- return 2 f( x-1 ) xx
4Rules of Recursion
- Base case Always have some base cases, which
can be solved without recursion - Making progress Recursive call must always make
progress toward a base case.
5Example
Factorial f(n) n (n-1) (n-2)
1 public static long factorial ( int n )
if ( n lt 1) return 1 else
return n factorial( n - 1 )
6Example
/ print integer digit by digit / public static
void printOut( int n ) if( n gt 10 )
printOut( n / 10 ) printDigit( n 10
) printOut(123) calls printOut(12)
printOut(12) calls printOut(1)
printOut(1) calls printDigit(1) printOut(12)
calls printDigit(2) printOut(123) calls
printDigit(3)
7Example
/ print list / public static void printList(
) if ( isEmpty( ) )
System.out.println(Empty list) else
printList( header.next ) private void
printList( ListNode n ) if ( n ! null )
System.out.println(n.element)
printList(n.next)
8Bad Use of Recursion
- public static int bad( int n)
-
- if( n 0)
- return 0
- else
- return bad( n/31 ) n -1
-
- bad(1) calls bad(1) ---gt not progress to the base
case - bad(2) cannot be evaluated either since it also
calls bad(1) - bad(3), bad(4), bad(5) call bad(2)
- This program doesnt work for any value of n,
except 0.
9Horrible Use of Recursion
/ Compute fibonaci numbers 1,1,2,3,5,8,13,.../ p
ublic static long fib ( int n ) if ( n lt
1) / fib(0) fib(1) 1 / return 1
else return fib( n - 1 ) fib( n - 2
) fib(n-1) fib( n - 1 - 1) fib( n - 1 -
2) fib(n-1) recomputes fib( n - 2 ) So, the
running time grows exponentially
10From recursion to loop
- / Compute fibonaci numbers 1,1,2,3,5,8,13,.../
- public static long fib ( int n )
-
- int a1, b1, next
- if ( n lt 1 ) return 1
- for ( i2 iltn i)
- next ab
- a b
- b next
-
- return next
-
11Tree
12Anatomy of Tree
root
edge, branch
nodes
depth
depth0 at root
parent
child
path
height
height0 at leaves
siblings
log
leaves
13Tree A Recursive Structure
14Tree
15Implementation of Trees
class TreeNode Object element
TreeNode firstChild TreeNode nextSibling
16Preorder Traversal
A B E F C D G H I
17Postorder Traversal
E F B C H I G D A
18Directory Tree
19Listing a directory (preorder traversal)
private void listAll( int depth )
printName( depth ) if ( isDirectory() )
for each file c in this directory (for each
child) c.listAll( depth 1
) public void listAll() listAll( 0 )
20Preorder listing of the directory
/home mark book ch1
ch2 ch3 junk alex
junk work
21Calculate the size of a directory (postorder
traversal)
public int size() int totalSize
sizeOfThisFile() if ( isDirectory() )
for each file c in this directory (for each
child) totalSize c.size()
return totalSize
22Trace of the size function
ch1 (3) 3 ch2 (2) 2
ch3 (4) 4 book (1) 10
junk (6) 6 mark (1) 17 junk
(8) 8 work (1) 1 alex
(1) 10 /home (1) 28
23Binary Tree
24Binary Tree
class BinaryNode Object element
BinaryNode left BinaryNode right
25Binary Tree
Balanced Binary Tree
Worse Case
number of nodes 2h1 - 1 height log(n1) - 1
26Inorder Traversal
D B E A C G F H
27Expression Tree
(a(bc))(((de)f )g) abcdefg