0% found this document useful (0 votes)
13 views

TREE Data Structure

Trees (full chapter). DSA-Btech 2nd year

Uploaded by

carlgta2915
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

TREE Data Structure

Trees (full chapter). DSA-Btech 2nd year

Uploaded by

carlgta2915
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

TREE DATA

STRUCTURE

Unit-IV
Tree Data Structure
• Tree is a non-linear data structure which organizes data in a
hierarchical structure and this is a recursive definition.
OR

• A tree is a connected graph without any circuits.

OR

• If in a graph, there is one and only one path between every pair of
vertices, then graph is called as a tree.
Example
Properties of Tree Data Structure

• There is one and only one path between every pair of


vertices in a tree.
• A tree with n vertices has exactly (n-1) edges.
• A graph is a tree if and only if it is minimally connected.
• Any connected graph with n vertices and (n-1) edges is a
tree.
Tree Terminology
1. Root
• The first node from where
the tree originates is called
as a root node.
• In any tree, there must be
only one root node.
• We can never have
multiple root nodes in a
tree data structure.
2. Edge

• The connecting link


between any two nodes
is called as an edge.
• In a tree with n number of
nodes, there are exactly
(n-1) number of edges.
3. Parent
• The node which has a
branch from it to any other
node is called as a parent
node.
• In other words, the node
which has one or more
children is called as a parent
node.
• In a tree, a parent node can
have any number of child
nodes.
4. Child
• The node which is
a descendant of
some node is
called as a child
node.
• All the nodes
except root node
are child nodes.
5. Siblings
• Nodes which belong to the
same parent are called as
siblings.
• In other words, nodes with
the same parent are
sibling nodes.
6. Degree
• Degree of a node is
the total number of
children of that node.
• Degree of a tree is the
highest degree of a
node among all the
nodes in the tree.
7. Internal Node
• The node which has at
least one child is
called as an internal
node.
• Internal nodes are also
called as non-terminal
nodes.
• Every non-leaf node is
an internal node.
8. Leaf Node
• The node which
does not have any
child is called as a
leaf node.
• Leaf nodes are also
called as external
nodes or terminal
nodes.
9. Level
• In a tree, each step
from top to bottom is
called as level of a
tree.
• The level count starts
with 0 and increments
by 1 at each level or
step.
10. Height
• Total number of edges
that lies on the longest
path from any leaf
node to a particular
node is called as
height of that node.
• Height of a tree is the
height of root node.
• Height of all leaf
nodes = 0
11. Depth
• Total number of edges from
root node to a particular node
is called as depth of that
node.
• Depth of a tree is the total
number of edges from root
node to a leaf node in the
longest path.
• Depth of the root node = 0
• The terms “level” and “depth”
are used interchangeably.
12. Subtree
• In a tree, each
child from a node
forms a subtree
recursively.
• Every child node
forms a subtree on
its parent node.
13. Forest
• A forest is a set of
disjoint trees.
Binary Tree
• Binary tree is a
special tree data
structure in which
each node can have
at most 2 children.
• Thus, in a binary tree,
Each node has either
0 child or 1 child or 2
children.
Unlabeled Binary Tree
• A binary tree is unlabeled if its nodes are not assigned any label.
Example

• Consider we want to draw all the binary trees possible with 3 unlabeled
nodes.
• Number of binary trees possible with 3 unlabeled nodes

= 2 x 3C3 / (3 + 1)
= 6C 3 / 4
=5
• Thus, With 3 unlabeled nodes, 5 unlabeled binary trees are possible.
• These unlabeled binary trees are as follows-
Labeled Binary Tree
• A binary tree is labeled if all its nodes are assigned a label.
Example
• Consider we want to draw all the binary
trees possible with 3 labeled nodes.
• Number of binary trees possible with 3
labeled nodes
= { 2 x 3C3 / (3 + 1) } x 3!
= { 6C 3 / 4 } x 6
=5x6
= 30

• Thus, With 3 labeled nodes, 30 labeled


binary trees are possible.
• Each unlabeled structure gives rise to 3!
= 6 different labeled structures.
Types of Binary Trees
1. Rooted Binary Tree
A rooted binary tree is a
binary tree that satisfies
the following 2
properties-

