Arithmetic expression-Binary Tree
What first raised the traversal problem was to evaluate the expressions stored in the computer. Example: (a +B× (C-d)-e/f. The expression is represented in a tree, 8-11-1. Operators are placed in the tree at non-terminal nodes, and operands are placed at leaf nodes.
After we traverse the binary tree in ascending, descending, and descending order, we can get the prefix, infix, and suffix of the expression:
Prefix:-+ a * B-cd/ef
Infix: a + B * c-d-e/f
Suffix: abcd-* + ef /-
The infix is the normal form of an arithmetic expression, but there are no parentheses. In a computer, suffix expressions are easy to evaluate.
Example 1Enter an arithmetic expression to determine whether the expression is valid. If the expression is invalid, an error message is displayed. If the expression is valid, the Expression Tree of the legal expression is output.
[Algorithm analysis] There are three situations where the expression is invalid: ① The left and right parentheses do not match; ② the variable name is invalid; ③ The operators do not have the variables or numbers involved in the operation.
The analysis Expression Tree shows that the root node of the expression and the root node of its child tree are operators. The order in the tree is from the back to the front in order of operation, the leaf of the Expression Tree is the variable or number involved in the operation.
| |
| |
For example, expression: a + (B-c)/d
Operation Sequence:③①②
|
Expression Tree 8-11-2
When processing, first find the operator "+"As the root node, and then determine that the left and right subtree nodes of the root node range in the expression string isAnd (B-c)/dThen, find the operator with the lowest operation level in the corresponding range as the root node of the subtree until there is no operator in the range, then the remaining variable or number is the leaf of the Expression Tree.
[Algorithm step]
①Set array exStores the characters of the expression string, ltRtAs the left and Right pointer of the node, the variable left, RightStores the Left and Right bounds of each character range.
②Set the initial value of the Left Border to 1.; The initial value in the right field is the string length.
③Determines whether the Left and Right brackets match. If the left and right brackets do not match, the input is incorrect.
④Searches for operators with the lowest operation level within the Left and Right boundaries of the expression, and determines whether the operators are involved in the calculation of variables or numbers. If not, the input expression is invalid. If yes, as the root node of the current subtree, set the left subtree pointer and Its left and right bounds, and set the right subtree pointer and Its left and right bounds.
⑤If the expression has no operator in the left and right bounds, It is a leaf node and determines whether the variable name or number is valid.
⑥Turn ④ until the expression characters are obtained.
H In the source program, D, WIt is used to store the coordinates of the node when drawing a text.
Program exptree;
Uses crt;
Type
Point = ^ tree;
Tree = record
Data: string;
Lt: point;
Rt: point;
End;
Var
N, len, k: integer;
Ex: string;
Letters: set of char;
Root: point;
Procedure
Error (er: byte );{Error message prompt}
Begin
Write ('enter error :');
Case er
1: writeln ('no letter ');
2, 3: writeln ('no expressint ');
4: writeln ('no +, *,-or /');
5: writeln ('no (or )');
End;
Write ('Press <enter>... '); readln; halt (1 );
End;
Procedure
Create (left, right: integer; var p: point );
Var q: point;
K, n: integer;
Begin {Find the operator with the lowest operation level}
If ex [left] = '('then
Begin
N: = left + 1; k: = 0;
While (n <right) and (k> = 0) do
Begin
If ex [n] = '('then inc (k );
If ex [n] = ')' then dec (k );
Inc (n );
End;
If n = right then
Begin
Dec (right); inc (left );
End;
End;
If right <left then error (1 );
N: = right; k: = 0;
Repeat
If ex [n] = ') 'Then inc (k );
If ex [n] = '('then dec (k );
Dec (n );
Until (ex [n] = '+') or (ex [n] = '-') and
(K = 0) or (n <left );
If n = left then error (3 );
If n> left then
Begin
With p ^ do
Begin
Data: = ex [n];
New (q); lt: = q;
New (q); rt: = q;
End;
Create (left, n-1, p ^. lt );
Create (n + 1, right, p ^. rt );
End
Else
{Not found '+ ''-'}
Begin
N: = right;
Repeat
If ex [n] = ') 'Then inc (k );
If ex [n] = '('then dec (k );
Dec (n );
Until (ex [n] = '*') or (ex [n] = '/') and
(K = 0) or (n <left );
If n = left then error (3 );
If n> left then
Begin
With p ^ do
Begin
Data: = ex [n];
New (q); rt: = q;
New (q); lt: = q;
End;
Create (left, n-1, p ^. lt );
Create (n + 1, right, p ^. rt );
End
Else
{Only string}
Begin {Returns the string of the leaf node}
For k: = left to right do
If not (ex [k] in letters) then
Error (1 );
P ^. data: = '';
For k: = left to right do
P ^. data: = p ^. data + ex [k];
P ^. lt: = nil;
P ^. rt: = nil;
End;
End;
End;
Procedure
Pr_tree (w, dep: integer; p: point );{Draw the generated Expression Tree}
Var h, I, lt, rt: integer;
Begin
H: = 40; for I: = 1 to dep do h: = h div 2;
Gotoxy (W-1, dep * 3); write (', p ^. data ,')');
If p ^. lt = nil then lt: = w
Else begin
Lt: = w-h; pr_tree (lt, dep + 1, p ^. lt)
End;
If p ^. rt = nil then rt: = w
Else begin
Rt: = w + h; pr_tree (rt, dep + 1, p ^. rt );
End;
If lt <> rt then
Begin
Gotoxy (w, dep * 3 + 1); write ('| ');
Gotoxy (lt, 3 * dep + 2); write ('| ');
For I: = lt to rt-2 do
Write ('-'); write ('| ');
End;
End;
Begin
Clrscr;
Letters: = ['A'... 'Z', 'A'... 'Z', '0'... '9'];
Repeat
Write ('enter expression: '); readln (ex );
Len: = length (ex)
Until len> 0;
N: = 1;
K: = 0;
While (n <= len) and (k> = 0) do {Determine whether the left parenthesis matches}
Begin
If ex [n] = '('then inc (k );
If ex [n] = ')' then dec (k );
Inc (n );
End;
If k <> 0 then error (5 );
New (root); create (1, len, root );
Pr_tree (40, 1, root); readln
End.
Note: PascalIn the program of the language, by using uses at the beginning of the programCommand to describe the units to be used. Its syntax is:
Uses <Unit Table>;{The Unit table is 1 separated by commas (,).Unit names}
Crt {Screen mode control, extended keyboard code, color, window, sound, and other functions}
Clrscr {Clear the current window or screen, and the cursor returns to the upper left corner}
What is Bit-map?
Bit-map uses a bit to mark the Value corresponding to an element, and the Key is the element. Because Bit is used to store data, the storage space can be greatly reduced.