0% found this document useful (0 votes)
11 views20 pages

Case 1

The document discusses data structures and algorithms related to stacks, queues, and their applications. It describes how to evaluate postfix expressions and handle function calls using stacks. It also provides examples of applications of queues and describes priority queue implementations using different data structures.

Uploaded by

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

Case 1

The document discusses data structures and algorithms related to stacks, queues, and their applications. It describes how to evaluate postfix expressions and handle function calls using stacks. It also provides examples of applications of queues and describes priority queue implementations using different data structures.

Uploaded by

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

16CS3202 - Data Structures II Yr CSE / III Sem

(ii) Postfix Expressions:


A postfix expression can be evaluated using the Stack data structure. To evaluate a postfix expression
using Stack, we can use the following steps:
1. Read all the symbols one by one from left to right in the given Postfix Expression
2. If the reading symbol is operand, then push it on to the Stack.
3. If the reading symbol is operator (+ , - , * , / etc.,), then perform TWO pop operations and store
the two popped operands in two different variables (operand1 and operand2). Then perform reading
symbol operation using operand1 and operand2 and push result back on to the Stack.
4. Finally, perform a pop operation and display the popped value as final result.
Example
Consider the following Expression,
Infix Expression : (5+3)*(8-2)
Postfix Expression : 5 3 + 8 2 - *

UNIT 2 Page 14
16CS3202 - Data Structures II Yr CSE / III Sem

Infix Expression : (5+3)*(8-2) = 48


Postfix Expression : 5 3 + 8 2 - * = 48

(iii)Function Call:
When there is a function call, all the important information that needs to be saved, suchas register values
(corresponding to variable names) and the return address (which can beobtained from the program
counter, which is typically in a register), is saved on a stack. Then the control is transferredto the new
function, which is free to replace the registers with its values. If it makes otherfunction calls, it follows
the same procedure. When the function wants to return, it looksat the stack at the top and restores all the
registers. It then makes the returnjump.The information savedis called either an activation record or
stack frame. There is always the possibility that we will run out of stack spaceby having too many
simultaneously active functions.
(iv) Infix to Postfix Conversion:
We can use stack to convert an infix expression into postfix form. To know about the basics of
expression, following topics will be useful.An expression is a collection of operators and operands that
represents a specific value. Based on the operator position, expressions are divided into 3 types, such as
Infix Expression
Postfix Expression
Prefix Expression
In infix expression, operator is used in between operands. For example, a+b. In postfix
expression, operator is used after operands. For example, ab+. In prefix expression, operator is used
before operands. For example, +ab.
To convert Infix Expression into Postfix Expression using a stack data structure, we can use the
following steps:
1. Read all the symbols one by one from left to right in the given Infix Expression.
2. If the reading symbol is operand, then directly print it to the result (Output).
3. If the reading symbol is left parenthesis '(', then Push it on to the Stack.
4. If the reading symbol is right parenthesis ')', then Pop all the contents of stack until respective left
parenthesis is poped and print each poped symbol to the result.
5. If the reading symbol is operator (+ , - , * , / etc.,), then Push it on to the Stack. However, first pop the
operators which are already on the stack that have higher or equal precedence than current operator and
print them to the result.
Example:
Consider the following Infix Expression...
(A+B)*(C-D)
The given infix expression can be converted into postfix expression using Stack data Structure as
follows:

UNIT 2 Page 15
16CS3202 - Data Structures II Yr CSE / III Sem

UNIT 2 Page 16
16CS3202 - Data Structures II Yr CSE / III Sem

The final Postfix Expression is as follows...


AB+CD-*

2.5 APPLICATIONS OF QUEUES:

When jobs are submitted to a printer, jobs sent to a printer are placed on a queue.
Lines at ticketcounters are queues, because service is first-come first-served.
Users on other machines are given access to files on a first-come first-served basis, so the data
structure is a queue.
Further examples include the following:
1. Calls to large companies are generally placed on a queue when all operators are busy.
2. In large universities, where resources are limited, students must sign a waiting list ifall computers are
occupied. The student who has been at a computer the longest isforced off first, and the student who has
been waiting the longest is the next user to beallowed on.

2.5.1 PRIORITY QUEUE


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.

There are two types of priority queues, such as:

UNIT 2 Page 17
16CS3202 - Data Structures II Yr CSE / III Sem

1. Max Priority Queue


2. Min Priority Queue

1. Max Priority Queue


