Académique Documents
Professionnel Documents
Culture Documents
• Preorder
• Inorder
• In a traversal of a binary tree, each element of • Postorder
the binary tree is visited exactly once. • Level order
• During the visit of an element, all action (make
a clone, display, evaluate the operator, etc.)
with respect to this element is taken.
b c
* +
e f
f +
d e -
g j a b c d
h i
/ * +a b - c d +e f
abdghei cf j
Gives prefix form of expression!
Inorder Traversal Inorder Example (visit = print)
public static void inOrder(BinaryTreeNode t)
{ a
if (t != null)
{ b c
inOrder(t.leftChild);
visit(t); bac
inOrder(t.rightChild);
}
}
b c b c
f f
d e d e
g h i j g h i j
gdhbei af j c g d h b e i a f jc
b c b c
f
d e
bca
g h i j
ghdi ebj f ca
b c
* +
e f
f d e
+ -
g h i j
a b c d
• Make a clone.
a b +c d - * e f + / • Determine height.
Gives postfix form of expression! •Determine number of nodes.
b fjc
gdhbei fjc
gdh ei
Inorder And Preorder
a Inorder And Postorder
b fjc
• Scan postorder from right to left using
gdh ei
• preorder = a b d g h e i c f j inorder to separate left and right subtrees.
• d is the next root; g is in the left • inorder = g d h b e i a f j c
subtree; h is in the right subtree. • postorder = g h d i e b j f c a
a
• Tree root is a; gdhbei are in left subtree; fjc
b fjc
are in right subtree.
d ei
g h