Английская Википедия:Binary expression tree

Материал из Онлайн справочника
Перейти к навигацииПерейти к поиску

Шаблон:Short description A binary expression tree is a specific kind of a binary tree used to represent expressions. Two common types of expressions that a binary expression tree can represent are algebraic[1] and boolean. These trees can represent expressions that contain both unary and binary operators.[1]

Like any binary tree, each node of a binary expression tree has zero, one, or two children. This restricted structure simplifies the processing of expression trees.

Construction of an expression tree

Example

The input in postfix notation is: a b + c d e + * * Since the first two symbols are operands, one-node trees are created and pointers to them are pushed onto a stack. For convenience the stack will grow from left to right.

Файл:Exp-tree-ex-2.svg
Stack growing from left to right

The next symbol is a '+'. It pops the two pointers to the trees, a new tree is formed, and a pointer to it is pushed onto the stack.

Файл:Exp-tree-ex-3.svg
Formation of a new tree

Next, c, d, and e are read. A one-node tree is created for each and a pointer to the corresponding tree is pushed onto the stack.

Файл:Exp-tree-ex-6.svg
Creating a one-node tree

Continuing, a '+' is read, and it merges the last two trees.

Файл:Exp-tree-ex-7.svg
Merging two trees

Now, a '*' is read. The last two tree pointers are popped and a new tree is formed with a '*' as the root.

Файл:Exp-tree-ex-8.svg
Forming a new tree with a root

Finally, the last symbol is read. The two trees are merged and a pointer to the final tree remains on the stack.[2]

Файл:Exp-tree-ex-9.svg
Steps to construct an expression tree a b + c d e + * *

Algebraic expressions

Файл:Exp-tree-ex-12.svg
Binary algebraic expression tree equivalent to ((5 + z) / -8) * (4 ^ 2)

Algebraic expression trees represent expressions that contain numbers, variables, and unary and binary operators. Some of the common operators are × (multiplication), ÷ (division), + (addition), − (subtraction), ^ (exponentiation), and - (negation). The operators are contained in the internal nodes of the tree, with the numbers and variables in the leaf nodes.[1] The nodes of binary operators have two child nodes, and the unary operators have one child node.

Boolean expressions

Файл:Exp-tree-ex-13.svg
Binary boolean expression tree equivalent to ((true <math>\lor</math> false) <math>\land</math> <math>\neg</math>false) <math>\lor</math> (true <math>\lor</math> false))

Boolean expressions are represented very similarly to algebraic expressions, the only difference being the specific values and operators used. Boolean expressions use true and false as constant values, and the operators include <math>\land</math> (AND), <math>\lor</math> (OR), <math>\neg</math> (NOT).

See also

References

Шаблон:Reflist

  1. 1,0 1,1 1,2 Шаблон:Cite web
  2. Ошибка цитирования Неверный тег <ref>; для сносок Gopal2010.353 не указан текст