0% found this document useful (0 votes)
93 views65 pages

MC4101-ADSA Unit-II 1

MC4101-ADSA_Unit-II

Uploaded by

Rathnakumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views65 pages

MC4101-ADSA Unit-II 1

MC4101-ADSA_Unit-II

Uploaded by

Rathnakumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 65

Paavai Institutions Department of MCA

UNIT-II

TREE STRUCTURES

UNIT-I 1. 1
Paavai Institutions Department of MCA

TECHNICAL TERMS

Tree
A tree is a collection of nodes. The collection can be empty; otherwise, a tree consists of a
distinguished node r, called the root, and zero or more nonempty (sub) trees T1, T2,…,Tk, each
of whose roots are connected by a directed edge from r.

Root
This is the unique node in the tree to which further sub-trees are attached.

Here, A is the root.

Degree of the node


The total number of sub-trees attached to that node is called the degree of the
node.
For node A, the degree is 2 and for B and C, the degree is 0.

Leaves
These are the terminal nodes of the tree. The nodes with degree 0 are always the
leaves.
Here, B and C are leaf nodes.

UNIT-I 1. 2
Paavai Institutions Department of MCA

Internal nodes
The nodes other than the root and the leaves are called internal nodes.
Here, C is the internal node.

Parent node
The node which is having further sub-branches is called the parent node of those
sub-branches.
Here, node C is the parent node of D and E

Level of the tree


The root node is always considered at level zero, then its adjacent children are
supposed to be at level 1 and so on.

UNIT-I 1. 3
Paavai Institutions Department of MCA

Here, node A is at level 0, nodes B and C are at level 1 and nodes D and E are at level 2.

Forest
A tree may be defined as a forest in which only a single node (root) has no predecessors.
Any forest consists of a collection of trees.

Binary tree
A binary tree is a finite set of nodes which is either empty or consists of a root and two
disjoint binary trees called the left sub-tree and right sub-tree.

Path in a tree
A path in a tree is a sequence of distinct nodes in which successive nodes are connected
by edges in the tree.

Full binary tree


A full binary tree is a tree in which all the leaves are on the same level and every non-leaf
node has exactly two children.

Complete binary tree


A complete binary tree is a tree in which every non-leaf node has exactly two children
not necessarily to be on the same level.

Binary tree traversal


Traversing a binary tree means moving through all the nodes in the binary tree, visiting
each node in the tree only once.

UNIT-I 1. 4
Paavai Institutions Department of MCA

Binary search tree


A binary search tree is a special binary tree, which is either empty or it should satisfy
the following characteristics:
Every node has a value and no two nodes should have the same value i.e) the
values in the binary search tree are distinct
 The values in any left sub-tree is less than the value of its parent node
 The values in any right sub-tree is greater than the value of its parent node
 The left and right sub-trees of each node are again binary search trees

Threaded binary tree


In threaded binary tree, the NULL pointers are replaced by some addresses. The left
pointer of the node points to its predecessor and the right pointer of the node points to its
successor.

UNIT-I 1. 5
Paavai Institutions Department of MCA

CONTENTS

2. LINEAR DATA STRUCTURES


2.1 Need for non-linear structures

2.2 Trees and its representation

2.3 Binary Tree

2.4 Expression trees

2.5 Binary tree Traversals

2.6 General trees-Left child right sibling data structures

2.7 Applications of trees

2.8 Huffman Algorithm

2.9 Binary search tree

2.10 Question Bank

UNIT-I 1. 6
Paavai Institutions Department of MCA

LECTURE NOTES
2.1 NEED FOR NON-LINEAR STRUCTURES

2.1.1 DATA STRUCTURE- NON LINEAR STRUCTURE

A data structure is classified into two categories: Linear and Non-Linear data structures.
A data structure is said to be linear if the elements form a sequence, for example Array, Linked
list, queue etc. Elements in a nonlinear data structure do not form a sequence, for example Tree,
Hash tree, Binary tree, etc.

There are two ways of representing linear data structures in memory. One way is to have
the linear relationship between the elements by means of sequential memory locations. Such
linear structures are called arrays. The other way is to have the linear relationship between the
elements represented by means of links. Such linear data structures are called linked list.

2.1.2 NEEDS
Some examples will show us that why nonlinear data structures are important. Tree is one
of the non-linear data structures.
 In this genealogy tree, the node at the top of the tree is the head of the family. There are
three nodes under this one. Then there are nodes under these three nodes i.e. the sons of
these three family members. You may have seen the tree like this of some other family.
You can make the tree of your family too.
 The thing to be noted in this genealogical tree is that it is not a linear structure like linked
list, in which we have to tell that who is the father and who is the son. We make it like a
tree. We develop the why the tree top-down, in which the father of the family is on the
top with their children downward.
 We can see the similar tree structure of a company. In the tree of a company, the CEO of
the company is on the top, followed downwardly managers and assistant managers. This
process goes downward according to the administrative hierarchy of the company.
 Our tree structure is different from the actual tree in the way that the actual tree grows
from down to up. It has root downside and the leaves upside. The data structure tree is

UNIT-I 1. 7
Paavai Institutions Department of MCA

opposite to the real tree as it goes upside down. This top-down structure in computer
science makes it easy to understand the tree data structure.
 There may be situations where the data, in our programs or applications, is not in the
linear order. There is a relationship between the data that cannot be captured by a linked
list or other linear data structure. Here we need a data structure like tree. Ones, the
searching in linear data structures is very tedious. Suppose such a name in a telephone
directory having 100000 entries. If this to traverse the list from the starting nodes.
 A non-linear data structure is a data structure in which the data items in the memory are
not allocated contiguously i.e. the data items are dispersed in the memory. The first data
item will have a link to the second data item and second data item will have a link to the
third data item and so on.
 Uses memory efficiently that the free contiguous memory in not an requirement for
allocating data items

 The length of the data items is not necessary to be known prior to allocation

 Overhead of the link to the next data item


 In linear data structures, data elements are organized sequentially and therefore they are
easy to implement in the computer’s memory.
 In nonlinear data structures, a data element can be attached to several other data elements
to represent specific relationships that exist among them.
 Non-Linear data structure is that if one element can be connected to more than two
adjacent element then it is known as non-linear data structure.

2.2 TREES AND ITS REPRESENTATION

