CS210- Lecture 11 June 23, 2005 Agenda Applications of Traversals Expression Tree Evaluation Data Structures for representing trees A Vector based structure for Binary Trees A Linked structure for Binary Trees 6/30/2016 CS210-Summer 2005, Lecture 11 1 Expression Tree Evaluation A postorder traversal of the binary tree can be used to solve the expression tree evaluation problem. In this problem, we are given an arithmetic expression tree, that is, a binary tree where each external node has a value associated with it and each internal node has an arithmetic operation associated with it and we want to compute the value of the arithmetic expression represented by the tree. 6/30/2016 CS210-Summer 2005, Lecture 11 2 Picture of a Expression tree + * / 10 2 6/30/2016 3 CS210-Summer 2005, Lecture 11 4 3 Algorithm to evaluate expression tree Algorithm evaluateExpression(T, v): if T.isInternal(v) then let o be the operator stored at v X <- evaluateExpression(T, T.left(v)) Y <- evaluateExpression(T, T.right(v)) return x o y else return the value stored at v. 6/30/2016 CS210-Summer 2005, Lecture 11 4 Application of inorder traversal Most important application of inorder traversal arises when we store an ordered sequence of elements in a binary tree, defining a structure called binary search tree. 6/30/2016 CS210-Summer 2005, Lecture 11 5 Binary Search Trees Let S be a set whose elements have an order relation. For example, S could be a set of integers. A binary search tree for S is a proper binary tree such that: Each internal node v of T, stores an element of S, denoted by x(v). For each internal node v of T, the elements stored in the left subtree of v are less than or equal to x(v) and the elements stored in the right subtree of v are greater than or equal to x(v). The external nodes of T do not store any element. 6/30/2016 CS210-Summer 2005, Lecture 11 6 Binary Search Tree 7 13 3 2 11 An inorder traversal of the internal nodes of a binary search tree T visits the elements in nondecreasing order. 6/30/2016 CS210-Summer 2005, Lecture 11 7 A Vector based structure for binary trees A Simple structure for representing a binary tree T is based on a way of numbering the nodes of T. For every node v of T, let p(v) be the integer defined as follows: If v is the root of T, then p(v) = 1 If v is the left child of node u, then p(v) = 2p(u). If v is the right child of node u, then p(v) = 2p(u) + 1. 6/30/2016 CS210-Summer 2005, Lecture 11 8 A Vector based structure for Binary Trees 1 What if parent index =0 Parent ? 2 3 5 4 8 6/30/2016 9 10 7 6 11 12 13 CS210-Summer 2005, Lecture 11 14 15 9 A Vector based structure for Binary Trees 1 3 2 5 4 9 8 0 1 2 3 4 6/30/2016 5 6 7 10 8 7 6 11 12 13 14 15 9 10 11 12 13 14 15 16 CS210-Summer 2005, Lecture 11 10 A linked structure for Binary Trees A natural way to realize a binary tree T is to use a linked structure. parent right left element 6/30/2016 CS210-Summer 2005, Lecture 11 11 A linked structure for Binary trees ф root size 5 A ф ф B ф C ф ф ф D 6/30/2016 E CS210-Summer 2005, Lecture 11 12