Binary Tree application example: Expression Processing

Source: Internet
Author: User

Binary Tree application example: Expression Processing

A natural way for computers to solve arithmetic expressions is to use the expression tree.

An Expression Tree is a binary tree. Its nodes contain two types of objects: operators and final values.

An operator is an object with an operand while the final value is an object without an operand.

Expression Tree idea: operators stored in parent nodes, whose operands are composed of child trees extended by their child nodes. The operands may be the final values, or they may be other expressions. The expression is expanded in the subtree, and the final value resides in the leaf node.

The advantage of this organizational structure is that the expression tree makes it very easy to convert an expression into three common forms of representation: prefix, infix, and suffix.

To obtain these representations, we only need to traverse the nodes of the Expression Tree in the forward, Central, or backward order.

 

To traverse the tree in the forward order. The obtained prefix expression is X/-74 10 32 + 23 17.

To calculate a prefix expression, we enclose each operator and the two operands following it in parentheses. Therefore, this prefix expression can be converted:

(X (/(-74 10) 32) (+ 23 17) = 80

Infix expressions are the most familiar expressions in mathematics, but they are not suitable for computer processing. If we traverse the trees in the graph in the ascending order, we can obtain the infix expression 74-10/32X23 + 17.

Note that the infix expressions cannot identify the order of calculation, but both prefix expressions and suffix expressions can. However, we can fix this problem by adding parentheses to each part of the expression during tree traversal. After all the brackets are filled, the infix expression can be converted:

(74-10)/32X(23 + 17) = 80

Suffix expressions are very suitable for computer processing. If you traverse the tree in the graph in the descending order, you can obtain the suffix expression 74 10-32/23 17 + X.

To calculate a back-order expression, enclose each operator and the two operands that are immediately prior to it in parentheses. In this way, the suffix expression will be converted:

(74 10-) 32/) (23 17 +) X) = 80

One reason that suffix expressions are suitable for computer processing is that they can be easily computed by abstract stack machine. To process a suffix expression in the form of an abstract state machine, take the following steps: first, traverse the expression from left to right and press the value into the stack until an operator is encountered. Then, the operands required by the operator are popped up, the operator is applied to the operand, and the result is re-pushed into the stack. This process will be repeated until the entire expression is processed. By then, the final result of the expression is the only element that remains in the stack (as shown in ).

 

Code example 1 describes how to generate prefix, infix, and suffix expressions through the expressions stored in the Expression Tree. For this purpose, three functions are provided: preorder, inorder, and postorder. They traverse the Expression Tree in the forward, middle, and backward directions. Each function accepts two parameters: node and list.

Set the parameter node to the root node of the expression tree to be traversed. The continuous recursive call sets the node as the root node of the subtree. When each function is called for the first time, an empty linked list initialized by list_init is sent to the parameter list. For each traversal, add them to the list of linked lists in the order of access nodes. When the first recursive call is finally returned, the list contains the nodes that are traversed in the forward, middle, and backward directions.

Example 1: implement a function for traversing a binary tree

 

/* Traverse. c */
# Include "list. h"
# Include "traverse. h"

/* Preorder traversal */
Int perorder (const BiTreeNode * node, Lis list)
{
/* Load the list and the pre-sequence table of the tree */
If (! Bitree_is_eob (node ))
{
If (list_ins_next (list, list_tail (list), bitree_data (node ))! = 0)
Return-1;

If (! Bitree_is_eob (bitree_left (node )))
If (preorder (bitree_left (node), list )! = 0)
Return-1;
If (! Bitree_is_eob (bitree_right (node ))
If (preorder (bitree_right (node), list )! = 0)
Return-1;
}
Return 0;
}

/* Sequential traversal in inorder */
Int inorder (const BiTreeNode * node, List * list)
{
/* Load the list and the central order list of the tree */
If (! Bitree_is_eob (node ))
{
If (! Bitree_is_ebo (bitree_left (node )))
If (inorder (bitree_left (node), list )! = 0)
Return-1;

If (list_ins_next (list, list_tail (list), bitree_data (node ))! = 0)
Return-1;

If (! Bitree_is_eob (bitree_right (node )))
If (inorder (bitree_right (node), list )! = 0)
Return-1;
}
Return 0;
}

/* Postorder sequential traversal */
Int postorder (const BiTreeNode * node, List * list)
{
/* Load the list and the post-tree sequence table */
If (! Bitree_is_eob (node ))
{
If (! Bitree_is_eob (bitree_left (node ))
If (postorder (bitree_left (node), list )! = 0)
Return-1;

If (! Bitree_is_eob (bitree_right (node ))
If (postorder (bitree_right (node), list )! = 0)
Return-1;

If (list_ins_next (list, list_tail (list), bitree_data (node ))! = 0)
Return-1;
}
Return 0;
}

This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151316.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.