0% found this document useful (0 votes)
58 views175 pages

Unit 1 (Trees)

The document outlines the syllabus for a Data Structures & Algorithms course at MIT School of Computing, focusing on trees, including basic terminology, binary trees, binary search trees, and threaded binary trees. It explains the differences between linear and non-linear data structures and provides detailed tree terminology, representations, and types of trees. Additionally, it covers various tree traversal methods and operations, along with case studies related to expression trees and Huffman's coding.

Uploaded by

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

Unit 1 (Trees)

The document outlines the syllabus for a Data Structures & Algorithms course at MIT School of Computing, focusing on trees, including basic terminology, binary trees, binary search trees, and threaded binary trees. It explains the differences between linear and non-linear data structures and provides detailed tree terminology, representations, and types of trees. Additionally, it covers various tree traversal methods and operations, along with case studies related to expression trees and Huffman's coding.

Uploaded by

yashrajnikam4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

MIT Art Design and Technology University

MIT School of Computing, Pune

21BTCS402-Data Structures & Algorithms

Class - S.Y. (SEM-II)

Unit - I TREES

Prof. Chhaya Mhaske

AY 2022-2023 SEM-II
Unit I - Syllabus

Unit I – TREES 09 hours

• Tree- basic terminology, General tree and its representation


• Binary Tree – representation using sequential and linked organization ,properties, converting tree to binary
tree, binary tree traversals in-order, pre-order, post order, level wise -depth first and
breadth first, Operations on binary tree
• Binary Search Tree (BST)- BST operations
• Threaded binary tree- concepts, threading, insertion and deletion of nodes in in-order
threaded binary tree, in order traversal of in-order threaded binary tree
• Case Study- Use of binary tree in expression tree-evaluation and Huffman's coding
Lecture 1
Linear Data Structure
 The term “linear” means “belonging to a line”
 A Linear data structure consists of elements which are ordered i.e. in a
line.
 The elements form a sequence such that there is a first element,
second, …… and last element.
 Thus, the elements of a linear data structure have a one-one
relationship.
 Examples : Array.
Linear Data Structures
 Data structure where data elements are arranged sequentially or linearly where each
and every element is attached to its previous and next adjacent is called a linear data
structure.

 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.

 Linear Data Structures: Array, Stack, Queue, Link List

 Non-Linear Data Structures: Trees, Graphs


Linear and Non Linear Data Structure
Linear data structure 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.

 A Tree consists of finite set of


elements called Nodes and finite
set of directed/undirected lines
called Branches.
 Tree Terminology
Tree Terminology
Node and Branches
 In tree data structure, every
individual element is called as Node.
 Node in a tree data structure stores
the actual data of that particular
element and link to next element in
hierarchical structure.
 In a tree data structure, if we
have N number of nodes then we
can have a maximum of N-1 number
of links.
Tree Terminology
Root Node
 In a tree data structure, the first
node is called as Root Node.
 Every tree must have a root
node.
 We can say that the root node is
the origin of the tree data
structure.
 In any tree, there must be only
one root node. We never have
multiple root nodes in a tree.
Tree Terminology
EDGE
 In a tree data structure, the
connecting link between
any two nodes is called
as EDGE.

 In a tree with 'N' number of


nodes there will be a
maximum of 'N-1' number of
edges.
Tree Terminology
Parent Node
 In a tree data structure, the
node which is a predecessor of
any node is called as PARENT
NODE.
 In simple words, the node which
has a branch from it to any
other node is called a parent
node.
 Parent node can also be defined
as "The node which has child /
children".
Tree Terminology
Child Node
 In a tree data structure, the node
which is descendant of any node
is called as CHILD Node.
 In simple words, the node which
has a link from its parent node is
called as child node.
 In a tree, any parent node can
have any number of child nodes.
 In a tree, all the nodes except
root are child nodes.
Tree Terminology
 In a tree data structure, the node Internal Node

which has at least one child is called


as INTERNAL Node.
 In a tree data structure, nodes other
than leaf nodes are called as Internal
Nodes.
 The root node is also said to be
Internal Node if the tree has more
than one node.
 Internal nodes are also called as 'Non-
Terminal' nodes.
Tree Terminology
Terminal Node / External Node / Leaf Node
 In a tree data structure, the
node which does not have a
child is called as LEAF Node.
 In a tree data structure, the