2.2.1 DEFINITION OF TREE

A tree is a finite set of elements or nodes. If the set is non-empty, one of the nodes is
distinguished as the root node, while the remaining (possibly empty) set of nodes are grouped
into subsets, each of which is itself a tree. This hierarchical relationship is described by referring

UNIT-I 1. 8
Paavai Institutions Department of MCA

to each such subtree as a child of the root, while the root is referred to as the parent of each
subtree. If a tree consists of a single node, that node is called a leaf node.

2.2.2 BASIC TREE CONCEPTS

 A tree consists of a finite set of elements, called ‘nodes’, and a finite set of
directed lines, called ‘branches’, that connect the nodes.
 The number of branches associated with a node is the degree of the node.
 When the branch is directed toward a node, it is an indegree branch; when the
branch is directed away from the node, it is an outdegree branch.
 The sum of indegree and outdegree branches is the degree of the node.
 The indegree of the root is by definition is zero.
 A leaf is any node with an outdegree of zero.
 A node that is not a root or a leaf is known as an internal node.
 A node is a parent if it has successor nodes – that is, if it has an outdegree greater
than zero.
 Conversely, a node with a predecessor is a child. A child node has an indegree of
one.
 Two or more nodes with the same parent are siblings.
 A path is a sequence of nodes in which each node is adjacent to the next one.
 Every node in the tree can be reached by following a unique path starting from the
root.
 The level of a node is its distance from the root. Because the root has a zero
distance from itself, the root is at level 0. The children of the root are at the level
1.
 The height or length of the tree is the level of the leaf in the longest path from the
root plus 1. By definition, the height of an empty tree is -1.

UNIT-I 1. 9
Paavai Institutions Department of MCA

 A tree may be divided into subtrees. A subtree is any connected structure below
the root.
 The first node in a subtree is known as the root of the subtree and is used to name
the subtree.

UNIT-I 1. 10
Paavai Institutions Department of MCA

2.2.3 TREE REPRESENTATION

Root
Level 0 Branch
A
Internal Nodes
Level 1
B F G

Level 2 Leaf
C H I J

Level 3
D E Siblings

Sub Tree

Figure 2.1 Tree

Parents : A, B, C, G

Children : B, F, G, C, H, I, J, D, E

Siblings : { B, F, G }, { D, E }, { H, I, J }

Leaves : F, D, E, H, I, J

Length : 4

2.2.3.1 DEFINITIONS: ORDERED AND UNORDERED TREES

When a species splits into two, there is no significance to the order of the children.
Similarly, in organizations, if there are three research groups within the research and
development department, there is order that can naturally be imposed order on the three groups.
For other trees, however, the order of the children may be relevant. If the children are linearly
ordered, the tree is said to be an ordered tree. For example, the two trees in the figure are
identical if the trees represent unordered trees, but they are different if the trees are ordered.

UNIT-I 1. 11
Paavai Institutions Department of MCA

Figure 2.2 Two equal unordered trees but different ordered trees.

2.2.3.2 DEFINITIONS: PATHS, DEPTH OF A NODE, HEIGHT OF A TREE

The shape of a tree gives a natural flow from the root down to any node within the tree,
as is shown in the figure.

Figure 2.3 The natural flow from the root to any node within the tree.

A path within a tree is a sequence of nodes, (a0, a1, a2, ..., an) where each node in this
sequence is a child of the previous node in the sequence, i.e., ak + 1 is a child of ak. The length
of this path is n which can be interpreted as the number of edges connecting the nodes.

UNIT-I 1. 12
Paavai Institutions Department of MCA

In the following figure , the path (B, E, G) has length 2.

Figure 2.4 A path (B, E, G) of length 2.

2.3 BINARY TREES

 A binary tree is a tree in which no node can have more than two subtrees.
 These subtrees are designated as the left subtree and right subtree.
 Each subtree is a binary tree itself.

UNIT-I 1. 13
Paavai Institutions Department of MCA

B C

Left Subtree Right Subtree

D E F G

Figure 2.5 Left and Right Subtree

 The height of the binary trees can be mathematically predicted. The maximum height
of the binary tree which has N nodes,
Hmax = N

 A tree with a maximum height is rare. It occurs when the entire tree is built in one
direction. The minimum height of the tree, Hmin is determined by,
Hmin = ë Log2 N û + 1

 Given a height of the binary tree, H, the minimum and maximum number of nodes in
the tree are given as,
Nmin = H and,

Nmax = 2H - 1

 If the height of the tree is less, then it is easier to locate any desired node in the tree.
 To determine whether tree is balanced, the balance factor should be calculated.
 If HL represents the height of the left subtree and H R represents the height of the right
subtree then Balance factor,
B = HL – HR

 A tree is balanced if its balance factor is 0 and its subtrees are also balanced.

UNIT-I 1. 14
Paavai Institutions Department of MCA

 A binary tree is balanced if the height of its subtrees differs by no more than one and
its subtrees are also balanced.
 A complete tree has the maximum number of entries for its height.
 The maximum number is reached when the least level is full. The maximum number
is reached when the last level is full.

A
A A

B C
B C

D E F G

Figure 2.6 Complete Trees (at levels 0, 1, and 2)

 A tree is considered nearly complete if it has the minimum height for its nodes and all
nodes in the last level are found on the left.

A A A

B C B C B C

D D E D E F

Figure 2.7 Nearly Complete Trees (at level 2)

2.3.1 BINARY TREE REPRESENTATION

A binary tree is a tree which is either empty, or one in which every node:
 has no children; or
 has just a left child; or
 has just a right child; or
 has both a left and a right child.

UNIT-I 1. 15
Paavai Institutions Department of MCA

A Complete binary tree of depth K is a binary tree of depth K having 2k – 1 nodes.

Level 1

Level 2

Level 3
Figure 2.8 Binary Tree

A very simple representation for such binary tree results from sequentially numbering the
nodes, starting with nodes on level 1 then those on level 2 and so on. Nodes on any level are
numbered from left to right as shown in the above picture. This numbering scheme gives us the
definition of a complete binary tree. A binary tree with n nodes and of depth K is complete if its
nodes correspond to the nodes which are numbered one to n in the full binary tree of depth K.

2.3.2 ARRAY REPRESENTATION

