-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursionLecture.java
More file actions
173 lines (124 loc) · 5.17 KB
/
RecursionLecture.java
File metadata and controls
173 lines (124 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//public class RecursionLecture {
// Recursion is when a function can call itself.
// public static void countDown(int num) {
// System.out.println(num);
// countDown(num - 1);
// }
//
// // Recursion will always create a Stack Overflow Error if a "base case" is not defined.
//
// public static void main(String[] args) {
// countDown(10);
// }
// A "base case" is condition in which the function eventually does NOT call itself (recur) again.
// public static void countDown(int num) {
// if (num == 0) return; // <-- base case
// System.out.println(num);
// countDown(num - 1);
// }
////
// public static void main(String[] args) {
// countDown(10);
// }
// Recursion is a powerful way to express algorithmic solutions but use it with caution.
// Recursion should ONLY be used in cases when it makes the code much more readable.
// Any algorithm that uses recursion can be solved with looping (iteration) instead.
// public static void countDown(int num) {
// while(num > 0) {
// System.out.println(num);
// num--;
// }
// }
//
// public static void main(String[] args) {
// countDown(10);
// }
// Iterative (looping) algorithms are often more efficient than recursive algorithms.
// Some problems CANNOT be solved with recursion, particularly when the repetitions exceed 10k times.
// public static void countDownRecur(int num) {
// if(num==0) return;
// System.out.println(num);
// countDownRecur(num - 1);
// }
//
// public static void countDown(int num) {
// while(num > 0) {
// System.out.println(num);
// num--;
// }
// }
//
// public static void main(String[] args) {
// countDown(10000);
// countDownRecur(10000);
// }
// Recursion is most commonly used by programmers to solve problems that require a divide and conquer approach
// or involve data structures like trees and graphs where the complexity grows as you continue to traverse it.
/*
Recursion for backtracking to find a way through a maze
___________________________________
| _____ | | ___ | ___ ___ | | | |
| | | |_| |__ | |_| __|____ | | | |
| | | |_________|__ |______ |___|_| |
| |_| | _______ |______ | | ____|
| ___ | |____ | |______ | |_| |____ |
|___|_|____ | | ___ | |________ | |
| ________| | |__ | |______ | | | |
| | | ________| | __|____ | | | __| |
|_| |__ | | __|__ | ____| | |_| __|
| ____| | |____ | |__ | |__ |__ |
| |_______|_______|___|___|___|_____|
*/
/*
Recursion to move through a binary tree...
1
/ \
/ \
2 \
/ \ 3
4 5 / \
9 \
8
/ \
6 7
*/
// Another important concept to understand is the "call stack". The "call stack" is the area of memory in Java program
// where each method call is stacked when methods call other methods. Once the final method call is evaluated, it is
// removed from the top of the call stack and the remaining methods are removed as well.
// Consider the following pseudocode:
/*
methodA() {
methodB();
}
methodB() {
methodC();
}
methodC() {
sout("Hello");
}
methodA(); // when everything gets kicked off
*/
// The call stack for the above pseudocode would look something like this:
/*
methodC <-- stack frame
methodB <-- stack frame
methodA <-- stack frame
*/
// Think of it like a stack of plates, where each nested method call is a plate (stack frame) that is added to the stack.
// When methodC prints "hello" and finishes, it is removed (popped) from the top of the stack and so is methodB and finally methodA.
// In recursion, depending on how many method calls it takes to reach the base case, the stack may look like this:
/*
...
someMethod
someMethod
someMethod
someMethod
*/
// If no base case is reached, the computer will run out of memory and the program will crash with a Stack Overflow Error.
/*RECURSION MINI-EXERCISES*/
// TODO: use recursion to print out a given number up through 100
// TODO: use recursion to print letters of a string starting with the first and ending with the last (each call will work on a smaller substring)
// TODO: use recursion to add all numbers up from 1 to a given number
// BONUS TODO: use recursion to write a factorial method (very similar to the third todo)
// BONUS TODO: use recursion to reverse the characters in a string
//}