SlideShare a Scribd company logo
Trees
•Basic terminology
• Binary Trees
• Binary tree representation
• algebraic Expressions
• Complete Binary Tree
• Extended Binary Trees
• Array and Linked Representation of Binary trees
• Traversing Binary trees
• Threaded Binary trees
• Traversing Threaded Binary trees
• Huffman algorithm
Basic terminology
• Trees are a kind of data structure that is similar to normal trees.
Data trees also have roots, branches and leaves. ‘A tree is a data
structure that represents hierarchical relationships between
individual data items.
• An element of a tree is called a node. A branch connects two
nodes. The node at the upper end of a branch is a predecessor
of the node at the lower end and the node at the lower end is
the successor of the predecessor.
• There is a unique node that has no predecessor called the root
of the tree. The nodes of the tree that have no branches are
called leaf nodes. The nodes that have branches are called non-
leaf nodes. Children of the same parent are called siblings.
• The degree of a node is the number of successors of the node.
Thus the degree of a leaf is 0. Level of a node is given by the
length of the unique path from the root to the node
Basic terminology
Expression Tree
• Expressions can be represented by tree structures
• Operands are terminal (leaf) nodes and operator are non terminal nodes
– E.g. Arithmetic Expression Tree
– A-(C/5 * 2) + (D*5 % 4)
+
- %
A * * 4
/ 2 D 5
C 5
Binary trees
• The tree in which every node has a maximum of two branches
is called a binary tree. Every parent has a maximum of two
children, ‘right’ child and ‘left’ child.
• A binary tree can have any number of nodes.
Binary trees
Binary trees
• A strictly binary tree is one in which every non-leaf node has
non-empty right and left subtrees. Following figure a gives a
strictly binary tree.
• A strictly binary tree with n leaves contains 2n-1 nodes
• A strictly binary tree with all its leaves at level d is termed as a
complete binary tree of depth d. It contains 2d leaves at the
level d. The tree contains 2d leaves and 2d-1 non-leaf nodes.
Binary trees
• Each node in the tree is assigned a unique number, which
gives the nodes position in the tree. In complete binary tree
for any node K, left and right children of the node K are 2*K
and 2*K+1 respectively and the parent of K is the node K/2.
• Depth = log2n +1 where n is the no of nodes.
• Any node at level less than d-1 has two sons
Representation of a binary tree in memory:
• The structure of each node a binary tree contains the data field, a pointer to the
left child and a pointer to the right child. The figure shows this structure
• This structure can be defined as follows:
struct tnode
{
struct tnode *left;
int data;
struct tnode *right;
};
• There are two ways by which we can represent a binary tree.
• Linked representation of a binary tree
• Array representation of a binary tree
Representation of a binary tree in memory:
• Array representation of binary trees:
When a binary tree is represented by arrays three separate
arrays are required. One array stores the data fields of the
trees. The other two arrays represents the left child and right
child of the nodes.
• A complete binary tree has a simple array representation.
Suppose we number the nodes from left to right, beginning at
the top and ending at the bottom. Then we can store the
various data items in the corresponding elements of an array.
Representation of a binary tree in memory:
• Linked representation of binary trees
• Binary trees can be represented by links, where each node
contains the address of the left child and the right child.
These addresses are nothing but kinds to the left and right
child respectively. A node that does not have a left or a
right child contains a NULL value in its link fields.
• Linked representation uses three parallel arrays, INFO, LEFT
and RIGHT and a pointer variable ROOT. Each node N of T
will correspond to a location K such that –
• INFO[K] contains the data at node N
• LEFT[K] contains the location of left child node N
• RIGHT[K] contains the location of right child node N
• ROOT will contain the location of root R of T
Representation of a binary tree in memory:
Representation of a binary tree in memory:
Traversing binary tree
• Processing the contents of nodes of tree necessitates the visiting of
the nodes of the tree. This is called traversing of the tree
• Preorder or depth-first order traversal
• In this traversal the root node is visited before visiting the two
subtrees. The order followed is given sequentially by the following.
• 1. Visit the root
2. Visit the nodes in the left subtree in preorder
3. Visit the nodes in the right subtree in preorder
• Inorder or symmetric order traversal
• Here the root node is visited in-between the left and right subtrees.
The order of traverse is given as follows.
• 1. Traverse the left subtree in inorder
2. Visit the root
3. Traverse the right subtree in inorder.
• Postorder or breadth-first order traversal
• Here the left and right subtrees are traversed before visiting the
root. Following gives the sequential order followed for traversal.
• 1. Visit the nodes of the left subtree in postorder
2. Traverse the right subtree in postorder
3. Visit the root
15
Traversing binary tree
preorder inorder postorder
A
B C
D E F G
A
B C
D E F G
A
B C
D E F G
A B D E C F G D B E A F C G D E B F G C A
16
Traversing binary tree
Void inorder(struct node *root)
{
if (root!=NULL)
{
inorder(root->left);
printf(“%d”,root->info);
inorder(root->right);
} }
Void preorder(struct node *root)
{
if (root!=NULL)
{
printf(“%d”,root->info);
preorder(root->left);
preorder(root->right);
} }
Traversing binary tree
Void postorder(struct node *root)
{
if (root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf(“%d”,root->info);
} }
Traversing using STACK
• Preorder
• Initially push NULL on to STACK and then set
PTR:=ROOT. Then repeat the following steps until
PTR=NULL.
• (a) proceed down the left most path rooted at
PTR, processing each node N on the path and
pushing each right child R(N), if any, onto STACK.
The traversing ends after a node N with no left
child L(N) is processed.
• (b) Pop and assign to PTR the top element on
STACK. If PTR!=NULL, then return to step(a)
otherwise Exit.
Traversing using STACK
• Inorder
• Initially push NULL on to STACK and then set
PTR:=ROOT. Then repeat the following steps until
NULL is popped from STACK.
• (a) proceed down the left most path rooted at
PTR, and pushing each node N onto STACK and
stopping when a node N with no left child is
pushed onto STACK.
• (b) Pop and process the nodes on STACK. If NULL
is popped, then exit. If a node N with right child
R(N) is processed, set PTR=R(N)and return to
step(a).
Traversing using STACK
• Postorder
• Initially push NULL on to STACK and then set
PTR:=ROOT. Then repeat the following steps until
NULL is popped from STACK.
• (a) proceed down the left most path rooted at
PTR, pushing each node N onto STACK and if N
has a right child R(N), push –R(N) onto STACK.
• (b) Pop and process positive nodes on STACK. If
NULL is popped, then exit. If negative node is
popped, that is, if PTR= -N for some node N, set
PTR=N and return to step(a).
Traversing binary tree
• Q: a binary tree T has 9 nodes. The inorder and postorder traversal
of T yield the following sequence of nodes
Inorder: E A C K F H D B G
Postorder: E C K A H B G D F
Draw the tree T.
• Ans: tree T is drawn from root downward as follows.
(a) the root of T is obtained by choosing the last node in its postorder.
Thus F is the root of tree.
(b) the left child of the node F is obtained as follows: first use the
inorder of T to find the nodes in the left subtree T1 of F. Thus T1
consists of nodes E, A, C, K. then the left child is obtained by
choosing the last node in the postorder of T1. thus A is the left child
of F.
(c)Similarly, the right subtree T2 of F consists of the nodes H, D, B, G
and D is the root of T2. thus D is the right child of F.
Repeating the above process with each new node , we obtain the
required tree.
Threaded Binary Trees
• The special link created to simplify the tree operation is
called the thread. The thread makes it possible to perform
traversals, insertions and deletions without using either
stack or recursion. Binary trees with threads are termed
threaded binary trees. Thread is formed by changing the
NULL link to special link.
• In implementing the threaded trees, each thread is
distinguished from the tree pointer by associating it with a
Boolean variable. The Boolean value tells if the link is a
pointer to the nonempty subtree or it is a link to a node
higher in the tree. The value is set to TRUE if the link is a
thread
• In a right in-threaded binary tree each NULL right link is
replaced by threads connecting the nodes to its in-order
successor. Following fig. shows right in-threaded trees. The
threads are shown in dotted lines. The NULL right pointer
of the he right most nodes K is not replaced by the thread
as it does not have inorder successor
Threaded Binary Trees
Threaded Binary Trees
• In sequential representation of binary tree, negative or
positive value of a variable is set to indicate whether the
link is a pointer or a thread. In the linked array
representation, a thread is represented by the negative
value of the link while the positive value is set to indicate
the pointer.
• In a left in-threaded tree the thread to the inorder
predecessor of the node replaces each NULL left pointer. A
binary tree with both left and right threads is called an in-
threaded binary tree
• A right in-threaded tree yields more advantages than left
in-threaded tree. Right and left pre-threaded binary trees
are the binary trees in which the NULL right and left
pointers of the nodes are replaced by their preorder
successors and predecessors respectively. These concepts
are made use of in the traversal of binary trees
Threaded Binary Trees
Huffman algorithm
• An extended binary tree is a transformation of any binary tree
into a complete binary tree.
• This transformation consists of replacing every null subtree of
the original tree with ``special nodes.'' The nodes from the
original tree are then internal nodes, while the ``special
nodes'' are external nodes.
• For instance, consider the following binary tree.
Equivalent extended binary tree
Huffman algorithm
• Empty circles represent internal nodes, and filled circles
represent external nodes
• Every internal node in the extended tree has exactly two
children, and every external node is a leaf. The result is a
complete binary tree.
• Number of external nodes(Ne) is 1 more than the number
of internal nodes(Ni).i.e Ne=Ni+1.
• External path length (Le) to be the sum of all path lengths
summed over each path from the root R to an external
node.
• Le = 2+3+3+3+3+3+3 = 20
• internal path length (Li) to be the sum of all path lengths
summed over each path from the root R to an internal
node.
• Li = 0+1+1+2+2+2 = 8
• Le = Li +2*n, where n is the number of internal nodes.
Huffman algorithm
• Suppose T is a 2-tree with n external nodes is assigned a
nonnegative weights. The external weighted path length P
of the tree T is defined to be the sum of the weighted path
lengths.
• P = W1L1 + W2L2+………+WnLn
• Where Wi and Li denote the weight and path length of
external node respectively.
• Huffman's algorithm is a method for building an extended
binary tree with a minimum weighted path length from a
set of given weights. Initially construct a forest of singleton
trees, one associated with each weight. If there are at least
two trees, choose the two trees with the least weight
associated with their roots and replace them with a new
tree, constructed by creating a root node whose weight is
the sum of the weights of the roots of the two trees
removed, and setting the two trees just removed as this
new node's children. This process is repeated until the
forest consists of one tree
Huffman algorithm
• Huffman algorithm: suppose W1 and w2 are two minimum
weigths among the n given weigths W1, W2, …….Wn. Find the
tree T’ which gives a solution for the n-1 weigths w1+W2, W3,
W4,………..Wn.
Then, in the tree T’, replace the external node
by the subtree
The new 2-tree T is the desired solution.
W1+W2
W1 W2
Huffman algorithm
• Let us work through an example for the set of weights
{1,2,3,3,4} . In these intermediate steps we will display the
temporary weights assigned by the algorithm in the circled
nodes. Initially our forest is
• During the first step, the two trees with weights 1 and 2 are
merged, to create a new tree
with a root of weight 3.
• We now have three trees with weights of 3 at their roots. It
does not matter which two we
choose to merge. Let us choose
the tree we just created and one
of the singleton nodes of weight 3.
Huffman algorithm
• Now our two minimum trees
are the two singleton nodes of
weights 3 and 4. We will combine
these to form a new tree of
weight 7.
• Finally we merge our last
two remaining trees
Huffman algorithm
• The result is an extended binary tree of minimum weighted
path length 29. In the following diagram each circled node is
marked with its weighted path length
CS 102
Building a Tree
E
1
i
1
y
1
l
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
CS 102
Building a Tree
E
1
i
1
y
1
l
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
CS 102
Building a Tree
E
1
i
1
y
1
l
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
CS 102
Building a Tree
E
1
i
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
CS 102
Building a Tree
E
1
i
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
CS 102
Building a Tree
E
1
i
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
CS 102
Building a Tree
E
1
i
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
CS 102
Building a Tree
E
1
i
1
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
CS 102
Building a Tree
E
1
i
1
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
CS 102
Building a Tree
E
1
i
1
sp
4
e
82
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4 4
CS 102
Building a Tree
E
1
i
1
sp
4
e
82
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4 4
6
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4 4 6
What is happening to the characters with a low number of occurrences?
CS 102
Building a Tree
E
1
i
1
sp
4
e
82
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6
8
CS 102
Building a Tree
E
1
i
1
sp
4
e
82
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6 8
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6
8
10
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2r
2
s
2
4
n
2
a
2
4 4
6
8 10
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6
8
10
16
CS 102
Building a Tree
E
1
i
1
sp
4
e
82
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6
8
10 16
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6
8
10
16
26
CS 102
Building a Tree
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6
8
10
16
26
General tree
• A general tree will have more than two children or subtrees
unlike binary tree. It is a finite nonempty set of nodes. There
is no upper limit on the degree of a node
• Conversion of general tree to a binary tree
• Suppose T is general tree and T’ is correspondent binary tree
• Nodes in T and T’ will be same.
• Root of T’ will be the root of T.
General tree
• Left child of node N in T’ will be the first child of node N in the
general tree T and the right child in of node N in T’ will be the
next sibling of N in T.

