RECURSION: In programming, recursion is a call to the same method from a method. Recursion is a good problem solving approach Example1: write a function that computes the sum of numbers from 1 to n 1. Use a loop. 2. Recursively. 1-int sum (int n) { int s = 0; for (int i=0; i<n; i++) s+= i; return s; } 2-int sum (int n) { int s; if (n == 0) return 0; else s = n + sum(n-1); return s; } 1 Example2: Factorial (N!) • N! = (N-1)! * N [for N > 1] • 1! = 1 • 3! = 2! * 3= (1! * 2) * 3= 1 * 2 * 3 • Recursive design: - Decomposition: (N-1)! - Composition: * N - Base case: 1! public static int factorial(int n) { int fact; if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; // composition else // base case fact = 1; return fact; } public static intfactorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; public static int factorial(int 2) return fact; { } int fact; if (n > 1) fact = factorial(1) * 2; else fact = 1; public static int factorial(int 1) return fact; { } int fact; if (n > 1) fact = factorial(n - 1) * n; else fact = 1; return fact; } H.W: write arecursive function for: 1- gcd(m,n) 2 2- power(2^n) Binary Search Trees A Binary Search Tree is a binary tree with the following properties: All items in the left subtree are less than the root. All items in the right subtree are greater or equal to the root. Each subtree is itself a binary search tree. Basic Concepts Binary search trees provide an excellent structure for searching a list and at the same time for inserting and deleting data into the list. Valid Binary Search Trees (a), (b) - complete and balanced trees; (d) – nearly complete and balanced tree; (c), (e) – neither complete nor balanced trees Invalid Binary Search Trees 3 Trees: basic definitions and terminology Contrary to arrays, stacks, queues and sequences all of which are one-dimensional data structures, trees are two-dimensional data structures with hierarchical relationship between data items. Definition 1 A tree is a non-empty collection of vertices (nodes) and edges that satisfy certain requirements. Definition 2 A path in a tree is a list of distinct vertices in which successive vertices are connected by edges in the tree. One node in the tree is designated as the root. Each tree has exactly one path between the root and each of the other nodes. If there is more than one path between the root and some node, or no path at all, we have a graph. Definition 3 A set of trees is called a forest. Definition 4 (Recursive definition) A tree is either a single node or a root node connected to a forest. Example of a tree: More definitions Definition 5 An ordered tree is a tree in which the order of children is specified. Definition 6 A level (depth) of a node is the number of nodes on the path from that node to the root. Definition 7 The height (maximum distance) of a tree is the maximum level among all of the nodes in the tree. Definition 8 The path length of a tree is the sum of the levels of all the nodes in the tree. Definition 9 A tree where each node has a specific number of children appearing in a specific order is call a multiway tree. The simplest type of a multiway tree is the binary tree. Each node in a binary tree has exactly two children one of which is designated as a left child, and the other is designated as a right child. Definition 10 (Recursive definition) A binary tree is either an external node, or an internal node and two binary trees. 4 binary trees examples 1. Binary tree for representing arithmetic expressions. The underlying hierarchical relationship is that of an arithmetic operator and its two operands. Arithmetic expression in an infix form: (A - B) + C * (E / F) Note that a post-order traversal of this tree (i.e. visiting the left subtree first, right subtree next, and finally the root) returns the postfix form of the arithmetic expression, while the preorder traversal (root is visited first, then the left subtree, then the right subtree) returns the prefix form of the arithmetic expression. BST Operations We discuss four basic BST operations: traversal, search, insert, and delete; and develop algorithms for searches, insertion, and deletion. • Traversals • Searches • Insertion • Deletion Binary Tree Traversal To traverse a tree means to visit all the nodes in some specified order. For example, you might visit all the nodes in order of ascending key value. Thus the process is most easily described through recursion. The names given for particular style of traversal came from the position of root element with regard to the left and right nodes. Imagine that the left and right nodes are constant in space, then the root node could be placed to the left of the left node (pre-order), between the left and right node (in-order), or to the right of the right node (post-order). Depth-first traversal To traverse a non-empty binary tree in preorder, perform the following operations recursively at each node, starting with the root node: 1. Visit the root. 2. Traverse the left subtree. 3. Traverse the right subtree. To traverse a non-empty binary tree in inorder (symmetric), perform the following operations recursively at each node: 1. Traverse the left subtree. 2. Visit the root. 3. Traverse the right subtree. 5 To traverse a non-empty binary tree in postorder, perform the following operations recursively at each node: 1. Traverse the left subtree. 2. Traverse the right subtree. 3. Visit the root. Breadth-first traversal Trees can also be traversed in level-order, where we visit every node on a level before going to a lower level. This is also called Breadth-first traversal. Depth-first As an example the tree above , the traversals are Preorder traversal sequence: F, B, A, D, C, E, G, I, H (root, left, right) Inorder traversal sequence: A, B, C, D, E, F, G, H, I (left, root, right); note how this produces a sorted sequence Postorder traversal sequence: A, C, E, D, B, H, I, G, F (left, right, root) Breadth-first Level-order traversal sequence: F, B, G, A, D, I, C, E, H Example 1 Inorder a, b, c, d, e, f, g, h, i, j, k Preorder f, b, a, d, c, e, i, h, g, k, j Postorder a, c, e, d, b, g, h, j, k, i, f 6 Example 2 Inorder Traversal (LrR) The result is D, B, E, A, F, C, G Postorder Traversal (LRr) The result is D, E, B, F, G, C, A Preorder Traversal (rLR) The result is A, B, D, E, C, F, G 7 Example 3 A preorder traversal of the tree shown in figure above visits the nodes in the following order: A, B, C, D, E, F, G, H, I A postorder traversal of the tree shown in figure above visits the nodes in the following order: C, B, F, G, E, I, H, D, A An inorder traversal of the tree shown in figure above visits the nodes in the following order: B, C, A, F, E, G, D, I, H Example 4 Preorder Traversal 23, 18, 12, 20, 44, 35, 52 Postorder Traversal 12, 20, 18, 35, 52, 44, 23 Inorder Traversal 12, 18, 20, 23, 35, 44, 52 Inorder traversal of a binary search tree produces a sequenced list 8