leaf nodes are also called
as External Nodes.
 External node is also a node
with no child.
 In a tree, leaf node is also
called as 'Terminal' node.
Tree Terminology
Siblings
 In a tree data structure,
nodes which belong to same
parent are called
as SIBLINGS.

 In simple words, the nodes


with the same parent are
called Sibling nodes.
Tree Terminology
 In a tree data structure, the Degree of Node, Degree of Tree
total number of children of a
node is called as DEGREE of
that Node.
 In simple words, the Degree of
a node is total number of
children it has.
 The highest degree of a node
among all the nodes in a tree
is called as 'Degree of Tree‘.
Tree Terminology
• Out-degree: Total number of Out-degree of Node, In-degree of Tree,
Total degree
leaving vertices is known as
outdegree.
• In-degree: Total number of
entering vertices is known as
indegree.
• Total degree: The summation of
indegree and outdegree is
known as total degree.
Tree Terminology
Levels of Tree
 In a tree data structure, the root node
is said to be at Level 0 and the
children of root node are at Level 1
and the children of the nodes which
are at Level 1 will be at Level 2 and so
on...
 In simple words, in a tree each step
from top to bottom is called as a
Level and the Level count starts with
'0' and incremented by one at each
level (Step).
Tree Terminology
 In a tree data structure, the
total number of edges from
leaf node to a particular node
in the longest path is called
as HEIGHT of that Node.
 In a tree, height of the root
node is said to be height of
the tree.
 In a tree, height of all leaf
nodes is '0'.
Tree Terminology
Depth of Node, Depth of Tree
 In a tree data structure, the total number
of egdes from root node to a particular
node is called as DEPTH of that Node.
 In a tree, the total number of edges from
root node to a leaf node in the longest
path is said to be Depth of the tree.
 In simple words, the highest depth of any
leaf node in a tree is said to be depth of
that tree.
 In a tree, depth of the root node is '0'.
Tree Terminology
Path, Length of path
 In a tree data structure, the
sequence of Nodes and Edges from
one node to another node is called
as PATH between that two Nodes.
 Length of a Path is total number of
nodes in that path.
 In example the path A - B - E - J has
length 4.
Tree Terminology
Sub-tree
 In a tree data structure, each
child from a node forms a subtree
recursively.

 Every child node will form a


subtree on its parent node.
Tree Terminology
 Ancestor of node V: Any node, including V
itself, on the path from root to the node V.
 Proper Ancestor of node V: Any node,
excluding V, on the path from root to the
node V.

 Descendent of node V: Any node, including


V itself, on any path from the node V to the
leaf node.
 Proper Descendent of node V: Any node,
excluding V, on any path from the node V to
the leaf node.
Que:Find the below Tree terminilogy
Root
Internal Node
Parent Node
Child Node
Depth of tree, Depth of every Node
Path of 2 to 11, Length of path
Height of tree
Level of tree
Sub tree
Siblings
Out-degree of Node, In-degree of Tree, Total degree
Ancestor of node, Descendent of node
Tree Representation
 The tree data structure can be represented in two methods.
 Those methods are as follows...
1. List Representation
2. Left Child - Right Sibling Representation
Tree Representation
1. List Representation
 In this representation, we use two types of
nodes one for representing the node with data
called 'data node' and another for
representing only references called 'reference
node'.

 We start with a 'data node' from the root node


in the tree. Then it is linked to an internal node
through a 'reference node' which is further
linked to any other node directly. This process
repeats for all the nodes in the tree.
Tree Representation
2. Left Child - Right Sibling Representation

 In this representation, we use a list with one type of node


which consists of three fields namely Data field, Left child
reference field and Right sibling reference field.

 Data field stores the actual value of a node, left reference


field stores the address of the left child and right
reference field stores the address of the right sibling node.

 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.

 A tree in which every node can have a maximum of


two children is called Binary Tree.

 In a binary tree, every node can have either 0 children


or 1 child or 2 children but not more than 2 children.
Examples of Binary Tree
Types of Binary Tree
1. Strictly Binary Tree
2. Perfect Binary Tree/Complete Binary Tree
3. Almost Complete Binary Tree
4. Extended Binary Tree
5. Degenerate (or pathological) tree
6. Skewed Binary Tree
7. Balanced Binary Tree
Types of Trees
1. Strictly Binary Tree/Full Binary Tree
Either Two or Zero Children‘s
 In a binary tree, every node can have a maximum of two
