Case 1
Case 1
UNIT 2 Page 14
16CS3202 - Data Structures II Yr CSE / III Sem
(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
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.
UNIT 2 Page 17
16CS3202 - Data Structures II Yr CSE / III Sem
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
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.
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.
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
Terminology
UNIT 4 Page 1
16CS3202 - Data Structures II Yr CSE / III Sem
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.
UNIT 4 Page 6
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 4 Page 7
16CS3202 - Data Structures II Yr CSE / III Sem
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.
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
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);
}
}
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);
}
}
Algorithm
voidpreorder(node *temp)
{
if(temp!=NULL)
UNIT 4 Page 12