trees_2 - Trees II Non-Recursive PreOrder Traversal...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Trees – II Non-Recursive PreOrder Traversal Expression Trees Binary Search Tree - Searching - Insertion - Traversal - Creation
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Carry out a pre-order traversal of this tree void preorder(struct tree_node * p) { if (p !=NULL) { printf(“%d”, p->data); preorder(p->left_child); preorder(p->right_child); } } M P R Q T M P Q R T
Background image of page 2
Preorder Traversal: M P R S Q T M P Q R T S
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Carry out in-order traversal of this tree void inorder(struct tree_node *p) { if (p !=NULL) { inorder(p->left_child); printf(“%d\n”, p->data); inorder(p->right_child); } } IN ORDER TRAVERSAL: R P M T Q M P Q R T
Background image of page 4
In ORDER TRAVERSAL : R P S M T Q M P Q R T S
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Non Recursive implementation of preorder tree traversal In a preorder traversal, we visit the root node first, then we visit its left subtree (all the nodes) and finally visit its right subtree. How do we actually visit all the nodes of a subtree? We can write a non-recursive algorithm by making use of a stack. To start with we push the root node on the stack. Then we push the right child and the left child of the current node on a stack recursively. Then we pop them and print them. Look at this implementation: *p = root; if (p != NULL) { push(p); while ( stack not empty) { p = pop stack; printf(“%d “,p->data); if ( p->right_child != NULL) push ( p-> right_child); if (p ->left_child != NULL) push (p -> left_child); } } /* note the left child is pushed on top of right node, so that it gets popped up first */
Background image of page 6
Non Recursive Preorder Traversal: M P Q R T S
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Stack position Popped values M M Q P Q M P Q S R Q S M P R Q M P R S M P R S Q T - M P R S Q T
Background image of page 8
Expression Trees Many compilers make use of trees, as they serve as ideal representations for the hierarchical structure of a program. Design of a compiler is a complex process, which you shall learn in a later course. Let us look at one particular aspect of compiler, which deals with evaluation of arithmetic expressions. An arithmetic expression can be represented as a binary tree. Such a tree is called an Expression Tree. An expression tree is a binary tree representing an arithmetic expression where the leaf nodes of the tree represent the operands (variables or constants) and the internal nodes and the root node represent the operators. Constructing an Expression Tree Constructing an expression tree involves two parts: Lexical Analysis: Dividing the input into tokens, each of which represents either integer constant, an operator or a variable name. Parsing: Determining if the individual tokens represent a
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 27

trees_2 - Trees II Non-Recursive PreOrder Traversal...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online