children. But in strictly binary tree, every node should have
exactly two children or none. That means every internal
node must have exactly two children.
 A binary tree in which every node has either two or zero
number of children is called Strictly Binary Tree.
 Strictly binary tree is also called as Full Binary
Tree or Proper Binary Tree or 2-Tree.
 Number of leaf nodes = number of internal nodes + 1
Types of Trees
Every internal node must have
exactly two children’s and all
2. Perfect Binary Tree/Complete Binary Tree leaf node are at same level

 In Perfect binary tree, all the nodes must have exactly


two children and at every level of perfect binary tree
there must be 2level number of nodes. For example at
level 2 there must be 22 = 4 nodes and at level 3
there must be 23 = 8 nodes.
 A binary tree in which every internal node has
exactly two children and all leaf nodes are at same
level is called Perfect Binary Tree.
Types of Trees
3. Almost Complete Binary Tree

 A complete binary tree is a


binary tree in which every
level, except possibly the
last, is completely filled, and
all nodes in the last level are
as far left as possible.
 Nodes in the last level are
filled from left to right.
Types of Trees
3. Almost Complete Binary Tree
 An almost complete binary
tree is a binary tree that
satisfies the following 2
properties-
1. All the levels are
completely filled except
possibly the last level.
2. The last level must be
strictly filled from left to
right.
Types of Trees
4. Extended Binary Tree

 A binary tree can be converted into Full


Binary tree by adding dummy nodes to
existing nodes wherever required.

 The full binary tree obtained by


adding dummy nodes to a binary tree
is called as Extended Binary Tree.
Types of Trees
5. Degenerate (or pathological) tree

 A Tree where every internal node has


one child. Such trees are
performance-wise same as linked
list.
 A degenerate or pathological tree is
a tree having a single child either
left or right.
Types of Trees
6 . Skewed Binary Tree

 A skewed binary tree is a


pathological/degenerate tree in which
the tree is either dominated by the left
nodes or the right nodes.

 Thus, there are two types of skewed


binary tree: left-skewed binary tree
and right-skewed binary tree.
Types of Trees
7. Balanced Binary Tree
 It is a type of binary tree in which the difference between the height of the
left and the right sub-tree for each node is either 0 or 1.
Types of Trees : Examples
Types of Trees : Examples
Representation of Binary Tree
A binary tree data structure is represented using two methods.
Those methods are as follows...
1. Array Representation/Sequential Representation
2. Linked List Representation/Linked Representation
Representation of Binary Tree
1. Array Representation of Binary
Tree(Sequential)

 In array representation of a binary tree,


we use one-dimensional array (1-D
Array) to represent a binary tree. Level by Level (Left to right)
Representation of Binary Tree
2. Linked List Representation of
Binary Tree

 We use a doubly linked list to


represent a binary tree.
 In a doubly linked list, every node
consists of three fields. First field for
storing left child address, second for
storing actual data and third for
storing right child address.
Binary tree representation
To Find the Array representation and linked list representation of
below tree.
Binary Tree properties
 The minimum number of nodes at height h:
In any binary tree, the minimum number of nodes will be one more than the value of height.
This is the number of nodes required to construct a tree.
Height = h.
The minimum number of nodes = h+1

 The maximum number of nodes at height h:


The maximum number of nodes that can be inserted in any binary tree of height h will be
equal to 2h+1- 1.
Height = h.
Maximum nodes to inserted = 2h+1- 1
Binary Tree properties
 Total number of leaf nodes:
The number of leaf nodes in a binary tree is equal to the nodes with degree two, plus
one.
Say a binary tree has two children. Then the total number of leaf nodes of that binary
tree will be one greater than the nodes having two children.
Total number of leaf nodes = Nodes with 2 children + 1

 The maximum number of nodes at any level:


In a binary tree, the maximum number of nodes gained by any level is equal to the
power of 2 for that level.
In a simpler words, the level is donated by n.
Then, maximum nodes of that binary tree will be 2n.
Binary Tree properties
 In a Binary Tree with N nodes, Minimum possible height or levels is
equal to Log2(N+1)-1:
The maximum number of nodes at height h: 2h+1- 1
This property says that the minimum number of levels or a height of a
binary tree is the Log2 of (N+1)-1.

 In a Binary Tree with N nodes, Maximum possible height or levels is


