资源简介
一个简单的循环转递归的范式测试程序
代码片段和文件信息
#binary tree node
class Node:
father = None
left = None
right = None
key = 0
def __init__(selfkeyfather = Noneleft = Noneright = None):
super().__init__()
self.father = father
self.left = left
self.right = right
self.key = key
#a kind of simulation of a real procedure
class Procedure:
__context = {‘steps‘:[]}
__next = 0
def __init__(selfcontextsteps):
super().__init__()
context[‘steps‘] = steps
self.__context = context
def nextStep(self):
tem = self.__context[‘steps‘][self.__next](self.__context) #do as a function
self.__next = self.__next + 1
return tem
def isComplete(self):
if len(self.__context[‘steps‘]) < self.__next + 1:
return True
else:
return False
#a stack used to store doing procedures
class ProcedureStack:
__allProcedures = []
def push(self procedure):
self.__allProcedures.append(procedure)
def pop(self):
self.__allProcedures.pop(-1)
def top(self):
return self.__allProcedures[-1]
def isEmpty(self):
if len(self.__allProcedures) <= 0:
return True
else:
return False
#trditional recursion (used to test binary tree)
def PrintRecursion(root):
if root.left == None:
print(root.key)
return
PrintRecursion(root.left)
print(root.key)
PrintRecursion(root.right)
#step function1 in recursion simulation
def
- 上一篇:Python列表常用知识总结
- 下一篇:MicroPython中文教程
评论
共有 条评论