0% found this document useful (0 votes)
8 views35 pages

Dsc++ Unit III

This document provides an overview of tree data structures, including terminology such as root, edge, parent, child, and leaf nodes. It discusses various types of binary trees, their properties, and traversal methods, as well as the concept of priority queues and heap data structures. Additionally, it explains operations on max heaps and the characteristics of different types of threaded binary trees.

Uploaded by

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

Dsc++ Unit III

This document provides an overview of tree data structures, including terminology such as root, edge, parent, child, and leaf nodes. It discusses various types of binary trees, their properties, and traversal methods, as well as the concept of priority queues and heap data structures. Additionally, it explains operations on max heaps and the characteristics of different types of threaded binary trees.

Uploaded by

gaddekavyasree2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

UNIT-III

Tree - Terminology

In linear data structure data is organized in sequential order and in non-


linear data structure data is organized in random order. Tree is a very
popular non-linear data structure used in wide range of applications. A tree
data structure can be defined as follows...

DEF:Tree is a non linear data structure which organizes the data in a


hierarchical manner.

Key properties of Tree

A tree T is represented by nodes and edges, which includes:

1. T is empty (called null or empty tree).


2. T has a left subtree and right subtree

Terminology
Root:-In a tree data structure, the first node is called as Root Node. Every
tree must have root node. We can say that root node is the origin of tree
data structure. In any tree, there must be only one root node. We never
have multiple root nodes in a tree.

In the figure A is the root Node


Edge :- 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.

Parent :- Node which is predecessor of any node is called as PARENT


NODE i.e. node which has branch from it to any other node is called as
parent node. In the above figure node B is the parent node of E, F, and G
node and E, F, and G are called children of B.
Child :- 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.

Siblings :- Nodes which belong to same Parent are called as SIBLINGS. In


simple words, the nodes with same parent are called as Sibling nodes.
Children of same parent are called Sibling.

Leaf :- Node which does not have a child is called as LEAF Node. In simple
words, a leaf is a node with no child. In the figure E,F,C,G,C,H are the leaf
nodes

Level :- 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 the above figure, the node A is at level 0, the node
B, C, Dare at level 1, the nodes E, F, G, H are at level 2.

Tree Height :- Number of edges in the longest path from the root to the leaf
node.
the height of the tree is 3

Predecessor:-While displaying the tree, if some particular nodes previous to


some other nodes than that node is called the predecessor of the other node.
In our example, the node E is predecessor of node B.

Successor:-The node which occurs next to some other node is called


successor of that node. In our example, B is successor of F and G.

Internal Nodes:-The nodes in the tree which are other than leaf nodes and
the root node are called as internal nodes. These nodes are present in
between root node and leaf nodes in the tree that’s why these nodes are
called as internal node. Here, B and D are internal nodes.

Degree of tree :-The maximum degree of the node in the tree is called
the degree of the tree. Here, the degree of tree is 3.

binary tree
A binary tree is a finite set of nodes that is either empty or consist a root
node and two disjoint binary trees called the left subtree and the right
subtree.
In other words, a binary tree is a non-linear data structure in which each
node has maximum of two child nodes. The tree connections can be called as
branches
A binary tree is a special case of an ordered tree, where k is 2.
1) Complete Binary Tree
A binary tree T is said to be complete binary tree if -
1. All its levels, except possibly except possibly the last, have the maximum
number of nodes and
2. All the nodes at the last level appears as far left as possible.

2) Strictly Binary Tree


When every non leaf node in a binary tree is filled with left and right
subtrees, the tree is called a strictly binary tree.

3) Extended Binary Tree


The binary tree that is extended with zero (no nodes) or left or right node or
both the nodes is called an extended binary tree or a 2- tree.
The extended binary tree is shown in figure above. The extended nodes are
indicated by square box. Maximum nodes in the binary tree have one child
(nodes) shown at either left or right by
adding one or more children, the tree can be extended. The extended
binary tree is very useful for representation of algebraic expressions. The

left and right child nodes denote operands and parent node indicates
operator.