equal to N-1:
The minimum number of nodes at height h: h+1
This property says that the maximum number of levels or a height of a
binary tree is the N-1.
Need of converting a general tree into
binary tree
 General trees are those in which the number of subtrees for any node is not required to be 0, 1, or
2. The tree may be highly structured and therefore have 3 subtrees per node in which case it is
called a ternary tree. However, it is often the case that the number of subtrees for any node may be
variable. Some nodes may have 1 or no subtrees, others may have 3, some 4, or any other
combination. The ternary tree is just a special case of a general tree (as is true of the binary tree).

 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

 Root of binary tree = Root of general tree I


 Left child of current node in binary tree = Leftmost child of current node in general
tree
 Right child of current node in binary tree = Right sibling of current node in general tree
Conversion of General Tree into Binary Tree :
Example2
 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

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

1. Traverse the left subtree in inorder


2. Visit the root.
3. Traverse the right subtree in inorder

POSTORDER

1. Traverse the left subtree in postorder


2. Traverse the right subtree in postorder
3. Visit the root.
Traversal of tree
Examples:Traversal of tree
Code for the Traversal Techniques
void preOrder(Node *bt) void inOrder(Node *bt)
struct node
{ {
{
if (bt != NULL) int data; if (bt != NULL)
{ struct node *left; {
cout << bt → data; inOrder(bt → left);
preOrder(bt → left); struct node cout << bt → data;
preOrder(bt → right); *right; inOrder(bt → right);
} }; }
} }
void postOrder(Node *bt)
Void main() {
{ if (bt != NULL)
struct node *bt; {
preOrder(bt); postOrder(bt → left);
postOrder(bt → right);
}
cout << bt → data;
}
}
Preorder Traversals- Non recursive
The following operations are performed The nodes are processed in the order
[ 4, 7, 8,13, 18, 5, 2 ]
to traverse a binary tree in pre-order
using a stack:
 Start with root node and push onto
stack.
 Repeat while the stack is not empty
 POP the top element (PTR) from the
stack and process(print) the node.
 PUSH the right child of PTR onto to stack.
 PUSH the left child of PTR onto to stack.
Preorder Traversals- Non recursive
1. Set TOP = 1. STACK[1] = NULL and PTR = ROOT.
2. Repeat Steps 3 to 5 while PTR ≠ NULL:
3. Apply PROCESS to PTR->DATA.
4. If PTR->RIGHT ≠ NULL, then:
Set TOP = TOP + 1, and STACK[TOP] = PTR->RIGHT.
[End of If]
5. If PTR->LEFT ≠ NULL, then:
Set PTR = PTR -> LEFT.
Else:
Set PTR = STACK[TOP] and TOP = TOP - 1.
[End of If]
[End of Step 2 loop.]
6. Exit.
Inorder Traversals- Non recursive
The following operations are performed to The nodes are processed in the order
traverse a binary tree in in-order using a stack: [ 8, 7, 13, 4, 5, 18, 2 ]
1. Start from the root, call it PTR.
2. If PTR is not NULL :
Push PTR onto stack
Move to left of PTR(new PTR) and repeat step 2.
If PTR is NULL and stack is not empty :
Pop element from stack and set it as PTR.
Process(print) it and move to right of PTR (new
PTR)
Go to step 2
Inorder Traversals- Non recursive
1. Set TOP = 1, STACK[1] = NULL and PTR = ROOT.
2. Repeat while PTR ≠ NULL:
(a) Set TOP = TOP + 1 and STACK[TOP] = PTR.
(b) Set PTR = PTR->LEFT.
[End of loop.]
3. Set PTR = STACK[TOP] and TOP = TOP - 1.
4. Repeat Steps 5 to 7 while PTR ≠ NULL:
5. Apply PROCESS to PTR->INFO.
6. If PTR->RIGHT ≠ NULL, then:
(a) Set PTR = PTR->RIGHT.
(b) Go to Step 3.
[End of If]
7. Set PTR = STACK[TOP] and TOP = TOP -1.
[End of Step 4 loop.]
8. Exit.
Postorder Traversals- Non recursive
• Start from the root, call it PTR.
• If PTR is not NULL, Push PTR onto stack.
Move to left of PTR and repeat step 2.
• If PTR has a right child R, then PUSH -R
onto the stack.
• Pop and process positive element from
stack and set as PTR.
• If a negative node is popped, (PTR = -N),
then set PTR = N and go to step 2.
Postorder Traversals- Non recursive
The nodes are processed in the
1. Create an empty stack. order [ 8, 13, 7, 5, 2, 18, 4 ]
2. Start from the root node.
3. Repeat the below steps until stack is not empty.
If root is not NULL, push root->right and then root to the stack
and Set root as root->left.
If root becomes NULL, pop item from the stack and set it as root.
If the popped item has a right child and the right
child is at top of stack, then remove the right child from stack,
push the root back and set root as root’s right child.
Else print root’s data and set root as NULL.
Postorder Traversals- Non recursive
1. Set TOP = 1. STACK[1] = NULL and PTR = ROOT.
2. Repeat Steps 3 to 5 while PTR ≠ NULL:
3. Set TOP = TOP + 1 and STACK[TOP] = PTR.
4. If PTR->RIGHT ≠ NULL. then: [Push on STACK. ]
Set TOP = TOP + 1 and STACK[TOP] = PTR->-RIGHT.
[End of If structure.]
5. Set PTR = PTR->LEFT. [Updates pointer PTR.]
[End of Step 2 loop.]
6. Set PTR = STACK[TOP] and TOP = TOP - 1.
7. Repeat while PTR > 0:
(a) Apply PROCESS to PTR->INFO.
(b) Set PTR = STACK[TOP] and TOP = TOP - 1.
[End of loop.]
8. If PTR < 0, then:
(a) Set PTR = -PTR.
(b) Go to Step 2.
[End of If structure]
9. Exit.
Binary Tree Construction
Let us consider the below traversals:
Inorder sequence: D B E A F C
Preorder sequence: A B D E C F

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 }