• It has a root node.


• Each node has at most
2 children.
2. Full / Strictly Binary Tree
• A binary tree in
which every node
has either 0 or 2
children is called as
a Full binary tree.
• Full binary tree is
also called as
Strictly binary tree.
4. Almost Complete Binary Tree
An almost complete binary
tree is a binary tree that
satisfies the following 2
properties-

• All the levels are completely


filled except possibly the
last level.
• The last level must be
strictly filled from left to
right.
3. Complete / Perfect Binary Tree
A complete binary tree is a
binary tree that satisfies the
following 2 properties-

• Every internal node has


exactly 2 children.
• All the leaf nodes are at the
same level.

• Complete binary tree is also


called as Perfect binary
tree.
5. Skewed Binary Tree
A skewed binary tree is a binary
tree that satisfies the following 2
properties-
• All the nodes except one node
has one and only one child.
• The remaining node has no
child.
OR
• A skewed binary tree is a binary
tree of n nodes such that its
depth is (n-1).
Binary Tree Representation
• There are two primary ways to represent binary trees:

1. Linked Node Representation


2. Array Representation
1. Linked Node Representation
• This is the simplest way to represent a binary tree. Each node
contains data and pointers to its left and right children.

struct Node {

int data;

struct Node* left;


struct Node* right;
};
Example
2. Array Representation
• In array representation of a binary tree, we use one-dimensional
array (1-D Array) to represent a binary tree.
• For any node at index i:
Left Child: Located at 2 * i + 1
Right Child: Located at 2 * i + 2
Root Node: Stored at index 0
• If any node does not have any of its child, then NULL value is
stored at the corresponding index of the array.
• To represent a binary tree of depth ‘d' using array representation,
we need one dimensional array with a maximum size of 2d+1 - 1.
Example
Binary Tree Properties

Property-01:
Minimum number of nodes in a
binary tree of height H = H + 1

• Example-

To construct a binary tree of height = 4,


we need at least 4 + 1 = 5 nodes.
Property-02
• Maximum number of nodes in a
binary tree of height H= 2H+1 – 1
• Example-
• Maximum number of nodes in a
binary tree of height 3
= 23+1 – 1
= 16 – 1
= 15 nodes
Thus, in a binary tree of height = 3,
maximum number of nodes that
can be inserted = 15.
Property-03
• Total Number of leaf nodes in a Binary Tree
= Total Number of nodes with 2 children + 1
• Consider the following binary tree-

• Here,
Number of leaf nodes = 3
Number of nodes with 2 children = 2

Clearly, number of leaf nodes is one greater


than number of nodes with 2 children.
Property-04
• Maximum number of nodes at any level
‘L’ in a binary tree= 2L
• Example-

• Maximum number of nodes at level-2 in a


binary tree
= 22
=4
Thus, in a binary tree, maximum number of
nodes that can be present at level-2 = 4.
Binary Tree Traversal
• Tree Traversal refers to the process of visiting each node in a tree
data structure exactly once.
1. Preorder Traversal
Algorithm-

1. Visit the root


2. Traverse the left sub tree i.e.
call Preorder (left sub tree)
3. Traverse the right sub tree i.e.
call Preorder (right sub tree)

Root → Left → Right


Example
Applications
• Preorder traversal is used to get prefix expression
of an expression tree.
• Preorder traversal is used to create a copy of the
tree.
Inorder Traversal
Algorithm-

1. Traverse the left sub tree i.e.


call Inorder (left sub tree)
2. Visit the root
3. Traverse the right sub tree i.e.
call Inorder (right sub tree)

Left → Root → Right


Example
Application
• Inorder traversal is used to get infix expression of
an expression tree.
3. Postorder Traversal
Algorithm-

1. Traverse the left sub tree i.e.


call Postorder (left sub tree)
2. Traverse the right sub tree i.e.
call Postorder (right sub tree)
3. Visit the root

Left → Right → Root