More Related Content

PPTX
Tree
Raj Sarode
 
PPT
Lecture 5 tree.pptx
Abirami A
 
PPT
Tree-In Data Structure
United International University
 
PPT
358 33 powerpoint-slides_10-trees_chapter-10
sumitbardhan
 
PPT
Chapter 8 ds
Hanif Durad
 
PPT
Tree
Ghaffar Khan
 
PPTX
Unit – vi tree
Tribhuvan University
 
PPTX
Tree
bhumish
 
Lecture 5 tree.pptx
Abirami A
 
Tree-In Data Structure
United International University
 
358 33 powerpoint-slides_10-trees_chapter-10
sumitbardhan
 
Chapter 8 ds
Hanif Durad
 
Unit – vi tree
Tribhuvan University
 
Tree
bhumish
 

What's hot (20)

PPTX
Unit 6 tree
Dabbal Singh Mahara
 
PDF
Lecture notes data structures tree
maamir farooq
 
PPT
Tree data structure
Lovely Professional University
 
PPTX
Tree in data structure
Äshïsh Jäïn
 
PPT
introduction to_trees
Danish Aakash
 
PPTX
Data structures 3
Parthipan Parthi
 
PPTX
Trees
Burhan Ahmed
 
PDF
Dsc++ unit 3 notes
Guru Nanak Institute Of Tech
 
