Data structure practice--solving algebraic expressions with binary tree

Source: Internet
Author: User

This article is for the basic Data Structure series (6): Tree and two-fork tree matching practice.

"Project-solving algebraic expressions with binary trees"
Using a binary tree to represent algebraic expressions, each branch node of the tree represents an operator, and each leaf node represents an operand (for simplification, only two mesh is supported for the + 、-、 *,/, no parentheses, and the operand is just one digit character. This project only considers the entry in accordance with the above rules). Please design the algorithm, (1) According to the shape such as " 1+2?3?4/5 The string represents the expression, constructs the corresponding two-tree (), uses the thought of the post-order traversal to calculate the value of the expression, can reflect the first multiplication after the addition and subtraction of the rule, (2) to the construction of the two-fork tree, the expression value is calculated.

[refer to the answer] in the program btree.h, see Binary Tree Algorithm library.

#include <stdio.h>#include <string.h>#include <malloc.h>#include "btree.h"///s[i] to S[j] to construct a representation of a two-fork treeBtnode *crtree (CharS[],intIintj) {Btnode *p;intk,plus=0, Posi;if(I==J)//i and J are the same, meaning that there is only one character, constructed by a leaf node{p= (Btnode *)malloc(sizeof(Btnode));//Allocate storage spacep->data=s[i];//value is S[i]p->lchild=null; p->rchild=null;returnP }//The following is a case of i!=j     for(k=i; k<=j; k++)if(s[k]==' + '|| s[k]=='-') {plus++; Posi=k;//Last + or-position}if(plus==0)//No + or-the case (because if there is +,-, the front will be executed plus++)         for(k=i; k<=j; k++)if(s[k]==' * '|| s[k]=='/') {plus++;            Posi=k; }//above treatment takes precedence over +,-put on a higher level of two fork tree    //Due to future calculations, the use of post-sequential traversal ideas    //multiplication in the lower layer will be the first operation    //Thus embodies the "first multiplication and then plus minus" algorithms    //Create a branch node with the detected operator as the node value    if(plus!=0) {p= (Btnode *)malloc(sizeof(Btnode)); p->data=s[posi];//Node value is S[posi]P->lchild=crtree (s,i,posi-1);//Zuozi from s[i] to S[posi-1]P->rchild=crtree (s,posi+1, j);//Right subtree from s[poso+1] to S[j]        returnP }Else       //If there are no operators, returns null        returnNULL;}DoubleComp (Btnode *b) {DoubleV1,v2;if(B==null)return 0;if(B->lchild==null && B->rchild==null)//leaf node, should be a numeric character (this item does not consider an illegal expression)        returnb->data-' 0 ';The //leaf node directly returns the node value, and the number saved in the junction is in the form of a character, so the-' 0 'V1=comp (B->lchild);//Calculate left sub-tree firstV2=comp (B->rchild);//re-compute right sub-tree    Switch(B->data)//The results of left and right subtree operations are then calculated, using the idea of sequential traversal{ Case ' + ':returnV1+v2; Case '-':returnV1-v2; Case ' * ':returnV1*v2; Case '/':if(v2!=0)returnV1/v2;Else            Abort(); }}intMain () {Btnode *b;Chars[maxsize]="1+2*3-4/5";printf("Algebraic expression%s\n", s); B=crtree (s),0,strlen(s)-1);printf("corresponds to a two-fork tree:"); Dispbtnode (b);printf("\ n expression value:%g\n", Comp (b)); Destroybtnode (b);return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure practice--solving algebraic expressions with binary tree

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.