Download Linked Lists and Recursion

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Python: Linked Lists and
Recursion
Damian Gordon
Linked Lists
Linked Lists
23
HEAD
62
37
31
Linked Lists: Declaration
class ListNode:
def __init__(self, value, pointer):
self.value = value
self. pointer = pointer
# END ListNode.
Linked Lists: Declaration
node4
node3
node2
node1
=
=
=
=
ListNode(31,
ListNode(37,
ListNode(62,
ListNode(23,
None)
node4)
node3)
node2)
Linked Lists: Printing
print(node1.value)
print(node2.value)
print(node3.value)
print(node4.value)
Linked Lists: Printing
def PrintNodes():
print("[", nodeA.value, "] -> [",nodeB.value,
"] -> [",nodeC.value, "] -> [",nodeD.value, "] ->
NULL")
# END PrintNodes.
Linked Lists: Printing
def PrintNodes():
print("[", nodeA.value, "] -> [",nodeB.value,
"] -> [",nodeC.value, "] -> [",nodeD.value, "] ->
NULL")
# END PrintNodes.
[ 23 ] -> [ 62 ] -> [ 37 ] -> [ 31 ] -> NULL
Linked Lists: Printing
def PrintNodesWithLoop():
global HeadNode
Current = HeadNode
if (Current != None):
# THEN
while (Current.pointer != None):
# DO
print(Current.value)
Current = Current.pointer
# ENDWHILE;
print(Current.value)
else:
print("Empty list")
# ENDIF;
# END PrintNodesWithLoop.
Linked Lists: Printing
def PrintNodesWithLoop():
global HeadNode
Current = HeadNode
if (Current != None):
# THEN
while (Current.pointer != None):
# DO
print(Current.value)
Current = Current.pointer
# ENDWHILE;
print(Current.value)
else:
print("Empty list")
# ENDIF;
# END PrintNodesWithLoop.
23
62
37
31
Linked Lists: Printing
def PrintNodesWithLoopAndCount():
global HeadNode
Current = HeadNode
CountNodes = 0
if (Current != None):
while (Current.pointer != None):
print(Current.value)
Current = Current.pointer
CountNodes = CountNodes + 1
# ENDWHILE;
# Print out and count for last node
CountNodes = CountNodes + 1
print(Current.value)
print("Count:", CountNodes)
else:
print("Empty list")
# END PrintNodesWithLoopAndCount.
Linked Lists: Printing
def PrintNodesWithLoopAndCount():
global HeadNode
Current = HeadNode
CountNodes = 0
if (Current != None):
while (Current.pointer != None):
print(Current.value)
Current = Current.pointer
CountNodes = CountNodes + 1
# ENDWHILE;
# Print out and count for last node
CountNodes = CountNodes + 1
print(Current.value)
print("Count:", CountNodes)
else:
print("Empty list")
# END PrintNodesWithLoopAndCount.
23
62
37
31
Count: 4
Linked Lists: Create Empty List
def CreateEmptyList():
global HeadNode
HeadNode = None
# END CreateEmptyList.
Linked Lists: Delete a List
def DeleteAList():
global HeadNode
HeadNode = None
# END DeleteAList.
Linked Lists: Is the List Empty?
def ListIsEmpty():
global HeadNode
return HeadNode == None
# END ListIsEmpty.
Linked Lists: Find A Node
def FindANode(N):
global HeadNode
Current = HeadNode
Continued 
 Continued
Linked Lists: Find A Node
while ((Current.pointer != None) and (Current.value != N)):
# DO
Current = Current.pointer
# ENDWHILE;
# Print out and count for last node
if (Current.pointer == None):
# THEN
print(N, "is not in the list")
else:
print("Found value:", Current.value)
# ENDIF;
# END FindANode.
Linked Lists: Insert A Node
def InsertANode(Pos, N):
global HeadNode
Current = HeadNode
nodeX = ListNode(N, None)
PositionCounter = 1
CountNodes = 0
Continued 
 Continued
Linked Lists: Insert A Node
if Pos == 0:
HeadNode = nodeX
nodeX.pointer = Current
else:
while (Pos > PositionCounter):
Current = Current.pointer
PositionCounter = PositionCounter + 1
nodeX.pointer = Current.pointer
Current.pointer = nodeX
# ENDIF;
# END InsertANode.
Linked Lists: Delete A Node
def DeleteANode(N):
global HeadNode
Previous = None
Current = HeadNode
Continued 
 Continued
Linked Lists: Delete A Node
if Current.value == N:
# THEN
HeadNode = Current.pointer
else:
while ((Current.pointer != None) and (Current.value != N)):
# DO
Previous = Current
Current = Current.pointer
# ENDWHILE;
Previous.pointer = Current.pointer
# ENDIF;
# END DeleteANode.
Recursion
Recursion: Factorial
7! = 7 * (6 * 5 * 4 * 3 * 2 * 1)
is
7! = 7 * 6!
and
9! = 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
9!= 9 * (8 * 7 * 6 * 5 * 4 * 3 * 2 * 1)
9! = 9 * 8!
and
N! = N * (N-1)!
Recursion: Factorial
# PROGRAM RecursiveFactorial
def RecursiveFact(n):
if n==0:
# THEN
return 1
else:
return n * RecursiveFact(n-1)
# ENDIF;
# END RecursiveFact.
Continued 
 Continued
Recursion: Factorial
######## MAIN PROGRAM #########
InputVal = int(input("Enter number: "))
print (RecursiveFact(InputVal))
# END RecursiveFactorial.
Recursion: Fibonacci
• The Fibonacci numbers are
numbers where the next number
in the sequence is the sum of the
previous two.
• The sequence starts with 1, 1,
• And then it’s 2
• Then 3
• Then 5
• Then 8
• Then 13
Recursion: Fibonacci
# PROGRAM RecursiveFibonacci
def RecursiveFib(n):
if n==1 or n==2:
# THEN
return 1
else:
return RecursiveFib(n-1)+ RecursiveFib(n-2)
# ENDIF;
# END RecursiveFibonacci.
Continued 
 Continued
Recursion: Fibonacci
######## MAIN PROGRAM #########
InputVal = int(input("Enter number: "))
print (RecursiveFib(InputVal))
# END RecursiveFibonacci.
Recursion: Decimal to Binary Conversion
• How do we convert decimal numbers to binary?
•
•
•
•
•
•
•
Let’s try the number 23…
23 / 2 = 11 and remainder is 1
11/2 = 5 and remainder is 1
5/2 = 2 and remainder is 1
2/2 = 1 and remainder is 0
1/2 = 0 and remainder is 1
>> So DEC 23 is BIN 10111
Decimal to Binary Conversion
def DecToBin(n):
if n < 0:
# THEN
return 'Must be a positive integer'
elif n == 0:
return '0'
else:
return DecToBin(n//2) + str(n%2)
# ENDIF;
# END DecToBin.
Continued 
 Continued
Decimal to Binary Conversion
########### MAIN PROGRAM ###########
InputVal = int(input("Enter DECIMAL number: "))
print("The number", InputVal, "is",DecToBin(InputVal),
"in BINARY")
# END DecimalToBinaryConversion.
Linked Lists: Recursion
• How do we count the number of nodes in a linked list
recursively?
def RecursiveCount(Current):
:
:
return 1 + RecursiveCount(Current.pointer)
:
:
Linked Lists: Recursion
def RecursiveCount(Current):
if Current == None:
# THEN
return 0
else:
return 1 + RecursiveCount(Current.pointer)
# ENDIF;
# END RecursiveCount.
print("Recursive Count:", RecursiveCount(HeadNode))
Linked Lists: Recursion
• How do we print out the nodes in a linked list
recursively?
def RecursivePrint(Current):
:
:
print(Current.value)
RecursivePrint(Current.pointer)
:
Linked Lists: Recursion
def RecursivePrint(Current):
if Current == None:
# THEN
return
else:
print(Current.value)
RecursivePrint(Current.pointer)
# ENDIF;
# END RecursiveCount.
RecursivePrint(Current)
Linked Lists: Recursion
• How do we find a node in a linked list recursively?
def FindANode(Current, N):
:
:
return FindANode(Current.pointer, N)
:
:
Linked Lists: Recursion
def FindANode(Current, N):
if (Current.pointer == None):
# THEN
return None
elif (Current.value == N):
# THEN
print(N, "was found")
return N
else:
return FindANode(Current.pointer, N)
# ENDIF;
# END FindANode.
FindANode(Current, 37)
Linked Lists: Recursion
• How do we insert a node in a linked list recursively?
def InsertANode(Current, Pos, N):
:
nodeX = ListNode(N, None)
nodeX.pointer = Current.pointer
Current.pointer = nodeX
:
return InsertANode(Current.pointer, Pos, N)
Linked Lists: Recursion
def InsertANode(Current, Pos, N):
if (Current.pointer == None):
# THEN
return None
elif (Current.value == Pos):
# THEN
nodeX = ListNode(N, None)
nodeX.pointer = Current.pointer
Current.pointer = nodeX
return N
else:
return InsertANode(Current.pointer, Pos, N)
# ENDIF;
# END. InsertANode.
RetValue = InsertANode(Current, 37, 12345)
RecursivePrint(Current)
Linked Lists: Recursion
• How do we delete a node in a linked list recursively?
def DeleteANode(Current, N):
:
:
Current.pointer = DeleteANode(Current.pointer, N)
:
:
Linked Lists: Recursion
def DeleteANode(Current, N):
if (Current != None):
# THEN
if (Current.value == N):
# THEN
Current = Current.pointer
else:
Current.pointer = DeleteANode(Current.pointer, N)
# ENDIF;
# ENDIF;
return Current
# END DeleteANode.
RetValue = DeleteANode(Current, 12345)
RecursivePrint(Current)
etc.
Related documents