4) Full Binary Tree

A Binary Tree is full binary tree if and only if -

1. Each non- leaf node has exactly two child nodes.


2. All leaf nodes are at the same level.

Properties of Binary trees


1) The maximum number of nodes at level ‘l’ of a binary tree is 2l-1.
2) Maximum number of nodes in a binary tree of height ‘h’ is 2h – 1.
3) In a Binary Tree with N nodes, minimum possible height or minimum number of
levels is ⌈

Log2(N+1) ⌉

4) A Binary Tree with L leaves has at least ⌈ Log2L ⌉ + 1 levels


5) In Binary tree where every node has 0 or 2 children, number of leaf nodes
is always one more than nodes with two children.
Representation of Binary Trees
Let T be a Binary Tree. There are two ways of representing T in the memory as
follow

1. Sequential Representation of Binary Tree.


2. Link Representation of Binary Tree.

1) Linked Representation of Binary Tree

Consider a Binary Tree T. T will be maintained in memory by means of a


linked list representation which uses three parallel arrays; INFO, LEFT, and
RIGHT pointer
variable ROOT as follows. In Binary Tree each node N of Twill correspond to a
location k such that
 LEFT [k] contains the location of the left child of node N.

 INFO [k] contains the data at the node N.

 RIGHT [k] contains the location of right


child of node N. Representation of a node:

In this representation of binary tree root will contain the location of the
root R of T. If any one of the subtree is empty, then the corresponding
pointer will contain the null value if the
tree T itself is empty, the ROOT will contain the null value.

Example
Consider the binary tree T in the figure. A schematic diagram of the linked list
representation
of T appears in the following figure. Observe that each node is pictured with
its three fields, and that the empty subtree is pictured by using x for null
entries.

Binary Tree
Linked Representation of the Binary Tree

2) Sequential representation of Binary Tree


Let us consider that we have a tree T. let our tree T is a binary tree that us
complete binary tree. Then there is an efficient way of representing T in the
memory called the sequential representation or array representation of T.
This representation uses only a linear array TREE as follows:

 The root N of T is stored in TREE [1].


 If a node occupies TREE [k] then its left child is stored in TREE [2 *
k] and its right child is stored into TREE [2 * k + 1].

For Example:

Consider the following Tree:


Its sequential representation is as follows

Binary Tree traversals


Tree traversal is the process of visiting each node in the tree exactly
once. Visiting each node in a graph should be done in a systematic
manner. If search result in a visit to all the vertices, it is called a traversal.
There are basically three traversal techniques for a binary tree that are,

 Preorder traversal
 Inorder traversal
 Postorder traversal

1) Preorder traversal
To traverse a binary tree in preorder, following operations are carried out:

1. Visit the root.


2. Traverse the left sub tree of root.
3. Traverse the right sub tree of root.

Note: Preorder traversal is also known as NLR traversal.

Algorithm:

Algorithm preorder(t)
/*t is a binary tree. Each node of t has
three fields: lchild, data, and rchild.*/
{
If t! =0 then
{
Visit(t);
Preorder(t-
>lchild);
Preorder(t-
>rchild);
}
}
Example: Let us consider the given binary tree,

Therefore, the preorder traversal of the above tree will be:


7,1,0,3,2,5,4,6,9,8,10

2) Inorder traversal
To traverse a binary tree in inorder traversal, following operations are carried
out:

1. Traverse the left most sub tree.


2. Visit the root.
3. Traverse the right most sub tree.

Note: Inorder traversal is also known as LNR traversal.

Algorithm:

Algorithm inorder(t)

/*t is a binary tree. Each node of t has


three fields: lchild, data, and rchild.*/
{
If t! =0 then
{
Inorder(t-
>lchild);
Visit(t);
Inorder(t->rchild);
}
}
Example: Let us consider a given binary tree.

Therefore the inorder traversal of above tree will be: 0,1,2,3,4,5,6,7,8,9,10

3) Postorder traversal
To traverse a binary tree in postorder traversal, following operations are carried
out:

1. Traverse the left sub tree of root.


2. Traverse the right sub tree of root.
3. Visit the root.

Note: Postorder traversal is also known as LRN traversal.

Algorithm:

Algorithm postorder(t)

/*t is a binary tree .Each node of t has


three fields: lchild, data, and rchild.*/
{
If t! =0 then
{
Postorder(t-
>lchild);
Postorder(t-
>rchild); Visit(t);
}
}
Example: Let us consider a given binary tree.

Therefore the postorder traversal of the above tree will be:


0,2,4,6,5,3,1,8,10,9,7 Threaded Binary Tree
A binary tree can be represented by using array representation or linked
list representation. When a binary tree is represented using linked list
representation. If any node is not having a child we use a NULL pointer.
These special pointers are threaded and the binary tree having such
pointers is called a threaded binary tree. Thread in a binary tree is
represented by a dotted line. In linked list representation of a binary tree,
they are a number of a NULL pointer than actual pointers. This NULL
pointer does not play any role except indicating there is no child. The
threaded binary tree is proposed by A.J Perlis and C Thornton. There are
three ways to thread a binary tree.

1. In the in order traversal When The right NULL pointer of each leaf node can
be replaced by a thread to the successor of that node then it is called a
right thread, and the resultant tree called a right threaded tree or right
threaded binary tree.
2. When The left NULL pointer of each node can be replaced by a thread to the
predecessor of that node under in order traversal it is called left thread and
the resultant tree will call a left threaded tree.
3. In the in order traversal, the left and the right NULL pointer can be used to
point to predecessor and successor of that node respectively. then the
resultant tree is called a fully threaded tree.

In the threaded binary tree when there is only one thread is used then it is
called as one way threaded tree and when both the threads are used then it
is called the two way threaded tree. The pointer point to the root node when
If there is no in-order predecessor or in-order successor.
Consider the following binary tree:

The in-order traversal of a given tree is D B H E A F C G. Right threaded


binary tree for a given tree is shown below:

Advantages of Thread Binary Tree

Non-recursive pre-order, in-order and post-order traversal can be implemented


without a stack. Disadvantages of Thread Binary Tree

1. Insertion and deletion operation becomes more difficult.


2. Tree traversal algorithm becomes difficult.
3. Memory required to store a node increases. Each node has to store the
information whether the links is normal links or threaded links.

Types of Threaded of Binary Tree


There are three types of Threaded Binary Tree,
1) Right Threaded Binary Tree

Right threaded binary tree for a given tree is shown:


2)Left Threaded Binary Tree

Left threaded binary tree for a given tree is shown:

3) Fully Threaded Binary Tree


Fully threaded binary tree for a given tree is shown:

PRIORITY QUEUE

Def: Priority queue is a variant of queue data structure in which insertion is


performed in the order of arrival and deletion is performed based on the
priority.
In priority queue every element is associated with some priority.
Normally the priorities are specified using numerical values. In some cases
lower values indicate high priority and in some cases higher values indicate
high priority.In priority queues elements are processed according to their
priority but not according to the order they are entered into the queue.
Ex: let P be a priority queue with three elements a, b, c whose priority
factors are 2,1,1 respectively. Here, larger the number, higher is the
priority accorded to that element.When a new element d with higher
priority 4 is inserted, d joins at the head of the queue superseding the
remaining elements. When elements in the queue have the same
priority,
then the priority queue behaves as an ordinary queue following the principle of
FIFO amongst
such elements.
The working of a priority queue may be likened to a situation when a
file of patients waits for their turn in a queue to have an appointment with
a doctor. All patients are accorded equal priority and follow an FCFS
scheme by appointments. However, when a patient with bleeding injuries is
brought in, he/she is accorded higher priority and is immediately moved to
the head of the queue for immediate attention by the doctor. This is priority
queue at work.
There are two types of priority queues they are as follows...
1. Max Priority Queue
2. Min Priority Queue

Max Heap Data structure


