Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS210- Lecture 11 June 23, 2005 Agenda Applications of Traversals

Similar presentations


Presentation on theme: "CS210- Lecture 11 June 23, 2005 Agenda Applications of Traversals"— Presentation transcript:

1 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 1/26/2020 CS210-Summer 2005, Lecture 11

2 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. 1/26/2020 CS210-Summer 2005, Lecture 11

3 Picture of a Expression tree
+ * / 4 10 2 3 1/26/2020 CS210-Summer 2005, Lecture 11

4 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. 1/26/2020 CS210-Summer 2005, Lecture 11

5 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. 1/26/2020 CS210-Summer 2005, Lecture 11

6 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. 1/26/2020 CS210-Summer 2005, Lecture 11

7 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. 1/26/2020 CS210-Summer 2005, Lecture 11

8 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. 1/26/2020 CS210-Summer 2005, Lecture 11

9 A Vector based structure for Binary Trees
1 What if parent index = 0 Parent ? 2 3 7 5 6 4 13 14 15 9 10 11 12 8 1/26/2020 CS210-Summer 2005, Lecture 11

10 A Vector based structure for Binary Trees
1 3 2 7 5 6 4 13 14 15 9 10 11 12 8 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1/26/2020 CS210-Summer 2005, Lecture 11

11 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 1/26/2020 CS210-Summer 2005, Lecture 11

12 A linked structure for Binary trees
ф root size 5 A ф ф B C ф ф ф ф D E 1/26/2020 CS210-Summer 2005, Lecture 11


Download ppt "CS210- Lecture 11 June 23, 2005 Agenda Applications of Traversals"

Similar presentations


Ads by Google