1 / 4

F = lambda x : 1 if x == 0 else x*F(x-1) print F(3) F(3) waiting for 3*F(2)

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.

Télécharger la présentation

F = lambda x : 1 if x == 0 else x*F(x-1) print F(3) F(3) waiting for 3*F(2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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()

  3. 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

  4. 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

More Related