22C:21 Discrete Structures - PowerPoint PPT Presentation

About This Presentation
Title:

22C:21 Discrete Structures

Description:

22C:21 Discrete Structures Problem 7.2. System Stacks on Recursion – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 46
Provided by: Sau147
Category:

less

Transcript and Presenter's Notes

Title: 22C:21 Discrete Structures


1
22C21 Discrete Structures
  • Problem 7.2. System Stacks on Recursion

2
Stacks just to refresh your memory
  • List
  • FIFO push/pop
  • There is a built-in Stack ADT in java.
  • However, I used my own implementation
  • because I can
  • yes, its THAT easy
  • and a little personal touch is always better

3
The myStack class
  • // Instance variables
  • private static int stack
  • private static int top0
  • private static int MAX100
  • // Default constructor
  • public myStack()
  • stacknew intMAX
  • top-1

4
Push(element)
  • public void push(int b)
  • if(topgtMAX)
  • System.out.println("Overflow!")
  • return
  • stacktopb
  • this.print()

5
Pop()
  • public int pop()
  • if(toplt0)
  • System.out.println("Underflow!")
  • return(-1)
  • int temptop--
  • this.print()
  • return stacktemp

6
Fibonacci Numbers
  • F0 1
  • F1 1
  • Fn Fn-1 Fn-2 for n 2
  • So
  • F2 2, F3 3, F4 5, F5 8
  • and so on

