Trees and Binary Relations
Trees and Binary Relations
Debdeep Mukhopadhyay
IIT Madras
Outline
• Tree : definition and its relation with binary
relations
• Properties
• Expressions:
– Balancedness
– Infix, Postfix and Prefix
• Expression Trees
• Traversal of Trees: Inorder, Postorder and
Preorder
Trees and Relations
• They are an important class of binary
relations
• They have many applications in CS: data
structures, design of algorithms, analyzing
networks etc
• Can be expressed and formalized using
Directed Graphs (Digraphs)
Trees and Relations
• A Tree is a digraph with a nonempty set of
nodes such that:
– (i) there is exactly one node, called the root, which
has indegree 0
– (ii) every node other than the root has indegree 1
– (iii) for every node a of the tree, there is a directed
path from the root to a.
• Alternate Defn: A tree is a connected, directed
acyclic graph (see your graph theory text book)
Examples
Trees
Not-Trees
Terms
Restriction on the paths help to
traverse all the nodes of the tree Root
algorithmically and perform more
efficient searches than general
digraphs.
Father
Leaf
Height=2
Son
Properties
• They are many
• Few important results:
– There is a unique directed path from the root r
to any node a.
abc ( abc*+d
* +
+
*
abc*+de
(
+
Example
• a+b*c+(d*e+f)*g
+ abc*+de*f+g
abc*+de*f
( *
+ +
abc*+de*f+ abc*+de*f+g*+
+
Expression Trees
• Expression Trees are binary trees and compact
representations of expressions
• We shall discuss a technique to convert postfix
expressions to trees.
• Method:
– If the symbol is an operand, create a one-node tree
and push its pointer to a stack.
– If the symbol is an operator, we pop twice to obtain
pointers T1 and T2 to two trees.
– If T1 is first popped, then form a tree with the operator
as root. The left child points to T2 and right to T1.
Then push the new pointer to the stack.
Example
abc*+de*f+g*+
+ d e
a b c
a *
b c
a *
b c
Example
f + g
+ * +
* f
d e
a * a *
d e
b c b c
Example
*
+
+
+ g
*
* +
a * f
+ g
b c d e
a * f
*
b c d e
Height of the tree
• A binary tree with n nodes, n>0, is of height at
least ⎢⎣log n ⎥⎦
• Once, the tree is constructed we need to
traverse the tree.
• There are three distinct methods for traversal:
– Inorder
– Preorder
– Postorder
• The techniques may be recursively defined
Inorder
1. Traverse the left tree(wrt root), in order.
2. Traverse the root.
3. Traverse the right tree(wrt root), in order.
3 5
Preorder
1. Traverse the root.
2. Traverse the left tree(wrt root), in order.
3. Traverse the right tree(wrt root), in order.
4 5
Postorder
1. Traverse the left tree(wrt root), in order.
2. Traverse the right tree(wrt root), in order.
3. Traverse the root.
2 3
Traversing the expression tree
• Postorder
Traversal +
• abc*+de*f+g*+ *
+
(gives back the + g
postfix
*
expression) a * f
b c d e
Evaluate the expression tree
+
• Use Recursion
1. If the root is an
*
operand, return +
value. g
+
2. Else evaluate the left
tree. *
a * f
3. Evaluate the right
tree.
b c d e
4. Apply the operator on
the two returned
results.
Result: a+b*c+(d*e+f)*g
5. Return the result.
A possible representation
• #define OPERATOR 0
• #define OPERAND 1
• struct nodetype{
shortint utype;
union{
char chinfo;
float numinfo;
}info;
struct nodetype *left;
struct nodetype *right;
};
typedef struct nodetype *NODEPTR;