Trees
•Basic terminology
• Binary Trees
• Binary tree representation
• algebraic Expressions
• Complete Binary Tree
• Extended Binary Trees
• Array and Linked Representation of Binary trees
• Traversing Binary trees
• Threaded Binary trees
• Traversing Threaded Binary trees
• Huffman algorithm
Basic terminology
• Trees are a kind of data structure that is similar to normal trees.
Data trees also have roots, branches and leaves. ‘A tree is a data
structure that represents hierarchical relationships between
individual data items.
• An element of a tree is called a node. A branch connects two
nodes. The node at the upper end of a branch is a predecessor
of the node at the lower end and the node at the lower end is
the successor of the predecessor.
• There is a unique node that has no predecessor called the root
of the tree. The nodes of the tree that have no branches are
called leaf nodes. The nodes that have branches are called non-
leaf nodes. Children of the same parent are called siblings.
• The degree of a node is the number of successors of the node.
Thus the degree of a leaf is 0. Level of a node is given by the
length of the unique path from the root to the node
Basic terminology
Expression Tree
• Expressions can be represented by tree structures
• Operands are terminal (leaf) nodes and operator are non terminal nodes
– E.g. Arithmetic Expression Tree
– A-(C/5 * 2) + (D*5 % 4)
+
- %
A * * 4
/ 2 D 5
C 5
Binary trees
• The tree in which every node has a maximum of two branches
is called a binary tree. Every parent has a maximum of two
children, ‘right’ child and ‘left’ child.
• A binary tree can have any number of nodes.
Binary trees
Binary trees
• A strictly binary tree is one in which every non-leaf node has
non-empty right and left subtrees. Following figure a gives a
strictly binary tree.
• A strictly binary tree with n leaves contains 2n-1 nodes
• A strictly binary tree with all its leaves at level d is termed as a
complete binary tree of depth d. It contains 2d leaves at the
level d. The tree contains 2d leaves and 2d-1 non-leaf nodes.
Binary trees
• Each node in the tree is assigned a unique number, which
gives the nodes position in the tree. In complete binary tree
for any node K, left and right children of the node K are 2*K
and 2*K+1 respectively and the parent of K is the node K/2.
• Depth = log2n +1 where n is the no of nodes.
• Any node at level less than d-1 has two sons
Representation of a binary tree in memory:
• The structure of each node a binary tree contains the data field, a pointer to the
left child and a pointer to the right child. The figure shows this structure
• This structure can be defined as follows:
struct tnode
{
struct tnode *left;
int data;
struct tnode *right;
};
• There are two ways by which we can represent a binary tree.
• Linked representation of a binary tree
• Array representation of a binary tree
Representation of a binary tree in memory:
• Array representation of binary trees:
When a binary tree is represented by arrays three separate
arrays are required. One array stores the data fields of the
trees. The other two arrays represents the left child and right
child of the nodes.
• A complete binary tree has a simple array representation.
Suppose we number the nodes from left to right, beginning at
the top and ending at the bottom. Then we can store the
various data items in the corresponding elements of an array.
Representation of a binary tree in memory:
• Linked representation of binary trees
• Binary trees can be represented by links, where each node
contains the address of the left child and the right child.
These addresses are nothing but kinds to the left and right
child respectively. A node that does not have a left or a
right child contains a NULL value in its link fields.
• Linked representation uses three parallel arrays, INFO, LEFT
and RIGHT and a pointer variable ROOT. Each node N of T
will correspond to a location K such that –
• INFO[K] contains the data at node N
• LEFT[K] contains the location of left child node N
• RIGHT[K] contains the location of right child node N
• ROOT will contain the location of root R of T
Representation of a binary tree in memory:
Representation of a binary tree in memory:
Traversing binary tree
• Processing the contents of nodes of tree necessitates the visiting of
the nodes of the tree. This is called traversing of the tree
• Preorder or depth-first order traversal
• In this traversal the root node is visited before visiting the two
subtrees. The order followed is given sequentially by the following.
• 1. Visit the root
2. Visit the nodes in the left subtree in preorder
3. Visit the nodes in the right subtree in preorder
• Inorder or symmetric order traversal
• Here the root node is visited in-between the left and right subtrees.
The order of traverse is given as follows.
• 1. Traverse the left subtree in inorder
2. Visit the root
3. Traverse the right subtree in inorder.
• Postorder or breadth-first order traversal
• Here the left and right subtrees are traversed before visiting the
root. Following gives the sequential order followed for traversal.
• 1. Visit the nodes of the left subtree in postorder
2. Traverse the right subtree in postorder
3. Visit the root
15
Traversing binary tree
preorder inorder postorder
A
B C
D E F G
A
B C
D E F G
A
B C
D E F G
A B D E C F G D B E A F C G D E B F G C A
16
Traversing binary tree
Void inorder(struct node *root)
{
if (root!=NULL)
{
inorder(root->left);
printf(“%d”,root->info);
inorder(root->right);
} }
Void preorder(struct node *root)
{
if (root!=NULL)
{
printf(“%d”,root->info);
preorder(root->left);
preorder(root->right);
} }
Traversing binary tree
Void postorder(struct node *root)
{
if (root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf(“%d”,root->info);
} }
Traversing using STACK
• Preorder
• Initially push NULL on to STACK and then set
PTR:=ROOT. Then repeat the following steps until
PTR=NULL.
• (a) proceed down the left most path rooted at
PTR, processing each node N on the path and
pushing each right child R(N), if any, onto STACK.
The traversing ends after a node N with no left
child L(N) is processed.
• (b) Pop and assign to PTR the top element on
STACK. If PTR!=NULL, then return to step(a)
otherwise Exit.
Traversing using STACK
• Inorder
• Initially push NULL on to STACK and then set
PTR:=ROOT. Then repeat the following steps until
NULL is popped from STACK.
• (a) proceed down the left most path rooted at
PTR, and pushing each node N onto STACK and
stopping when a node N with no left child is
pushed onto STACK.
• (b) Pop and process the nodes on STACK. If NULL
is popped, then exit. If a node N with right child
R(N) is processed, set PTR=R(N)and return to
step(a).
Traversing using STACK
• Postorder
• Initially push NULL on to STACK and then set
PTR:=ROOT. Then repeat the following steps until
NULL is popped from STACK.
• (a) proceed down the left most path rooted at
PTR, pushing each node N onto STACK and if N
has a right child R(N), push –R(N) onto STACK.
• (b) Pop and process positive nodes on STACK. If
NULL is popped, then exit. If negative node is
popped, that is, if PTR= -N for some node N, set
PTR=N and return to step(a).
Traversing binary tree
• Q: a binary tree T has 9 nodes. The inorder and postorder traversal
of T yield the following sequence of nodes
Inorder: E A C K F H D B G
Postorder: E C K A H B G D F
Draw the tree T.
• Ans: tree T is drawn from root downward as follows.
(a) the root of T is obtained by choosing the last node in its postorder.
Thus F is the root of tree.
(b) the left child of the node F is obtained as follows: first use the
inorder of T to find the nodes in the left subtree T1 of F. Thus T1
consists of nodes E, A, C, K. then the left child is obtained by
choosing the last node in the postorder of T1. thus A is the left child
of F.
(c)Similarly, the right subtree T2 of F consists of the nodes H, D, B, G
and D is the root of T2. thus D is the right child of F.
Repeating the above process with each new node , we obtain the
required tree.
Threaded Binary Trees
• The special link created to simplify the tree operation is
called the thread. The thread makes it possible to perform
traversals, insertions and deletions without using either
stack or recursion. Binary trees with threads are termed
threaded binary trees. Thread is formed by changing the
NULL link to special link.
• In implementing the threaded trees, each thread is
distinguished from the tree pointer by associating it with a
Boolean variable. The Boolean value tells if the link is a
pointer to the nonempty subtree or it is a link to a node
higher in the tree. The value is set to TRUE if the link is a
thread
• In a right in-threaded binary tree each NULL right link is
replaced by threads connecting the nodes to its in-order
successor. Following fig. shows right in-threaded trees. The
threads are shown in dotted lines. The NULL right pointer
of the he right most nodes K is not replaced by the thread
as it does not have inorder successor
Threaded Binary Trees
Threaded Binary Trees
• In sequential representation of binary tree, negative or
positive value of a variable is set to indicate whether the
link is a pointer or a thread. In the linked array
representation, a thread is represented by the negative
value of the link while the positive value is set to indicate
the pointer.
• In a left in-threaded tree the thread to the inorder
predecessor of the node replaces each NULL left pointer. A
binary tree with both left and right threads is called an in-
threaded binary tree
• A right in-threaded tree yields more advantages than left
in-threaded tree. Right and left pre-threaded binary trees
are the binary trees in which the NULL right and left
pointers of the nodes are replaced by their preorder
successors and predecessors respectively. These concepts
are made use of in the traversal of binary trees
Threaded Binary Trees
Huffman algorithm
• An extended binary tree is a transformation of any binary tree
into a complete binary tree.
• This transformation consists of replacing every null subtree of
the original tree with ``special nodes.'' The nodes from the
original tree are then internal nodes, while the ``special
nodes'' are external nodes.
• For instance, consider the following binary tree.
Equivalent extended binary tree
Huffman algorithm
• Empty circles represent internal nodes, and filled circles
represent external nodes
• Every internal node in the extended tree has exactly two
children, and every external node is a leaf. The result is a
complete binary tree.
• Number of external nodes(Ne) is 1 more than the number
of internal nodes(Ni).i.e Ne=Ni+1.
• External path length (Le) to be the sum of all path lengths
summed over each path from the root R to an external
node.
• Le = 2+3+3+3+3+3+3 = 20
• internal path length (Li) to be the sum of all path lengths
summed over each path from the root R to an internal
node.
• Li = 0+1+1+2+2+2 = 8
• Le = Li +2*n, where n is the number of internal nodes.
Huffman algorithm
• Suppose T is a 2-tree with n external nodes is assigned a
nonnegative weights. The external weighted path length P
of the tree T is defined to be the sum of the weighted path
lengths.
• P = W1L1 + W2L2+………+WnLn
• Where Wi and Li denote the weight and path length of
external node respectively.
• Huffman's algorithm is a method for building an extended
binary tree with a minimum weighted path length from a
set of given weights. Initially construct a forest of singleton
trees, one associated with each weight. If there are at least
two trees, choose the two trees with the least weight
associated with their roots and replace them with a new
tree, constructed by creating a root node whose weight is
the sum of the weights of the roots of the two trees
removed, and setting the two trees just removed as this
new node's children. This process is repeated until the
forest consists of one tree
Huffman algorithm
• Huffman algorithm: suppose W1 and w2 are two minimum
weigths among the n given weigths W1, W2, …….Wn. Find the
tree T’ which gives a solution for the n-1 weigths w1+W2, W3,
W4,………..Wn.
Then, in the tree T’, replace the external node
by the subtree
The new 2-tree T is the desired solution.
W1+W2
W1 W2
Huffman algorithm
• Let us work through an example for the set of weights
{1,2,3,3,4} . In these intermediate steps we will display the
temporary weights assigned by the algorithm in the circled
nodes. Initially our forest is
• During the first step, the two trees with weights 1 and 2 are
merged, to create a new tree
with a root of weight 3.
• We now have three trees with weights of 3 at their roots. It
does not matter which two we
choose to merge. Let us choose
the tree we just created and one
of the singleton nodes of weight 3.
Huffman algorithm
• Now our two minimum trees
are the two singleton nodes of
weights 3 and 4. We will combine
these to form a new tree of
weight 7.
• Finally we merge our last
two remaining trees
Huffman algorithm
• The result is an extended binary tree of minimum weighted
path length 29. In the following diagram each circled node is
marked with its weighted path length
CS 102
Building a Tree
E
1
i
1
y
1
l
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
CS 102
Building a Tree
E
1
i
1
y
1
l
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
CS 102
Building a Tree
E
1
i
1
y
1
l
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
CS 102
Building a Tree
E
1
i
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
CS 102
Building a Tree
E
1
i
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
CS 102
Building a Tree
E
1
i
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
CS 102
Building a Tree
E
1
i
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
CS 102
Building a Tree
E
1
i
1
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
CS 102
Building a Tree
E
1
i
1
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
CS 102
Building a Tree
E
1
i
1
sp
4
e
82
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4 4
CS 102
Building a Tree
E
1
i
1
sp
4
e
82
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4 4
6
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4 4 6
What is happening to the characters with a low number of occurrences?
CS 102
Building a Tree
E
1
i
1
sp
4
e
82
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6
8
CS 102
Building a Tree
E
1
i
1
sp
4
e
82
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6 8
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6
8
10
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2r
2
s
2
4
n
2
a
2
4 4
6
8 10
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6
8
10
16
CS 102
Building a Tree
E
1
i
1
sp
4
e
82
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6
8
10 16
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6
8
10
16
26
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6
8
10
16
26
General tree
• A general tree will have more than two children or subtrees
unlike binary tree. It is a finite nonempty set of nodes. There
is no upper limit on the degree of a node
• Conversion of general tree to a binary tree
• Suppose T is general tree and T’ is correspondent binary tree
• Nodes in T and T’ will be same.
• Root of T’ will be the root of T.
General tree
• Left child of node N in T’ will be the first child of node N in the
general tree T and the right child in of node N in T’ will be the
next sibling of N in T.