-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRecursion.py
More file actions
263 lines (166 loc) · 5.18 KB
/
Recursion.py
File metadata and controls
263 lines (166 loc) · 5.18 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
**************************************************
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
root = TreeNode(1)
root.right = TreeNode(2)
root.right.left = TreeNode(3)
def traverse(node, output):
if node is None:
# we are visiting an empty child
return
# every recursive function needs a base case
# print("VISITING", node.val) # preorder traversal
traverse(node.left, output)
# print("VISITING", node.val) # inorder traversal
traverse(node.right, output)
output.append(node.val)
# print("VISITING", node.val) # post order traversal
return output
traverse(root)
# traverse(root)
# root = TreeNode(1)
# root.right = TreeNode(2)
# root.right.left = TreeNode(3)
# import sys
# sys.setrecursionlimit(10)
# def traverse(node):
# if node is None:
# # we are visiting an empty child
# return
# # every recursive function needs a base case
# print("VISITING", node, node.val)
# traverse(node.left)
# traverse(node.right)
# traverse(root)
Python program to do inorder traversal without recursion
# A binary tree node
class Node:
# Constructor to create a new node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Iterative function for inorder tree traversal
def inOrder(root):
# Set current to root of binary tree
current = root
stack = [] # initialize stack
done = 0
while True:
# Reach the left most Node of the current Node
if current is not None:
# Place pointer to a tree node on the stack
# before traversing the node's left subtree
stack.append(current)
current = current.left
# BackTrack from the empty subtree and visit the Node
# at the top of the stack; however, if the stack is
# empty you are done
elif(stack):
current = stack.pop()
print(current.data, end=" ") # Python 3 printing
# We have visited the node and its left
# subtree. Now, it's right subtree's turn
current = current.right
else:
break
print()
# Driver program to test above function
""" Constructed binary tree is
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
inOrder(root)
"""
***************************** RECURSION EXPLAINED*************************
Recursion is a function that calls itself
It calls itself till it does not call itself anymore. i.e the the conditon runs out
The condition where it stops calling itself is called the base ccase
when thinking of recursion, try to think about the base case first
#Implementing a countdown timer using recursion
import time
def recur_countdown_timer(n):
if n==0:
return n
else:
print(n)
time.sleep(1)
return recur_countdown_timer(n-1)
#z=5
print(recur_countdown_timer(5))
#Implementing a countdown timer using iteration
import time
def iter_countdown_timer(n):
while n>0:
print(n)
time.sleep(1)
n -=1
print(n)
z=5
print(f"Counting down from {z}: ")
iter_countdown_timer(z)
Using Recursion to implement a Factorial
********FACTORIAL*********
A factorial is a product of an interger n, and all the intergers below it
Factorial of 4 => 4*3*2*1 = 24
It is denoted by the symbol '!' after the number, so 5 factorial would be written as '5!'
Factorial of 0 is 1
What will be the base case if we were to do this recursively?
the base case here will be
n! = n* (n-1)!
for n> 0
factorial(n) = n * factorial(n-1)
for n > 0
def factorial_recur(n):
if n==0:
return 1 #This is to solve the first condition where the factorial of zero is one
else:
return n * factorial_recur(n-1)
z=0 #expecting 1
print(f"The value of {z}! is {factorial_recur(z)}")
z=1 #expecting 1
print(f"The value of {z}! is {factorial_recur(z)}")
z=5 #expecting 120
print(f"The value of {z}! is {factorial_recur(z)}")
*************FIBONACCI SERIES*******************
Recursive Implementation
Fibonacci Series is a series of intergers
value of the nth interger can be found by adding the values of the two previous intergers in the series
the series starts off with 0, 1, 1, 2, 3, 5, 8, 13 ...
What will be the base case if we did this recursively
First thing to look out for when doing a recursion is to look for the base cases
Base Case
if n = 0, return 0
if n = 1, return 1
doing this recursively
def fib_recur(n):
if n==0:
return 0
elif n==1:
return 1
else:
return fib_recur(n-1) + fib_recur(n-2)
def fib_runner(z):
print(f"The {z}th number of the fibonacci sequence is {fib_recur(z)}")
z=0
fib_runner(z)
z=1
fib_runner(z)
z=50
fib_runner(z)
Getting the rest of the fib series for every n> 1:
fib(n) = fib(n-1) + fib(n-2)
fib(2) = 1 + 0 --> fib(2) = fib(1) + fib(0)
fib(3) = 1 + 1 --> fib(3) = fib(2) + fib(1)
fib(4) = 2 + 1 --> fib(4) = fib(3) + fib(2)
fib(5) = 3 + 2 --> fib(5) = fib(4) + fib(3)
fib(6) = 5 + 3 --> fib(6) = fib(5) + fib(4)
fib(7) = 8 + 5 --> fib(7) = fib(6) + fib(5)
fib(8) = 1 + 8 --> fib(8) = fib(7) + fib(6)