40 likes | 194 Views
Factorial Recursion Runtime Stack. F = lambda x : 1 if x == 0 else x*F(x-1) print F(3) F(3) waiting for 3*F(2) F(2) waiting for 2*F(1) F(1) waiting for 1*F(0) F(0) returns 1. R. 4. RR. RL. 2. 5. RLL. RLR. RRL. RRR. 1. 3. RLLL. RLLR. RLRR. RLRL.
E N D
Factorial Recursion Runtime Stack F = lambda x : 1 if x == 0 else x*F(x-1) print F(3) F(3) waiting for 3*F(2) F(2) waiting for 2*F(1) F(1) waiting for 1*F(0) F(0) returns 1
R 4 RR RL 2 5 RLL RLR RRL RRR 1 3 RLLL RLLR RLRR RLRL # For the book BST code, do the following to get this tree: numbers = [4,2,1,3,5] intTree = BinaryTree() for e in numbers: intTree.insert(e) # The next pages show the runtime stack for: print intTree.inorder() # and print intTree.postorder()
definorderHelper(self, r): if r != None: self.inorderHelper(r.left) print r.element, " " self.inorderHelper(r.right) Inorder() inorderHelper(R) inorderHelper(RL), R.element, inorderHelper(RR) inorderHelper(RLL), RL.element, inorderHelper(RLR) inorderHelper(RLLL),RLL.element, inorderHelper(RLLR) 1 2 inorderHelper(RLRL), RLR.element, inorderHelper(RLRR) 3 4 inorderHelper(RRL),RR.element, inorderHelper(RRR) 5
defpostorderHelper(self, root): if root != None: self.postorderHelper(root.left) self.postorderHelper(root.right) print root.element, " ” Postorder(R) postorderHelper(RL), postorderHelper(RR), R.element postorderHelper(RLL), postorderHelper(RLR), RL.element postorderHelper(RLLL), postorderHelper(RLLR), RLL.element 1 postorderHelper(RLRL), postorderHelper(RLRR), RLR.element 3 2 postorderHelper(RRL), postorderHelper(RRR), RR.element 5 4