PPTX
Mca iii dfs u-4 tree and graph
Rai University
 
PPTX
non linear data structure -introduction of tree
Siddhi Viradiya
 
PPTX
Binary tree
MKalpanaDevi
 
PPTX
Data structure tree - intermediate
MD. MARUFUZZAMAN .
 
PPTX
Tree data structure
Dana dia
 
PPTX
Data structure tree- advance
MD. MARUFUZZAMAN .
 
PPSX
Data Structure (Tree)
Adam Mukharil Bachtiar
 
PPTX
Trees data structure
Mahmoud Alfarra
 
PPTX
Types of Tree in Data Structure in C++
Himanshu Choudhary
 
DOCX
Trees and Graphs in data structures and Algorithms
BHARATH KUMAR
 
PPT
Trees
noraidawati
 
PPT
Trees
Gaditek
 
Unit 6 tree
Dabbal Singh Mahara
 
Lecture notes data structures tree
maamir farooq
 
Tree data structure
Lovely Professional University
 
Tree in data structure
Äshïsh Jäïn
 
introduction to_trees
Danish Aakash
 
Data structures 3
Parthipan Parthi
 
Dsc++ unit 3 notes
Guru Nanak Institute Of Tech
 
Mca iii dfs u-4 tree and graph
Rai University
 
