Chapter 4: Tree - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 4: Tree

Description:

book (1) work (1) junk (8) junk (6) ch1 (3) ch2 (2) ch3 (4) 2110211 Intro. ... book (1) 10. junk (6) 6. mark (1) 17. junk (8) 8. work (1) 1. alex (1) 10 /home (1) 28 ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 28
Provided by: veeramu
Category:
Tags: book1 | chapter | tree

less

Transcript and Presenter's Notes

Title: Chapter 4: Tree


1
Chapter 4 Tree
2
Recursion (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

3
Recursion
  • public static int f( int x )
  • if ( x 0 ) / Base case /
  • return 0
  • else / General case /
  • return 2 f( x-1 ) xx

4
Rules 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.

5
Example
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 )
6
Example
/ 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)
7
Example
/ 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)
8
Bad 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.

9
Horrible 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
10
From 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

11
Tree
12
Anatomy of Tree
root
edge, branch
nodes
depth
depth0 at root
parent
child
path
height
height0 at leaves
siblings
log
leaves
13
Tree A Recursive Structure
14
Tree
15
Implementation of Trees
class TreeNode Object element
TreeNode firstChild TreeNode nextSibling
16
Preorder Traversal
A B E F C D G H I
17
Postorder Traversal
E F B C H I G D A
18
Directory Tree
19
Listing 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 )
20
Preorder listing of the directory
/home mark book ch1
ch2 ch3 junk alex
junk work
21
Calculate 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
22
Trace 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
23
Binary Tree
24
Binary Tree
class BinaryNode Object element
BinaryNode left BinaryNode right
25
Binary Tree
Balanced Binary Tree
Worse Case
number of nodes 2h1 - 1 height log(n1) - 1
26
Inorder Traversal
D B E A C G F H
27
Expression Tree
(a(bc))(((de)f )g) abcdefg
Write a Comment
User Comments (0)
About PowerShow.com