Each node contains info, left, right and father fields. The left, right and father fields of a
node point to the node’s left son, right son and father respectively.
Using the array implementation, we may declare,
#define NUMNODES 100
struct nodetype
{
int info;
int left;
int right;
int father;
};
struct nodetype node[NUMNODES];
UNIT-I 1. 16
Paavai Institutions Department of MCA

This representation is called linked array representation.

Under this representation,

info(p) would be implemented by reference node[p].info,

left(p) would be implemented by reference node[p].left,

right(p) would be implemented by reference node[p].right,

father(p) would be implemented by reference node[p].father respectively.

The operations,

isleft(p) can be implemented in terms of the operation left(p)

isright(p) can be implemented in terms of the operation right(p)

Example: -

Fig (a) Fig (b)

Figure 2.9 Binary Tree –Array Representation


The above trees can be represented in memory sequentially as follows

UNIT-I 1. 17
Paavai Institutions Department of MCA

Figure 2.10 Binary Tree –Array Representation


The above representation appears to be good for complete binary trees and wasteful for
many other binary trees. In addition, the insertion or deletion of nodes from the middle of a tree
requires the insertion of many nodes to reflect the change in level number of these nodes.

2.3.3 LINKED REPRESENTATION

The problems of sequential representation can be easily overcome through the use of a
linked representation. Each node will have three fields LCHILD, DATA and RCHILD as
represented below

UNIT-I 1. 18
Paavai Institutions Department of MCA

LCHILD DATA RCHILD

Fig (a) Fig (b)

Figure 2.11 Binary Tree –Linked List Representation


In most applications it is adequate. But this structure makes it difficult to determine the
parent of a node since this leads only to the forward movement of the links.

UNIT-I 1. 19
Paavai Institutions Department of MCA

Using the linked implementation, we may declare,

stuct nodetype
{
int info;
struct nodetype *left;
struct nodetype *right;
struct nodetype *father;
};
typedef struct nodetype *NODEPTR;

This representation is called dynamic node representation. Under this representation,

info(p) would be implemented by reference pinfo,

left(p) would be implemented by reference pleft,

right(p) would be implemented by reference pright,

father(p) would be implemented by reference pfather.

2.3.4 PRIMITIVE OPERATION ON BINARY TREES

(1) maketree() function

Which allocates a node and sets it as the root of a single-node binary tree, may be
written as follows;

NODEPTR maketree(x)
int x;

UNIT-I 1. 20
Paavai Institutions Department of MCA

{
NODEPTR p;

p = getnode(); / * getnode() function get a available node */


pinfo = x;
pleft = NULL;
pright=NULL;
return(p);
}
(2) setleft(p,x) function

Which sets a node with contents x as the left son of node(p)


setleft(p,x)
NODEPTR p;
int x;
{
if(p = = NULL)
printf(“insertion not made”);
else if ( pleft ! = NULL)
printf(“invalid insertion “);
else
pleft = maketree (x);
}

2.3.5 CONVERSION OF A GENERAL TREE TO BINARY TREE

2.3.5.1 GENERAL TREE

 A General Tree is a tree in which each node can have an unlimited out degree.
 Each node may have as many children as is necessary to satisfy its requirements.
Example: Directory Structure
UNIT-I 1. 21
Paavai Institutions Department of MCA

B F G

C H I J

Figure 2.12 General Tree

 It is considered easy to represent binary trees in programs than it is to represent


general trees. So, the general trees can be represented in binary tree format.

2.3.5.2 CHANGING GENERAL TREE TO BINARY TREE

 The binary tree format can be adopted by changing the meaning of the left and
right pointers. There are two relationships in binary tree,
1. Parent to child
2. Sibling to sibling
Using these relationships, the general tree can be implemented as binary tree.

2.3.5.3 ALGORITHM

1. Identify the branch from the parent to its first or leftmost child. These branches from
each parent become left pointers in the binary tree
2. Connect siblings, starting with the leftmost child, using a branch for each sibling to its
right sibling.
3. Remove all unconnected branches from the parent to its children

UNIT-I 1. 22
Paavai Institutions Department of MCA

B E F

C D G H I

(a) General Tree

A A

B E F B E F

C D G H I C D G H I

Step 1: Identify all leftmost children Step 2: Connect Siblings

A
A

B
B E F

C E
C D G H I
D F
Step 3: Delete unneeded branches
G

UNIT-I 1. 23
Paavai Institutions Department of MCA

(B) THE RESULTING BINARY


TREE

Figure 2.13 Changing General Tree to Binary Tree

2.4 EXPRESSION TREES

2.4.1 DEFINITION

An expression tree represents an algebraic expression in such a way that stores its
structure and shows how the order of operations applies.

We're interested in a few different operators. We break these operators down into two
categories:

 Binary Operators - operators that take two inputs


+
- (here, subtraction)
*
/ (both integer and floating-point division)
% (modulus)
^ or ** (exponentiation)
 Unary Operators - operators that take one input
- (here, negation)
Note that we don't mention parentheses. The expression tree's structure removes the need to
talk about parentheses, as the structure encodes precedence.

When we have a single expression based on a binary operator, we draw the expression tree as
follows:

 The operator is the root of the tree.

UNIT-I 1. 24
Paavai Institutions Department of MCA

 The operands are the children. Because some operations are not commutative, order does
matter. The operand before the operator is the left child and the operand after the
operator is the right child.
Thus, we get a tree with a root and two children.

2.4.2 EXAMPLES
Consider arithmetic expressions like (35 - 3*(3+2)) / 4:
 For our discussion we'll consider the four arithmetic operators: +, -, *, /.
 We'll study integer arithmetic
=> Here, the /-operator is integer-division (e.g., 2/4 = 0)
 Each operator is a binary operator
=> Takes two numbers and returns the result (a number)
 There is a natural precedence among operators: \, *, +, -.
We'll use parentheses to force a different order, if needed:
Thus 35 - 3*3+2/4 = 26 (using integer arithmetic)
Whereas (35 - 3*(3+2)) / 4 = 5
Now let's examine a computational procedure for expressions:
At each step, we apply one of the operators.
The rules of precedence and parentheses tell us the order.
The computational procedure can be written as an expression tree:

UNIT-I 1. 25
Paavai Institutions Department of MCA

Figure 2.14 Expression Tree

 In an expression tree:
 The leaves are numbers (the operands).