Steps to construct binary tree using Inorder and postorder traversals:


1. Find the root node using the postorder traversal.
2. Find the left subtrees and the right subtrees using in-order traversal
by finding the index of the root node of respective subtrees.
3. Once the root node is found, we can recurse down on the right and
left subtrees, i.e., right subarray and left subarray split at respective
root index to repeat the same process until we find at most a single
element in either sub-array.
Binary Tree Construction
Post-order Sequence : 452631
In-order Sequence : 425163

 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

Pre-order Traversal : 1-2-4-5-3-6-7


In-order Traversal : 4-2-5-1-6-3-7
Post-order Traversal : 4-5-2-6-7-3-1
Level-order Traversal : 1-2-3-4-5-6-7
Operations in Binary Tree
 Insertion
 Deletion
 Search
 Traversal
Operations in Binary Tree : Insertion
 For given a binary tree and a key, insert the key
into the binary tree at the first position
available in level order.
 The idea is to do an iterative level order
traversal of the given tree using queue.
 If we find a node whose left child is empty, we
make a new key as the left child of the node.
Else if we find a node whose right child is
empty, we make the new key as the right
child.
 We keep traversing the tree until we find a
node whose either left or right child is empty.
Operations in Binary Tree : Deletion
 Given a binary tree, delete a node from it by making sure that the
tree shrinks from the bottom (i.e. the deleted node is replaced by
the bottom-most and rightmost/leftmost node).

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().

 Auxiliary Space: O(N) , Recursive call for each node


tree considered as stack space.
Binary Search Tree
 Binary search tree is a binary tree which has
special property called BST.
 Binary Search Tree is a binary tree where
each node contains only smaller values in
its left subtree and only larger values in
its right subtree.
 Note: Every binary search tree is a binary
tree, but all the binary trees need not to be
binary search trees.
Binary Search Tree
Binary Search Tree is a node-based binary tree
data structure which has the following
properties:

 The left subtree of a node contains only nodes


with keys lesser than the node’s key.
 The right subtree of a node contains only nodes
with keys greater than the node’s key.
 The left and right subtree each must also be a
binary search tree.
Binary Search Tree
Examples of Binary Search Trees

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.

 To insert an element in BST, we have


to start searching from the root node;
if the node to be inserted is less than
the root node, then search for an
empty location in the left subtree.
Else, search for the empty location in
the right subtree and insert the data.
Binary Search Tree operation : Insert
Binary Search Tree operation : Delete
When we delete a node, three possibilities arise.