non linear data structure -introduction of tree
Siddhi Viradiya
 
Binary tree
MKalpanaDevi
 
Data structure tree - intermediate
MD. MARUFUZZAMAN .
 
Tree data structure
Dana dia
 
Data structure tree- advance
MD. MARUFUZZAMAN .
 
Data Structure (Tree)
Adam Mukharil Bachtiar
 
Trees data structure
Mahmoud Alfarra
 
Types of Tree in Data Structure in C++
Himanshu Choudhary
 
Trees and Graphs in data structures and Algorithms
BHARATH KUMAR
 
Trees
Gaditek
 
Ad

Similar to Tree (20)

PPT
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
PPTX
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
PPTX
Tree.pptx
worldchannel
 
PDF
Module - 5_Trees.pdf
AnuradhaJadiya1
 
PDF
Binary tree
Ssankett Negi
 
PPT
Binary search Tree and avl tree , treee.ppt
sjain87654321
 
PPTX
DSA-Unit-2.pptx
SeethaDinesh
 
PDF
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure
SanketDawbhat
 
PPTX
tree-160731205832.pptx
MouDhara1
 
PPTX
Introduction to tree ds
Viji B
 
PPT
BINARY SEARCH TREE
ER Punit Jain
 
PPTX
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
tpvvsreenivasarao
 
PPTX
TreesTreesTreesTreesTreesTreesTrees.pptx
rprahulcoder
 
PPTX
Unit 5 Tree.pptx
SurajSharma266169
 
PPTX
Tree
knightofheart
 
PDF
unit-2-dsa-tree introduction of tree and terninology
sayalijscoe2
 
PPTX
Tree Data Structure in Advanced Data Structure
shailajacse
 
PPTX
NON-LINEAR DATA STRUCTURE-TREES.pptx
Rajitha Reddy Alugati
 