7
Modified Fibonacci Code
  • public static BigInteger fibonacci(int n)
  • System.out.println("fibonacci("n") has been
    called...")
  • SysStack.push(n)
  • if((n 0) (n 1))
  • System.out.println("fibonacci("n")
    returning value...")
  • int garbage SysStack.pop()
  • return new BigInteger ("1")
  • else
  • BigInteger number1 fibonacci(n-1)
  • BigInteger number2 fibonacci(n-2)
  • System.out.println("fibonacci("n") returning
    value...")
  • int moregrabageSysStack.pop()
  • return number1.add(number2)

8
Output for n3
  • fibonacci(3) has been called...
  • Current Stack
  • --
  • 3
  • --
  • fibonacci(2) has been called...
  • Current Stack
  • --
  • 2
  • 3
  • --

9
Output for n3 contd.
  • fibonacci(1) has been called...
  • Current Stack
  • --
  • 1
  • 2
  • 3
  • --
  • fibonacci(1) returning value...
  • Current Stack
  • --
  • 2
  • 3
  • --

10
Output for n3 contd.
  • fibonacci(0) has been called...
  • Current Stack
  • --
  • 0
  • 2
  • 3
  • --
  • fibonacci(0) returning value...
  • Current Stack
  • --
  • 2
  • 3
  • --

11
Output for n3 contd.
  • fibonacci(2) returning value...
  • Current Stack
  • --
  • 3
  • --
  • fibonacci(1) has been called...
  • Current Stack
  • --
  • 1
  • 3
  • --

12
Output for n3 contd.
  • fibonacci(1) returning value...
  • Current Stack
  • --
  • 3
  • --
  • fibonacci(3) returning value...
  • Current Stack
  • --
  • --

should be empty at the end
13
What happens for n5?
Fibonacci(5)
5
4
3
5
System Stack
Empty
14
What happens for n5?
Fibonacci(5)
5
4
3
Fibonacci(4)
4
5
System Stack
15
What happens for n5?
Fibonacci(5)
5
4
3
Fibonacci(4)
Fibonacci(3)
3
4
5
System Stack
16
What happens for n5?
Fibonacci(5)
5
4
3
Fibonacci(4)
Fibonacci(3)
2
Fibonacci(2)
3
4
5
System Stack
17
What happens for n5?
Fibonacci(5)
5
4
3
Fibonacci(4)
Fibonacci(3)
1
2
Fibonacci(2)
3
4
1
0
Fibonacci(1)
5
System Stack
18
What happens for n5?
Fibonacci(5)
5
4
3
Fibonacci(4)
Fibonacci(3)
2
Fibonacci(2)
3
4
0
5
System Stack
19
What happens for n5?
Fibonacci(5)
5
4
3
Fibonacci(4)
Fibonacci(3)
0
2
Fibonacci(2)
3
4
0
Fibonacci(0)
5
System Stack
20
What happens for n5?
Fibonacci(5)
5
4
3
Fibonacci(4)
Fibonacci(3)
2
Fibonacci(2)
3
4
5
System Stack
21
What happens for n5?
Fibonacci(5)
5
4
3
Fibonacci(4)
Fibonacci(3)
1
3
4
5
System Stack
22
What happens for n5?
Fibonacci(5)
5
4
3
Fibonacci(4)
Fibonacci(3)
1
1
Fibonacci(1)
3
4
5
System Stack
23
What happens for n5?
Fibonacci(5)
5
4
3
Fibonacci(4)
Fibonacci(3)
3
4
5
System Stack
24
What happens for n5?
Fibonacci(5)
5
4
3
Fibonacci(4)
2
4
5
System Stack
25
What happens for n5?
Fibonacci(5)
5
Fibonacci(4)
2
Fibonacci(2)
2
4
5
System Stack
26
What happens for n5?
Fibonacci(5)
5
Fibonacci(4)
2
Fibonacci(2)
1
1
0
Fibonacci(1)
2
4
5
System Stack
27
What happens for n5?
Fibonacci(5)
5
Fibonacci(4)
2
Fibonacci(2)
0
2
4
5
System Stack
28
What happens for n5?
Fibonacci(5)
5
Fibonacci(4)
2
Fibonacci(2)
0
0
Fibonacci(0)
2
4
5
System Stack
29
What happens for n5?
Fibonacci(5)
5
Fibonacci(4)
2
Fibonacci(2)
2
4
5
System Stack
30
What happens for n5?
Fibonacci(5)
5
Fibonacci(4)
4
5
System Stack
31
What happens for n5?
Fibonacci(5)
5
3
5
System Stack
32
What happens for n5?
Fibonacci(5)
5
Fibonacci(3)
3
3
5
System Stack
33
What happens for n5?
Fibonacci(5)
5
Fibonacci(3)
3
Fibonacci(2)
2
3
5
System Stack
34
What happens for n5?
Fibonacci(5)
5
Fibonacci(3)
3
Fibonacci(2)
1
1
0
Fibonacci(1)
2
3
5
System Stack
35
What happens for n5?
Fibonacci(5)
5
Fibonacci(3)
3
Fibonacci(2)
0
2
3
5
System Stack
36
What happens for n5?
Fibonacci(5)
5
Fibonacci(3)
3
Fibonacci(2)
0
0
Fibonacci(0)
2
3
5
System Stack
37
What happens for n5?
Fibonacci(5)
5
Fibonacci(3)
3
Fibonacci(2)
2
3
5
System Stack
38
What happens for n5?
Fibonacci(5)
5
Fibonacci(3)
3
1
3
5
System Stack
39
What happens for n5?
Fibonacci(5)
5
Fibonacci(3)
3
1
Fibonacci(1)
1
3
5
System Stack
40
What happens for n5?
Fibonacci(5)
5
Fibonacci(3)
3
3
5
System Stack
41
What happens for n5?
Fibonacci(5)
5
5
System Stack
42
What happens for n5?
System Stack
Empty
43
Note
  • Actual system stack holds returned values
  • Just the indices has been shown
  • Compare with FastFibonacci()

44
Now you know!
45
Quiz 7
  • You are given a collection with 6 items. The
    priorities of these items are shown below
  • 9 30 25 22 7 50
  • 1. Draw all possible max-heaps (as binary trees)
    for this collection of items.
  • 2. Start with the max-heap (30, 50, 9, 25, 7,
    22). Perform the following sequence of operations
    on this max-heap
  • Insert(45)
  • Insert(29)
  • Delete()
  • Delete()
  • Show the max-heap after each operation.
  • 3. Answer in a sentence or two Given a max-heap
    can you find the item with 2nd highest priority
    in O(1) time? Explain your answer.
  • 4. Answer in a sentence or two Given a max-heap
    can you find the item with smallest priority in
    O(1) time? Explain your answer.
Write a Comment
User Comments (0)
About PowerShow.com