2,017,408 algorithm problem

Thought: the expression tree traversal sequence with the necessary brackets is the infix expression. Expression outermost layer (corresponding to the root node) i.e. operand (i.e. leaf nodes) need to add parentheses

Code:

typedef struct the Node { 
    char Data [10]; 
    struct Node * left, * right; 
} the BTree; 
void BtreetToE (the BTree * the root) 
{ 
    BtreeToExp (the root,. 1); 
} 
void BtreeToExp (the BTree * the root, int Deep) 
{ 
    IF ( root == Null) return; // null node returns 
    the else IF (directory root-> left == NULL && directory root-> right == Null) 
        the printf ( "% S", directory root-> Data); // output operand, without brackets 
    the else { 
        IF (Deep>. 1 the printf ( "(")); // add a layer of sub-expressions in parentheses if 
         BtreeToExp (directory root-> left, Deep +. 1); 
        the printf ( "% S", directory root-> Data ); 
        BtreeToExp (directory root-> right, Deep +. 1); 
        IF (Deep>. 1) the printf ( ")"); // add a layer if sub-expressions in parentheses 
    } 
}

  

Guess you like

Origin www.cnblogs.com/yangmenda/p/11705100.html