Example
Applications
• Postorder traversal is used to get postfix
expression of an expression tree.
• Postorder traversal is used to delete the tree.
• This is because it deletes the children first and then
it deletes the parent.
Breadth First Traversal
• Breadth First Traversal of a tree prints all the nodes
of a tree level by level.
• Breadth First Traversal is also called as Level Order
Traversal.

• Application-
• Level order traversal is used to print the data in
the same order as stored in the array
representation of a complete binary tree.
Example
Threaded Binary Tree
• In the linked representation of binary trees, more than one half of the
link fields contain NULL values which results in wastage of storage
space. If a binary tree consists of n nodes, then n+1 link fields
contain NULL values.
• So, in order to effectively manage the space, a method was devised by
Perlis and Thornton in which the NULL links are replaced with special
links known as threads. Such binary trees with threads are known as
threaded binary trees.
• A Threaded Binary Tree is a variant of a normal Binary Tree that
facilitates faster tree traversal (in-order) and does not require a Stack
or Recursion.
• It decreases the memory wastage by setting the null pointers of a
leaf node to the in-order predecessor or in-order successor.
Types of Threaded Binary tree
• There are two types of Threaded Binary Trees:

1. One-way Threaded Binary Tree


2. Two-way Threaded Binary Tree
1. One-way Threaded Binary Tree
• In this type, if a node has a right null pointer, then this right pointer
is threaded towards the in-order successor’s node if it exists.
However, the link to the other node (predecessor or successor)
remains as a null pointer.
• Node Structure of Single-Threaded Binary Trees:
struct Node{
int data;
struct Node* left;
struct Node* right;
bool rightThreaded;
};
Example
2. Two-way Threaded Binary Tree
• In this type, the left null pointer of a node is made to point towards the
in-order predecessor node and the right null pointer is made to point
towards the in-order successor node.

• Node Structure of Double-Threaded Binary Trees:

struct Node{ Null Null


left data right
int data;
Node* left; rightThreaded = false
leftThreaded = false
Node* right;
bool rightThreaded;
bool leftThreaded;
};
2. Two-way Threaded Binary Tree

leftThreaded=0 left reference points to the in-order predecessor

leftThreaded=1 left reference points to the left child

rightThreaded=0 right reference points to the in-order successor

rightThreaded=1 right reference points to the right child


Example
Double Threaded binary tree with a dummy
node
Double Threaded binary tree with a dummy
node
• Need of a Dummy Node: As we saw that references left most
reference and rightmost reference pointers have nowhere to point
to so we need a dummy node, and this node will always present
even when the tree is empty.
• In this dummy node, we will put rightThreaded = 1 and its right
child will point to itself and leftThreaded = 0, so we will construct
the threaded tree as the left child of the dummy node.
Example
Advantages of Threaded Binary Tree
• No need for stacks or recursion: Unlike binary trees,
threaded binary trees do not require a stack or recursion for
their traversal.
• Optimal memory usage: It decreases memory wastage.
• Time complexity: In-order traversal in a threaded binary
tree is fast because we get the next node in O(1) time than
a normal binary tree that takes O(Height).
• Backward traversal: In a double-threaded binary tree, we
can even do a backward traversal.
Disadvantages of Threaded Binary tree
• Complicated insertion and deletion: By storing the in-
order predecessor/ successor for the node with a null
left/right pointer, we make the insertion and deletion of a
node more time-consuming and a highly complex
process.
• Extra memory usage: We use additional memory in the
form of rightThread and leftThread variables to distinguish
between a thread from an ordinary link.
Applications of Threaded Binary Tree
• Efficient In-Order Traversal: Threaded binary trees enhance in-order
traversal efficiency by eliminating the need for recursion or stacks
• Space Efficiency: Threaded trees save space by eliminating null pointers,
reducing memory overhead
• Compiler Design: frequently access to syntax trees and symbol tables in
required for example in optimizers
• Fast Searching and Retrieval: Threaded binary trees enable faster
navigation, improving the performance of search operations
• Threaded Tree-based Iterators: Threaded trees are useful for
implementing efficient iterators for various tree traversal orders
• Binary Search Tree Operations: Threaded trees enhance efficiency in
operations like finding minimum/maximum elements or
predecessors/successors

You might also like