Tree
Tree
3
ROOT Level 0
Level 1
Level 2
v v v
v v v Level 3
Fig: A Tree
4
Terminologies
Node: Each data item in a tree is called a node. It is the basic
structure in a tree. It specifies the data and links (branches) to
other data items. There are 13 nodes in the above tree.
5
Terminologies…
Degree of a tree: It is the maximum degree of nodes in a given
tree. In the above tree, the degree of node A is 3 and another
node I also have degree 4. In the whole tree, this value is the
maximum. So, the degree of the above tree is 4.
6
Terminologies…
Siblings: The children nodes of a given parent node are called
siblings. They are also called brothers. In the above tree,
E and F are siblings of parent node B
K, L and M are siblings of parent node I
Level: The entire tree structure is leveled in such a way that the
root node is always at level 0. Then its immediate children are at
level 1 and their immediate children are at level 2 and so on up to
the terminal nodes. In general, if a node is at level n, then its
children are at level n+1. In the above tree, there are four levels
from level 0 to level 3.
7
Terminologies…
Path: It is a sequence of consecutive edges from the source
to the destination node. In the above tree, the path
between A and J is given by the node pairs: (A, B), (B, F)
and (F, J).
8
Binary Tree
A binary tree is the most commonly used non-linear
data structure.
A binary tree is a finite set of data items which is either
empty or is partitioned into three disjoint subsets. The
first subset consists of a single data item called the
root of the binary tree. The other two subsets are
themselves binary trees called the left subtree and
right subtree of the original binary tree.
In a binary tree, the maximum children of any node is
at most two.
9
ROOT Level 0
Level 1
v v Level 2
v v Level 3
Note:
If A is the root of a binary tree and B is the root of its left or right subtree, then A is
called father of B and B is called left or right son of A.
A node that has no sons (such as H, I, F or J) is called a leaf.
Node n1 is an ancestor of node n2 (and n2 is a descendant of n1) if n1 is either the
father of n2 or the father of some ancestor of n2. For example, in the above binary
tree A is an ancestor of I, and J is a descendant of C, but E is neither an ancestor nor
a descendant of C.
A node n2 is a left descendant of node n1 if n2 is either the left son of n1 or a
descendant of the left son of n1. Similarly right descendant can be defined.
11
Types of Binary Trees
Strictly Binary Tree
Complete Binary Tree
12
Strictly Binary Tree
If every non-terminal node in a binary tree consists of
non-empty left and right subtrees, then such a tree is
called strictly binary tree.
In this binary tree, all
the non-terminal
nodes such as C and D
have non-empty left
and right subtrees.
Note: A complete
binary tree of depth d
contains a total of
(2d+1-1) nodes with 2d
leaves at level d and
2d-1 non-leaf nodes.
v v v v
15
1
2 3
4 v 5 v 6 v 7 v
8 9 v v 10
17
Is the following an almost complete binary tree?
v v v
18
Is the following an almost complete binary tree?
v v v v
19
Binary Search Tree (BST)
A binary search tree is a binary tree that is either
empty or in which each node possesses a key that
satisfies the following three conditions:
i. For every node X in the tree, the values of all the keys
in its left subtree are smaller than the key value in X.
ii. Keys in its right subtree are greater than the key value
in X.
iii. The left and right subtrees of the root are again binary
search trees.
20
v v
v v
v v v
v
22
C Implementation of BST
For the implementation of a binary search tree, a binarysearchtree
template is defined using a self-referential structure in linked list
format and after this, nodes of the binary search tree are inserted and
removed using dynamic memory allocation functions like malloc()
and free(). We consider rootnode as an external pointer that keeps the
address of the root node of the binary search tree. The following code
defines the structure for binary search tree and creates a rootnode.
struct binarysearchtree
{
int info;
struct binarysearchtree *leftlink;
struct binarysearchtree *rightlink;
};
struct binarysearchtree *rootnode;
rootnode=NULL;
Whenever rootnode=NULL, the binary search tree is empty.
23
Insertion in BST
I. Inserting a node into an empty tree: In this case, the
node inserted into the tree is considered as the root
node.
II. Inserting a node into a non-empty tree: In this case,
we compare the new node to the root node of the tree.
a. If the value of the new node is less than the value of the root
node, then if the left subtree is empty, the new node is appended
as the left leaf of the root node else we search continuous down
the left subtree.
b. If the value of the new node is greater than the value of the root
node, then if the right subtree is empty, the new node is
appended as the right leaf of the root node else we search
continuous down the right subtree.
c. If the value of the new node is equal to the value of the root node,
then print “DUPLICATE ENTRY” and return.
24
Recursive Function for Insertion in BST
Function Call: insertion(&rootnode,item); insertion(temp->leftlink, x);
Function Definition: }
void insertion(struct binarysearchtree **root, int x)
{ else if(temp->info<x)
struct binarysearchtree *temp,*node; {
temp=*root; if(temp->rightlink==NULL)
if(temp==NULL) //empty tree {
{ node=(struct binarysearchtree *)malloc(sizeof(struct
binarysearchtree));
node=(struct binarysearchtree *)malloc(sizeof(struct
binarysearchtree)); node->info=x;
node->info=x; node->leftlink=NULL;
node->leftlink=NULL; node->rightlink=NULL;
node->rightlink=NULL; temp->rightlink=node;
*root=node; }
} else
else if(temp->info>x) insertion(temp->rightlink, x);
{ }
if(temp->leftlink==NULL)
{ else
node=(struct binarysearchtree *)malloc(sizeof(struct {
binarysearchtree)); printf("\n DUPLICATE ENTRY!!!");
node->info=x; return;
node->leftlink=NULL; }
node->rightlink=NULL; }
temp->leftlink=node;
}
else
25
Deletion in BST
The node to be deleted may be a leaf node or a node with one child
or a node with two children.
a) Deleting a leaf node: In this case, the node is simply deleted from
the tree and NULL is set to its parent pointer.
Ex: Deleting 25 *rootnode
1011
1022 20 1033
1011
1044
NULL 12 NULL NULL 30 1055
1022 1033
v v
v v
v
v
v
27
Deletion in BST…
c) Deleting a node with two children: In this case, the node
being deleted is replaced by:
leftmost node of its right child subtree
OR rightmost node of its left child subtree
Ex: Deleting 30
v
v v
v
v
28
Algorithm for Deletion in BST
1) If leaf node
– set NULL to its parent pointer.
2) If node with one child
– redirect its child pointer to its parent pointer.
3) If node with two child
– replace node being deleted by
I. Leftmost node of its right subtree
II. OR rightmost node of its left subtree
29
Tree Traversal
Tree traversal is one of the most common operations
performed on tree data structures.
Traversing a binary tree is a way in which each node in
the tree is visited exactly once in a systematic manner.
We know that the order of traversal of nodes in a linear list
is from first to last, however there is no such “natural”
linear order for the nodes of a tree.
We have the following three orderings or methods for
traversing a non-empty binary tree:
Preorder (Depth-first order) Traversal
Inorder (Symmetric order) Traversal
Postorder Traversal
All theses traversal techniques are defined recursively.
30
Preorder Traversal
In this technique, first of all we process the root R of the binary
tree T. Then, we traverse the left subtree T1 of R in preorder
(which means that we traverse root of subtree T1 first and then its
left subtree). After visiting left subtree of R, then we take over
right subtree T2 of R and process all the nodes in preorder.
v
v v 5
v v v
Preorder Traversal:
25 17 4 2 20 35 30 26 50 45 65 55
31
Traverse the given binary tree in preorder
v v v v
v v
Answer: A B D E I C F G J
32
Algorithm for preorder traversal
1. If root=NULL, return.
2. Visit the root.
3. Traverse the left subtree in preorder.
4. Traverse the right subtree in preorder.
33
Postorder Traversal
In this technique, first of all we process the left subtree
T1 of R in postorder, then right subtree T2 of R in
postorder and at the last, the root R.
v
Postorder Traversal: v 5
G D BH I E F C A
v v
Preorder Traversal:
A B D G C E H I F
34
Traverse the given binary tree in postorder
v v v v
v v v v
Answer: I E J F C G K L H D B A Preorder: A B C E I F J D G H K L
35
Algorithm for postorder traversal
1. If root=NULL, return.
2. Traverse the left subtree in postorder.
3. Traverse the right subtree in postorder.
4. Visit the root.
36
Inorder Traversal
In this traversal technique, first of all we process the
left subtree T1 of the root R in inorder, then we process
the root and at last the right subtree T2 of R.
v
v 5
v v
Inorder Traversal:
D G B A H E I C F
37
Perform inorder traversal on the given tree
v v v v
v v v v
Answer: E I C F J B G D K H L A
38
Algorithm for inorder traversal
1. If root=NULL, return.
2. Traverse the left subtree in inorder.
3. Visit the root.
4. Traverse the right subtree in inorder.
39
Note:
By traversing a binary search tree (BST) in inorder, the
nodes are always traversed (and therefore printed) in
ascending order.
If we want to traverse and print data in descending order
in a binary search tree, then follow following steps:
1. Traverse right subtree in inorder.
2. Visit the root.
3. Traverse the left subtree in inorder.
40
Task
1) Suppose the following eight numbers are inserted in
order into an empty binary search tree T:
50, 33, 44, 77, 35, 60, 40, 100
Draw the tree T.
41
TU Exam Question (2066)
A binary tree T has 12 nodes. The in-order and pre-
order traversals of T yield the following sequence of
nodes:
In-order: VPNAQRSOKBTM
Pre-order: SPVQNARTOKBM
42
Answer: The binary tree T is constructed from its root downwards
as follows:
a) The root of binary tree T is obtained by choosing the first node
in its preorder (since preorder is root-left-right). Thus the
node S is the root of tree T.
root
b) The left child of the root node S is obtained as follows: First we
use the inorder of T to find the nodes in the left subtree T1 of S
(since inorder is left-root-right). Thus the left subtree T1
consists of the nodes V, P, N, A, Q and R. Then the left child
of S is obtained by choosing the first node in the preorder of
T1 (which clearly appears in the preorder of T). Thus P is the
left child of S.
43
c) Similarly, the right subtree T2 of S consists of the nodes
O, K, B, T and M. Thus the right child of S is obtained by
choosing the first node in the preorder of T2. Thus the
node T is the right child of S.
44
e) The right subtree T2 of P consists of the nodes N, A, Q and R. Thus
the right child of P is obtained by choosing the first node in the
preorder of T2. Thus the node Q is the right child of S.
45
g) By seeing the inorder traversal, we find that the right subtree T2 of node
T consists of only one node M. Thus the right child of node T is M.
h) Now node V does not have any child because it is the leftmost node
according to the inorder traversal. Therefore it’s a leaf.
i) To find the left child of node Q, the left subtree T1 of node Q
consists of the nodes N and A (since V and P are already occupied).
By seeing the preorder, it is clear that the left child of node Q is N.
46
j) The right subtree T2 of node Q consists of only one node R. Thus the
right child of node Q is R.
k) Now node O does not have a left child. By seeing the inorder, the right
subtree of node O consists of nodes K and B. By seeing the preorder,
node K is the right child of node O.
47
l) Node M is also a leaf node since it is the rightmost node in the inorder
traversal.
m) Node N does not have a left child since its left subtree is empty.
n) The right subtree of node N consists of the only one node i.e. A.
o) Node K does not have a left child as its left subtree is empty which is
obvious from its inorder.
p) The right subtree of node K contains the only remaining node i.e. B.
Hence the final binary tree T is:
48
Fig: Final Binary Tree
49
Classwork
A binary tree T has 9 nodes. The inorder and preorder
traversals of T yield the following sequence of nodes:
In-order: E A C K F H D B G
Pre-order: F A E K C D H G B
Draw the tree T.
50
Answer:
52
Answer:
54
Application of Binary Tree
A binary tree (or more appropriately strictly binary tree)
can be used to represent an expression containing
operands and binary operators.
A binary tree used to represent a binary expression is
called a binary expression tree (BET).
The root of BET contains the operator and its two
children contains operands.
For example, X + Y can be represented as:
55
Application of Binary Tree…
If we have an expression with more arithmetic operators
like (X+Y)/(X-Y), then we have to construct two subtrees
and then these two subtrees are added to the root node as:
56
Classwork
Represent the following arithmetic expressions by
their corresponding binary trees and find out their
postfix equivalent:
a) A+B*C
b) (A+B)*C
c) A+(B-C)*D$(E*F)
d) (A+B*C)$((A+B)*C)
57
Fig (a): A+B*C
Postfix: ABC*+
Fig (b): (A+B)*C
Postfix: ABC*+
58
Fig (c): A+(B-C)*D$(E*F)
Postfix: ABC-DEF*$*+
59
Fig (d): (A+B*C) $((A+B)*C)
Postfix: ABC*+AB+C*$
60
Huffman Algorithm (Huffman Coding)
Huffman codes are used to compress data by
representing each alphabet by a unique binary
code (or bit-string) in an optimal way.
Consider the problem of using bit strings to encode the letters of the English
alphabet (where no distinction is made between lowercase and uppercase
letters). We can represent each letter by a bit string of length 5, because
there are only 26 letters and there are 25 (=32) bit strings of length 5. Here,
the total number of bits used to encode data is 5 times the number of
characters in the text when each character is encoded with 5 bits.
Problem and Solution: If it is possible to find a
coding scheme with fewer bits to code these letters
then we can save memory and also reduce the
transmittal time of the data.
61
Huffman Coding…
To encode letters using different length bit-strings, the
letters that occur more frequently are encoded using
shorter bit-strings and letters occurring rarely are encoded
using longer bit-strings.
When letters are encoded using varying numbers of bits,
some method must be used to determine where the bits for
each character start and end.
For example, if e were encoded with 0, a with 1, and t with
01, then the bit string 0101 could represent eat, tea, eaea,
or tt.
One way to ensure that no bit-string corresponds to more
than one sequence of letters is to encode letters so that the
bit string for a letter never occurs as the first part of the bit
string for another letter. Codes with this property are called
prefix codes.
62
Huffman Coding…
An example of prefix code: If e is encoded with 0, a with 10, and t
with 11, then the bit string 10110 is the encoding of ate.
A prefix code can be represented using a binary tree, where the
characters are the labels of the leaves in the tree. The edges of the tree
are labeled so that an edge leading to a left child is assigned a 0 and an
edge leading to a right child is assigned a 1. The bit string used to
encode a character is the sequence of labels of the edges in the unique
path from the root to the leaf that has this character as its label.
o 1
o 1
e
a t
63
Huffman Coding…
The Huffman coding or Huffman algorithm takes as input
the frequencies (which are the probabilities of occurrences or
repetitions known in advance) of symbols in a string of text and
produces as output a prefix code that encodes the string using
the fewest possible bits, among all possible binary prefix codes
for these symbols.
Given symbols and their frequencies, the goal is to construct a
rooted binary tree where the symbols are the labels of the
leaves.
The algorithm begins with a forest of trees each consisting of one vertex,
where each vertex has a symbol as its label and where the weight of this
vertex equals the frequency of the symbol that is its label. At each step, we
combine two trees having the least total weight into a single tree by
introducing a new root and placing the tree with larger weight as its left
subtree and the tree with smaller weight as its right subtree. Furthermore,
we assign the sum of the weights of the two subtrees of this tree as the total
weight of the tree. The algorithm is finished when it has constructed a tree,
that is, when the forest is reduced to a single tree.
64
Question: Use Huffman coding to encode the following
symbols with the frequencies listed: A:0.08, B:0.10, C:0.12,
D:0.15, E:0.20, F:0.35. What is the average number of bits
used to encode a character?
Answer:
0.08 0.10 0.12 0.15 0.20 0.35
Initial
A B C D E F Forest
65
0.18 0.20 0.27 0.35
0 1 0 1 Step2
E F
B A D C
0 1
0 1 0 1
1 E 0 1 Step5
F 0
D C B A
Fig: Final Huffman Tree
(or Huffman Coding of symbols)
Thus the Huffman encoding produced encode A by 111, B by 110, C by 011, D
by 010, E by 10, and F by 00.
Thus the average number of bits used to encode a symbol using Huffman
encoding = 3*0.08 + 3*0.10 + 3*0.12 + 3*0.15 + 2*0.20 + 2*0.35 = 2.45.
67
The Huffman Algorithm
Assumptions:
1. Let |C| be the number of symbols ai with frequencies wi, i=1, 2, …, n.
2. The tree is constructed in bottom-up manner starting from the |C| leaves and |C|-1
merging operations.
3. We use priority queue Q to keep nodes ordered by their frequencies.
Algorithm:
HuffmanAlgorithm(C)
{
n=|C|;
Q=C;
for(i=1;i<=n-1;i++)
{
z=Allocate_Node();
x=Extract_Min(Q);
y=Extract_Min(Q);
right(z)=x;
label_edge(z, x)=1;
left(z)=y;
label_edge(z, y)=0;
w(z)=w(x)+w(y);
Insert(Q, z);
}
} // The Huffman coding for the symbol ai is the concatenation of the labels
// of the edges in the unique path from the root to the vertex ai. 68