1. Node to be deleted is the leaf(no child)


2. Node to be deleted has only one child
3. Node to be deleted has two children
Binary Search Tree operation : Delete
1) Node to be deleted is the leaf: Simply remove it from the tree.

 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.

5 Time complexity is usually O(n). Time complexity is usually O(log n).

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.

 Two way threading/Double Threaded BT


 Where both left and right NULL pointers are made to point to inorder predecessor and inorder
successor respectively.

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

Right Threaded Binary Tree

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

You can take dummy node


called as header node.
Right child of the header node
always points to itself.
The threaded binary tree is
represented as the left child of
the header node.
Threaded Binary Tree: One-Way

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

// single threaded // double threaded


struct Node struct Node
{ {
int data; int data ;
Node *left ; Node *left ;
Node *right ; Node *right ;
bool rightThread ; bool leftThread ;
} bool rightThread ;
}

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

Add following numbers and redraw the TBT


a. Insert 13
b. Insert 15
139
Deletion in threaded binary tree
 Leaf node
 A node with 1 child
 A node with 2 children
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

Perform following delete operation and redraw the TBT


a. Delete 14
b. Delete 17
c. Delete 10
147
Inorder traversal of TBT
 The idea of threaded binary trees is to make inorder traversal faster and do it without
stack and without recursion.
 Steps to find inorder Traversal of TBT
1. Find out leftmost element of root node and consider leftmost node of root node as current
node
2. Repeat following steps up to current node is not equal to NULL
1. Display data of current node
2. If current node is a thread node, then go to inorder successor
and consider it as a current node
Else
Find out the leftmost child in right subtree and consider it as a current node
}
(Note: if there is no leftmost node , consider root of right subtree as a
current node)
Inorder traversal of TBT
void inOrder(struct Node *root)
{
struct Node* leftMost(struct Node *n) struct Node *cur = leftmost(root);
{ while (cur != NULL)
{
if (n == NULL) printf("%d ", cur->data);
return NULL; // If this node is a thread node, then go
to inorder successor
if (cur->rightThread)
while (n->left != NULL) cur = cur->right;
else // Else go to the leftmost child in
n = n->left; right subtree
cur = leftmost(cur->right);
}
return n; }
}
function to find leftmost node in a tree rooted with n
code to do inorder traversal in a threaded binary tree
Inorder traversal of TBT
Inorder traversal of TBT
Inorder traversal of TBT
Inorder traversal of TBT : Example
 Example
 In-order traversal -G,F,B,A,D,C,E

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

both threads and ordinary links need to be maintained.


157
Case Study
Use of binary tree in expression tree-evaluation
and Huffman's coding

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-

 Huffman Coding implements a rule known as a prefix rule.


 This is to prevent the ambiguities while decoding.
 It ensures that the code assigned to any character is not a prefix of
the code assigned to any other character.
Construction of Huffman Tree
The steps involved in the construction of Huffman Tree are as follows:

 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:

Total number of bits in Huffman encoded message


= Total number of characters in the message x Average code length per
character
= ∑ ( frequencyi x Code lengthi )
Construction of Huffman Tree : Example
 A file contains the following characters
with the frequencies as shown.
 If Huffman Coding is used for data
compression, determine-
1. Huffman Code for each character
2. Average code length
3. Length of Huffman encoded message
(in bits)
Construction of Huffman Tree : Example
 To write Huffman Code for any character,
traverse the Huffman Tree from root node to the
leaf node of that character. Following this rule, the
Huffman Code for each character is-
a = 111
e = 10
i = 00
o = 11001
u = 1101
s = 01
t = 11000

 From here, we can observe-


 Characters occurring less frequently in the text are
assigned the larger code.
 Characters occurring more frequently in the text are
assigned the smaller code.
Construction of Huffman Tree : Example
Average Code Length- Length of Huffman Encoded Message-

Average code length Total number of bits in Huffman encoded message


= ∑ ( frequencyi x code lengthi ) / ∑ = Total number of characters in the message x
( frequencyi ) Average code length per character
= { (10 x 3) + (15 x 2) + (12 x 2) + (3 = 58 x 2.52
x 5) + (4 x 4) + (13 x 2) + (1 x = 146.16
5) } / (10 + 15 + 12 + 3 + 4 + 13 + ≅ 147 bits
1)
= 2.52

You might also like