Pertemuan ke-4 - Binary tree and expression - 2101640795 - Michael Kesta
Binary Tree
In computer science, a binary tree is a tree data structure in which each node has at most two children, which are
referred to as the left child and the right child. A recursive definition using just set theory notions is
that a (non-empty) binary tree is a tuple (L, S, R), where L and R are binary trees or the empty set and S is a singleton set.[1] Some authors
allow the binary tree to be the empty set as well.
Types of
binary tree
·
A rooted
·
A full binary tree
·
A perfect binary tree
·
In a complete binary tree
every level
·
In the infinite complete binary tree
·
A balanced binary tree
·
A degenerate (or pathological) tree
Full binary tree
Complete binary tree
Binary Expression tree
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.
Traversal
An
algebraic expression can be produced from a binary expression tree by
recursively producing a parenthesized left expression, then printing out the
operator at the root, and finally recursively producing a parenthesized right
expression. This general strategy (left, node, right) is known as an in-order
traversal. An alternate traversal strategy is to recursively print
out the left subtree, the right subtree, and then the operator. This traversal
strategy is generally known as post-order traversal. A third strategy is to print
out the operator first and then recursively print out the left and right
subtree known as pre-order traversal.
Infix traversal
When
an infix expression is printed, an opening and closing parenthesis must be
added at the beginning and ending of each expression. As every subtree
represents a subexpression, an opening parenthesis is printed at its start and
the closing parenthesis is printed after processing all of its children.
Pseudocode:
Algorithm infix (tree)
/*Print the infix expression for an expression tree.
Pre : tree is a pointer to an expression tree
Post: the infix expression has been printed*/
if (tree not empty)
if (tree token is operator)
print (open parenthesis)
end if
infix (tree left subtree)
print (tree token)
infix (tree right subtree)
if (tree token is operator)
print (close parenthesis)
end if
end if
end infix
Postfix traversal
The
postfix expression is formed by the basic postorder traversal of any binary
tree. It does not require parentheses.
Pseudocode:
Algorithm postfix (tree)
/*Print the postfix expression for an expression tree.
Pre : tree is a pointer to an expression tree
Post: the postfix expression has been printed*/
if (tree not empty)
postfix (tree left subtree)
postfix (tree right subtree)
print (tree token)
end if
end postfix
Prefix traversal
The
prefix expression formed by prefix traversal uses the standard pre-order tree
traversal. No parentheses are necessary.
Pseudocode:
Algorithm prefix (tree)
/*Print the prefix expression for an expression tree.
Pre : tree is a pointer to an expression tree
Post: the prefix expression has been printed*/
if (tree not empty)
print (tree token)
prefix (tree left subtree)
prefix (tree right subtree)
end if
end prefix
Komentar
Posting Komentar