In max priority queue, elements are inserted in the order in which they arrive the queue and always
maximum value is removed first from the queue. For example assume that we insert in order 8, 3, 2, 5
and they are removed in the order 8, 5, 3, 2. The following are the operations performed in a Max
priority queue:
1. isEmpty() - Check whether queue is Empty.
2. insert() - Inserts a new value into the queue.
3. findMax() - Find maximum value in the queue.
4. remove() - Delete maximum value from the queue.
Max Priority Queue Representations:
There are 6 representations of max priority queue.
1. Using an Unordered Array (Dynamic Array)
2. Using an Unordered Array (Dynamic Array) with the index of the maximum value
3. Using an Array (Dynamic Array) in Decreasing Order
4. Using an Array (Dynamic Array) in Increasing Order
5. Using Linked List in Increasing Order
6. Using Unordered Linked List with reference to node with the maximum value

a. Using an Unordered Array (Dynamic Array)


In this representation elements are inserted according to their arrival order and maximum
element is deleted first from max priority queue. For example, assume that elements are inserted in the
order of 8, 2, 3 and 5. And they are removed in the order 8, 5, 3 and 2.

Each operation is according to this representation:


isEmpty(): If 'front == -1' queue is Empty. This operation requires O(1) time complexity that
means constant time.
insert(): New element is added at the end of the queue. This operation requires O(1) time
complexity that means constant time.
findMax(): To find maximum element in the queue, we need to compare with all the elements in
the queue. This operation requires O(n) time complexity.
remove() : To remove an element from the queue first we need to perform findMax() which
requires O(n) and removal of particular element requires constant time O(1). This operation
requires O(n) time complexity.

b. Using an Unordered Array (Dynamic Array) with the index of the maximum value
In this representation elements are inserted according to their arrival order and maximum
element is deleted first from max priority queue. For example, assume that elements are inserted in the
order of 8, 2, 3 and 5. And they are removed in the order 8, 5, 3 and 2.

UNIT 2 Page 18
16CS3202 - Data Structures II Yr CSE / III Sem

Each operation is according to this representation:


isEmpty() - If 'front == -1' queue is Empty. This operation requires O(1) time complexity that
means constant time.
insert() - New element is added at the end of the queue with O(1) and for each insertion we need
to update maxIndex with O(1). This operation requires O(1) time complexity that means
constant time.
findMax() - To find maximum element in the queue is very simple as maxIndex has maximum
element index. This operation requires O(1) time complexity.
remove() - To remove an element from the queue first we need to perform findMax() which
requires O(1) , removal of particular element requires constant time O(1) and update maxIndex
value which requires O(n). This operation requires O(n) time complexity.

c. Using an Array (Dynamic Array) in Decreasing Order


In this representation elements are inserted according to their value in decreasing order and maximum
element is deleted first from max priority queue. For example, assume that elements are inserted in the
order of 8, 5, 3 and 2. And they are removed in the order 8, 5, 3 and 2.

Each operation is according to this representation:


isEmpty() - If 'front == -1' queue is Empty. This operation requires O(1) time complexity that
means constant time.
insert() - New element is added at a particular position in the decreasing order into the queue
with O(n), because we need to shift existing elements inorder to insert new element in
decreasing order. This operation requires O(n) time complexity.
findMax() - To find maximum element in the queue is very simple as maximum element is at
the beginning of the queue. This operation requires O(1) time complexity.
remove() - To remove an element from the queue first we need to perform findMax() which
requires O(1), removal of particular element requires constant time O(1) and rearrange
remaining elements which requires O(n). This operation requires O(n) time complexity.

d. Using an Array (Dynamic Array) in Increasing Order


In this representation elements are inserted according to their value in increasing order and maximum
element is deleted first from max priority queue. For example, assume that elements are inserted in the
order of 2, 3, 5 and 8. And they are removed in the order 8, 5, 3 and 2.

Each operation is according to this representation:


isEmpty() - If 'front == -1' queue is Empty. This operation requires O(1) time complexity that
means constant time.

UNIT 2 Page 19
16CS3202 - Data Structures II Yr CSE / III Sem

insert() - New element is added at a particular position in the increasing order into the queue
with O(n), because we need to shift existing elements inorder to insert new element in increasing
order. This operation requires O(n) time complexity.
findMax() - To find maximum element in the queue is very simple as maximum element is at
the end of the queue. This operation requires O(1) time complexity.
remove() - To remove an element from the queue first we need to perform findMax() which
requires O(1), removal of particular element requires constant time O(1) and rearrange
remaining elements which requires O(n). This operation requires O(n) time complexity.

e.Using Linked List in Increasing Order


In this representation, we use a single linked list to represent max priority queue. In this
representation elements are inserted according to their value in increasing order and node with
maximum value is deleted first from max priority queue. For example, assume that elements are inserted
in the order of 2, 3, 5 and 8. And they are removed in the order 8, 5, 3 and 2.

Each operation is according to this representation:


isEmpty() - If 'head == NULL' queue is Empty. This operation requires O(1) time complexity
that means constant time.
insert() - New element is added at a particular position in the increasing order into the queue
with O(n), because we need to the position where new element has to be inserted. This operation
requires O(n) time complexity.
findMax() - To find maximum element in the queue is very simple as maximum element is at
the end of the queue. This operation requires O(1) time complexity.
remove() - To remove an element from the queue is simply removing the last node in the queue
which requires O(1). This operation requires O(1) time complexity.

f. Using Unordered Linked List with reference to node with the maximum value
In this representation, we use a single linked list to represent max priority queue. Always we
maintain a reference (maxValue) to the node with maximum value. In this representation elements are
inserted according to their arrival and node with maximum value is deleted first from max priority
queue. For example, assume that elements are inserted in the order of 2, 8, 3 and 5. And they are
removed in the order 8, 5, 3 and 2.

Each operation is according to this representation:


isEmpty() - If 'head == NULL' queue is Empty. This operation requires O(1) time complexity
that means constant time.
insert() - New element is added at end the queue with O(1) and update maxValue reference with
O(1). This operation requires O(1) time complexity.

UNIT 2 Page 20
16CS3202 - Data Structures II Yr CSE / III Sem

findMax() - To find maximum element in the queue is very simple as maxValue is referenced to
the node with maximum value in the queue. This operation requires O(1) time complexity.
remove() - To remove an element from the queue is deleting the node which referenced by
maxValue which requires O(1) and update maxValue reference to new node with maximum
value in the queue which requires O(n) time complexity. This operation requires O(n) time
complexity.

UNIT 2 Page 21
16CS3202 - Data Structures II Yr CSE / III Sem

UNIT III
NON LINEAR DATA STRUCTURES-TREE
3.1 Tree ADT
3.2 Representation of trees
3.3 Binary Tree ADT
3.4 Expression trees
3.5 Applications of trees
3.6 BST ADT
3.7 Tree traversals
3.8 AVL Trees
3.9 B-Tree
3.10 Heaps
3.10.1 Binary heaps
3.10.2 Applications of binary heaps
3.11 Binomial heaps

3.1 Tree ADT


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 data structure used in wide range of
applications.
A tree data structure can be defined as follows...
Tree is a non-linear data structure which organizes data in hierarchical structure and this
is a recursive definition.
A tree data structure can also be defined as follows...
Tree data structure is a collection of data (Node) which is organized in hierarchical
structure and this is a recursive definition
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.
Example

Terminology

UNIT 4 Page 1
16CS3202 - Data Structures II Yr CSE / III Sem

In a tree data structure, we use the following terminology...


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

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

3. Parent
In a tree data structure, the node which is predecessor of any node is called as PARENT
NODE. In simple words, the node which has branch from it to any other node is called as parent node.
Parent node can also be defined as "The node which has child / children".

4. Child

UNIT 4 Page 2
16CS3202 - Data Structures II Yr CSE / III Sem

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.

5. Siblings
In a tree data structure, nodes which belong to same Parent are called as SIBLINGS. In simple
words, the nodes with same parent are called as Sibling nodes.

6. Leaf
In a tree data structure, the node which does not have a child is called as LEAF Node. In simple
words, a leaf is a node with no child.
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.

UNIT 4 Page 3
16CS3202 - Data Structures II Yr CSE / III Sem

7. Internal Nodes
In a tree data structure, the node which has at least one child is called as INTERNAL Node. In
simple words, an internal node is a node with at least one child.
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.

8. Degree
In a tree data structure, the 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'

9. Level
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).

UNIT 4 Page 4
16CS3202 - Data Structures II Yr CSE / III Sem

10. Height
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'.

11. Depth
In a tree data structure, the total number of edges 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'.

12. Path

UNIT 4 Page 5
16CS3202 - Data Structures II Yr CSE / III Sem

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
below example the path A - B - E - J has length 4.

13. Sub Tree


In a tree data structure, each child from a node forms a sub tree recursively. Every child node
will form a sub tree on its parent node.

3.3 Binary Tree ADT


In a normal tree, every node can have any number of children. 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 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 as 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.
Example

UNIT 4 Page 6
16CS3202 - Data Structures II Yr CSE / III Sem

TYPES OF BINARY TREE:


a) Complete Binary Tree
b) Full Binary Tree
c) Left Skewed Binary Tree
d) Right Skewed Binary Tree
Complete Binary Tree
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 and in complete binary tree all the nodes must
have exactly two children and at every level of complete binary tree there must be 2 level number of
nodes 2h+1 1 where h is the 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 Complete Binary Tree.

Complete binary tree is also called as Perfect Binary Tree

Full Binary Tree:


A full binary tree (sometimes proper binary tree or 2-tree or strictly binary tree) is a tree in
which every node other than the leaves has two children.
So you have no nodes with only 1 child. Appears to be the same as strict binary tree.

UNIT 4 Page 7
16CS3202 - Data Structures II Yr CSE / III Sem

Left Skewed Binary Tree:


A binary tree in which each node is having either only left sub-trees or no sub-trees is called as
left skewed binary tree. In this program, binary tree is first created. Then every child node is parsed.
While parsing only the left sub-trees are considered as valid and if any right sub-tree is found then that
binary tree is not a left skewed binary tree.

Right Skewed Binary Tree:


A binary tree in which each node is having either only right sub-trees or no sub-trees is called as
right skewed binary tree. In this program, binary tree is first created. Then every child node is parsed.
While parsing only the right sub-trees are considered as valid and if any left sub-tree is found then that
binary tree is not a right skewed binary tree.

Applications of binary trees:


Binary Search Tree - Used in many search applications where data is constantly entering/leaving,
such as the map and set objects in many languages' libraries.

UNIT 4 Page 8
16CS3202 - Data Structures II Yr CSE / III Sem

Hash Trees - used in p2p programs and specialized image-signatures in which a hash needs to be
verified, but the whole file is not available.
Syntax Tree - Constructed by compilers and (implicitly) calculators to parse expressions.

3.2 BINARY TREE REPRESENTATIONS


A binary tree data structure is represented using two methods. Those methods are as follows...
Array Representation
Linked List Representation
Consider the following binary tree...

1. Array Representation
In array representation of binary tree, we use a one dimensional array (1-D Array) to represent a
binary tree.
Consider the above example of binary tree and it is represented as follows...

To represent a binary tree of depth 'n' using array representation, we need one dimensional
array with a maximum size of 2n+1 - 1.
2. Linked List Representation
We use double linked list to represent a binary tree. In a double 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.
In this linked list representation, a node has the following structure...

The above example of binary tree represented using Linked list representation is shown as follows...

UNIT 4 Page 9
16CS3202 - Data Structures II Yr CSE / III Sem

Declaration of Binary Tree:


typedefstruct node
{
int data;
struct node *left;
struct node *right;
}node;

Insertion:
node *get_node()
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->left=NULL;
temp->right=NULL;
return temp;
}
void insert(node *root,node *New)
{
charch;
New=get_node();
cout<<"\nEnter the Element";
cin>>New->data;
if(root==NULL)
root=New;
else
cout<<nWhere to insert left/right of %d"<<root->data;
ch=getche();

UNIT 4 Page 10
16CS3202 - Data Structures II Yr CSE / III Sem

if((ch=='r')||(ch=='r'))
{
if(root->right==NULL)
{
root->right=New;
}
else
insert(root->right,New);
}
else
{
if(root->left==NULL)
{
root->left=New;
}
else
insert(root->left,New);
}
}

3.7 BINARY TREE TRAVERSALS


When we wanted to display a binary tree, we need to follow some order in which all the nodes
of that binary tree must be displayed. In any binary tree displaying order of nodes depends on the
traversal method.
Displaying (or) visiting order of nodes in a binary tree is called as Binary Tree Traversal.
There are three types of binary tree traversals.
In - Order Traversal
Pre - Order Traversal
Post - Order Traversal
Consider the following binary tree...

1. In - Order Traversal (Left Child- Root - Right Child)


In In-Order traversal, the root node is visited between left child and right child.

UNIT 4 Page 11
16CS3202 - Data Structures II Yr CSE / III Sem

In this traversal, the left child node is visited first, then the root node is visited and later we go
for visiting right child node.
This in-order traversal is applicable for every root node of all subtrees in the tree. This is
performed recursively for all nodes in the tree.
That means here we have visited in the order of I - D - J - B - F - A - G - K - C - H using In-
Order Traversal.
In-Order Traversal for above example of binary tree is
I-D-J-B-F-A-G-K-C H

Algorithm
Until all nodes

voidinorder(node *temp)
{
if(temp!=NULL)
{
inorder(temp->left);
cout>>temp->data;
inorder(temp->right);
}
}

2. Pre - Order Traversal (Root - Left Child- Right Child)


In Pre-Order traversal, the root node is visited before left child and right child nodes. In this
traversal, the root node is visited first, then its left child and later its right child. This pre-order traversal
is applicable for every root node of all subtrees in the tree.
That means here we have visited in the order of A-B-D-I-J-F-C-G-K-H using Pre-Order
Traversal.
Pre-Order Traversal for above example binary tree is
A-B-D-I-J-F-C-G-K H

Algorithm

voidpreorder(node *temp)
{
if(temp!=NULL)

UNIT 4 Page 12

You might also like