Unit 1 (Trees)
Unit 1 (Trees)
Unit - I TREES
AY 2022-2023 SEM-II
Unit I - Syllabus
In linear data structure, single level is involved. Therefore, we can traverse all the
elements in single run only.
Linear data structures are easy to implement because computer memory is arranged
in a linear way.
Linear data structures work well mainly in the development of application software.
Non-Linear Data Structures
Data structures where data elements are not arranged sequentially or linearly are
called non-linear data structures.
In a non-linear data structure, single level is not involved. Therefore, we can’t traverse
all the elements in single run only.
Non-linear data structures are not easy to implement in comparison to linear data
structure.
Non-linear data structures work mainly well in image processing and Artificial
Intelligence.
Linear and Non-Linear Data Structure
In linear (or sequential) organization, all the elements of the data
can be arranged in a particular sequence, and each element has a
unique successor (and/or predecessor) in the sequence.
When each element may have one or more successors (or
predecessors), it is called a non-linear data structure.
In a linear data structure, data elements are In a non-linear data structure, data elements
arranged in a linear order where each and every are attached in hierarchically manner.
element is attached to its previous and next
adjacent.
In linear data structure, single level is involved. Whereas in non-linear data structure, multiple
levels are involved.
Its implementation is easy in comparison to While its implementation is complex in
non-linear data structure. comparison to linear data structure.
Data elements can be traversed in a single run Data elements can’t be traversed in a single run
only. only.
Its examples are: array, stack, queue, linked list, Its examples are: trees and graphs.
etc.
Applications of linear data structures are mainly Applications of non-linear data structures are in
in application software development. Artificial Intelligence and image processing.
Tree : Basic Concepts
Node and Branches
Tree is a non-linear data
structure which organizes data
in hierarchical structure.
If that node has left a child, then left reference field stores
the address of that left child node otherwise stores NULL.
If that node has the right sibling, then right reference field
stores the address of right sibling node otherwise stores
NULL.
General Tree
• Non-Linear Data Structures
• Each node can have zero or many(arbitrary)
number of children's.
• There are many sub-trees in a general tree.
• In general tree, there is no limitation on the degree
of a node.
• The topmost node of a general tree is called the
root node.
• In a general tree, each node has in-degree(number
of parent nodes) one and maximum out-
degree(number of child nodes) n.
Binary Tree
In a normal tree, every node can have any number of
children. A binary tree is a special type of tree data
structure in which every node can have a maximum of
2 children. One is known as a left child and the other
is known as right child.
General trees can be represented as ADT's in whatever form they exist. However, there are some
substantial problems. First, the number of references for each node must be equal to the
maximum that will be used in the tree. Obviously, some real problems are presented when another
subtree is added to a node which already has the maximum number attached to it. It is also
obvious that most of the algorithms for searching, traversing, adding and deleting nodes become
much more complex in that they must now cope with situations where there are not just two
possibilities for any node but multiple possibilities.
Need of converting a general tree into
binary tree
Fortunately, general trees can be converted to binary trees. They don’t often end up being well
formed or full, but the advantages accrue from being able to use the algorithms for processing
that are used for binary trees with minor modifications. Therefore, each node requires only two
references but these are not designated as left or right. Instead they are designated as the
reference to the first child and the reference to next sibling. Therefore the usual left pointer really
points to the first child of the node and the usual right pointer points to the next sibling of the
node.
One obvious saving in this structure is the number of fields which must be used for references. In
this way, moving right from a node accesses the siblings of the node ( that is all of those nodes on
the same level as the node in the general tree). Moving left and then right accesses all of the
children of the node (that is the nodes on the next level of the general tree).
Conversion of General Tree into Binary Tree
Method_1
Step1: Delete all the branches originating in every node except the left most branch.
Step2 : Draw edges from a node to the node which in the right, if any, which is
situated in the same level(siblings).
Conversion of General Tree into Binary Tree
Conversion of General Tree into Binary Tree
Conversion of General Tree into Binary Tree
Method_2
Consider one by one each node in tree and apply following steps on it.
Root of binary tree = Root of general tree
Left child of node in binary tree = Leftmost child of node in general
tree
Right child of node in binary tree = Right sibling of the node in
general tree
Conversion of General Tree into Binary Tree : Example1
A
A
C
Binary Tree E
B C D
F D
E F G H I
H
Binary
Tree
Traversal of tree
• Inorder (LDR) : I - D - J - B - F - A - G - K - C – H
PREORDER (Depth First)
• Preorder(DLR) : A - B - D - I - J - F - C - G - K - H
1. Visit the root.
• Postorder(LRD) : I - J - D - F - B - K - G - H - C - A 2. Traverse the left subtree in preorder
3. Traverse the right subtree in preorder
INORDER
POSTORDER
Method:
In a Preorder sequence, leftmost element is the root of the tree. So we know ‘A’
is root for given sequences.
By searching ‘A’ in Inorder sequence, we can find out all elements on left side of
‘A’ are in left subtree and elements on right are in right subtree. We recursively
follow above steps.
Binary Tree Construction
Pre-order Sequence : 1 2 4 5 3 6
In-order Sequence : 4 2 5 1 6 3
Binary Tree Construction
Inorder Traversal : { 4, 2, 1, 7, 5, 8, 3, 6 }
Postorder Traversal : { 4, 2, 7, 8, 5, 6, 3, 1 }
From Postorder definition, we can find the root first, the last element
in the sequence is 1.
If 1 is the root of the binary tree, then using the in-order definition,
elements to the left will be present in the left subtree, and elements
to the right will be present in the right subtree with root as 1,
respectively.
Again find root of left subtree and right subtree by using posorder
traversal. Repeats same steps.
Binary Tree Construction
Binary Tree Construction
Can you construct the binary tree, given two traversal sequences?
Depends on which two sequences are given.
Example1:
• Preorder : 1 ,2 ,4 ,8 ,9 ,10 ,11 ,5 ,3 ,6 ,7
• Inorder : 8 ,4 ,10 ,9 ,11 ,2 ,5, 1, 6, 3 ,7
Example2:
• Postorder : 9, 1, 2, 12, 7, 5, 3, 11, 4, 8
• Inorder : 9 , 5, 1, 7, 2, 12, 8, 4, 3, 11
Binary Tree Construction
• Example3:
• Preorder : A ,B ,D ,H ,I ,J ,K ,E ,C, F, G
• Inorder : H ,D ,J ,I,K ,B ,E, A, F, C ,G
• Example4:
• Postorder : I, A, B, L, G, E, C, K, D, H
• Inorder : I , E, A, G, B, L, H, D, C, K
Binary Tree Traversals
Tree Traversal algorithms can be classified broadly into two categories:
Depth-First Search (DFS) Algorithms
Breadth-First Search (BFS) Algorithms
Binary Tree Traversals
Tree Traversal using Depth-First Search (DFS) algorithm can be further classified into three
categories:
Preorder Traversal (current-left-right): Visit the current node before visiting any nodes inside the
left or right subtrees. Here, the traversal is root – left child – right child. It means that the root
node is traversed first then its left child and finally the right child.
Inorder Traversal (left-current-right): Visit the current node after visiting all nodes inside the left
subtree but before visiting any node within the right subtree. Here, the traversal is left child –
root – right child. It means that the left child is traversed first then its root node and finally the
right child.
Postorder Traversal (left-right-current): Visit the current node after visiting all the nodes of the
left and right subtrees. Here, the traversal is left child – right child – root. It means that the left
child has traversed first then the right child and finally its root node.
Binary Tree Traversals
Tree Traversal using Breadth-First Search (BFS) algorithm can be
further classified into one category:
Level Order Traversal: Visit nodes level-by-level and left-to-right
fashion at the same level. Here, the traversal is level-wise. It means
that the most left child has traversed first and then the other
children of the same level from left to right have traversed.
Binary Tree Traversals
Algorithm:
Starting at the root, find the deepest and rightmost node in the
binary tree and the node which we want to delete.
Replace the deepest rightmost/leftmost node’s data with the node
to be deleted.
Then delete the deepest rightmost/leftmost node.
Operations in Binary Tree : Deletion
• Algorithm:
• Starting at the root, find the
deepest and rightmost node
in the binary tree and the
node which we want to
delete.
• Replace the deepest
rightmost node’s data with
the node to be deleted.
• Then delete the deepest
rightmost node.
Operations in Binary Tree : Search
• The approach to search for any particular element in the tree node
is to perform any tree traversal on the given tree and check if there
exists any node with the given searched value or not.
• If found to be true, then print “Element is Found”. Otherwise, print
“Element Not Found”.
• Search Max element and Min element
Operations in Binary Tree :
Search Max element and Min element
In Binary Search Tree, we can find maximum by
traversing right pointers until we reach the rightmost
node. But in Binary Tree, we must visit every node to
figure out maximum.
So the idea is to traverse the given tree and for every
node return maximum of 3 values and compare that
three values.
[Link]’s data.
[Link] in node’s left subtree.
[Link] in node’s right subtree.
Operations in Binary Tree :
Search Max element and Min element
int findMax(Node* root) int findMin(Node *root)
{
{
if (root == NULL) if(root==NULL)
{
return INT_MIN; return INT_MAX;
}
int res = root->data; int res=root->data;
int lres = findMax(root->left); int left=findMin(root->left);
int right=findMin(root->right);
int rres = findMax(root->right); if(left<res)
{
if (lres > res) res=left;
res = lres; }
if(right<res)
if (rres > res) {
res=right;
res = rres; }
return res; return res;
}
}
Operations in Binary Tree :
Search Max element and Min element
• We can find max and min element in binary tree with the help of
level order traversal of binary tree without recursion.
• The property of level order traversal is that it passes through each
element of the binary tree. Utilizing this property we can keep a
track of the maximum element encountered in complete traversal.
Then simply return the element.
• So by using level order traversal, we can find the maximum
element/minimum element without using recursion.
Operations in Binary Tree :
Search Max element and Min element
Time Complexity: O(N), where N is number of
nodes as every node of tree is traversed once by
findMax() and findMin().
20 30 60
12 25 5 40 70
10 15 27 2 65 80
Operations in Binary Search Tree
Create/Construct
Insert
Delete
Search
Traversal
Binary Search Tree operation : Create
• To construct BST, we need to apply BST properties.
• Data Values : 100,50,20,10,60,70,90
• Data values : MON, TUES, WED, THRUS, FRI, SAT, SUN
• Consider 10 Names of your friends and construct BST
Binary Search Tree operation : Insert
A new key in BST is always inserted at
the leaf.
It is the simplest case to delete a node in BST. Here, we have to replace the leaf node with
NULL and simply free the allocated space.
Binary Search Tree operation : Delete
2) Node to be deleted has only one child:
In this case, we have to replace the target node(node which u want to
delete)with its child, and then delete the child node.
Binary Search Tree operation : Delete
3) Node to be deleted has two children: Use inorder successor
This case of deleting a node in BST is a bit complex among other two cases.
In such a case, the steps to be followed are listed as follows -
First, find the inorder successor of the node to be deleted.
After that, replace that node with the inorder successor until the target
node is placed at the leaf of tree.
And at last, replace the node with NULL and free up the allocated space.
The inorder successor is required when the right child of the node is not
empty. We can obtain the inorder successor by finding the minimum
element in the right subtree.
Binary Search Tree operation : Delete
3) Node to be deleted has two children: Use inorder successor
Binary Search Tree operation : Delete
This case of deleting a node in BST is a bit complex among other two cases.
In such a case, the steps to be followed are listed as follows -
First, find the inorder predecessor of the node to be deleted.
After that, replace that node with the inorder predecessor until the target node is
placed at the leaf of tree. 3) Node to be deleted has two children: Use inorder
predecessor
And at last, replace the node with NULL and free up the allocated space.
The inorder predecessor is required when the left child of the node is not empty. We
can obtain the inorder predecessor by finding the maximum element in the left
subtree.
Binary Search Tree operation : Search
• Whenever an element is to be searched, start searching from the root
node. Then if the data is less than the key value, search for the
element in the left subtree. Otherwise, search for the element in the
right subtree. Follow the same algorithm for each node.
Binary Search Tree operation : Search
In Binary search tree, searching a node is easy because elements in BST are stored
in a specific order.
The steps of searching a node in Binary Search tree are listed as follows -
First, compare the element to be searched with the root element of the tree.
If root is matched with the target element, then return the node's location.
If it is not matched, then check whether the item is less than the root element,
if it is smaller than the root element, then move to the left subtree.
If it is larger than the root element, then move to the right subtree.
Repeat the above procedure recursively until the match is found.
If the element is not found or not present in the tree, then return NULL.
Binary Search Tree operation : Search
Ascending and Descending order of BST
• Inorder traversal of BST prints it in ascending order.
• In-order means left-root-right.
• You can also do right-root-left. : It’s just the way of traversal, that’s
it!! :)
• This is called reverse in-order by some people. So, in a BST, if you do
reverse in-order, you get descending order sorted array.
Example:To perform the Insertion and Deletion operation
on below tree
Insert -20
Delete- 90, 22, 50
Advantages of Binary search tree
Searching an element in the Binary search tree is easy as we always
have a hint that which subtree has the desired element.
As compared to array and linked lists, insertion and deletion
operations are faster in BST.
Applications of Binary search tree
BSTs are used for a lot of applications due to its ordered structure.
BSTs are used for indexing and multi-level indexing.
They are also helpful to implement various searching algorithms.
It is helpful in maintaining a sorted stream of data.
TreeMap and TreeSet data structures are internally implemented
using self-balancing BSTs.
Binary Search Tree operation : Traversal
DFS Traversals:
• Preorder
• Inorder
• Postorder
BFS Traversals:
Difference between binary tree and
binary search tree
Sr. No. Binary Tree Binary Search Tree
1 BINARY TREE is a nonlinear data structure where BINARY SEARCH TREE is a node based binary tree that further
each node can have at most two child nodes. has right and left subtree that too are binary search tree.
2 BINARY TREE is unordered hence slower in process Insertion, deletion, searching of an element is faster in BINARY
of insertion, deletion, and searching. SEARCH TREE than BINARY TREE due to the ordered
characteristics
3 Binary trees allow duplicate values. Binary Search Tree does not allow duplicate values.
4 The speed of deletion, insertion, and searching Because the Binary Search Tree has ordered properties, it conducts
operations in Binary Tree is slower as compared to element deletion, insertion, and searching faster.
Binary Search Tree because it is unordered.
6 There are several types. Most common ones are the The most popular ones are AVL Trees, Splay Trees, Tango Trees, T-
Complete Binary Tree, Full Binary Tree, Extended Trees.
Binary Tree
Conversion of a binary tree to binary search tree
without changing spatial structure of given binary tree
Conversion of a binary tree to binary search tree
without changing spatial structure of given binary tree
1. Create a temp array A[] that stores inorder traversal of the tree.
2. Sort the temp array A[].
3. copy array elements to tree nodes one by one.
Conversion of a binary tree to binary search tree
without changing spatial structure of given binary tree
Conversion of a binary tree to binary search tree
without changing spatial structure of given binary tree
Conversion of a binary tree to binary search tree
without changing spatial structure of given binary tree
Examples:
Threaded Binary Tree : Need
In a binary tree, there are many nodes that have an empty left child or empty right
child or both.
In a linked representation of a binary tree, the number of null links (null pointers) are
actually more than non-null pointers.
Total 12 pointers
7 null pointers
5 actual pointers
122
Threaded Binary Tree
In general, for any binary tree with n nodes there will be -
(n+1) null pointers and
2n total pointers.
Objective : make effective use of these null pointers ( A. J. perils & C. Thornton )
i.e. Replace all the null pointers by the appropriate pointer values called threads.
Binary tree with such pointers are called threaded binary tree.
Utilize these fields in such a way so that -
Empty left child of a node points to its inorder predecessor.
Empty right child of the node points to its inorder successor.
123
Threaded Binary Tree
One way threading/Single Threaded BT
Where a NULL right pointers is made to point to inorder successor(if successor exits)
In one-way threaded binary trees, a thread will appear either in the right or left link field of a
node.
If it appears in the right link field of a node then it will point to the next node that will appear on
performing in order traversal. Such trees are called Right threaded binary trees.
If thread appears in the left field of a node then it will point to the nodes inorder predecessor.
Such trees are called Left threaded binary trees.
124
Threaded Binary Tree : Example
9 NULL pointers
In-order :- 30 40 50 60 65 69 72 80
125
One-Way Threaded Binary Tree : Example
The empty left child field of a node can be used to point to its inorder predecessor. OR
The empty right child field of a node can be used to point to its in-order successor.
A field holding the address of its in-order successor is known as thread.
In-order :- 30 40 50 60 65 69 72 80
126
Two-Way Threaded Binary Tree : Example
The empty left child field of a node can be used to point to its inorder predecessor. AND
The empty right child field of a node can be used to point to its in-order successor.
In-order :- 30 40 50 60 65 69 72 80
127
Threaded Binary Tree with head node
Inorder :
D,B,F,E,A,G,C,L,J,H,K
129
Threaded Binary Tree : Two-Way
A thread will also appear in the left field of a node and will point to the preceding node in the
in-order traversal of tree T.
The left pointer of the first node and the right pointer of the last node will contain the null
value.
Inorder :
D,B,F,E,A,G,C,L,J,H,K
130
Two-Way Threaded Binary Tree with Header Node
The NULL left pointer of the first node and the NULL right pointer of the last node points to the
Header Node.
NULL
Left
Right
Inorder :
D,B,F,E,A,G,C,L,J,H,K
131
Threaded Binary Tree: Representation
In the memory representation of a threaded binary tree, it is necessary to distinguish
between a normal pointer and a thread.
The structure of a node in a threaded binary tree is a bit different from that of a normal
binary tree.
Each node of a threaded binary tree contains two extra pieces of information, namely left
thread and right thread.
The left and right thread fields of a node can have two values:
0: Indicates a normal link to the child node
1: Indicates a thread pointing to the inorder predecessor or inorder successor
132
Node Structure in a Threaded Binary Tree
133
Insertion in TBT
NULL
0 20 0
0 18 1 0 39 0
0 15 1 1 25 1 1 55 1
1 12 1
Insert 16 Insert 22
134
Insertion in Threaded Binary Tree
135
Insertion in Threaded Binary Tree
136
Insertion in Threaded Binary Tree
137
Display (Inorder) TBT
138
Insertion NULL
0 20 0
0 18 1 0 39 0
0 15 1 1 25 1 1 55 1
1 12 1
142
Deletion in threaded binary tree
143
Deletion in threaded binary tree
144
Deletion in threaded binary tree
5 10 13 14 15 16 20 30
145
Deletion in threaded binary tree
146
Deletion in threaded binary tree
NULL
B C
F D E
G
153
Inorder traversal of TBT : Example
Linked representation with the threads.
In-order traversal -G,F,B,A,D,C,E
154
Advantages of Threaded Binary Tree
The traversal operation is more faster than that of its unthreaded version.
With threaded binary tree non-recursive implementation is possible which can run faster
and does not require the botheration of stack management.
We can efficiently determine the predecessor and successor nodes starting from any node.
Any node can be accessible from any other node.
Threads are usually move to upward whereas links are downward.
One can move in their direction and nodes are in fact circularly linked.
155
Advantages of Threaded Binary Tree
In this Tree it enables linear traversal of elements.
It eliminates the use of stack as it perform linear traversal, so save memory.
Enables to find parent node without explicit use of parent pointer.
Threaded tree give forward and backward traversal of nodes by in-order fashion.
Nodes contain pointers to in-order predecessor and successor.
For a given node, we can easily find inorder predecessor and successor. So, searching
is much more easier.
Disadvantages of Threaded Binary Tree
Disadvantages of threaded binary tree:
Insertion into and deletions from a threaded tree are although time consuming operations but
these are very easy to implement.
This tree require additional bit to identify the threaded link.
Every node in threaded binary tree need extra information(extra memory) to indicate whether
its left or right node indicated its child nodes or its inorder predecessor or successor. So, the
node consumes extra memory to implement.
Insertion and deletion are way more complex and time consuming than the normal one since
158
Expression Tree
The expression tree is a binary tree in
which each internal node corresponds to
the operator and each leaf node
corresponds to the operand.
Inorder traversal of expression tree
produces infix expression.
Preorder traversal of expression tree
produces prefix expression.
Postorder traversal of expression tree
produces postfix expression.
3 + ((5+9)*2)
Create an expression tree from postfix
expression
Scan the given expression Left to Right.
If it is an operand then
Make a node; set left and right pointers as NULL
Push the node to stack.
If it is an operator then
Make a node; Pop 2 nodes
1st popped node is attached as right child
2nd popped node is attached as left child.
Push the node to stack.
Repeat the above process till expression is scanned and stack is empty
Create an expression tree from postfix
expression : Example
AB+CDE/-*
945*86/-/
Create an expression tree from prefix
expression
Scan the given expression Right to Left.
If it is an operand then
Make a node; set left and right pointers as NULL
Push the node to stack.
If it is an operator then
Make a node; Pop 2 nodes
1st popped node is attached as left child
2nd popped node is attached as right child.
Push the node to stack.
Repeat the above process till expression is scanned and stack is empty
Create an expression tree from prefix
expression : Example
*+AB*C+DE
Expression tree-evaluation
*+52*3+87
Applications of Expression Tree
Use of Expression tree
The main use of these expression trees is that it is used to evaluate,
analyze and modify the various expressions.
It is also used to find out the Associativity of each operator in the
expression.
It is also used to solve the postfix, prefix, and infix expression
evaluation.
Huffman's coding
Huffman Coding is a technique of compressing data to reduce its size
without losing any of the details.
It was first developed by David Huffman.
Huffman Coding is generally useful to compress the data in which there
are frequently occurring characters.
Huffman Coding is a famous Greedy Algorithm.
It uses variable length encoding.
It assigns variable length code to all the characters. The code length of a
character depends on how frequently it occurs in the given text. The
character which occurs most frequently gets the smallest code. The
character which occurs least frequently gets the largest code.
Huffman's coding
• Encoding: In computers, encoding is the process of putting a
sequence of characters (letters, numbers, punctuation, and certain
symbols) into a specialized format for efficient transmission or
storage.
• Decoding is the opposite process -- the conversion of an encoded
format back into the original sequence of characters.
• Fixed Length Encoding : All the letters/symbols are represented
using an equal number of bits.
• Variable length Encoding : All the letters/symbols are represented
using an different number of bits.
Huffman's coding
Prefix Rule-
Step-01:
Create a leaf node for each character of the text.
Leaf node of a character contains the occurring frequency of that character.
Step-02:
Arrange all the nodes in increasing order of their frequency value.
Construction of Huffman Tree
Step-03:
Considering the first two nodes having minimum frequency,
Create a new internal node.
The frequency of this new node is the sum of frequency of those two nodes.
Make the first node as a left child and the other node as a right child of the newly
created node.
Step-04:
Keep repeating Step-02 and Step-03 until all the nodes form a single tree.
The tree finally obtained is the desired Huffman Tree.
Construction of Huffman Tree
Important Formulas-
The following 2 formulas are important to solve the problems based on
Huffman Coding-
Formula-01:
Construction of Huffman Tree
Formula-02: