ASSIGNMENT-4 LINEAR DATA STRUCTURE QUEUE
• Explain Queue fundamentals with Queue Insertion &
deletion algorithms
• Representation of Queue using Array and link list.
• Describe Circular Queue Insertion & Deletion
algorithm with an example.
• Differentiate: Simple Queue Vs Circular Queue.
• Explain DeQueue and its types in detail
• Explain Priority Queue. Explain the use of it in
computer field.
• Differentiate: Simple Queue Vs Priority Queue
• Write Advantage & disadvantage of circular queue
over simple queue.
• Queue application – SIMULATION
• What is the difference between LIFO and FIFO?
SUBMIT ON
13/09/2010
A nonlinear data structure is one in which its
element do not form a sequence
Many time we observe hierarchical relationship
between various data elements. This hierarchical
relationship between data elements can be
easily represented using tree.
A tree consists multiple node with each node
containing zero ,one or more pointers to other
nodes.A
B C
D E F G
A tree is a nonlinear data structure representing
more elements are called NODE.
Each node of a tree stores data value & has zero
or more pointers pointing to other nodes are
known as CHILD NODE.
The node at top of tree is known as root node of
the tree.
Node at lowest level are known as LEAF NODE.
A
B C
D E F G
ROOT NODE
INTERMEDIATE
NODE
LEAF NODE
The PARENT NODE is immediate predecessor of a node Each child node has one parent node.
Any node having child node as well as parent node is known as INTERNAL NODE.
DEGREE:-The maximum number of children that is possible for a node is known as degree of a node.
SIBBLING the node which have same parent are called sibbling.D,E,F ARE sibbling
LEVEL each node belongs to particular level number.
A
B C
D E F G
Level 0
Level 1
Level 2
DEPTH –it’s a highest level no of any leaf node.in
fig node D,E,F,G has level 2 so its depth is 2. The
depth of a node n is the length of the path from
the root to the node.
The height of a tree is the length of the path from
the root to the deepest node in the tree. A
(rooted) tree with only one node (the root) has a
height of zero
ANCESTOR & DESCENDANT :-A node N1 is said to
ancestor of node N2 if N1 is parent node of N2 Or
parent of parent node of N2
Node N2 is said to be descendant of Node N1.The
node N2 is said left descendant of N1 if it is
belongs to left subtree and N2 is said right
descendant of N1 if it is belongs to right subtree
BINARY TREE:-A binary tree consist of set of nodes
that is either empty or has following properties
1. One of node is designated as root node
2. The remaining nodes are partitioned into two
disjoint subset called left subtree & right subtree
respectively each of which is a binary tree.
3. Each node in binary tree can have atmost 2
children only
Left
subtree
Right
subtree
FULL BINARY TREE:-A binary tree is full binary
tree (sometimes proper/perfect binary tree ) if
and only if
1. each non leaf node has exactly two child node
2. all leaf node are at same level.
STRICTLY BINARY TREE:-A binary tree is
strictly binary tree if and only if
Each node has exactly two child nodes or no
nodes
complete binary tree
A binary tree is a complete binary tree if and only if
Level 0 t o h-1 represent a full binary tree of height
h-1
One or more nodes in level h-1 may have 0, or 1
child nodes
EXTENDED BINARY TREE(2-TREE)
A binary tree is said to be extended binary
tree if all of its nodes has either zero or two
degree .
The nodes having zero degree known as
external node represented as squares & nodes
with degree two known as internal node
represented as circles.
Properties of binary trees
The number of nodes n in a perfect binary tree can be found using this formula: n = 2h + 1 − 1where h is the height of the tree.
The number of nodes n in a complete binary tree is minimum: n = 2h and maximum: n = 2h + 1 − 1where h is the height of the tree.
The number of nodes n in a perfect binary tree can also be found using this formula: n = 2L − 1where L is the number of leaf nodes in the tree.
The number of leaf nodes n in a perfect binary tree can be found using this formula: n = 2h where h is the height of the tree.
The number of NULL links in a Complete Binary Tree of n-node is (n+1).
The number of leaf node in a Complete Binary Tree of n-node is UpperBound(n / 2).
ARRAY REPRESENTATION
Binary tree is represented sequentially in memory
by using single one dimensional array. All the
node of a tree are assigned a sequence number.
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.
LINKED REPRESENTATION
The linked representation of binary tree is
implemented using a linked list having INFO part
& two Pointer
Info part contains data values & two pointers
left & right are used to point to left & right
subtree of a node respectively
Traversal is the process of visiting every node
once
Preoder, Inorder, Postorder
Preorder Traversal:
1. Visit the root
2. Traverse left subtree
3. Traverse right subtree
Inorder Traversal:
1. Traverse left subtree
2. Visit the root
3. Traverse right subtree
Postorder Traversal:
1. Traverse left subtree
2. Traverse right subtree
3. Visit the root
CONVERSE PREORDER – if the word left and
right are interchanged in preceding definition
we obtain converse preorderA
F
GE
DB
C
A D G E F B C
Process root node
Right node
Left Node
PREORDER(T)-
Given a binary tree whose root node address is
given by a pointer T.
S & TOP denotes stack & top index
P denotes current node in tree.
1.[Intialize]
if T= NULL
then write (‘EMPTY TREE’)
return
Else TOP 0
call PUSH(S,TOP,T)
2.[Process each stacked branch address]
Repeat step -3 while TOP>0
3.[get stored address & branch left]
P POP(S,TOP)
Repeat while P != NULL
write(DATA(P))
if RPTR(P)!=NULL
then call PUSH(S,TOP,RPTR(P))
PLPTR(P)
4.[Finished]
return
RPREORDER(T)
1.[Process the root node]
If T!= NULL
Then write(DATA(T))
Else write(‘EMPTY TREE’)
return
2.[Process the left subtree]
If LPTR(T)!= NULL
Then call RPREORDER(LPTR(T))
3.[Process the right subtree]
If RPTR(T)!=NULL
then call RPREORDER(RPTR(T))
4.[finished]
return
INORDER
A
F
GE
DB
C
C B A E F G D A
CONVERSE INORDER
Right node
Process root node
Left Node
Converse inorder
G D F E A B C
RINORDER(T) Given a binary tree whose root
node address is given by a pointer T.
S & TOP denotes stack & top index
P denotes current node in tree.
1.[Check for empty tree]
If T = NULL
Then write(‘EMPTY TREE’)
return
2.[Process the left subtree]
If LPTR(T)!= NULL
Then call RINORDER(LPTR(T))
3.[Process the root node]
write (DATA (T))
POSTORDER
A
F
GE
DB
C
C B F E G D A
CONVERSE POSTORDER
Right node
Left Node
Process root node
Converse post order
G F E D C B A
POSTORDER(T)
1.[Intialize]
If T = NULL
Then write(‘EMPTY TREE’)
return
Else P T
TOP 0
2.[Traverse in post order]
Repeat thru step 5 while true
3.[Traverse left]
Repeat while P != NULL
call PUSH(S,TOP,P)
PLPTR(P)
4.[Process a node whose left & right subtree
have been traversed]
Repeat while S[TOP] <0
P POP(S,TOP)
Write(DATA(P))
If TOP = 0
then return
5.[Branch right & then mark node from
which we branched]
P RPTR (S[TOP])
S[TOP] - S[TOP]
RPOSTORDER(T)
1.[Check for empty tree]
If T = NULL
Then write(‘EMPTY TREE’)
return
2.[Process the left subtree]
If LPTR(T)!= NULL
Then call RPOSTORDER(LPTR(T))
3.[Process the right subtree]
If RPTR(T)!=NULL
then call RPOSTORDER(RPTR(T))
4.[Process the root node]
write (DATA (T))
5.[finished]
return
Formation of binary tree from its traversal
If preorder traversal is given then 1st node is
root node
If postorder is given then last node is root
INORDER:- D B H E A I F J C G
PREORDER : A B D E H C F I J G
A is root node
In inorder all nodes which are on leftside of A
belongs to left subtree & those which are on
right side of A belongs to right subtree.
Inorder: n1 n2 n3 n4 n5 n6 n7 n8 n9
Postorder: n1 n3 n5 n4 n2 n8 n7 n9 n6
n6
IN:- n1 n2 n3 n4 n5
Post:- n1 n3 n5 n4 n2
IN:- n7 n8 n9
Post:- n8 n7 n9
BINARY SEARCH TREE a binary search tree
(BST) has the following
properties:
The left subtree of a node
contains only nodes with
keys less than the node's
key.
The right subtree of a
node contains only nodes
with keys greater than the
node's key.
Both the left and right
subtrees must also be
binary search trees
CREATION OF BINARY SEARCH TREE
40,15,65,35,5,45,75,95,85,05,30
MARCH,MAY,NOV,AUG,APR,JAN,DEC,JUL,FEB,
JUN,OCT & SEP.
Searching BST If we are searching for 15, then node is
found
If we are searching for a key < 15, then we
should search in the left subtree.
If we are searching for a key > 15, then we
should search in the right subtree.
Example Insertion
Insert ( 20 )
5
10
30
2 25 45
20 >10, right
20 < 30, left
20 < 25, left
Insert 20 on left
20
10
30
Example Deletion (Leaf)
Three cases:
(1) the node is a leaf
– Delete it immediately
– Delete ( 25 )
5
10
30
2 25 45
25 > 10, right
25 < 30, left
25 = 25, delete
5
10
30
2 45
Example Deletion (Internal Node)
(2) the node has one childAdjust a pointer from the parent to bypass that node.
• Delete ( 5 )
5
10
30
2 25 45
2
10
30
2 25 45
delete(3) the node has 2 children
replace the key of that node with the minimum element at the right subtree
delete the minimum element
Example Deletion (Internal
Node)• Delete ( 10 )
5
10
30
2 25 45
5
25
30
2 25 45
5
25
30
2 45
Replacing 10
with smallest
value in right
subtree
Deleting leaf Resulting tree
THREADED BINARY TREE:-
In a binary tree more than 50% of link field are with
null values thereby wasting memory space.So to
avoid null values in the node we just set the threads
which links to its child or some other node.
The wasted NULL links in storage representationof
binary tree can be replaced by threads
There are 3 ways to thread a binary tree,these
correspond to inorder,preoreder and postorder.
Mostly we use inorder traversal.
Threaded Binary Trees (Continued)
o If a node has no left child, store a pointer to
the node’s inorder predecessor
o If a node has no right child, store a pointer to
the node’s inorder successor
o Here 1st and last node node do not have inorder
predecessor and successor node.To maintain
uniformity of setting of threads we are
maintaining a dummy code,HEADER NODE
HEAD
A
B D
C E G
F
RIGHT THREADED BINARY TREE:-
in this tree the right NULL pointer of each node
points to inorder successor.
Inorder Traversal
CBAEFDG
LEFT THREADED BINARY TREE:-in this tree the
left null pointer of each node points to its
inorder predecessor.
HEAD
A
B D
C E G
F
Inorder Traversal
CBAEFDG
FULL THREADED BINARY TREE:- In this tree
both right & left NULL pointer points to its
inorder successor & inorder predecessor
respectively
HEAD
A
B D
C E G
F
Inorder Traversal
CBAEFDG
• Storage representation of Threaded Binary tree.
• Here we need two extra variable to differentiate between
thread and links.
• Te variable lthread & rthread are used to indicate whether
left and right pointers are normal pointers or thread.
• Lthread(1) =left normal pointer
• Lthread(0) = left thread
• rthread(1) =right normal pointer
• rthread(0) = right thread
ADVANTAGE OF THREADED BINARY TREE:-
Traversal operation is faster
We can efficiently determine predecessor &
successor nodes starting from any node
Any node can be accessed from any other node.
Threads are usually upward whereas links are
downward. Thus in a threaded node one can
move either direction.
EXPRESSION TREE:-
It’s a binary tree which stores an arithmetic
expression.
The leaves of expression tree are operands & all
internal nodes are the operators.
(A + B * C) – ( ( D * E + F ) / G )
-
+ /
*A
CB
+ G
*
DE
F
C
B
A
*
B CA
Construction of an expression tree:-
Use postfix notation to construct expression tree.
(A + B * C) – ( ( D * E + F ) / G )
Postfix:- ABC * + DE * F + G / -
*
B C
A
+
(1)(2) (3)
Balancing Binary Search Trees
Strict balance
The tree must always be balanced perfectly
Pretty good balance
Only allow a little out of balance
Adjust on access
Self-adjusting
Many algorithms exist for keeping binary
search trees balanced
Adelson-Velskii and Landis (AVL) trees (height-
balanced trees)
Splay trees and other self-adjusting trees
B-trees and other multiway search trees
AVL - Good but not Perfect
BalanceAVL trees are height-balanced binary search trees
That satisfies following conditions
• For every node, heights of left and right subtree can differ by no more than 1
• The left & right subtree of a tree node are also AVL Tree
• Or A binary search tree is said to be AVL tree if all its
nodes have a balance factors 1,0,or -1.
Balance factor of a node
• height(left subtree) - height(right subtree)
Balancing factor is used to determine whether given BST is balanced or not
Different value of Balancing Factor(BF)
BF=0 if BST is perfectly balanced
BF=1 if height of left subtree is one greater than
height of right subtree.
BF=-1 if height of rigtht subtree is one greater
than height of left subtree.
BF<= -2 and BF>=2 that BST is said to be
unbalanced
In AVL tree the value of BF for a node can be -1,0
or 1.
66
40 80
30 50
+1
0 0
0 0
Balanced BST (AVL tree)
66
40 80
30 50
+2
+1 0
0 0
Un Balanced BST
30 30
00
INSERT A NODE INTO AVL TREE
Insertion of a node may or may not result in
unbalanced tree.
Certain situation where insertion may not result
in unbalanced tree are as follows
Node is inserted as child node of which having
one child node
Node is inserted in subtree of lesser height.
Node is inserted in tree having both subtree
having same height.
Insert and Rotation in AVL Trees Insert operation may cause balance factor to
become 2 or –2 for some node
So after the Insert, go back up to the root node
by node, updating heights
If a new balance factor is 2 or –2, adjust tree by
rotation around the node
Let the node that needs rebalancing be .
There are 4 cases:
Outside Cases (require single rotation) :
1. Insertion into left subtree of left child
2. Insertion into right subtree of right child
Inside Cases (require double rotation) :
3. Insertion into right subtree of left child .
4. Insertion into left subtree of right child.
Insertions in AVL Trees
j
k
X Y Z
“Right rotation” done!
(“Left rotation” is mirror
symmetric)
Outside Case Completed
AVL property has been restored!
h
h+1
h
LEFT ROTATION:-
Insertion into left subtree of left child
consider balanced tree shown in figure.
Insertion of node with value 95 result in unbalanced tree
95
66
4080
70 90
66
4080
70 90
-1
00
0 0
-2
0
0
0
-1
-1
RIGHT ROTATION:-
Insertion into left subtree of left child
Insertion of the node with value 25 results in
unbalanced tree.
66
4080
30 50
66
4080
30 50
25
00
0 0
+1
Case 3:-Unbalance occur due to insertion into right subtree of left child.This involves two rotations,Insert node with value 55.
66
4080
30 50
00
0 0
+1
66
4080
30 50
-10
0-1
+2
55
0
Case 4:-Unbalance occur due to insertion into left subtree of right child.This involves two rotations,Insert node with value 70.
66
4080
75 90
-1
00
0 0
66
4080
75 90
-2
0+1
+10
70
0
B-TREE:-B tree is a specialized multiway tree used to
store the records in a disk.
Here each node can store multiple values and can
point to multiple subtrees.A b-tree of order m
satisfies following conditions.
An m-way search tree all nodes are of degree <=m
Each node contains following attribute
All the elements stored in a node are in ascending
order
All the elements stored in left subtree of Pi are less
than of Pi and All the elements stored in right
subtree of Pi are greater than of Pi
P0 K1 P1 K2 P2 …. Kn PnKi = key values
Pi = pointers to subtree of T
20 40
10 15 25
30
35 45 50
A B tree of Order 3
B tree of order 3 means any node
can store atmost 2 elements
and point to atmost 3 subtree
Create btree of order 4 (any node can store
atmost 3 element and point to atmost 4 subtree)
66,90,40,75,30,35,80,70,20,50,45,55,110,100,
120
66
(1) (2)
(3)
(4)
(5)
(6)
TREEDELETE(HEAD,T):-
X is the node marked for deletion
PARENT is pointer which denotes address of
parent of node marked for deletion.
CUR:-denotes address of node to be deleted
1.[intialize]
If LPTR(HEAD )!= HEAD
Then CUR LPTR(HEAD)
PARENT HEAD
D ‘L’
Else write (‘NODE NOT FOUND’)
return
[search for the node marked for deletion]
FOUND false
Repeat while not FOUND and CUR != NULL
If DATA(CUR) = X
Then FOUND true
Else if X < DATA(CUR)
then (branch left)
PARENT CUR
CUR LPTR(CUR)
D ‘L’
else (branch right)
PARENT CUR
CUR RPTR(CUR)
D ‘R’
If FOUND = false
Then write(‘NODE NOT FOUND’)
return
3.[perform indicated deletion and restructure
the tree]
If LPTR(CUR) = NULL
Then (empty left sub tree)
Q RPTR(CUR)
Else if RPTR(CUR)=NULL
then (empty right subtree)
Q LPTR(CUR)
else (check right child for successor)
SUC RPTR(CUR)
if LPTR(SUC) = NULL
then LPTR(SUC)LPTR(CUR)
Q SUC