=> The value of a leaf is the number.
 The non-leaf nodes are operators.
=> The value of a non-leaf node is the result of the operator applied to the
values of the left and right child.
 The root node corresponds to the final value of the expression.

UNIT-I 1. 26
Paavai Institutions Department of MCA

Figure 2.15 Expression Tree

2.4.2. 1 EXERCISE 1
Draw an expression tree for the expression 10/4 + 2*(5+1). Compute the values at each
non-leaf node.
We will now look at a variety of issues related to expression trees:
 How to build a tree given an expression.
 How to evaluate an expression tree, once it's built.
 How to convert expressions to postfix form and why that's important.

Let's start with building the tree:


 The computational procedure for extracting the sub-parts (numbers, and operators) of an
expression like (35-3*(3+2))/4 is called parsing.
 To simplify the code, we'll make extensive use of parentheses:
o Every number will be surrounded by parentheses.

UNIT-I 1. 27
Paavai Institutions Department of MCA

o Every operator with its operand will be enclosed in parentheses.


o Of course, parsing can be done with theses restrictions
=> But the code is more complicated
o Thus, the expression (35-3*(3+2))/4 becomes (((35)-((3)*((3)+(2))))/(4))

2.4.2.2 EXERCISE 2
Convert the expression 10/4 + 2*(5+1) into the form above, with complete parentheses.
We will build the tree as we go along in the parsing:
We'll use this class as the template for each tree node:
class ExprTreeNode {
ExprTreeNode left, right; // The usual pointers.
boolean isLeaf; // Is this a leaf?
int value; // If so, we'll store the number here.
char op; // If not, we need to know which operator.

2.4.3 ALGORITHM
The general idea in our parsing algorithm is:
Algorithm: parse (e)
Input: expression string e
Output: root of expression subtree for e
1. node = make new tree node.
// Bottom-out: it's a number
2. if e is a number
3. node.value = number extracted from string
4. return node
5. endif

UNIT-I 1. 28
Paavai Institutions Department of MCA

// Recurse
6. node.op = extract operator from e
7. node.left = parse (substring of e to the left of op)
8. node.right = parse (substring of e to the right of op)
9. return node

We'll need to write code to look inside a string and identify the left and right
substrings (on either side of the operator).

2.5 BINARY TREE TRAVERSALS

 A binary tree traversal requires that each node of the tree be processed once and only
once in a predetermined sequence.
 The two general approaches to the traversal sequence are,
1. Depth first traversal
2. Breadth first traversal
 In depth first traversal, the processing proceeds along a path from the root through
one child to the most distant descendent of that first child before processing a second
child. In other words, in the depth first traversal, all the descendants of a child are
processed before going to the next child.
 In a breadth-first traversal, the processing proceeds horizontally form the root to all
its children, then to its children’s children, and so forth until all nodes have been
processed. In other words, in breadth traversal, each level is completely processed
before the next level is started.

2.5.1 DEPTH-FIRST TRAVERSAL

There are basically three ways of binary tree traversals. They are:

1. Pre Order Traversal


2. In Order Traversal

UNIT-I 1. 29
Paavai Institutions Department of MCA

3. Post Order Traversal


In C, each node is defined as a structure of the following form:

struct node
{
int info;
struct node *lchild;
struct node *rchild;
}
typedef struct node NODE;

2.5.2 BINARY TREE TRAVERSALS (RECURSIVE PROCEDURE)

2.5.2.1 INORDER TRAVERSAL

Steps: 1. Traverse left subtree in inorder


2. Process root node
3. Traverse right subtree in inorder

Algorithm C Coding

void inorder_traversal ( NODE * T)

UNIT-I 1. 30
Paavai Institutions Department of MCA

Algorithm inoder traversal (Bin-Tree T) if( T ! = NULL)


{
Begin inorder_traversal(T->lchild);
If ( not empty (T) ) then
printf(“%d \t “, T->info);
Begin inorder_traversal(T->rchild);
Inorder_traversal ( left subtree ( T ) ) }
Print ( info ( T ) ) / * process node */ }
Inorder_traversal ( right subtree ( T ) )
End
End

B E

C D F

Figure 2.16 Inorder Traversal

The Output is : C à B à D àA àE à F

2.5.2.2 PREORDER TRAVERSAL

Steps: 1. Process root node


2. Traverse left subtree in preorder
3. Traverse right subtree in preorder

UNIT-I 1. 31
Paavai Institutions Department of MCA

Algorithm C function

void preorder_traversal ( NODE * T)


{
Algorithm preorder traversal (Bin-Tree T) if( T ! = NULL)
{
Begin printf(“%d \t”, T->info);
If ( not empty (T) ) then preorder_traversal(T-
>lchild);
Begin preorder_traversal(T-
>rchild);
Print ( info ( T ) ) / * process node * / }
Preoder traversal (left subtree ( T ) ) }
Inorder traversal ( right subtree ( T ) )
End
End

B
UNIT-I E 1. 32

C D F
Paavai Institutions Department of MCA

Figure 2.17 Preorder Traversal

The Output is : A àB à C à D à E à F

2.5.2.3 POSTORDER TRAVERSAL

Steps : 1. Traverse left subtree in postorder


2. Traverse right subtree in postorder
3. Process root node

Algorithm C function

Postorder Traversal void postorder_traversal ( NODE * T)


{
Algorithm postorder traversal (Bin-Tree T) if( T ! = NULL)
{
Begin postorder_traversal(T-
>lchild);
If ( not empty (T) ) then postorder_traversal(T-
>rchile);
Begin printf(“%d \t”, T->info);
Postorder_traversal ( left subtree ( T ) ) }
Postorder_traversal ( right subtree( T)) }

UNIT-I 1. 33
Paavai Institutions Department of MCA

Print ( Info ( T ) ) / * process node */


End
End

B E

C D F

Figure 2.18 Postorder Traversal

The Output is : C à D à B à F à E à A

UNIT-I 1. 34
Paavai Institutions Department of MCA

2.5.3 NON – RECURSIVE ALGORITHM

2.5.3.1 INORDER_TRAVERSAL

#define MAXSTACK 100