PPTX
TREES.pptx
19EBKCS055KarishmaVe
 
PPTX
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DRCARIBOU
 
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
Tree.pptx
worldchannel
 
Module - 5_Trees.pdf
AnuradhaJadiya1
 
Binary tree
Ssankett Negi
 
Binary search Tree and avl tree , treee.ppt
sjain87654321
 
DSA-Unit-2.pptx
SeethaDinesh
 
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure
SanketDawbhat
 
tree-160731205832.pptx
MouDhara1
 
Introduction to tree ds
Viji B
 
BINARY SEARCH TREE
ER Punit Jain
 
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
tpvvsreenivasarao
 
TreesTreesTreesTreesTreesTreesTrees.pptx
rprahulcoder
 
Unit 5 Tree.pptx
SurajSharma266169
 
unit-2-dsa-tree introduction of tree and terninology
sayalijscoe2
 
Tree Data Structure in Advanced Data Structure
shailajacse
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
Rajitha Reddy Alugati
 
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DRCARIBOU
 
Ad

More from invertis university (10)

DOCX
Data link control notes
invertis university
 
DOC
Program listds
invertis university
 
PPTX
Hashing
invertis university
 
PDF
Dsa book
invertis university
 
PPT
data structure on bca.
invertis university
 
PPT
data structure on bca.
invertis university
 
PPTX
System security
invertis university
 
Data link control notes
invertis university
 
Program listds
invertis university
 
data structure on bca.
invertis university
 
data structure on bca.
invertis university
 
System security
invertis university
 

Recently uploaded (20)

PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 

