MC4101-ADSA Unit-II 1
MC4101-ADSA Unit-II 1
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.
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
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.
UNIT-I 1. 4
Paavai Institutions Department of MCA
UNIT-I 1. 5
Paavai Institutions Department of MCA
CONTENTS
UNIT-I 1. 6
Paavai Institutions Department of MCA
LECTURE NOTES
2.1 NEED FOR NON-LINEAR STRUCTURES
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
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.
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
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
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
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.
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
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
D E F G
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
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
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
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.
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
The operations,
Example: -
UNIT-I 1. 17
Paavai Institutions Department of MCA
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
UNIT-I 1. 19
Paavai Institutions Department of MCA
stuct nodetype
{
int info;
struct nodetype *left;
struct nodetype *right;
struct nodetype *father;
};
typedef struct nodetype *NODEPTR;
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;
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
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 A
B E F B E F
C D G H I C D G H I
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
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:
When we have a single expression based on a binary operator, we draw the expression tree as
follows:
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
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
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.
UNIT-I 1. 27
Paavai Institutions Department of MCA
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).
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.
There are basically three ways of binary tree traversals. They are:
UNIT-I 1. 29
Paavai Institutions Department of MCA
struct node
{
int info;
struct node *lchild;
struct node *rchild;
}
typedef struct node NODE;
Algorithm C Coding
UNIT-I 1. 30
Paavai Institutions Department of MCA
B E
C D F
The Output is : C à B à D àA àE à F
UNIT-I 1. 31
Paavai Institutions Department of MCA
Algorithm C function
B
UNIT-I E 1. 32
C D F
Paavai Institutions Department of MCA
The Output is : A àB à C à D à E à F
Algorithm C function
UNIT-I 1. 33
Paavai Institutions Department of MCA
B E
C D F
The Output is : C à D à B à F à E à A
UNIT-I 1. 34
Paavai Institutions Department of MCA
2.5.3.1 INORDER_TRAVERSAL
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
2.5.3.2 PREORDER_TRAVERSAL
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
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.
UNIT-I 1. 37
Paavai Institutions Department of MCA
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
UNIT-I 1. 39
Paavai Institutions Department of MCA
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:
UNIT-I 1. 40
Paavai Institutions Department of MCA
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
Trees are one of the most useful data structures in Computer Science. Some of the
common applications of trees are:
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.
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:
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.
UNIT-I 1. 44
Paavai Institutions Department of MCA
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.
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.
"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.
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.
UNIT-I 1. 47
Paavai Institutions Department of MCA
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.
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
UNIT-I 1. 49
Paavai Institutions Department of MCA
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.
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.
UNIT-I 1. 50
Paavai Institutions Department of MCA
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.
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;
}
UNIT-I 1. 51
Paavai Institutions Department of MCA
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
{
tinfo = elt;
tlchild = NULL;
trchild = NULL;
}
UNIT-I 1. 52
Paavai Institutions Department of MCA
}
else if ( elt < Tinfo)
tlchild = InsertBST(elt, tlchild);
else if ( elt > Tinfo)
trchild = InsertBST( elt, trchild);
return T;
}
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;
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.
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(Tlchild)
3. End
if ( Tlchild = = NULL)
return tree;
else
return FindMin(Treelchild);
}
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(Trchild)
3. End
UNIT-I 1. 55
Paavai Institutions Department of MCA
if ( Trchild = = NULL)
return tree;
else
return FindMax(Treerchild);
}
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)
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
if(T = = NULL)
printf(“ element not found \n”);
else if ( elt < Tinfo)
Tlchild = DeleteBST(elt, Tlchild);
else if ( elt > Tinfo)
UNIT-I 1. 57
Paavai Institutions Department of MCA
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.
Note : The last node in the tree not threaded. In example F is at rightmost and not
threaded.
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.
UNIT-I 1. 59
Paavai Institutions Department of MCA
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”,qinfo);
p = q right;
UNIT-I 1. 60
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 = pfather;
}while( ! isleft(p) && q ! = NULL);
if ( q ! = NULL)
{
printf(“%d \n”,qinfo);
p = qright;
}
}/* end while */
} while (q ! = NULL);
}
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;
return oper(symb,opnd1,opnd2);
}
UNIT-I 1. 63
Paavai Institutions Department of MCA
PART A -2 Marks
UNIT-I 1. 64
Paavai Institutions Department of MCA
UNIT-I 1. 65