Exercises Software Development I 11 Recursion, Binary (Search) Trees Towers of Hanoi // Tree Traversal January 16, 2013 Software Development I Winter term 2012/2013 Institute for Pervasive Computing Johannes Kepler University Linz {farrahi,riener,schmittner}@pervasive.jku.at
Information Regarding the 2nd Test General information Date: Tuesday, January 29, 2013; 17:15-18:45, Lecture halls HS 1, 10, 16, 19 Exercise groups G1, G3, G5 (instructor A. Riener): HS 1 Exercise groups G2, G4 (instructor K. Farrahi): HS 19 Exercise group G6 (instructor T. Schmittner): HS 16 Registration via KUSSS not required! Mandatory only for those with 6 points or more on first test! Length: 45 minutes (extension possible to 60min.) What? 3 short programming examples (select 3 out of 4; topic range: Classes/Objects, Strings, Lists, Inheritance/Polymorphism, Recursion) Allowed material/documents: 1 sheet of paper with notes, format A4, onesided, e.g. loop constructs, type conversion table, switch/case construct) Not allowed: Books, Writing pad, Notebook, Smartphones, Calculators, or any other electronic equipment Few test examples from previous years are loaded to the course webpage Additional test (resit): Start of April 2013 (whole semester material) Software Development I // Exercises // 11 Recursion and Binary Trees // 2
Semester Summary // Question Time Summary and Preparation for 2nd Test Wednesday, January 23, 2013 Plan: 1) Recapitulation/summary of important concepts, problems, etc. 2) Show some potential problems/questions for 2nd test If you have specific questions please send them per Email to riener@pervasive.jku.at (Deadline: Sunday January 20, 2012). Assignment Scores Please double-check the points for your assignments in the online system with the points on your returned assignments... Please see me (your course instructor) on divergence... Software Development I // Exercises // 11 Recursion and Binary Trees // 3
Recursion: Motivation and Definition Many complex real-world problems can be solved very elegant (compact in terms of LOC) by using recursive algorithms In particular true for mathematical problems (where the problem itself is formulated recursively), e.g. Fibonacci numbers, calculation of the Factorial, etc.) Definition Method that calls itself directly or indirect Basic elements: termination condition (anchor of the recursive function; used for escaping) recursive (function) calls Indirect recursion Two or more functions calls each other (in a loop ) At least one of these functions is declared as recursive function (termination condition/ anchor) Recursive defined solutions are mostly less efficient than iterative programs Recursive functions are in general more easy to read/understand as compared to iterative solutions Software Development I // Exercises // 11 Recursion and Binary Trees // 4
Recursion: Basic Structure Direct Recursion () n Terminate? Processing1 Recursion () y Processing2 Recursion () { if (TerminationCondition) { // Recursion anchor Processing2; } else { Processing1; Recursion (); } } Recursive call Software Development I // Exercises // 11 Recursion and Binary Trees // 5
Recursion: Towers of Hanoi This problem comes from history Monks in Vietnam were asked to carry 64 gold disks from one tower (stack) to another. Each disk is of a different size. There are 3 stacks, a source stack, a destination stack and an intermediate stack. A disk is placed on one of three stacks but no disk can be placed on top of a smaller disk. The source tower holds 64 disks. How will the monks solve this problem? How long will it take them? Software Development I // Exercises // 11 Recursion and Binary Trees // 6
Recursion: Binary (Search) Trees Definition 'Binary Tree': A binary tree is either empty (null) or it consists of a node K with an associated element E (=data) and two binary (sub)trees B 1 and B 2 Recursive definition K K K B 1 E B 2 E E B B B Software Development I // Exercises // 11 Recursion and Binary Trees // 7
Recursion: Binary Search Tree Definition 'Binary Search Tree': A binary search tree is a binary tree with sorted elements Elements/values in the left sub-tree are always "smaller" than the node element (i.e., B 1.element < B.element), elements in the right sub-tree are "equal or greater" as compared to the (base) node (B 2.element >= B.element) "Root" Linz K Node B 1 B 2 Enns Wels E B Bruck Graz Leaves Software Development I // Exercises // 11 Recursion and Binary Trees // 8
Recursion: Binary Search Tree Common operations Create a new binary (search) tree Insert a node (element) Search/lookup for a node (element) Traversal (iterate over all elements) Delete a node (element) Tree traversal Directed iteration over all elements of the tree Three options Preorder traversal Inorder traversal Postorder traversal All the 3 options may be implemented recursively Software Development I // Exercises // 11 Recursion and Binary Trees // 9