1 / 34

AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering

This course covers the concepts of recursion and binary trees, including algorithms, data structures, and implementation. Topics include factorial, Ackermann function, Fibonacci sequence, finding the smallest element in an array, and traversing binary trees.

karlj
Download Presentation

AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering

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. AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Recursion and Binary Tree Course No.: 0511363 Fall 2014

  2. Recursion • Recursion: The process of solving a problem by reducing it to smaller versions of itself • Ex: The factorial of a natural number. • This is written as n! and pronounced "n factorial"

  3. Example • The factorial function can be calculated by a simple function based directly on the definition: intfact(int n) { ifn == 0 return 1; else return n * fact(n-1); }

  4. Ackermannfunction int A(int m, int n) { if (m == 0) return n+1; else if (n == 0 && m > 0) return A(m-1, 1); else if (m > 0 && n > 0) return A(m-1, A(m, n-1)); }

  5. Fibonacci Sequence • F=0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, . . • F(0)=0, F(1)=1, F(2)=1, F(3)=2,……. • We can write F(n) as: • Find F(5)? • Fibonacci Sequence Function: intf(unsigned n) { if(n < 2) return n; // basis else return f(n – 1) + f(n – 2) ; // recursion }

  6. Smallest Element in the Array #include<iostream.h> int Min(int[] , int ) ; main(){ const int size = 5 ; int x[size] = {9,3,10,2,7}; cout<<" The minimum element is: "<<Min(x,size )<<endl; } int Min(int x[], int size) { int min ; if(size==1) return x[size-1] ; else { min = Min(x , size-1) ; if(x[size-1] <= min) return x[size-1] ; else return min ; } }

  7. Binary Tree • A tree is a common hierarchical data structure used for many computer science applications. • Definition: A binary tree, T, is either empty or such that • T has a special node called the root node. • T has two sets of nodes, LT and RT, called the left subtree and right subtree of T, respectively. • LT and RT are binary trees. • Every node in a binary tree has at most two children

  8. Binary Tree • Example: • In figure: • (a), the root node is A, LA=empty, and RA= empty. • (b), the root node is A, LA= {B}, and RA= empty, The root node of LA = B, LB= empty, and RB= empty • (c), the root :A, LA= empty, RA= {C}. The root of Rc= C, Lc= empty, and Rc= empty.

  9. Definitions • A node in the binary tree is called a leaf if it has no left and right children. • The depth (Level) of a node A, in a binary tree is the length of the path from A to the root of the tree. Thus the root is at depth 0. • The depth of the tree is equal to the deepest leaf • The height of a nod A is the number of nodes on the longest path from A to a leaf. Thus all leaves are at height 0. If the binary tree is empty, the height is 0. • The height of the tree is equal to the height of root.

  10. Example • The Depth of C is 1, the height Of A is 3. • The Depth of H is 3,height (H) Is 0 • The depth of D is 2, the height of D is 1 • The Depth of the Tree is always equal to the height of the tree. • If there is a path from B to D, B is called an ancestor of D and D a descendant of B.

  11. Implementation of Binary Trees • To implement a tree, each node has its data and two pointers one to each child of the node. • The declaration of tree nodes is similar in structure to that for doubly linked list, in that a node is a structure consisting of the Information plus two pointers (Left and Right) to other nodes.

  12. Implementation of Binary Trees #include<iostream.h> #include<conio.h> structnode { intdata; node *left; node *right; }; node* insert(int x) { node* p = new node; p->data = x; p->left=NULL; p->right=NULL; return p; }

  13. Cont. void setl(node* p,int x) { if(p==NULL) cout<<"void insertion \n"; else if(p -> left !=NULL) cout<<" invalid insertion \n"; else p ->left = insert(x); cout<<" The inserted node is on the left of : "<<p->data<<endl; } void setr(node* p,int x) { if(p==NULL) cout<<"void insertion \n"; else if(p -> right !=NULL) cout<<" invalid insertion \n"; else p ->right = insert(x); cout<<" The inserted node is on the right of : "<<p->data<<endl; }

  14. Height function int height(node*tree) { if(tree == NULL) return -1; else return 1+max(height(tree ->left),height(tree ->right)); } int max(int &x,int &y) { if(x>=y) return x; else return y; }

  15. Create binary Tree main() { node* root; node* p,q; p=q=NULL; intnum =5; root = insert(num); setl(root,4); p= root-> left; setr(root,3); q= root-> right; setl(p,2); setr(p,1); setr(q,6); }

  16. Insertion Algorithm void insertAlg(node* tree,int x) { node* p=tree; node* prev=0; while(p!=0){ prev=p; if (x> p->data) p=p->right; else p=p->left; } if (tree==0) tree=insert(x); else if (x> prev->data) prev->right=insert(x); else prev->left=insert(x); }

  17. Traversal of Binary Trees • It is the moving through all the nodes of the binary tree, visiting each node in turn. • The key element in traversal orders is that to decide if we are to visit the node itself before traversing either subtrees or after traversing both subtrees. • In a traversal of a binary tree, each element of the binary tree is visited exactly once. • During the visit of an element, all action (make a clone, display, evaluate the operator, etc.) with respect to this element is taken.

  18. Traversal of Binary Trees Ways • There are 3 ways to traverse that binary tree: 1. Preorder traversal: ABC 2. Inorder traversal: BAC 3. Postorder traversal: BCA.

  19. Example • Preorder: 12345 • Inorder: 14352 • Postorder:45321

  20. a b c f e d j g h i Preorder Example • Traverse this tree in Preorder way • a b d g h e I c f j

  21. / * + e f + - a b c d Preorder Of Expression Tree / * + a b – c d + e f Gives prefix form of expression!

  22. Preorder Traversal Function void preorder(node* tree) { if(tree!=NULL) { cout<<tree->data<<" "; preorder(tree->left); preorder(tree->right); } }

  23. a b c f e d j g h i Inorder Example g d h b e I a f j c

  24. a b c f e d j g h i Inorder Example by projection g d h b e I a fjc

  25. / * + e f + - a b c d Inorder Of Expression Tree a + b * c - d / e + f Gives infix form of expression (without parentheses)!

  26. Inorder Traversal Function void inorder(node* tree) { if(tree!=NULL) { inorder (tree->left); cout<<tree->data<<" "; inorder(tree->right); } }

  27. a b c f e d j g h i Postorder Example g h d I e b j f c a

  28. / * + e f + - a b c d Postorder Of Expression Tree a b + c d - * e f + / Gives postfix form of expression!

  29. Postorder Traversal Function void postorder(node* tree) { if(tree!=NULL) { postorder(tree->left); postorder(tree->right); cout<<tree->data<<" "; } }

  30. Expression Trees • The leaves of an expression tree are “operands”. • The other nodes are operators. • This particular tree happens to be binary, because all of operations are binary. • We evaluate an expression tree, by applying the operator at the root to the values obtained recursively evaluating the left and right subtrees.

  31. Expression Tree construction • Suppose the input is: ab+cde+** • The first two symbols are operands, so create one-node tree and push pointers to them onto a stack. • Next “+” is read, so two pointers to trees are popped and a new tree is formed. • A pointer to the tree is pushed onto the stack.

  32. Cont. • Next, c, d and e are read and a for each a one-node tree is created and pushed onto the stack. • Next a “+” is read, the top two most tree pointers are popped and a new tree is formed. • A pointer to the new tree is pushed onto the stack.

  33. Cont. • Next, “*” is read the two pointers on top of the stack are popped and a new tree is formed. • A pointer for the new tree is pushed onto the stack.

  34. Cont. • Next, a “*” is read the top two pointers are popped and new is formed. • Its pointer is pushed onto the stack.

More Related