Heap data structure is a specialized binary tree based data structure. Heap is a
binary tree with special characteristics. In a heap data structure, nodes are
arranged based on thier values. A
heap data some times also called Binary Heap.
structure as

There are two types of heap data structures and they are as follows...
1. Max Heap
2. Min Heap
Every heap data structure
h has t e following properties...
Property #1 (Ordering): Nodes must be arranged in an order according
to their values based on Max heap or Min heap.
Property #2 (Structural): All levels in a heap must be full except the last
level and all nodes must be filled from left to right strictly.
Max Heap
Max heap data structure is a specialized full binary tree data structure. In a max
heap nodes are arranged based on node value.
Example
Above tree is satisfying Ordering property and Structural property according to
both Heap data the Max
structure.
Operations on Max
Heap
The following operations are performed on a Max heap data structure...
1. Finding Maximum
2. Insertion
3. Deletion
Finding Maximum Value
e Op ration in Max Heap
Finding the node which has maximum value in a max heap ismax heap, the root root node value
y
very simple. In node has the maximum value than all other
nodes. So, directly we can displa as maximum value in max
heap.
Insertion Operation in Max Heap
Insertion Operation in max heap is performed as follows...
 Step 1 - Insert the newNode as last leaf from left to right.
 Step 2 - Compare newNode value with its Parent node.
 Step 3 - If newNode value is greater than its parent, then swap both of
them.
 Step 4 - Repeat step 2 and step 3 until newNode value is less than its
parent node (or) newNode reaches to root.
Example
Consider the above max heap. Insert a new node with value 85.
 Step 1 - Insert the newNode with value 85 as last leaf from left to right.
That means newNode is added as a right child of node with value 75. After
adding max heap is as follows...

 Step 2 - Compare newNode value (85) with its Parent node value (75).
That means 85 > 75
 Step 3 - Here newNode value (85) is greater than its parent value (75),
then swap both of

them. After swapping, max heap is as follows...


 Step 4 - Now, again compare newNode value (85) with its parent
8 node value (
9).

Here, newNode value smaller than its parent node value we stop insertion
(85) is (89). So,
process. Finally, max heap after insertion of a new node with value 85 is as
follows...

Deletion Operation in Max Heap


In a max heap, deleting last node is very simple as it does not disturb max
heap properties.

Deleting root node from a max heap is little difficult as it disturbs


a the max he p
properties. We
use the following steps toe delet root node from a max heap...
 Step 1 - Swap the root node with last node in max heap
 Step 2 - Delete last node.
 Step 3 - Now, compare root value with its left child value.
 Step 4 - If root valuealler than its left child, then compare left child with
is sm sibling. Else its right
goto Step 6
 Step 5 - If left child is larger than its right p root with left
value sibling, then sw
a
child otherwise swap root with its right child.
 Step 6 - If root valueger than its left child, then compare root value with
is lar child value. its right
 Step 7 - If root value is smaller than its right child, rootwith right
child otherwise stop the then swap
process.
 Step 8 - Repeat the same until root node fixes at its exact position.

Example
Consider the above max heap. Delete root node (90) from the max heap.
 Step 1 - Swap the root node (90) with last node 75 in max heap. After
swapping max heap is as follows...
 Step 2 - Delete last node. Here the last node is 90. After deleting node
with value 90 from heap, max heap is as follows...

 Step 3 - Compare root node (75) with its left child (89).

Here, root value (75)er than its left child value (89). So, compare left child
is small (89) with

its right sibling (70).


 Step 4 - Here, left child value (89) is larger than its right sibling
(70), So, swap root

(75) with left child (89).


 Step 5 - Now, again compare 75 with its left child (36).

Here, node with value 75 is larger than its left child. So, we compare node
75 with its right child 85.

ap both of them.
 Step 6 - Here, node with value 75 is smaller than its right child
w (85). So, we s

After swapping max heap is as follows...


th value 75 with its left child (15).
 Step 7 - Now, compare node
i w
Here, node with valueg 75 is larger than its left child (15) and it does
not have right child. eSo we stop the process.Finally, max heap after
deleting root node (90) is as follows...

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy