Presentation is loading. Please wait.

Presentation is loading. Please wait.

CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.

Similar presentations


Presentation on theme: "CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics."— Presentation transcript:

1 CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics

2 Preliminaries - I (Recursive) Definition: A tree is a collection of nodes. The collection can be empty; otherwise, a tree consists of a distinguished node r, called the root, and zero or more nonempty (sub)trees T 1, T 2,..., T k, each of whose roots are connected by a directed edge from r. The root of each subtree is said to be a child of r, and r is the parent of each subtree root. Izmir University of Economics2

3 Preliminaries - II Observation: For a tree with N nodes, there are N-1 edges. Proof: Each edge connects a node to its parent and every node except the root has one parent. Izmir University of Economics3

4 Preliminaries - III The root is A. Node F has A as a parent and K, L, and M as children. Each node may have an arbitrary number of children, possibly zero. Nodes with no children are known as leaves (leaf node). B, C, H, I, P, Q, K, L, M, and N are leaves. Nodes with the same parent are siblings; thus K, L, and M are all siblings. Grandparent and grandchild relations can be defined in a similar manner. Izmir University of Economics4

5 Preliminaries - IV A path from node n 1 to n k is defined as a sequence of nodes n 1, n 2,..., n k such that n i is the parent of n i+1 for 1 ≤ i < k. The length of this path is the number of edges on the path, namely k - 1. There is a path of length zero from every node to itself. Notice that there is exactly one path from the root to each node. For any node n i, the depth of n i is the length of the unique path from the root to n i. Thus, the root is at depth 0. The height of n i is the length of the longest path from n i to a leaf. Thus all leaves are at height 0. The height of a tree is equal to the height of the root. The depth of a tree is equal to the depth of the deepest leaf which is always equal to the height of the tree. If there is path from n 1 to n 2 (n 1 ≠n 2 ), then n 1 is an (proper) ancestor of n 2 and n 2 is a (proper) descendant of n 1. Izmir University of Economics5

6 Implementation of Trees One way is to have in each node besides its data, a link to to each child of that node. However, since the number of children per node can vary greatly and is not known in advance, it might be infeasible. The solution is simple: keep the children in a linked list of tree nodes. Izmir University of Economics6

7 Tree Traversals with an Application - I One popular use is the directory structure in many common operating systems. The root of this directory is /usr. (The asterisk next to the name indicates that /usr is itself a directory.) /usr has three children, mark, alex, and bill, which are themselves directories. Thus, /usr contains three directories and no regular files. The filename /usr/mark/book/ch1.r is obtained by following the leftmost child three times. Each / after the first indicates an edge; the result is the full pathname. Izmir University of Economics7

8 Suppose we would like to list the names of all of the files in the directory. Our output format will be that files that are at depth d will have their names indented by d tabs. The strategy below is known as preorder traversal. In a preorder traversal, work at a node is performed before (pre) its children. If there are N file names, then the running time is O(N). Izmir University of Economics8 Tree Traversals with an Application - II void list_directory (DirEntry* D){ listAll ( D, 0 ); } void listAll (DirEntry* D, unsigned int depth){ if ( D != NULL){ printName ( depth, D->name ); if( isDirectory(D)) for each child c of D listAll ( c, depth+1 ); }

9 Another common method of traversing a tree is the postorder traversal. With it, the work at a node is performed after (post) its children are evaluated. As an example, the same directory structure as before, is represented with the numbers in parentheses (the number of disk blocks taken up by each file). Since the directories are themselves files, they have size too. The running time of calculating the size each node is O(N) again. Izmir University of Economics9 Tree Traversals with an Application - III unsigned int size(DirEntry* D){ unsigned int totalSize = 0; if(D != NULL){ totalSize=sizeOfThisFile(D); if( isDirectory(D) ) for each child c of D totalSize += size( c ); } return( totalSize ); }

10 Binary Trees A binary tree is a tree in which no node can have more than two children. Subtrees T L and T R which could both be possibly empty. The depth of an average binary tree is considerably smaller than N ( actually O( ) ). For a special type of binary tree, namely the binary search tree, the average value of depth is O (log N). Unfortunately it can be as large as N - 1. Izmir University of Economics10

11 Implementation Izmir University of Economics11 typedef struct tree_node *tree_ptr; struct tree_node { element_type element; tree_ptr left; tree_ptr right; }; typedef tree_ptr TREE; Because a binary tree has at most two children, we can keep direct links to them. The declaration of tree nodes is similar in structure to that for doubly linked lists, in that a node is a structure consisting of the element information plus two pointers (left and right) to other nodes. Trees are generally drawn as circles connected by lines. NULL links are not explicitly drawn. Why? A binary tree with N nodes, has N+1 NULL links. Proof: N i : #nodes with i children N 0 +N 1 +N 2 =N (1) // #nodes N 1 +2*N 2 =N-1 (2) // #edges multiply (1) by 2 and subtract (2) 2*N 0 +N 1 =2*N-(N-1)=N+1

12 Izmir University of Economics12 An Example: Expression Trees The leaves of an expression tree are operands, such as constants or variable names, and the other nodes contain operators. Operator nodes might have 1 (unary minus), 2, or more than two children. We can evaluate an expression tree, T, by applying the operator at the root to the values obtained by recursively evaluating the left and right subtrees. Inorder traversal: produce an infix expression by first recursively processing left, then, output the operator at the root, and finally recursively process the right. the left subtree evaluates to a + (b * c) and the right subtree evaluates to ((d *e) + f )*g. The entire tree therefore represents (a + (b*c)) + (((d * e) + f)* g).

13 We now give an algorithm to convert a postfix expression into an expression tree. Since we already have an algorithm to convert infix to postfix, we can generate expression trees from the two common types of input. The method we describe strongly resembles the postfix evaluation algorithm of Section 3.2.3. Constructing an Expression Tree - I 13Izmir University of Economics stack=  ; while (!EndOf(expression)){ read(ch); if (isOperand(ch)) push(MakeNode(ch, NULL, NULL), stack); else if (isOperator(ch)){ T1=topAndPop(stack); T2=topAndPop(stack); push(MakeNode(ch, T2, T1), stack); } MakeNode(data,left,right) creates a struct tree_node and returns a pointer to it

14 Izmir University of Economics14 Constructing an Expression Tree - II As an example, suppose the input is: a b + c d e + * * a b + c d e + * *

15 Izmir University of Economics15 Constructing an Expression Tree - III a b + c d e + * *

16 Homework Assignments 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.8, 4.31, 4.33, 4.40, 4.43, 4.45, You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics16


Download ppt "CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics."

Similar presentations


Ads by Google