Summary Lecture Exercises Programming Exercises Objectives

J.E.D.I Figure 1.54 Heapsort Example

4.8 Summary

• A binary tree is an abstract data type that is hierarchical in structure • A binary tree could be classified as skewed, strict, full or complete • Link representation is the most natural way to represent a binary tree • Three ways to traverse a tree are preorder, inorder, and postorder • Binary tree traversals could be used in applications such as duplication of a binary tree and checking the equivalence of two binary trees • The heapsort algorithm utilizes the heap ADT and runs in On lg n time Data Structures 69 J.E.D.I

4.9 Lecture Exercises

1. Traversal. Traverse the following tree in preorder, inorder and postorder. a b c Data Structures 70 J.E.D.I 2. Heapsort. Arrange the following elements in ascending order. Show the state of the tree at every step. a C G A H F E D J B I b 1 6 3 4 9 7 5 8 2 12 10 14

4.10 Programming Exercises

1. Stack may be used in the evaluation of expressions. An alternative approach is to use binary trees. Expressions as viewed by users are in infix form but postfix form is the one suitable for the computer to evaluate expressions. In this programming exercise, create an algorithm that will do the conversion of infix expression into its postfix form using binary tree. Five binary operations are present, and are listed here according to precedence: Operation Description Exponentiation highest precedence Multiplication and Division + - Addition and Subtraction In your implementation, consider the precedence and priority of operators. 2. Modify the heapsort algorithm to output the keys in descending order instead of ascending order by sifting up smaller keys instead of larger ones. Data Structures 71 J.E.D.I 5 Trees

5.1 Objectives

At the end of the lesson, the student should be able to: • Discuss the basic concepts and definitions on trees • Identify the types of trees: ordered, oriented and free tree • Use the link presentation of trees • Explain the basic concepts and definitions on forests • Convert a forest into its binary tree representation and vice versa using natural correspondence • Traverse forests using preorder, postorder, level-order and family-order • Create representation of trees using sequential allocation • Represent trees using arithmetic tree representations • Use trees in an application: the equivalence problem

5.2 Definitions and Related Concepts