Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees1 Recursion Recursion is a concept of defining a method that makes a call to itself.

Similar presentations


Presentation on theme: "Trees1 Recursion Recursion is a concept of defining a method that makes a call to itself."— Presentation transcript:

1 Trees1 Recursion Recursion is a concept of defining a method that makes a call to itself.

2 Trees2 Recursion LinearSum(A,5) LinearSum(A,4) LinearSum(A,3) LinearSum(A,2) LinearSum(A,1) Algorithm LinearSum(A, n) Input: an integer array A of n elements Output: The sum of the n elements if n=1 then return A[0] else return LinearSum(A, n-1)+A[n-1] The recursive method should always possess—the method terminates. We did it by setting :” if n=1 then return A[0]” Return A[0]=4 Return 4+A[1]=7 return 7+A[2]=13 return 13+A[3]=15 return 15+A[4]=20 A={4,3,6,2,5} The compiler of any high level computer language uses a stack to handle recursive calls.

3 Trees3 Factorial Example: f(n)=n!=n×(n-1)×(n-2)×…×2×1 and 0!=1. It can also be written as f(n)=n×f(n-1) and f(0)=1. Java code: Public static int recursiveFactorial(int n) { if (n==0) return 1; else return n*recursiveFactorial(n-1); }

4 Trees4 Factorial recursiveFactorial(4) recursiveFactorial(3) recursiveFActorial(2) recursiveFactorial(1) recursiveFactorial ( 0) Public static int recursiveFactorial(int n){ if (n==0) return 1; else return n*recursiveFactorial(n-1);} The recursive method should always possess—the method terminates. We did it by setting :” if n=0 then return 1” Return f(0)=1 return f(1)=1*1=1 return f(2)=2*f(1)=2 return f(3)=3*f(2)=6 return f(4)=4*f(3)=24 f(n)=n*f(n-1) for n>0 and f(0)=1.

5 Trees5 ReverseArray Algorithm ReverseArray(A, i, j): input: An array A and nonnegative integer indices i and j output: The reversal of the elements in A starting at index i and ending at j if i<j then {Swap A[i] and A[j] ReverseArray(A, i+1, j-1)} return A={1, 2, 3, 4}. ReverseArray{A, 0, 3) ReverseArray{A, 1 2)A=(4,3,2,1} A={4,2,3,1}

6 Trees6 Part-C Trees University Fac. of Sci. & Eng. Bus. School Law School CS Dept. EE Dept. Math. Dept.

7 Trees7 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child relation Applications: Organization charts File systems arithmetic expression Computers”R”Us SalesR&DManufacturing LaptopsDesktops US International EuropeAsiaCanada Remark: A tree is a special kind of graph, where there is not circuit.

8 Trees8 File System on Computers H: CS5302OthersCS5301 a.javab.java net source readmeNode.javaStack.java

9 Trees9 subtree Tree Terminology Root: node without parent (e.g. A) Internal node: node with at least one child (e.g. A, B, C, F) External node (or leaf ): node without children (E, I, J, K, G, H, D) Ancestors of a node: parent, grandparent, grand-grandparent, etc. Depth of a node: number of ancestors Height of a tree: maximum depth of any node (3) Descendant of a node: child, grandchild, grand-grandchild, etc. A B DC GH E F IJ K Subtree: tree consisting of a node and its descendants

10 Trees10 Tree Terminology Ordered tree: There is a linear ordering defined for the children of each node. Example: The order among siblings are from left to right. Ch1 Ch2 Ch3; S1.1 S1.2; S2.1 S2.1; 1.2.1 1.2.2 1.2.3; Book Ch1Ch3Ch2 S.2.1S.2.2 S.1.1 S.1.2 1.2.11.2.21.2.3

11 Trees11 Tree ADT (§ 6.1.2) A set of nodes with parent- child relationship. Generic methods: integer size() boolean isEmpty() Accessor methods: root() returns the tree’s root. parent(p) returns p’s parent children(p) returns an iterator of the children of node v. Element() return the object stored on this node. Query methods: boolean isInternal(p) boolean isExternal(p) boolean isRoot(p) Update method: object replace (p, o): replace the content of node p with o. Additional update methods may be defined by data structures implementing the Tree ADT

12 Trees12 Binary Trees (§ 6.3) A binary tree is a tree with the following properties: Each internal node has at most two children (exactly two for proper binary trees) The children of a node are an ordered pair We call the children of an internal node left child and right child Alternative recursive definition: a binary tree is either a tree consisting of a single node, or a tree whose root has an ordered pair of children, each of which is a binary tree Applications: arithmetic expressions decision processes searching A B C FG D E H I

13 Trees13 Arithmetic Expression Tree Binary tree associated with an arithmetic expression internal nodes: operators external nodes: operands Example: arithmetic expression tree for the expression (2  ( a  1)  (3  b))    2 a1 3b

14 Trees14 Decision Tree Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions Example: drinking decision Want to drink ? How about coffee? Bye-Bye Here it is Yes No Yes No How about tee Here it is Yes Here is your water No

15 Trees15 Inorder Traversal (just for binary tree) In an inorder traversal a node is visited after its left subtree and before its right subtree Algorithm inOrder(v) if hasLeft (v) inOrder (left (v)) visit(v) if hasRight (v) inOrder (right (v)) 3 1 2 5 6 79 8 4

16 Trees16 Inorder Traversal (Another example) 6 4 2 8 5 9 710 11 7 13. The number on a node is bigger than the numbers on its left sub-tree and smaller than the numbers on its right sub-tree.

17 Trees17 BinaryTree ADT (§ 6.3.1) The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT Additional methods: position left(p) position right(p) boolean hasLeft(p) boolean hasRight(p) Update methods may be defined by data structures implementing the BinaryTree ADT

18 Trees18 Print Arithmetic Expressions Specialization of an inorder traversal print operand or operator when visiting node print “(“ before traversing left subtree print “)“ after traversing right subtree Algorithm printExpression(v) if hasLeft (v) print( “(’’ ) inOrder (left(v)) print(v.element ()) if hasRight (v) inOrder (right(v)) print ( “)’’ )    2 a1 3b ((2  ( a  1))  (3  b))

19 Trees19 Print Arithmetic Expressions    2 a1 3b ((x+((2  ( a  1))  (3  b)))-10) x + - 10

20 Trees20 Remarks: They like this part the best because it is easy and contains more example. 5.41


Download ppt "Trees1 Recursion Recursion is a concept of defining a method that makes a call to itself."

Similar presentations


Ads by Google