Tree

  • 1. Trees •Basic terminology • Binary Trees • Binary tree representation • algebraic Expressions • Complete Binary Tree • Extended Binary Trees • Array and Linked Representation of Binary trees • Traversing Binary trees • Threaded Binary trees • Traversing Threaded Binary trees • Huffman algorithm
  • 2. Basic terminology • Trees are a kind of data structure that is similar to normal trees. Data trees also have roots, branches and leaves. ‘A tree is a data structure that represents hierarchical relationships between individual data items. • An element of a tree is called a node. A branch connects two nodes. The node at the upper end of a branch is a predecessor of the node at the lower end and the node at the lower end is the successor of the predecessor. • There is a unique node that has no predecessor called the root of the tree. The nodes of the tree that have no branches are called leaf nodes. The nodes that have branches are called non- leaf nodes. Children of the same parent are called siblings. • The degree of a node is the number of successors of the node. Thus the degree of a leaf is 0. Level of a node is given by the length of the unique path from the root to the node
  • 4. Expression Tree • Expressions can be represented by tree structures • Operands are terminal (leaf) nodes and operator are non terminal nodes – E.g. Arithmetic Expression Tree – A-(C/5 * 2) + (D*5 % 4) + - % A * * 4 / 2 D 5 C 5
  • 5. Binary trees • The tree in which every node has a maximum of two branches is called a binary tree. Every parent has a maximum of two children, ‘right’ child and ‘left’ child. • A binary tree can have any number of nodes.
  • 7. Binary trees • A strictly binary tree is one in which every non-leaf node has non-empty right and left subtrees. Following figure a gives a strictly binary tree. • A strictly binary tree with n leaves contains 2n-1 nodes • A strictly binary tree with all its leaves at level d is termed as a complete binary tree of depth d. It contains 2d leaves at the level d. The tree contains 2d leaves and 2d-1 non-leaf nodes.
  • 8. Binary trees • Each node in the tree is assigned a unique number, which gives the nodes position in the tree. In complete binary tree for any node K, left and right children of the node K are 2*K and 2*K+1 respectively and the parent of K is the node K/2. • Depth = log2n +1 where n is the no of nodes. • Any node at level less than d-1 has two sons
  • 9. Representation of a binary tree in memory: • The structure of each node a binary tree contains the data field, a pointer to the left child and a pointer to the right child. The figure shows this structure • This structure can be defined as follows: struct tnode { struct tnode *left; int data; struct tnode *right; }; • There are two ways by which we can represent a binary tree. • Linked representation of a binary tree • Array representation of a binary tree
  • 10. Representation of a binary tree in memory: • Array representation of binary trees: When a binary tree is represented by arrays three separate arrays are required. One array stores the data fields of the trees. The other two arrays represents the left child and right child of the nodes. • A complete binary tree has a simple array representation. Suppose we number the nodes from left to right, beginning at the top and ending at the bottom. Then we can store the various data items in the corresponding elements of an array.
  • 11. Representation of a binary tree in memory: • Linked representation of binary trees • Binary trees can be represented by links, where each node contains the address of the left child and the right child. These addresses are nothing but kinds to the left and right child respectively. A node that does not have a left or a right child contains a NULL value in its link fields. • Linked representation uses three parallel arrays, INFO, LEFT and RIGHT and a pointer variable ROOT. Each node N of T will correspond to a location K such that – • INFO[K] contains the data at node N • LEFT[K] contains the location of left child node N • RIGHT[K] contains the location of right child node N • ROOT will contain the location of root R of T
  • 12. Representation of a binary tree in memory:
  • 13. Representation of a binary tree in memory:
  • 14. Traversing binary tree • Processing the contents of nodes of tree necessitates the visiting of the nodes of the tree. This is called traversing of the tree • Preorder or depth-first order traversal • In this traversal the root node is visited before visiting the two subtrees. The order followed is given sequentially by the following. • 1. Visit the root 2. Visit the nodes in the left subtree in preorder 3. Visit the nodes in the right subtree in preorder • Inorder or symmetric order traversal • Here the root node is visited in-between the left and right subtrees. The order of traverse is given as follows. • 1. Traverse the left subtree in inorder 2. Visit the root 3. Traverse the right subtree in inorder. • Postorder or breadth-first order traversal • Here the left and right subtrees are traversed before visiting the root. Following gives the sequential order followed for traversal. • 1. Visit the nodes of the left subtree in postorder 2. Traverse the right subtree in postorder 3. Visit the root
  • 15. 15 Traversing binary tree preorder inorder postorder A B C D E F G A B C D E F G A B C D E F G A B D E C F G D B E A F C G D E B F G C A
  • 16. 16 Traversing binary tree Void inorder(struct node *root) { if (root!=NULL) { inorder(root->left); printf(“%d”,root->info); inorder(root->right); } } Void preorder(struct node *root) { if (root!=NULL) { printf(“%d”,root->info); preorder(root->left); preorder(root->right); } }
  • 17. Traversing binary tree Void postorder(struct node *root) { if (root!=NULL) { postorder(root->left); postorder(root->right); printf(“%d”,root->info); } }
  • 18. Traversing using STACK • Preorder • Initially push NULL on to STACK and then set PTR:=ROOT. Then repeat the following steps until PTR=NULL. • (a) proceed down the left most path rooted at PTR, processing each node N on the path and pushing each right child R(N), if any, onto STACK. The traversing ends after a node N with no left child L(N) is processed. • (b) Pop and assign to PTR the top element on STACK. If PTR!=NULL, then return to step(a) otherwise Exit.
  • 19. Traversing using STACK • Inorder • Initially push NULL on to STACK and then set PTR:=ROOT. Then repeat the following steps until NULL is popped from STACK. • (a) proceed down the left most path rooted at PTR, and pushing each node N onto STACK and stopping when a node N with no left child is pushed onto STACK. • (b) Pop and process the nodes on STACK. If NULL is popped, then exit. If a node N with right child R(N) is processed, set PTR=R(N)and return to step(a).
  • 20. Traversing using STACK • Postorder • Initially push NULL on to STACK and then set PTR:=ROOT. Then repeat the following steps until NULL is popped from STACK. • (a) proceed down the left most path rooted at PTR, pushing each node N onto STACK and if N has a right child R(N), push –R(N) onto STACK. • (b) Pop and process positive nodes on STACK. If NULL is popped, then exit. If negative node is popped, that is, if PTR= -N for some node N, set PTR=N and return to step(a).
  • 21. Traversing binary tree • Q: a binary tree T has 9 nodes. The inorder and postorder traversal of T yield the following sequence of nodes Inorder: E A C K F H D B G Postorder: E C K A H B G D F Draw the tree T. • Ans: tree T is drawn from root downward as follows. (a) the root of T is obtained by choosing the last node in its postorder. Thus F is the root of tree. (b) the left child of the node F is obtained as follows: first use the inorder of T to find the nodes in the left subtree T1 of F. Thus T1 consists of nodes E, A, C, K. then the left child is obtained by choosing the last node in the postorder of T1. thus A is the left child of F. (c)Similarly, the right subtree T2 of F consists of the nodes H, D, B, G and D is the root of T2. thus D is the right child of F. Repeating the above process with each new node , we obtain the required tree.
  • 22. Threaded Binary Trees • The special link created to simplify the tree operation is called the thread. The thread makes it possible to perform traversals, insertions and deletions without using either stack or recursion. Binary trees with threads are termed threaded binary trees. Thread is formed by changing the NULL link to special link. • In implementing the threaded trees, each thread is distinguished from the tree pointer by associating it with a Boolean variable. The Boolean value tells if the link is a pointer to the nonempty subtree or it is a link to a node higher in the tree. The value is set to TRUE if the link is a thread • In a right in-threaded binary tree each NULL right link is replaced by threads connecting the nodes to its in-order successor. Following fig. shows right in-threaded trees. The threads are shown in dotted lines. The NULL right pointer of the he right most nodes K is not replaced by the thread as it does not have inorder successor
  • 24. Threaded Binary Trees • In sequential representation of binary tree, negative or positive value of a variable is set to indicate whether the link is a pointer or a thread. In the linked array representation, a thread is represented by the negative value of the link while the positive value is set to indicate the pointer. • In a left in-threaded tree the thread to the inorder predecessor of the node replaces each NULL left pointer. A binary tree with both left and right threads is called an in- threaded binary tree • A right in-threaded tree yields more advantages than left in-threaded tree. Right and left pre-threaded binary trees are the binary trees in which the NULL right and left pointers of the nodes are replaced by their preorder successors and predecessors respectively. These concepts are made use of in the traversal of binary trees
  • 26. Huffman algorithm • An extended binary tree is a transformation of any binary tree into a complete binary tree. • This transformation consists of replacing every null subtree of the original tree with ``special nodes.'' The nodes from the original tree are then internal nodes, while the ``special nodes'' are external nodes. • For instance, consider the following binary tree. Equivalent extended binary tree
  • 27. Huffman algorithm • Empty circles represent internal nodes, and filled circles represent external nodes • Every internal node in the extended tree has exactly two children, and every external node is a leaf. The result is a complete binary tree. • Number of external nodes(Ne) is 1 more than the number of internal nodes(Ni).i.e Ne=Ni+1. • External path length (Le) to be the sum of all path lengths summed over each path from the root R to an external node. • Le = 2+3+3+3+3+3+3 = 20 • internal path length (Li) to be the sum of all path lengths summed over each path from the root R to an internal node. • Li = 0+1+1+2+2+2 = 8 • Le = Li +2*n, where n is the number of internal nodes.
  • 28. Huffman algorithm • Suppose T is a 2-tree with n external nodes is assigned a nonnegative weights. The external weighted path length P of the tree T is defined to be the sum of the weighted path lengths. • P = W1L1 + W2L2+………+WnLn • Where Wi and Li denote the weight and path length of external node respectively. • Huffman's algorithm is a method for building an extended binary tree with a minimum weighted path length from a set of given weights. Initially construct a forest of singleton trees, one associated with each weight. If there are at least two trees, choose the two trees with the least weight associated with their roots and replace them with a new tree, constructed by creating a root node whose weight is the sum of the weights of the roots of the two trees removed, and setting the two trees just removed as this new node's children. This process is repeated until the forest consists of one tree
  • 29. Huffman algorithm • Huffman algorithm: suppose W1 and w2 are two minimum weigths among the n given weigths W1, W2, …….Wn. Find the tree T’ which gives a solution for the n-1 weigths w1+W2, W3, W4,………..Wn. Then, in the tree T’, replace the external node by the subtree The new 2-tree T is the desired solution. W1+W2 W1 W2
  • 30. Huffman algorithm • Let us work through an example for the set of weights {1,2,3,3,4} . In these intermediate steps we will display the temporary weights assigned by the algorithm in the circled nodes. Initially our forest is • During the first step, the two trees with weights 1 and 2 are merged, to create a new tree with a root of weight 3. • We now have three trees with weights of 3 at their roots. It does not matter which two we choose to merge. Let us choose the tree we just created and one of the singleton nodes of weight 3.
  • 31. Huffman algorithm • Now our two minimum trees are the two singleton nodes of weights 3 and 4. We will combine these to form a new tree of weight 7. • Finally we merge our last two remaining trees
  • 32. Huffman algorithm • The result is an extended binary tree of minimum weighted path length 29. In the following diagram each circled node is marked with its weighted path length
  • 33. CS 102 Building a Tree E 1 i 1 y 1 l 1 k 1 . 1 r 2 s 2 n 2 a 2 sp 4 e 8
  • 34. CS 102 Building a Tree E 1 i 1 y 1 l 1 k 1 . 1 r 2 s 2 n 2 a 2 sp 4 e 8 2
  • 35. CS 102 Building a Tree E 1 i 1 y 1 l 1 k 1 . 1 r 2 s 2 n 2 a 2 sp 4 e 8 2
  • 36. CS 102 Building a Tree E 1 i 1 k 1 . 1 r 2 s 2 n 2 a 2 sp 4 e 8 2 y 1 l 1 2
  • 37. CS 102 Building a Tree E 1 i 1 k 1 . 1 r 2 s 2 n 2 a 2 sp 4 e 8 2 y 1 l 1 2
  • 38. CS 102 Building a Tree E 1 i 1 r 2 s 2 n 2 a 2 sp 4 e 8 2 y 1 l 1 2 k 1 . 1 2
  • 39. CS 102 Building a Tree E 1 i 1 r 2 s 2 n 2 a 2 sp 4 e 8 2 y 1 l 1 2 k 1 . 1 2
  • 40. CS 102 Building a Tree E 1 i 1 n 2 a 2 sp 4 e 8 2 y 1 l 1 2 k 1 . 1 2 r 2 s 2 4
  • 41. CS 102 Building a Tree E 1 i 1 n 2 a 2 sp 4 e 8 2 y 1 l 1 2 k 1 . 1 2 r 2 s 2 4
  • 42. CS 102 Building a Tree E 1 i 1 sp 4 e 8 2 y 1 l 1 2 k 1 . 1 2 r 2 s 2 4 n 2 a 2 4
  • 43. CS 102 Building a Tree E 1 i 1 sp 4 e 8 2 y 1 l 1 2 k 1 . 1 2 r 2 s 2 4 n 2 a 2 4
  • 44. CS 102 Building a Tree E 1 i 1 sp 4 e 8 2 y 1 l 1 2 k 1 . 1 2 r 2 s 2 4 n 2 a 2 4 4
  • 45. CS 102 Building a Tree E 1 i 1 sp 4 e 82 y 1 l 1 2 k 1 . 1 2 r 2 s 2 4 n 2 a 2 4 4
  • 46. CS 102 Building a Tree E 1 i 1 sp 4 e 82 y 1 l 1 2 k 1 . 1 2 r 2 s 2 4 n 2 a 2 4 4 6
  • 47. CS 102 Building a Tree E 1 i 1 sp 4 e 8 2 y 1 l 1 2 k 1 . 1 2 r 2 s 2 4 n 2 a 2 4 4 6 What is happening to the characters with a low number of occurrences?
  • 48. CS 102 Building a Tree E 1 i 1 sp 4 e 82 y 1 l 1 2 k 1 . 1 2 r 2 s 2 4 n 2 a 2 4 4 6 8
  • 49. CS 102 Building a Tree E 1 i 1 sp 4 e 82 y 1 l 1 2 k 1 . 1 2 r 2 s 2 4 n 2 a 2 4 4 6 8
  • 50. CS 102 Building a Tree E 1 i 1 sp 4 e 8 2 y 1 l 1 2 k 1 . 1 2 r 2 s 2 4 n 2 a 2 4 4 6 8 10
  • 51. CS 102 Building a Tree E 1 i 1 sp 4 e 8 2 y 1 l 1 2 k 1 . 1 2r 2 s 2 4 n 2 a 2 4 4 6 8 10
  • 52. CS 102 Building a Tree E 1 i 1 sp 4 e 8 2 y 1 l 1 2 k 1 . 1 2 r 2 s 2 4 n 2 a 2 4 4 6 8 10 16
  • 53. CS 102 Building a Tree E 1 i 1 sp 4 e 82 y 1 l 1 2 k 1 . 1 2 r 2 s 2 4 n 2 a 2 4 4 6 8 10 16
  • 54. CS 102 Building a Tree E 1 i 1 sp 4 e 8 2 y 1 l 1 2 k 1 . 1 2 r 2 s 2 4 n 2 a 2 4 4 6 8 10 16 26
  • 55. CS 102 Building a Tree E 1 i 1 sp 4 e 8 2 y 1 l 1 2 k 1 . 1 2 r 2 s 2 4 n 2 a 2 4 4 6 8 10 16 26
  • 56. General tree • A general tree will have more than two children or subtrees unlike binary tree. It is a finite nonempty set of nodes. There is no upper limit on the degree of a node • Conversion of general tree to a binary tree • Suppose T is general tree and T’ is correspondent binary tree • Nodes in T and T’ will be same. • Root of T’ will be the root of T.
  • 57. General tree • Left child of node N in T’ will be the first child of node N in the general tree T and the right child in of node N in T’ will be the next sibling of N in T.

Editor's Notes