inorder_traversal (tree)
NODEPTR tree;
{
struct stack
{
int top;
NODEPTR item[MAXSTACK];
}s;
NODEPTR p;

s.top = -1;
p = tree;
do
{/* travel down left branches as far as possible, saving
pointers to nodes passed */
while(p!=NULL)
{
push(s,p);
p = p  left;
}
/* check if finished */

if ( !empty (s) )
{ / * at this point the left subtree is empty */
p=pop(s);
UNIT-I 1. 35
Paavai Institutions Department of MCA

printf(“%d \n”, pinfo); /* visit the root */


p =pright; /* traverse right subtree */
}
}while( !empty (s) || p! = NULL );
}

2.5.3.2 PREORDER_TRAVERSAL

#define MAXSTACK 100


preorder_traversal (tree)
{
NODEPTR tree;
{
struct stack
{
int top;
NODEPTR item[MAXSTACK];
}s;
NODEPTR p;

s.top = -1;
p = tree;
do
{
/* travel down left branches as far as possible, saving
pointers to nodes passed */
if(p!=NULL)
{

UNIT-I 1. 36
Paavai Institutions Department of MCA

printf(“%d \n”, pinfo); /* visit the root */


if(pright!=NULL)
push(s,pright); /* push the right subtree
on to the stack */
p=pleft;
}
else
p=pop(s);
}while( ! empty(s) || p! = NULL )
}

2.6 GENERAL TREES

A general tree T is a finite set of one or more nodes such that there is one designated node r,

called the root of T, and the remaining nodes are partitioned into n 0 disjoint subsets T1, T2, , Tn,

each of which is a tree, and whose roots r1, r2, , rn, respectively, are children of r.

Figure 2.19 General Tree

UNIT-I 1. 37
Paavai Institutions Department of MCA

2.6.1 REPRESENTING GENERAL TREES

The representation of a general tree poses some hard choices:


 May the number of children per node vary over a large range (possibly with no
logical upper limit)?
 Setting an upper limit renders some trees unrepresentable.
 Even with an upper limit, allocating a fixed number of pointers within each node may
waste a huge amount of space.
 How can the pointers be organized for easy traversal? Does this require a secondary
data structure within the node?
 Does the scheme provide for efficient search? Node insertion? node deletion?

Figure 2.20 Representing General Tree

2.6.2 LINKED NODES IMPLEMENTATION

The binary node type presented earlier can be extended for a general tree representation. The
pointers can be managed by:
 allocating a uniform-sized array of node pointers and limiting the number of
children a node is allowed to have;
 allocating a dynamically-sized array of node pointers, custom-fit to the number

UNIT-I 1. 38
Paavai Institutions Department of MCA

of children the node currently has (and re-sizing manually as needed);


 using a linked list of node pointers;
 using a vector of node pointers, growing as needed.

Figure 2.21 Linked node implementation

2.6.3 LIST OF CHILDREN REPRESENTATION


For each tree node, store its data element, a (logical) pointer to its parent node, and a list
of (logical) pointers to its children, using a array as the underlying physical structure:

UNIT-I 1. 39
Paavai Institutions Department of MCA

Figure 2.22 List of children representation

2.6.4 LEFT-CHILDREN/RIGHT-SIBLING REPRESENTATION

For each tree node, store its data element, a (logical) pointer to its parent node, and (logical)
pointers to its left child and its right sibling, using a array as the underlying physical structure:

Figure 2.23 Left-Children/Right-Sibling Representation

2.6.5 PARENT POINTER REPRESENTATION


For each tree node, store its data element and a (logical) pointer to its parent node, using a
array as the underlying physical structure:

UNIT-I 1. 40
Paavai Institutions Department of MCA

Figure 2.24 Parent Pointer Representation

2.7 APPLICATIONS OF TREES

1. Binary search and similar algorithms use the logarithmic height of decision trees to
implement efficient searches
2. Heaps and binary search trees use the logarithmic height of tree data structures to
efficiently step from root to leaf
3. Hence heap, merge , and BST sorting algorithms are much more efficient than
simpler
4. Using binary tree, one can create expression trees. The leaves of the expression tree
are operands such as constants, variable names and the other node contains the
operator (binary operator).
5. Evaluate an expression tree by applying the operator at the root to the values obtained
by recursively evaluating the left and right sub trees.
6. Represent organization

UNIT-I 1. 41
Paavai Institutions Department of MCA

7. Represent computer file systems

8. Networks to find best path in the Internet

9. Chemical formulas representation

10. Ordered storage to be used in binary search


11. Decision trees
12. Encoding

2.7.1 APPLICATIONS OF TREES IN COMPUTER SCIENCE

Trees are one of the most useful data structures in Computer Science. Some of the
common applications of trees are:

1. The library database in a library, a student database in a school or college, an employee


database in a company, a patient database in a hospital or any database for that matter
would be implemented using trees.
2. The file system in your computer i.e folders and all files, would be stored as a tree.

3. When you search for a word in a file or misspell a word and you get a list of possible
correct words, you are using a tree. Because, the file would be indexed as a tree and for
the same reason you get answers instantly.

4. When you watch a youtube video or surf anything on the internet, the data which would
be present in a computer somewhere in the world would travel to your computer passing
through many intermediate computers called routers. The routers heavily use trees for
routing.

5. Most probably, Google maps uses trees (apart from using graphs) for all the locations in
it.
6. One reason to use trees might be because you want to store information that naturally
forms a hierarchy. For example, the file system on a computer:

UNIT-I 1. 42
Paavai Institutions Department of MCA

file system
———–

/ <-- root
/ \
... home
/ \
ugrad course
/ / | \
... cs101 cs112 cs113
7. If we organize keys in form of a tree (with some ordering e.g., BST), we can search
for a given key in moderate time (quicker than Linked List and slower than arrays). Self-
balancing search trees like AVL and Red-Black trees guarantee an upper bound of
O(Logn) for search.
8. We can insert/delete keys in moderate time (quicker than Arrays and slower than
Unordered Linked Lists). Self-balancing search trees like AVL and Red-Black trees
guarantee an upper bound of O(Logn) for insertion/deletion.
9. Like Linked Lists and unlike Arrays, Pointer implementation of trees don’t have an upper
limit on number of nodes as nodes are linked using pointers.

2.8 HUFFMAN ALGORITHM

2.8.1 HUFFMAN TREE

Huffman tree are used for developing an optimal encoding scheme for the given
frequency of occurrence of each symbol in a message (string). Find the two symbols that appear
least frequency and assigned them with different bit using 0’s and 1’s. And combine these two

UNIT-I 1. 43
Paavai Institutions Department of MCA

symbols whose code represents the knowledge that a symbol is either 0 or 1 frequency. Such a
way the Huffman tree is construct for remaining symbols in the given message

Once the Huffman tree is constructed, the code of any symbol in the alphabet can be
constructed by starting at the leaf representing that symbol and Climbing Up to the root.
Initially the code is assigned to NULL. Each time a left branch is climbed 0 is appended to the
beginning of the code; each time a right branch is climbed 1 is appended to the beginning of the
code.

To read the codes from a Huffman tree, start from the root and add a '0' every time you
go left to a child, and add a '1' every time you go right.

Then, repeat these steps until there is only one node left:

 Find the two nodes with the lowest weights.

 Create a parent node for these two nodes. Give this parent node a weight of the
sum of the two nodes.

 Remove the two nodes from the list, and add the parent node.

2.8.2 HUFFMAN ALGORITHM

/* initialize the set of root nodes */


rootnodes = the empty ascending priority queue;
/* construct a node for each symbol */
for(i=0; i<n; i++)
{
p = maketree(frequency[i]);
position[i] = p; / * a pointer to the leaf containing the ith symbol */
pqinsert(rootnodes,p);
}

UNIT-I 1. 44
Paavai Institutions Department of MCA

while ( rootnodes contains more than one item)


{
p1 = pqmindelete(rootnodes);
p2 = pqmindelete(rootnodes);
/* combine p1 and p2 as branches of a single tree */
p = maketree( info(p1) + info (p2) );
setleft(p,p1);
setright(p,p2);
pqinsert( rootnodes, p);
}
/* the tree is now constructed; use it to find codes */
root = pqmindelete(rootnodes);
for( i=0; i<n; i++)
{
p = position [i];
code[i] = the null bit string;
while( p ! = root )
{
/* travel up the tree */
if( isleft(p) )
code[i] = 0 followed by code[i];
else
code[i] = 1 followed by code[i];
p = father(p);
}
}

This problem is that of finding the minimum length bit string which can be used to
encode a string of symbols. One application is text compression:

UNIT-I 1. 45
Paavai Institutions Department of MCA

What's the smallest number of bits (hence the minimum size of file) we can use to store
an arbitrary piece of text?

Huffman's scheme uses a table of frequency of occurrence for each symbol (or character)
in the input. This table may be derived from the input itself or from data which is representative
of the input. For instance, the frequency of occurrence of letters in normal English might be
derived from processing a large number of text documents and then used for encoding all text
documents. We then need to assign a variable-length bit string to each character that
unambiguously represents that character. This means that the encoding for each character must
have a unique prefix. If the characters to be encoded are arranged in a binary tree:

An encoding for each character is found by following the tree from the route to the
character in the leaf: the encoding is the string of symbols on each branch followed.

For example: Encoding tree for ETASNO

String Encoding
TEA 10 00 010
SEA 011 00 010
TEN 10 00 110

Notes:

1. As desired, the highest frequency letters - E and T - have two digit encodings, whereas all
the others have three digit encodings.
2. Encoding would be done with a lookup table.

A divide-and-conquer approach might have us asking which characters should appear in the
left and right subtrees and trying to build the tree from the top down. As with the optimal binary
search tree, this will lead to to an exponential time algorithm.

UNIT-I 1. 46
Paavai Institutions Department of MCA

A greedy approach places our n characters in n sub-trees and starts by combining the two
least weight nodes into a tree which is assigned the sum of the two leaf node weights as the
weight for its root node.

The time complexity of the Huffman algorithm is O(nlogn). Using a heap to store the weight
of each tree, each iteration requires O(logn) time to determine the cheapest weight and insert the
new weight. There are O(n) iterations, one for each item.

2.8.3 DECODING HUFFMAN-ENCODED DATA


Curious readers are, of course, now asking

"How do we decode a Huffman-encoded bit string? With these variable length strings, it's
not possible to break up an encoded string of bits into characters!"

The decoding procedure is deceptively simple. Starting with the first bit in the stream,
one then uses successive bits from the stream to determine whether to go left or right in the
decoding tree. When we reach a leaf of the tree, we've decoded a character, so we place that
character onto the (uncompressed) output stream. The next bit in the input stream is the first bit
of the next character.

2.9 BINARY SEARCH TREE

Binary tree that all elements in the left subtree of a node n are less than the contents of n,
and all elements in the right subtree of n are greater than or equal to the contents of n.

Uses : used in sorting and searching

UNIT-I 1. 47
Paavai Institutions Department of MCA

Figure 2.25 Binary Search Tree

2.9.1 APPLICATIONS OF BINARY TREES (find the duplication element from the list)

Binary tree is useful data structure when two-way decisions must be made at each
point in a process. For example find the all duplicates in a list of numbers. One way doing this
is each number compare with it’s precede it. However, it involves a large number of
comparisons.

The number of comparisons can be reduced by using a binary tree.

Step 1: from root, each successive number in the list is then compared to
the number in the root.
Step 2: If it matches, we have a duplicate.
Step 3: If it is smaller, we examine the left subtree.
Step 4: If it is larger, we examine the right subtree.
Step 5: If the subtree is empty, the number is not a duplicate and is placed

UNIT-I 1. 48
Paavai Institutions Department of MCA

into a new node at that passion in the tree.


Step 6: If the subtree is nonempty, we compare the number of the contents
of root of the subtree and entire process is repeated till all node
completed.
/* read the first number and insert it into a sinlge-node binary tree */
scanf(“%d”,&number);
tree = maketree (number);
while(there are numbers left in the input)
{
scanf(“%d”, &number);
p = q = tree;
while ( number ! = info(p) && q ! = NULL)
{
p = q;
if ( number < info (p) )
q = left (p);
else
q = right (p);
}
if(number = = info(p) )
printf(“ %d %s “, number, “is a duplicate”);
else if ( number < info(p) )
setleft( p, number );
else
setright(p, number);
}

UNIT-I 1. 49
Paavai Institutions Department of MCA

2.9.2 APPLICATION OF BIANRY TREE - (Sort the number in Ascending Order)

If a binary search tree is traversed in inorder(left,root,right) and the contents of each node
are printed as the node is visited, the numbers are printed in ascending order.

Figure 2.26 Binary Search Tree

For convince the above binary search tree if it is traversed inorder manner the result order is,

30, 46, 50, 58, 60, 70, 77 and 80 is ascending order sequence.

2.9.3 APPLICATION BINARY TREE – (Expression Tree)

Representing an expression containing operands and binary operators by a strictly binary


tree. The root of the strictly binary tree contains an operator that is to be applied to the results of
evaluating the expressions represented by the left and right subtrees. A node representing an
operator is a nonleaf, whereas a node representing an operand in a leaf.

UNIT-I 1. 50
Paavai Institutions Department of MCA

2.9.4 BINARY SEARCH TREE OPERATIONS

The basic operation on a binary search tree(BST) include, creating a BST, inserting an
element into BST, deleting an element from BST, searching an element and printing element of
BST in ascending order.

The ADT specification of a BST:

ADT BST
{
Create BST() : Create an empty BST;
Insert(elt) : Insert elt into the BST;
Search(elt,x) : Search for the presence of element elt and
Set x=elt, return true if elt is found, else
return false.
FindMin() : Find minimum element;
FindMax() : Find maximum element;
Ordered Output() : Output elements of BST in ascending order;
Delete(elt,x) : Delete elt and set x = elt;
}

2.9.4.1 INSERTING AN ELEMENT INTO BINARY SEARCH TREE

Algorithm InsertBST(int elt, NODE *T)


[ elt is the element to be inserted and T is the pointer to the root of the tree]

If (T= = NULL) then


Create a one-node tree and return
Else if (elt<key) then
InsertBST(elt, Tlchild)

UNIT-I 1. 51
Paavai Institutions Department of MCA

Else if(elt>key) then


InsertBST(elt, Trchild)
Else
“ element is already exist
return T
End

C coding to Insert element into a BST

struct node
{
int info;
struct node *lchild;
struct node *rchild;
};
typedef stuct node NODE;
NODE *InsertBST(int elt, NODE *T)
{
if(T = = NULL)
{
T = (NODE *)malloc(sizeof(NODE));
if ( T = = NULL)
printf ( “ No memory error”);
else
{
tinfo = elt;
tlchild = NULL;
trchild = NULL;
}
UNIT-I 1. 52
Paavai Institutions Department of MCA

}
else if ( elt < Tinfo)
tlchild = InsertBST(elt, tlchild);
else if ( elt > Tinfo)
trchild = InsertBST( elt, trchild);
return T;
}

2.9.4.2 SEARCHING AN ELEMENT IN BST

Searching an element in BST is similar to insertion operation, but they only return the
pointer to the node that contains the key value or if element is not, a NULL is return;

Searching start from the root of the tree;

If the search key value is less than that in root, then the search is left subtree;

If the search key value is greater than that in root, then the search is right subtree;

This searching should continue till the node with the search key value or null pointer(end
of the branch) is reached.

In case null pointer(null left/righ chile) is reached, it is an indication of the absence of the
node.

Algorithm SearchBST(int elt, NODE *T)


[ elt is the element to be inserted and T is the pointer to the root of the tree]
1. If (T= = NULL) then
Return NULL
2. If (elt<key) then /* elt is less than the key in root */
return SearchBST(elt, Tlchild)
Else if(elt>key) then /* elt is greater than the key in root */
UNIT-I 1. 53
Paavai Institutions Department of MCA

return SerachBST(elt, Trchild)


Else
return T
End
NODE * SearchBST(int elt, NODE *T)
{
if(T = = NULL)
return NULL;

if ( elt < Tinfo)


return SearchBST(elt, tlchild);
else if ( elt > Tinfo)
return SearchBST( elt, trchild);
else
return T;
}

2.9.4.3 FINDING MINIMUM ELEMENT IN A BST

Minimum element lies as the left most node in the left most branch starting from the root.
To reach the node with minimum value, we need to traverse the tree from root along the left
branch till we get a node with a null / empty left subtree.

Algorithm FindMin(NODE * T)
1. If Tree is null then
return NULL;
2. if lchild(Tree) is null then
return tree
else

UNIT-I 1. 54
Paavai Institutions Department of MCA

return FindMin(Tlchild)
3. End

NODE * FindMin( NODE *T )


{
if(T = = NULL)
return NULL;

if ( Tlchild = = NULL)
return tree;
else
return FindMin(Treelchild);
}

2.9.4.4 FINDING MAXIMUM ELEMENT IN A BST

Maximum element lies as the right most node in the right most branch starting from the
root. To reach the node with maximum value, we need to traverse the tree from root along the
right branch till we get a node with a null / empty right subtree.

Algorithm FindMax(NODE * T)
1. If Tree is null then
return NULL;
2. if rchild(Tree) is null then
return tree
else
return FindMax(Trchild)
3. End

UNIT-I 1. 55
Paavai Institutions Department of MCA

NODE * FindMax( NODE *T )


{
if(T = = NULL)
return NULL;

if ( Trchild = = NULL)
return tree;
else
return FindMax(Treerchild);
}

2.9.4.5 DELETING AN ELEMENT IN A BST

The node to be deleted can fall into any one of the following categories;
1. Node may not have any children ( ie, it is a leaf node)
2. Node may have only on child ( either left / right child)
3. Node may have two children ( both left and right)

Algorithm DeleteBST(int elt, NODE * T)


1. If Tree is null then
print “Element is not found”
2. If elt is less than info(Tree) then
locate element in left subtree and delete it
else if elt is greater than info(Tree) then
locate element in right subtree and delete it
else if (both left and right child are not NULL) then /* node with two children */

UNIT-I 1. 56
Paavai Institutions Department of MCA

begin
Locate minimum element in the right subtree
Replace elt by this value
Delete min element in right subtree and move the remaining tree as its
right child
end
else
if leftsubtree is Null then
/* has only right subtree or both subtree Null */
replace node by its rchild
else
if right subtree is Null then
replace node by its left child\
end

free memory allocated to min node


end
return Tree
End

NODE *DeleteBST(int elt, NODE * T)


{
NODE * minElt;

if(T = = NULL)
printf(“ element not found \n”);
else if ( elt < Tinfo)
Tlchild = DeleteBST(elt, Tlchild);
else if ( elt > Tinfo)
UNIT-I 1. 57
Paavai Institutions Department of MCA

Trchild = DeleteBST(elt, Trchild);


else
if(Tlchild && T rchild)
{
minElt = FindMin(Trchild);
Tinfo = minEltinfo;
Trchild = DeleteBST(Tinfo, Trchild);
}
else
{
minElt = Tree;
if (Tlchild = = NULL)
T = Trchild;
else if ( Trchild = = NULL)
T = Tlchild;
Free (minElt);
}
return T;
}

2.9.5 THREADED BINARY TREE

2.9.5.1 DEFINITION

A binary tree is threaded by making all right child pointers that would normally be null
point to the inorder successor of the node, and all left child pointers that would normally be null
point to the inorder predecessor of the node."

UNIT-I 1. 58
Paavai Institutions Department of MCA

In binary trees, left and right child pointers of all leaf nodes(empty subtrees) are set to
NULL while traversing the tree. Normally, a stack is maintained to hold the successor of the
node in traversal. Replacing such NULL pointers with pointers to successor in traversal could
eliminate the use of stack. These pointers are called as threads, and such a tree is called threaded
binary tree.

If right child pointers nodes with empty right subtree, may be made to point its successor
then the tree is called right in-threaded binary trees.

If left child pointers nodes with empty left subtree, may be made to point its successor
then the tree is called left in-threaded binary trees.

2.9.5.2 RIGHT IN-THREADED BINARY TREES

Note : The last node in the tree not threaded. In example F is at rightmost and not
threaded.

2.9.5.3 IMPLEMENTING A RIGHT IN-THREADED BINARY TREE

To implement right in-threaded binary tree we need info, left , right and an extra logical
field rthread is included within each node to indicate whether or not its right pointer is a thread.

lchild info rchild isthread

UNIT-I 1. 59
Paavai Institutions Department of MCA

Thus a node is defined as follows :


struct nodetype
{
int info;
struct nodetype * left; /* pointer to left son */
struct nodetype *right; /* pointer to right son */
int rthread; /* rthread is TURE if right is NULL
} (or) a non-NULL thread */
typedef struc nodetype *NODEPTR;

We present a routine to implement inorder traversal of a right in-threaded binary tree;

intrav2(tree)
NODEPTR tree;
{
NODEPTR q, p;
p = tree;
do
{
q=NULL;
while(p!=NULL) / * traverse left branch */
{
q = p;
p = p  left;
}
if(q ! = NULL)
{
printf(“%d \n”,qinfo);
p = q  right;
UNIT-I 1. 60
Paavai Institutions Department of MCA

while(qrthread && p!=NULL)


{
printf(“%d \n”, p info);
q = p;
p = pright;
}
}
}while(q! = NULL);
}
intrav3(tree)
NODEPTR tree;
{
NODEPTR q, p;
q = NULL;
p = tree;
do
{
while(p!=NULL) / * traverse left branch */
{
q = p;
p = p  left;
}
if(q ! = NULL)
{
printf(“%d \n”, qinfo);
p = qright;
}
while( q ! = NULL && p = = NULL )
UNIT-I 1. 61
Paavai Institutions Department of MCA

{
do
{
/* node(q) has no right son. Back up until a left son or the tree root is
encountered */

p = q;
q = pfather;
}while( ! isleft(p) && q ! = NULL);

if ( q ! = NULL)
{
printf(“%d \n”,qinfo);
p = qright;
}
}/* end while */
} while (q ! = NULL);
}

2.9.6 HETEROGENEOUS BINARY TREES

Information contained in different nodes of binary tree is of different type. Such a tree is
called Heterogeneous Binary Tree. To represent such a tree in C we may use a union as follows :

#define OPERATOR 0
#define OPERAND 1
struct nodetype
{
short int utype; / * operator or operand */

UNIT-I 1. 62
Paavai Institutions Department of MCA

union
{
char chinfo;
float numinfo;
} info;
struct nodetype *left;
struct nodetype *right;
};
typedef struct nodetype *NODEPTR;

float evalbintree (tree)


NODEPTR tree;
{
float opnd1,opnd2;
char symb;
if( treeutype = = OPERAND)
return(treenuminfo);
/* treeutype = = OPERATOR */
opnd1 = evalbintree(treeleft); /* evaluate the left subtree */
opnd2 = evalbintree(treeright); /* evaluate the right subtree */

symb = treechinfo; /* extract the operator */


/* apply the operator and return the result */

return oper(symb,opnd1,opnd2);
}

UNIT-I 1. 63
Paavai Institutions Department of MCA

2.10 QUESTION BANK

PART A -2 Marks

1. List out few of the Application of tree data-structure?


2. Draw a binary Tree for the expression :
A * B - (C + D) * (P / Q)
3. Write a program in ‘C' language which accepts inorder and preorder traversal
outputs of a Binary Tree as input and prints the corresponding Binary tree.
3. Define tree?
4. Write a function and the node data structure to visit all of the nodes in a binary
tree.
5. Write a function to find the depth of a binary tree.
6. Define Depth of tree?
7. Define Degree of a node?
8. Define Degree of a tree?
9. Define Terminal node or leaf?
10. Define Non-terminal node?
11. Define sibling?
12. Define binary tree?
13. Define expression tree?
14. Construction of expression trees?
15. Define tree traversal and mention the type of traversals?
16. Compare General tree with binary tree.
17. Show the maximum number of nodes in an binary tree of height H is 2H+1-1
18. List the steps involved in deleting a node from a binary search tree.
19. A full node is a node with two children. Prove that the number of full nodes
plus one is equal to the number of leaves in a non empty binary tree.
20. List the applications of tree.

UNIT-I 1. 64
Paavai Institutions Department of MCA

PART B -16 Marks

1. What is a binary search tree? Explain with example?


2. Explain binary tree traversals?
3. What is a threaded binary tree? Explain its operation with example?
4. Explain the expression trees?
5. Write the procedure to convert general tree to binary tree.
6. What is a Binary tree? Explain Binary tree traversals in C?
7. Explain Representing lists as Binary tree? Write algorithm for finding Kth element and
deleting an element?
8. Write a program to find duplicate numbers in an input list which includes the routines
maketree and setleft?
9. Explain Threaded binary tree with its type?
10. Explain Binary tree representation?

UNIT-I 1. 65

You might also like