Presentation is loading. Please wait.

Presentation is loading. Please wait.

AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.: 0511363.

Similar presentations


Presentation on theme: "AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.: 0511363."— Presentation transcript:

1 AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.: 0511363 Fall 2014

2 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 I.T has a special node called the root node. II.T has two sets of nodes, LT and RT, called the left subtree and right subtree of T, respectively. III.LT and RT are binary trees. Every node in a binary tree has at most two children

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

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

5 Example The Depth of C is 1, the height Of A is 2. 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. Because there is a path from B to D, so B is called an ancestor of D and D a descendant of B.

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

7 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" The factorial recursive definition is:

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

9 Ackermann function 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)); }

10 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: int f(unsigned n) { if(n < 2) return n; // basis else return f(n – 1) + f(n – 2) ; // recursion }

11 Smallest Element in the Array #include 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 ; }

12 Implementation of Binary Trees #include struct node { int data; node *left; node *right; }; typedef node* ptr; ptr insert(int x) { ptr p = new node; p->data = x; p->left=NULL; p->right=NULL; return p; }

13 Cont. void setl(ptr 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 data<<endl; } void setr(ptr 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 data<<endl; }

14 Height function int height(ptr tree) { if(tree == NULL) return 0; 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() { ptr root; ptr p,q; p=q=NULL; int num =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 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.

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

18 Example Preorder: 12345 Inorder: 14352 Postorder:45321

19 Preorder Example Traverse this tree in Preorder way a b d g h e I c f j a bc d e f g hi j

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

21 Preorder Traversal Function void preorder(ptr tree) { if(tree!=NULL) { cout data<<" "; preorder(tree->left); preorder(tree->right); }

22 Inorder Example g d h b e I a f j c a bc d e f g hi j

23 Inorder Example by projection g d h b e I a fjc a bc d e f g hi j

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

25 Postorder Traversal Function void inorder(ptr tree) { if(tree!=NULL) { inorder (tree->left); cout data<<" "; inorder(tree->right); }

26 Postorder Example g h d I e b j f c a a bc d e f g hi j

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

28 Postorder Traversal Function void postorder(ptr tree) { if(tree!=NULL) { postorder (tree->left); postorder(tree->right); cout data<<" "; }

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

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

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

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

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


Download ppt "AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.: 0511363."

Similar presentations


Ads by Google