0% found this document useful (0 votes)
51 views138 pages

Unacademy - Programming & Data Structures

The document provides an introduction to the C programming language, highlighting its features, history, and basic concepts such as tokens, identifiers, constants, and variables. It explains the structure of C programs, including data types, storage classes, and operators. Additionally, it covers the syntax for defining constants and the rules for naming variables and constants.

Uploaded by

Akash Kumar
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)
51 views138 pages

Unacademy - Programming & Data Structures

The document provides an introduction to the C programming language, highlighting its features, history, and basic concepts such as tokens, identifiers, constants, and variables. It explains the structure of C programs, including data types, storage classes, and operators. Additionally, it covers the syntax for defining constants and the rules for naming variables and constants.

Uploaded by

Akash Kumar
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/ 138

1

C LANGUAGE

Chapter 1

Chapter 1
1) C-PROGRAMMING Token:
1.1) Basics of c-programming Smallest individual unit (Keywords, identifiers, constants, strings, special
symbols, operators).
Simple Portability Syntax based

Case sensitive
Powerful

Fast and Features of C Structure oriented


efficient
Modular Middlelevel
Use pointers
Compiler based Platform
dependent
Fig. 1.1 Features of C

Introduction:
y C is a programming language designed and written by Dennis Ritchie.
y It was designed and developed at AT & T Bell Laboratories, USA, in 1972.
y In the late 70’s, C began to replace more familiar languages such as
ALGOL, PL/I etc. It gradually gained popularity.
y Major portions of operating systems such as Windows, Linux are written Table 1.1 Keywords in C
in C mainly due to its speed of execution. Identifiers:
y Device drivers and embedded systems are majority written in C. � It is a name used to identify a variable, constant or a function.
y Like any other language, C-programming language is composed of y An identifier is a combination of letters, numbers and special symbol
alphabets, digits and special symbols which are used to form constants, (only _ underscore)
variables, keywords to write instructions further. These combinations of
instructions form a C-program. Example:
Var123
Valid
Alphabets Variable_10
Constants Identifers
Digits Program
Demo_1
Variables Instructions
Special
Keywords
Symbols
Invalid Var@123
idenfiers V$demo
Fig. 1.2
Hell#
C-character set:
y A character set denotes any alphabets, digits or special symbol used to Fig. 1.3
represent information. � C does not allow punctuation characters and special symbols such as
y The following table shows the C-character set: ?,-, @, #, $, % within identifiers.

Alphabets A – Z, a – z

Digits 0–9
C LANGUAGE

C LANGUAGE

Special symbol ~ ' ! @ # % ^ & * () _ - + = | \ { } [ ] : ; “ ' < > , . ? /

1 2
Chapter 1

Chapter 1
Whitespace: Semicolon:
y Whitespace in C programming language are blank lines, tabs or new y A semicolon “;” is a statement terminator in C.
lines, and blank spaces. y Every statement must be ended with a semicolon.
y These whitespace helps the compiler in understanding, and identifying y It indicates the end of a logical entry.
when one element in the statement ends, and next element starts.
Constants and variables:
Example: int age; y Alphabets, numbers and special symbols, when properly combined
A single whitespace or blankspace between int, and age enables the form constants & variables.
compiler to identify int datatypes, and age as the variable of int datatype. y Constant is an entity that does not change during the execution of the
whereas: int age = age1 + age2; program.
the space between age, = age1, + and age2 is not necessary, but for y Variable is an entity that may change during the execution of the program.
readability purposes, it is always preferred to add them.
Rules to name variables and constants:
Comments: 1) The variables or constant name is a combination of 1 to 31 characters,
y They are ignored by the compiler. i.e. length can be maximum 30.
y Comments are like helping texts in a C-program. 2) These characters can be alphabets, digits, underscores.
y Mostly added to increase the readability of a program. 3) The first character in the variable or constant name must be an alphabet
Single line comments: // or underscore.
Multi-line comments: /* _________ 4) No comma, whitespaces are allowed within the variable or constant
_________ name.
_________ */ 5) No special symbol other than underscore and no keyword can be used
Instruction: as constant or variable name.
y It is a combination of keyword, variable, and constants.
Example:
Example: int a = 10;

Program
y It is a collection of instructions.

Example:

header file
Table 1.2
#include <stdio.h>
function
void main() C-program structure:
{ int a = 1; int b = 2; variable y Preprocessor commands
int c = a + b; y Functions
operation
y Variables
printf("sum%d", c);
y Statements and expressions
} y Comments
print statement
C LANGUAGE

C LANGUAGE

3 4
Chapter 1

Chapter 1
Constants: Example:
y Value that cannot be changed during the execution of the program

Table 1.3

Note:

Fig. 1.4 Types of Constants


1) Numeric constant:
� numeric digits (may or may not have decimal point).
should have at least one digit, no commas or space, either positive
� 
or negative sign.
� by default sign is positive.

Table 1.4
3) String constants:
� It has zero, one or more than one character.
� enclosed within double quotes “”
� at the end of string \0 is automatically placed by compiler.
� \0 refers to as NULL string.

Example:
“Hello”
“8”
“593”
“”
“A”

Fig. 1.5 Types of Numeric Constants


4) Symbolic constants:
2) Character constants:
� using the keyword: define
� Single character enclosed within single quotes.
� when one constant is to be used several times.
C LANGUAGE

C LANGUAGE

� A character constant is a single alphabet, a single digit or a single


� A symbolic constant is a tag used to replace a number, e.g. pi can be
special symbol enclosed within single quotes.
used to replace 3.14.
Example: ‘a’, ‘B’, ‘8’, ‘=’

5 6
Chapter 1

Chapter 1
� These sequences of characters may be numeric constant, character 1) Primary datatypes:
constant or string constant.
� Generally defined at the beginning of the program with header files.
Integer Real Character
Syntax: #define name value

Example: Defining a constant pi whose value is 3.14159625


#define pi 3.14159625

Some more examples:


#define max 100
#define CH ‘a’
#define Name “Somesh”

Table 1.5

1.2 TYPES OF VARIABLES


Datatypes in C:
y An attribute given to every data used in the program is known as data
type for the data. A variable is declared with a data type so that it holds
a value of that type.
y The type of variable specifies how much space it requires in storage
and how it is stored in memory.

1)
2)
3)
4)
5)
E 6)
7)
8)
9)
10)
11)
Table 1.6
C LANGUAGE

C LANGUAGE

Fig. 1.6 Data-Types in C

7 8
Chapter 1

Chapter 1
Scope

Default value
Lifetime

Storage
Place of storage
Class

Fig.Fig.
: Features that storage
1.9 Features classClasses
of Storage decides
y The use of storage class about
makes a variable
the programs more efficient and swift.
y The syntax of declaring the storage class of a variable is:
Syntax: Storage_class_name datatype variable_name;
Example: Storage_class_name int a;
Fig. 1.7
y There are four types of storage classes, namely automatic, external,
The variable of integer data type in C Programming can be signed or unsigned
static and register.
number. Three different declarations of type integer are short, int, and long.
All of them in size, e.g. “short” has a lower range than “int” and “long” has Types of storage class
a larger range than “int”. Though the actual size in bytes depends on the
architecture on which program is being written.

Class: Automatic Static External Register


Keywords: (auto) (static) (extern) (register)

Fig. 1.10
A storage class decides about the following aspects of a variable:
2) Secondary datatype: 1) Lifetime: It is the time between creation & destruction of a variable.
   � These data types are divided or defined using primitive datatypes 2) Scope: The progress range/location where the variable is available for use.
3) Default Value: The default value which is taken by uninitialized variable.
Secondary Datatype 4) Place of storage: The place in memory which is allocated to the variable.

structure enumeration union

Fig. 1.8 Types of Secondary Datatypes

1.2.2 Scope and lifetime variables:


Storage classes:
y In addition to datatypes, each variable has one or more attribute known
as the storage class.
C LANGUAGE

C LANGUAGE

9 10
Chapter 1

Chapter 1
Types of storage class: Example 2:
Consider the given C-program statements:
func1()
{ extern int x;
x++;
printf(“func1:%d\n”, x);
}
int x = 189;
func2()
{ x++;
printf(“func2:%d\n”, x);
}
Table 1.7 All about Storage Classes main()
y To make global static variables, declare the static variable outside of { func1();
all functions. func2();
y Static variables can only be initialized by constants or constant }
expressions. Output: func1:190
y An static variable is initialized once, and it retains its value during the func2:191
recursive function calls.

Example 1:
Consider the given C-program statements: Previous Years’ Questions
int x = 12
static int y = 15;
func()
{ static int x;
x = x + 2;
printf(“inside func(): x = %d, y = %d\n”, x, y);
}
main()
{ int x = 13;
func();
func();
printf(“inside main ():x = %d, y = %d\n”, x, y);
}
(GATE-2000)
Output: Inside func():x = 2, y = 15
Inside func():x = 4, y = 15
Inside main():x = 13, y = 15
In func(), x is initialized to 0 and x = 0 is used, since it is a static variable,
C LANGUAGE

C LANGUAGE

hence its value is retained between function calls. Since y has been
initialized outside main and func(), it is accessible to both the functions.

11 12
Chapter 1

Chapter 1
..

a) b) c) d)
Sol: c) ( )

i)
ii)
iii)
iv)

a) b)
c) d)
Sol: d) ( )
a) b)
C LANGUAGE

C LANGUAGE

13 14
c) d)

Sol: d) ( )
a) b)

Chapter 1

Chapter 1
Operators types

Unary Binary

Operates Operates
on only one on two
c) d) operand operands

Fig. 1.12

Bitwise operator:
y Manipulation at bit level can be performed using bitwise operators.
Sol: d) ( )
y These operators perform operation on individual bits.

1.3 OPERATORS IN C
Operator Meaning
y An operator specifies an operation to be performed that yields a value.
y An operand is a data item on which an operator acts upon.

Other Size of
Bitwise
operators operator
operator
Comma
Arithmetic
operators
operator
Assignment Table 1.8 Bitwise Operators
Conditional Operators
operator
Operator

Logical Increment
Operator & Decrement
operator
Relational
Operator

Fig. 1.11 Types of Operators in C


y Operators can be of two types primarily:
1) unary operator 2) binary operators
C LANGUAGE

C LANGUAGE

15 16
Chapter 1

Chapter 1
Bitwise AND (&): Syntax: ~ operand.
y Binary operator represented as & Bitwise left shift (<<):
y Binary operator represented by <<.
Syntax:
Operand 1 Operand 2

Operand No. of bits


whose to be shifted
bits are
Table 1.9
to be
Syntax: operand 1 & operand 2
Bitwise inclusive OR (|):
shifted left
y Binary operator represented by |.
y Shifting bits results in equal no. of bits being vacated on the other side,
these vacated spaces are filled with 0 bits.

Example:
Let x = 0001 0011 0000 0100, then x<<4 means left shift x by 4 bits, then:

0001
 0011 0000 0100 0001

Lost bits Filled bits
Table 1.10
Syntax: operand 1 | operand 2 ∴ After left shifting: 0011 0000 0100 0000
Bitwise XOR (^): Bitwise right shift (>>):
y produces output 1 for only those input combinations that have odd y Binary operator represented by >>.
number of 1’s. y Similar to left shift except it shifts bit in the opposite direction as of
y Binary operator represented by ^. left shift i.e. shift bit to the right side.
y Similar to left shift, here the bits are shifted to right & bits are vacated
in the left and right shifting on unsigned quantity always fill with zero in
the vacated positions. In right shifting on signed quantity, then will fill
with sign bits (“arithmetic shift”) on the vacated positions.

Example:
Let unsigned int x= 0001 0011 0000 0100, then x>>4 means right shift x by
Table 1.11
4 bits, then:
Syntax: operand 1 ^ operand 2
Compliment (~): 0000 0001 0011 0000 0100
 
y One’s compliment operator represented by ~. Filled bits Lost bits
y unary operator
After right shifting: 0000 0001 0011 0000
C LANGUAGE

C LANGUAGE

Table 1.12

17 18
Chapter 1

Chapter 1
Arithmetic operator: Operator Meaning
y On the basis of no. of operands:

Arithmetic Operator

unary operator binary operator


+, –
g
+, –, *, /, %

Fig. 1.13
g
y On the basis of value of operands: Table 1.14

Fig. 1.14
Binary arithmetic operator:
Logical operators:
Operator Meaning y They are also known as Boolean operators.
y Used for combining expressions.
y They return 0 for false and 1 for true.

Operator Meaning

Table 1.13

Table 1.15

Relational operator:
y These operators are used to compare values of two expressions
C LANGUAGE

C LANGUAGE

depending on their relations. Non − zero value → True


y Relational expression is an expression with relational operators. Zero value → False

19 20
Chapter 1

Chapter 1
AND operator (&&): Compound assignment operator:
y Binary operator represented as &&. y Combination of arithmetic operator with assignment operator.

Compound assignment operator Alternate


x+=5 x=x+5
x–=5 x=x– 5

x/=5 x = x/5
Table 1.16 Truth table for AND x% = 5 x = x%5
Syntax: (condition 1) && (condition 2)
x*=5 x = x*5
OR operator (||):
y Binary operator represented as ||. Table 1.19
Increment and decrement operator:
y Unary operator
y Increment operator represented by ++.
y Decrement operator represented by ––.

Table 1.17 Truth table for OR


Syntax: (condition 1) || (condition 2)
NOT operator (!):
y unary operator represented by !. Table 1.20

y negates the value of the condition. y ++ increment by 1


y -- decrement by 1.
y The increment and decrement operator can be either: (1) prefix; (2)
postfix;

Types
Table 1.18 Truth table for NOT
Syntax: ! (Condition)
Assignment operator:
y used to assign values to variables, denoted by symbol ‘=’.
Syntax:
Operand 1 = Operand 2

Should be a Can be a variable, Fig. 1.15


variable expression or Prefix increment/decrement:
C LANGUAGE

C LANGUAGE

a constant y The value of the variable is incremented or decremented, then the value
is used in the operation.

21 22
Chapter 1

Chapter 1
Syntax: ++x or Operator variable; y printf statements can also be used.
––x
Example: a > b ? printf (“a is larger”): printf (“b is larger”);
Example:
Let x =5, then y=++x means increment the value of x by 1 and then use the
value to be assigned to y. ∴ y = 6
Rack Your Brain
Similarly, y=––x means decrement the value of y by 1 and then use the
value to be assigned to y. ∴ y = 1
Which of the following is not a unary operator?
Postfix increment/decrement: 1) ++ 2) – 3) sizeof 4) ?:
y First the value of variable is used, then it is incremented or decremented.
Syntax: x++ or Variable operator; Comma operator:
x–– y Represented by ‘,’
y It is used to allow different expressions to appear in places where exactly one expression
Example: would be used.
Let x =5, then y = x++ means assign y = 5, then increment the value of x by y Also commonly used as a separator.
1 i.e. after execution y = 5 & x = 6.
Similarly, y = x–– means assign y = 5, then decrement the value of x by 1 i.e. Example:
after execution y = 5 & x = 4.

Conditional operator:
y It is a ternary operator which requires three expressions as operands.
y Represented by ? and : space
y Syntax: Text expression ? expression 1: expression 2

Table 1.21
Example:
Sum = (a = 10, b = 7, c = 3, a + b + c);
Here sum would be a + b + c i.e. sum = 20.

Sizeof operator:
y unary operator
y returns the size of operand in bytes.
y The parameter passed can be a variable, constant or any datatype (int, float, character).
Syntax: sizeof(parameter);
Fig. 1.16
Example: Variable\ constant\ Datatype
max = a > b ? a: b
Example:
This translates to that if (a > b) True, then max = a
sizeof(int); would return the bytes occupied by integer datatype.
C LANGUAGE

C LANGUAGE

False, then max = b

23 24
Chapter 1

Chapter 1
Rack Your Brain

Consider the given C-program:


#include <stdio.h>
int main()
{printf(“%u%u%u%u”, sizeof(schar), sizeof(int), sizeof(float)
sizeof(double));
return 0;
}
1) 1 4 4 8 2) 1 2 4 8 3) 1 4 8 16 4) Machine dependent

Previous Years’ Question

a) b) c) d)

Sol: c)

a) b) c) d)

Sol: d)

Sol: 0) ( )
C LANGUAGE

C LANGUAGE

25 26
Chapter 1

Chapter 1
1.4 TYPE CONVERSION
y It includes converting data type of one operand into
data type of another operand to perform operations.

Type Conversion

Implicit Type Explicit type Rules for automatic binary type conversion:
Conversion conversion 1) With a binary operator, if both the operands have dissimilar data types
then the operand with lower rank gets converted into the data type of
higher rank operand. It is known as data type promotion.
by compiler user defined 2) In case of unsigned datatypes:
i) One operand is unsigned long int, others are converted to unsigned
long int as well as the result is stored as unsigned long int.
Automatic type Type conversions ii) One operand is unsigned int and other is long int, then:
Conversions in assignments Case I If long int can represent all values of unsigned int, then
unsigned is converted to long int and the result is also stored
in long int etc.
Case II Both are converted to unsigned long int along with the result.
Automatic Automatic iii) One operand is unsigned int, then others along with the result are
unary binary converted and stored as unsigned int.
conversion conversions Consequences of these promotions and demotions
1) Few higher order bits get dropped when higher order rank data type is
Fig. 1.17 converted to a lower order rank data type. E.g. when int is converted to
Implicit type conversion: short.
y Done by the C compiler based on some predefined C-language rules. 2) During the conversion of the float data type into int, the fractional part
gets removed.
Implicit type Conversion
3) Digits are rounded off while conversion of double type to float type.
4) The sign may be dropped in case of conversion of signed type to unsigned
types.
5) There is no increased accuracy or precision when an int is converted to
Automatic type Type conversion in
conversion
float or float to double.
assignment : If operands
are of different types,
then the RHS operands
are converted into the
Automatic unary type conversion: Automatic binary type of LHS operand.
All operands of type char & short are type conversion
converted to int before operation.
C LANGUAGE

C LANGUAGE

Fig. 1.18

27 28
Chapter 1

Chapter 1
Highest Rank Precedence and associativity of operators:
long double y For an expression having more than one operator, there exists certain
precedence and associativity rules for its evaluation.
double

float Example:
unsigned long int Given an expression: 2 + 3 * 5
long int It contain 2 operations: + and *, if + performed before *, then result would
be 25 and if * performed before +, then result would be 17.
unsigned int
∴ To remove this ambiguity of which operation to perform first the
int
C-languages specifies the order of precedence & associativity of operators.
char, short int y Operator precedence: Determines which operator is performed first in
Lowest Rank an expression.
y Operator associativity: It is used when two operators of same
Fig. 1.19 Rank of Datatypes in C precedence appear in an expression.
Explicit type conversion (Type casting): Associativity can be: Left to right
Some cases such as: or
Right to left
float z ; 

int
= x 20, =y 3;      …(i)
z = x / y; 

The value of z would be 6.0 and not 6.666666
y These cases require the implementation of explicit type conversion.
y This allows user to specify our own/user defined conversion.
y Also known as Type casting or Coercions.
y Type casting is implemented using the cast operator.
y The cast operator is a unary operator which converts an expression to a particular
datatype temporarily.
Syntax : (Datatype) expression;
  
Cast Operator

y Using the cast operator (float) in (i).


z = (float) x/y;
then the value of z would be 6.666667

Lower Higher
Datatype Datatype
C LANGUAGE

C LANGUAGE

Fig. 1.20

29 30
Chapter 1

Chapter 1
Role of parentheses:
y Parenthesis are used to change the order of precedence of any operation.
y All the operations enclosed in parenthesis are performed first.

Example:
I. Without parenthesis II. With parenthesis
36/6+3 36/(6+3)

(1) (1)
(2)
6 9

(2)
9               4

y Sometimes to increase the readability of the code and expression


parenthesis are used.
Example: x = a!=b & c * d >= m%n
Better: x = (a!= b) & ((c*d)>=(m%n))

Modify the order of


expression evaluation
Change Role of ( )
Order of Simplify complex
precedence expressions

Increase the
readability of
code

Fig. 1.21

1.5 FLOW CONTROL IN C


Table 1.22 Operator Precedence and Associativity Table y Control statements enable us to specify the order in which the various
instructions in the program are to be executed.
Example: 5 + 16 / 2 * 4 y It determines the flow of control in C.
y Since / and * have higher precedence than +
Control Statements
∴ they are evaluated first.
y / and * have same precedence, so which would be evaluated first amongst the two is
determined by the associativity rules. Since, /and * are left to right associative.
Selection Iteration Jump
∴ / is performed before *. Statements Statement Statement
The given expression can be considered as:
5 + (16/2) * 4
Solving: 5 + (8 * 4) if if else switch for while do while break continue goto return
⇒ 5 + 32
C LANGUAGE

C LANGUAGE

⇒ 37
Fig. 1.22 Types of Control Statements

31 32
Chapter 1

Chapter 1
y Compound statements or a block are a group of statements which are
enclosed within a pair of curly braces { }.
A compound statement is syntactically equivalent to a single statement.

If else statements:
y Bi-directional conditional control statement.
y The statement tests one or more conditions and executes the block
based on the outcome of the test.
y Any non-zero value is regarded as true, whereas 0 is regarded as false.

If...........else Fig. 1.24 Types of Loop Statements


While loop:
y First the condition is evaluated, if it is true, then the body of loop is
Single if Nested
statement
If.....else
if else
else if ladder executed.
Syntax : Syntax : Syntax : Syntax: While (condition) OR While (condition)
if (condition) if (condition) if (condition1) Statement; { Statement;
statement1; statement1; {if (condition2)
Statement;
or else statement1;

if (condition) statement2; else statement2;
{statement; or }

if (condition) else }
{statement; {if (condition 3)
} statement 3;
else
Note: While loops are used when the number of iterations are not known
} statement4; in advance.
True
Condition else }
False {statement;
Statement1
Statement1

}
False
Next
True False
While (Condition)
Statement Condition

Statement1 Statement2

True
Next Statement Body of loop
Next statement
Fig. 1.23 Types of If-else Statements out of the loop
Loops:
y Used when one wants to execute a part of a program or a block of Fig. 1.25 Flow Chart for While Loop
statements several times. y Each execution of loop body is called an iteration.
y With the help of loops, one can execute a part of program repeatedly Example: #include <stdio.h>
C LANGUAGE

C LANGUAGE

till some condition is true. main()

33 34
Chapter 1

Chapter 1
{ int i = 1;
while(i <=5)
{ printf(“%d\t”, i);
i++;
}
}
Output: 1 2 3 4 5
This C-program prints the numbers from 1 to 5. The variable i is initialized
to 1 and then the condition is checked whether (i < = 5) if true, then the
printf statement is executed. The value of i is incremented by 1 each time
the loop is executed.

Sol: ( )

Do-while loop:
y Similar to while loop just that in do while loop first the statements are
executed, then the condition is checked.
y This results in the execution of the statements at least once even if
the condition is false for the first iteration.
C LANGUAGE

C LANGUAGE

y The body of for loop can have single statement or a block of statements.

35 36
Chapter 1

Chapter 1
y In for loop:
Expression 1: Initialization expression, executed only once, and is generally an assignment
expression.
Expression 2: Test expression or condition, tested before each iteration, generally uses
relational or logical operators.
Expression 3: Update expression or updation, executed each time for body of loop is
executed.
y Firstly, the initialization expression is executed, and the loop variables are initialized,
then the condition is checked.
y If the condition expression is true, then the body of the loop is executed, and the
updation expression is executed.
y This continues till the condition expression is true, and once the condition expression
becomes false, the loop terminates and control is transferred to the statement following
Some loops that are valid:
the for loop.
y for(; n > 0; n/=10)//initialization of n should be mentioned before loop.
Initialization y for(i = 0, j = 10; i < =j; i++, j--)//multiple expression separated by comma
expression & semi-colon.
y for(; ; )//infinite loop.

False Example:
Condition

True

Body of loop

Update expression

Out of loop
Next Statements

Fig. 1.26 Flow Chart of “for” Loop


y For loops are used when the number of iterations are known in advance.
Example:
# include <stdio.h>
main()
{ int i;
   for(int i = 1; i < = 5; i++)
  printf(“%d\t”,i);
C LANGUAGE

C LANGUAGE

}
Output: 1 2 3 4 5

37 38
Chapter 1

Chapter 1
1.6 BREAK, CONTINUE AND GOTO STATEMENTS

a) b) c) d)
Sol: b)
Hint ( ) Fig. 1.27 Types of Control Statements
y These loop control statements enable the control of programs/loop to
be transferred.

Nesting of loops: Break statement:


y Loop within a loop y Used when it becomes necessary to come out of the loop even before
y Any type of loop can be nested inside any other type of loop. the loop condition becomes false.
y Break statement causes the immediate exit from the loop.
Infinite loops:
y Loops that go on executing infinitely. Syntax: break;
y The loops that do not terminate are called infinite loops. y Popularly used in switch statements.

Example: While loop:

while(1) for (; ;) do
{...................... {...................... {......................
..................... ..................... .....................
} } } while (1);

Infinite while loop Infinite for loop Infinite do while loop

y To come out of such loops break or goto statements are used.


C LANGUAGE

C LANGUAGE

39 40
Chapter 1

Chapter 1
Do-While Loop

Do-while Loop:

For Loop

For Loop

Continue statement:
y Used when one wants to skip some statements and execute the next
iteration.
Syntax: continue;
y Generally used with condition statements, when a condition statement
is encountered and is true, then the statements after the keyword
continue are skipped & the loops condition is checked for next iteration. Goto statement:
y Unconditional control statement
y Transfers the flow of control to another part of the program.
Syntax: goto label;
 Statement;
 Statement;
………………..
Example: ………………..
While Loop ………………..

label:
 Statement;
 Statement;
………………..
C LANGUAGE

C LANGUAGE

………………..

41 42
Chapter 1

Chapter 1
y The label can be placed anywhere:
Often leads to spaghetti code (Ambiguous & not understandable)
G
Complex Poor
control flow programming

Difficulty
in maintenance Why not goto Difficult
of code to debug

Ambiguity in Spaghetti
program control Code

Fig. 1.30
Although there might be situation such as complex nested loops or deeply
Fig. 1.28 Types of Goto Statements nested loops where goto or jump statements can improve the readability
of code.

Example:

Forward goto Backward goto

goto label; label:


Statement; Statement;
Statement; Statement;
Statement; Statement;
Skipped executed
__________ __________
__________ __________
__________ __________

label : goto label ;


Statement; Statement;
Statement; Statement;
__________ executed __________ Skipped
__________ __________
__________ __________

Fig. 1.29
C LANGUAGE

C LANGUAGE

43 44
Chapter 1

Chapter 1
Return statement: y Constants:
y ends the execution of function and returns the control of execution to the calling 1) Should be integer or character type.
function. 2) can be constants or constant expressions.
y does not mandatorily need any conditional statements.
y Depending on the type of function it may or may not return values.

Fig. 1.31 Types of Functions


Syntax: return expression;

Example:
Void Function Non-void Function
void func() return_datatype func()
{ { return value;
} }

1.7 SWITCH CASE


y Multi-directional conditional control statement.
y Popularly used for menu driven programs.
y Uses three keywords: switch, case, break, default.
Syntax: switch(expression)
{ case constant 1: Statement;
break; Fig. 1.32
case constant 2: statement;
break;
____________
____________
____________
default: Statements; -
}
y Expression: can be anything:
1) An expression yielding an integer value.
2) Value of any integer
C LANGUAGE

C LANGUAGE

3) Character variable
4) Function call returning a value.

45 46
Chapter 1

Chapter 1
Types of functions:

Previous Years’ Question

B
F
F
F

include

a)
b)
c)
Fig. 1.33 Types of Functions
d)

Sol: c) ( )
1) M

1.8 FUNCTIONS AND PROGRAM STRUCTURES


1.8.1 Basics of functions:
y Self-contained sub-program with a well-defined task.
y Although function calls are overhead but are still used. 2) C

Understandable
Modular & Compact P
Easy to
representation
modify and
(Divide and Conquer)
debug
Why functions?

Easily multiple 3) C
calling of code
Avoid rewriting
Code Reuse of
(Repetition) Code
(reusability)
C LANGUAGE

C LANGUAGE

Fig. 1.33
Table 1.22
47 48
Chapter 1

Chapter 1
Syntax: Types of functions
return_type function_name (arguments) (on basis of return type & Arguments)

{
Statement;
_________ With Without
_________ Arguments Arguments

_________
}
With Without With Without
return type return type return type return type

int func(int a) void func(int a) int func(void) void fun(void)


Function
Fig. 1.34 Types of Functions Basis of Return Type and Arguments
Function arguments:
y The calling function sends some values to the called function, these
values are known as arguments or parameters.

Example: Program to find sum of two numbers: A


#include <stdio.h>
int sum (int x, int y); //function declaration
main () Calling function ?
U
{ int a, b, s; → main()
printf(“enter the value of a & b:”); Called function? Fig. 1.35 Types of Function Arguments
scanf(“%d%d”, &a, &b); → sum()
s = sum(a, b); //function
printf(“sum=%d”,b);
}
int sum (int x, int y) //function definition
{ Example: Program showing formal and actual arguments:
int s; #include <stdio.h>
s = x + y; mul(int p, int q)//formal arguments
return s; { int prod;
} prod = p*q;
return prod;
C LANGUAGE

C LANGUAGE

}
sum (int p, int q)//formal arguments

49 50
3)
4)

Chapter 1

Chapter 1
{ int s;
:
s = p + q;
return s;
}
main() 5)
{int a = 2, b = 3;
printf(“%d\t”, mul(a, b));//actual arguments
printf(“%d\t”, mul(5, 4));//actual arguments
printf(“%d\t”, mul(a+b, b–a));//actual arguments
printf(“%d\t”, sum(a, b));
printf(“%d\t”, sum(mul(a, b), b));
}
Output: 6 20 5 5 9
6)

1)

2) 1)
2)
3)

3)
4)

M E

and
:
C LANGUAGE

C LANGUAGE

7)

5) 8)
51 52
M E

Chapter 1

Chapter 1
and
printf(“strings are same\n”);
7) else
printf(“strings are different\n”);
8) strcpy (S3, S1);
printf (“S3 string: %s\n”, S3);
strcpy (S1, S2);
String library functions: printf (“new string: %s\n”, S1);
� There are several library functions to manipulate strings, all present in }
the header file strings. Output:
length S1: 8
length S2: 8
Strings different
S3 string: Banglore
New string: Banglore, Manglore

Local, global and static variables:

Fig. 1.36 String Library Functions


Example: Consider the given C-program
#include < stdio.h>
#include < string.h>
main ()
{ char S1 = “Banglore”;
char str S3[10];
char S2 = “Manglore”;
int length1=strlen (S1);
int length2=strlen (S2);
C LANGUAGE

C LANGUAGE

printf (“length S1: %d\n”,length1);


Fig. 1.37 Types of Variables
printf(“length S2:%d\n”,length2);
if ((strcmp(S1, S2)) == 0)
53 54
Chapter 1

Chapter 1
Point to remember:
y One should be able to define the solution of the problem in terms of a
similar type of smaller problem.
y There should be a termination condition; otherwise, the recursive call
would never terminate.

Popular Problems

Factorial Power Fibonacci Tower of


Hanoi
Fig. 1.38

Advantages of Recursion

Code is compact Elegant and Divide and conquer Some inherent data
(modular) understandable approach simplifies structures are
Recursion: the concept recursive
y It is the process when a function calls itself.
y Powerful technique whose complicated algorithms are solved by the Fig. 1.39
divide and conquer approach.
y The sub-problems are defined in terms of the problem itself. Disadvantages of Recursion
A function should have the capability of calling itself.

The function that calls itself, again and again, is called a recursive function.
Less Greater time Memory
Example:
efficient requirement requirements

Fig. 1.40
Factorial:
Factorial of a positive integer n can be represented as a product of all
integers from 1 to n.
n! = 1 * 2 * 3 * …………….. *n
It can be written as:
n! = n * (n – 1)!
 1 ;n = 0
Here: n! = 
C LANGUAGE

C LANGUAGE

n * (n − 1) ! ;n > 0

55 56
Chapter 1

Chapter 1
Program to find the factorial:
#include <stdio.h>
long fact (int n); Rack Your Brain
main()
{ int num; 1) GCD of two numbers.
printf(“enter the number”); 2) Finding second maximum of three distinct numbers.
scanf(“%d” & num); Hint. 1) GCD of two numbers:
f(a, b)
printf(“factorial: %Id”, fact(num)); { if(a == 0)
}    return b;
long fact(int n) if(b = = 0)
{ if (n = = 0)    return a;
 return(1); if(a == b)
   return a;
else
else
  return (n*fact(n–1));    if(a > b)
}    return f(a – b, b);
else
   return f(a, b – a)
}
3) Second maximum of three distinct numbers:
int func(int a, int b, int c)
{ if((a >= b) && (c < b))
   return b;
else
if(a > = b)
  return func(a, c, b);
else
return func(b, a, c); }

Value passing in functions:

1)

2)

3)

4)

5)

6)

7)
C LANGUAGE

C LANGUAGE

8)

Table 1.23

57 58
Chapter 1

Chapter 1
!

Fig. 1.41 Types of Value Passing Techniques


C LANGUAGE

C LANGUAGE

59 60
Chapter 1

Chapter 1
during the
1) a) b) c) d)

Sol: d) ( )
:

2)

3)

4)

5)
C LANGUAGE

C LANGUAGE

6)

Table 1.24 Call by Value vs Call by Reference vs Call by Address

61 62
Chapter 1

Chapter 1
a)
b)
c)
d)

Sol: d) ( )
Sol:

1.9 POINTERS AND ARRAYS


1.9.1 Pointers and addresses:
y A pointer is a variable which stores the memory address of another
variable in the memory.
y Real power of C lies in pointers.
y The memory of the computer is made up of bytes arranged in a
sequential.
y Each byte has an index number called the address of that byte.
y If there are n bytes, the address would vary from 0 to (n – 1) bytes.

Example: 64 MB RAM
a) b) c) d) then, 64 MB
⇒ 64 × 220 bytes
Sol: b) ( ⇒ 67108864 bytes
Then: the address of these bytes would be: 0 to 67108863.
C LANGUAGE

C LANGUAGE

63 64
Chapter 1

Chapter 1
Implementing Pointer variables:
Returning more than
data structures one value from a function y Stores the memory address.
such as linked y Like all variables, it has a name, is declared and occupies space in
list, trees, graphs. memory.
Accessing dynamically
Use of allocated memory y Pointer: Because it points to a memory location.
Efficient pointers Syntax: data_type *pointer_name;
code int *ptr
y * (asterik) often read as “value at”.
Compact Easy to access
and array elements
Example: int *ptr, age = 50;
modular
int *sal, salary = 10000;
Fig. 1.42
ptr = &age;
Address operator: sal = &salary;
y denoted by ‘&’ (Ampersand) and read as ‘address of’. age salary
y returns the address of a variable when placed before it. ptr 50 sal 10000

y *ptr means value at variable ptr, since ptr stores the address of age.
Example: &a: address of a
∴ *ptr = * (&age) which is value at address of age.
y Popularly used in scanf() function.
Program to print address of variable. This returns the value of variable age = 50
#include <stdio.h> y Similarly: as sal = &salary
main () ∴ *sal= * (&salary), which means the value at the address of salary.
{ int var = 10; y One can access the variable ‘age’ by using (*ptr) since ptr is storing the
float demo = 11.1; location of age, hence (*ptr), can be used in place of the variable name.
printf(“value of var = %d, address of var = %u\n”, var, &var); This is called dereferencing pointer variables.
printf(“value of demo= %f, address of demo = %u\n”,
demo, & demo); Example: int a = 10;
} int b = 21;
Output: Value of var = 10, address of var = 65524 int *ptr1=&a;
Value of demo = 11.100000, address of demo = 65520 int *ptr2=&b;
Now:

Statement Equivalent to
;

V
Table 1.25
y Often referred to as indirection operation translating to “value at the
C LANGUAGE

C LANGUAGE

address.”

65 66
Chapter 1

Chapter 1
!

Example: #include <stdio.h>


main()
{ int a = 67;
float b = 45.6;
int*ptr1=&a;
float *ptr2 = &b;
printf(“Value of ptr1 = Address of a = %u\n”, ptr1);
printf(“Value of ptr2 = Address of b = %u\n”, ptr2);
printf(“Address of ptr1 = %u\n”, &ptr1);
printf(“Address of ptr2 = %u\n”, &ptr2);
printf(“Value of a = %d%d%d\n”, a, *ptr1, *(&a));
printf(“Value of b = %f%f%f\n”, b, *ptr2, *(&b));
}

Output:
Value of ptr1 = Address of a = 65524
Value of ptr2 = Address of b = 65520
-
Address of ptr1 = 65518
Address of ptr2 = 65516
Value of a = 67 67 67
Pointer arithmetic:
Value of b = 45.600000 45.600000 45.600000
y All types of operations are not possible with pointers.

Valid operations

Addition Subtraction of Increment subtraction of


of integer an integer from and same type
to a pointer a pointer decrement pointers.
C LANGUAGE

C LANGUAGE

Fig. 1.43 Types of Value Passing Techniques


y Pointer arithmetic is different since it is performed relative to the size
of the base type of pointer.

67 68
Chapter 1

Chapter 1
Example: int *pi; Pointer to pointer:
if: pi = 1000, let int size be 2 bytes, then (pi)++; is 1002 and not 1001. y When the address of a pointer variable is stored in another variable, it
This is because the data type int occupies 2 bytes considering 16-bit is called pointer to a pointer variable.
computer. y Similarly, one can have a pointer to pointer to a pointer variable, and
this can be extended to any limit.
Syntax: data_type **ptr;
1000 1001 1002 1003 1004 1005 1006 here: ptr is a pointer to pointer
5000 5001
pi 1000
Example: int a = 15;
p1
int *ptra = &a;
5000 5001 int **pptra = &ptra;
1) pi++; pi 1002
4000 3000 2000

3000 2000 15

pptra ptra a
5000 5001
2) pi+=2; pi 1004

pi = (pi + 2) = 1000 + 2*(size of int)


pi = (pi + 2) = 1000 + 2*2
pi = (pi + 3) = 1004

Table 1.27

S.No. Pointer Expression Meaning


1)

2)

3)

4)
C LANGUAGE

C LANGUAGE

Sol: (
Table 1.26

69 70
Chapter 1

Chapter 1
Pointers and arrays:
y Elements of an array are stored in contiguous memory location.
int a[5] = {10, 11, 12, 13, 14};
Let int occupy 2 bytes.
Then:

1000 1002 1004 1006 1008


Pointer to an array:
10 11 12 13 14
y Useful in multidimensional Arrays
a[0] a[1] a[2] a[3] a[4]
Syntax:
data_type pointer[array size];
base address (1000) int *ptr[10];//ptr is a pointer to an array of size 10.

y There is a close-knit relationship between a pointer and an array. Array


name is a pointer that only refers to the initial address of the array
known as the base address of the array.
y Compiler accesses the array elements by converting subscript notation
Example:
to pointer notation.
Program to differentiate between pointer to integer and pointer to an array
y Since the name of the array is a constant pointer that points to the
of integer.
first element of the array, therefore by pointer arithmetic when pointer
#include <stdio.h>
variable is incremented, it points to fix the location of its base type,
main() {
thereby making it possible to access all the elements of the array.
int *p;
Example: int(*ptr)[5];
int a[5] = {10, 11, 12, 13, 14}; int a[5] = {1, 2, 3, 4,};
p = a; // points to the first element of array a
Representation Equivalent to Value ptr = a; // points to the whole array a.
printf(“ p = %u , ptr = %u \n”, p, ptr);
p++; ptr++;
printf(“ p = %u, ptr = %u \n”, p, ptr);
}
Output:
//(let int occupy 2 byte)
p = 1000, ptr = 1000
p = 1002, ptr = 1010
C LANGUAGE

C LANGUAGE

Table 1.28
Fig. 1.44

71 72
Chapter 1

Chapter 1
Also, sizeof(p) = 2 sizeof(*p) = 2
sizeof(ptr) = 2 sizeof(*ptr) = 10

Pointers and 2D arrays:


y In 2-D arrays, the first subscript represents rows and the second
subscript represents column.

Example: int a[3][4] = {{10, 11, 12, 13}, {20, 21, 22, 23}, {30, 31, 32, 33}};

Table 1.29
Considering the above Example:

y 2D arrays are stored in Row major order, i.e. rows are placed next to
each other.

Fig. 1.45
a: points to 0th 1-D array →address(100)
(a+1): points to 1st 1-D array →address(108)
(a+2): points to 2nd 1-D array →address(106)
Fig. 1.46
Hence: (a+i) points to ith element of a or points to ith 1-D array of a Pointers and 3-D arrays:
y Elements are accessed using 3 sub-scripts
Now: *(a+i) gives the base address of ith 1-D array.
int a[2][3][2] = {{{1, 10}, {2, 11}, {3, 12}},
Both(a+i) and *(a+i) are pointers but their base types are different, here: {{4, 13}, {5, 14}, {6, 15}}};
(a+i) is an array of 4 integers y A 3-D array is considered to be an array of 2-D arrays, where each
*(a+i) is equivalent to a[i] which is integer value here. elements is a 2-D array.
C LANGUAGE

C LANGUAGE

73 74
Chapter 1

Chapter 1
Representation Meaning

Rack Your Brain

Let int a[3][2][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};


(Assume int of 2 bytes and array start from 100), then fill the table:

Representation Meaning

Table 1.30

Table 1.31

!
C LANGUAGE

C LANGUAGE

Sol: ( )

75 76
Chapter 1

Chapter 1
Character pointer and functions:
• Character pointers are used to initialize string constant.

Example: char *ptr = “Demo”;


Here ptr is a character pointer which points to the first character
of the string constant “Demo”, i.e, the base address of this string
constant.

Sol: ( )

Pointers and functions:


y Arguments to the functions can be passed in two ways:
Table 1.32 String as Arrays vs String as Pointers
1) Call by value.
2) Call by reference.
y function can return pointers too.
Syntax:
type *func(type1, type2 ….);
float *func(int, char); // function returns pointer to float.

Example:
int *fun()
{int x = 5;
int *p = &x;
return p;
}
main()
{int *ptr;
ptr = fun();
–––––
–––––
–––––
}
C LANGUAGE

C LANGUAGE

Let x be stored at 1000 address, then p = 1000.

77 78
79
C LANGUAGE Chapter 1

C LANGUAGE Chapter 1
c)
a)

Sol:
a)
:

80
b)
d)

(
)
Chapter 1

Chapter 1
1.10 Structures:
Basics of structures:
y A structure is a group of items in which each item is identified by its
own identifier, each of which is known as a member of the structure.
y Used to store related fields of different data types.
y Capable of storing heterogeneous data.

Student

Name Roll No. Mark


(String) (int) (float)

Fig. 1.47
Syntax: struct student{
char name[20];
Sol:
int roll no;
float marks;
};

Fig. 1.48
Both declaration creates three variable which enables access individual
structure members as:

stu1.name, stu1.rollno, stu1.marks;


Let char occupy 1 byte, int 2 bytes and float 4 bytes then the entire structure
student would reserve (1×20+2+4) = 26 bytes.

Sol: Structure initialization:


C LANGUAGE

C LANGUAGE

y The number, order, and type of values must be same as the structure
template/ definition.

81 82
Chapter 1

Chapter 1
y The initializing values can only be constant expressions. main()
struct student { {struct stud class[100], t;
char name[20]; int j, k, n; scanf (“Enter the number of students”, & n);
int roll no; for (k=0; k<n; k++)
float marks; scanf(\”Enter roll no., dept code and marks”
} stu1 = {“Ravi”, 13, 98}; %d %s %f”, &class[k].roll, &class[k].code, &class[k].marks);
struct student stu2 = {“Ravi”, 24, 86}; for(j=0; j<n-1; j++)
for(k=j+1; k<n; k++)
Size of structure {if(class[j].roll > class[k].roll}
{t=class[j];
class[j]=class[k];
Size of size of (stu1) class[k]=t;
(struct student) }
size of (stu2)

Fig. 1.49
}
1.10.2 Array of structures: for(k=0; k<n; k++){
y each element of an array is a structure type. printf(“roll no %d code %s marks%f\n”, class[k].roll, class[k].code,
class[k].marks);
Example: struct student stu[10]; }
Here stu is an array of 10 elements where each element is a structure Arrays within structure:
having three members name, roll no &marks. y C allows the use of arrays as structure members.

Example:
struct student {
char name[20];
int rollno;
int submarks[4];
};
The array submarks represent subject marks. Each student has Four
subject and each subject mark is denoted by submarks[0], submarks[1],
submarks[2], submarks[3].

Example: , .

Program to sort by roll no.(bubble sort)


#include <stdio.h>
struct stud
{int roll;
char code[25];
float marks;
C LANGUAGE

C LANGUAGE

};

83 84
Chapter 1

Chapter 1
Structures and functions:
y structures can be passed as arguments to functions. A function can
have a return value as a structure.

Pointers to structures: 1) 1)
y A pointer to a structure stores the start address of a structure variable.
2) 2)
Example:
struct student { 3) 3)
char name[20];
Table 1.33
int roll no;
int marks;
Example:
};
Program to add two complex numbers.
struct student stu, *ptr;
#include <stdio.h>
y ptr is a pointer that can point to the variable of type struct student.
struct complex {float re; //real part
ptr = &stu;
float in; //imaginary part
Accessing members through structure pointer
}
struct complex x, y;
struct complex add(x,y)
*
{struct complex t;
(*ptr).name; (*ptr).name;
t.re=x.re+y.re;
(*ptr).rollno; (*ptr).rollno;
(*ptr).marks: (*ptr).marks: t.im=x.im+y.im;
() parenthesis are necessary arrow operator formed by return(t);}
since precedence, is greater combination of hypen (-) main()
than *operator and greater than (>) symbol.
{struct complex a, b, c; // variables a, b, c
C LANGUAGE

C LANGUAGE

has the same precedence as printf(“enter two complex numbers”);


operator and associativity from
scanf(“%f %f\n”, &a.re, &a.im);
left to right.
      Fig. 1.50

85 86
Chapter 1

Chapter 1
scanf(“%f %f\n”, &b.re, &b.im); Here struct tag is a self-referential structure as it contains Two pointers
c=add(a,b); ptr1 and ptr2 of type struct tag.
printf(“The addition of real no. a and b are %f %f \n”, c.re, c.im); Linked lists are the most popular application of self-referential data
} structures.
A linked list has two parts, a data and a pointer, which together form a
Here the complex no. a has two parts real represented as a.re and imaginary node.
represented as a.im. Similarly, for complex no. b and the result of the
addition of complex no. c.
Pointer address of next node.
Example: Data
Program to add complex no. using pointers.
#include <stdio.h> Node
struct complex{
float re;
float im; Information
} struct complex *x, *y, *t; part
void add(x, y, t)
{ t → re = x → re + y → re;
Syntax: struct node {int info;
t → im = x → im + y → im;
struct node *link;
}
}
main()
Union:
{ struct complex a, b, c;
y It is also a collection of heterogeneous data types.
printf(“enter two complex no.”);
y The only difference between struct and union is the way memory is
scanf(“%f %f\n” &a.re, &a.im);
allocated to its members.
scanf(“%f %f”, &b.re, &b.im);
y In structure: each member has its own memory location
add(&a, &b, &c);
y In union: members share the same memory location.
printf(“the addition of a and b are %f %f”, c.re, c.im);
y When a or the union is declared, the compiler allocates sufficient
}
memory to hold the largest member in the union.
Self-referential structures:
Note: Union is used for saving memory
y Structure that contains pointers to structures of its own type
Syntax: union name
Syntax:
{
struct tag {
datatype member1;
datatype member1;
datatype member2;
datatype member2;
––––
–––––––––
––––
–––––––––
};
–––––––––
Similar to structures, unions can have union variables to access the
struct tag *ptr1;
members
struct tag *ptr2;
C LANGUAGE

C LANGUAGE

87 88
Chapter 1

Chapter 1
Arrays of union can be Function can take and
declared return union variables
Union can be
as argument
nested

declaration
Features of union

Pointer to union
possible
Union can be
self referential :

Fig. 1.51 a)
b)
c)
d)

Sol: a) ( )

Typedef:
y allows you to define a new name for an existing datatype.
Syntax: typedef datatype_name new_name;

Example: typedef int marks;


y now marks is a synonym for integer i.e marks sub1, sub2;

a) b) c) d)

Sol: a) ( )
C LANGUAGE

C LANGUAGE

89 90
Chapter 1

Chapter 1
Chapter Summary

y C-programming given by Dennis Ritchie in 1972.


y Some of its features are portability, syntax based, case-sensitive, middle level,
simple yet powerful, fast and efficient.
y The character set includes alphabets, digits and symbols. C has a total of 32
keywords.
y Components of basic C-program;

y C has integer, float, double and character as primary data types and structure,
union, and enumeration as secondary or user-defined data types.
int will normally be the natural size for a particular machine. short is often 16 bits
long, and int is of 16 or 32 bits. Each compiler allocates storage to each data type
based on the hardware, subject only to the the restriction that shorts and ints are
at least 16 bits, longs are at least 32 bits, and short is no longer than int which is
no longer than long.
y Datatype size is machine-dependent.
y C has four storage classes, namely automatic, static, external and register, that
C LANGUAGE

C LANGUAGE

specify the lifetime, scope, initial value & place of storage of a variable.

91 92
Chapter 1

Chapter 1
y C has Arithmetic, Assignment, increment decrement, relational, logical, conditional, y Recursion is a major application of function but is not preferred as a good
comma, sizeof, and bitwise operators to perform various operations. programming practice due to the memory and time requirement overhead.
y Operators are either unary(one operand) or binary(two operands) y Pass by value/call be value and pass by reference/ Call by reference are two major
y The conditional operator is a ternary operator(?:) value passing techniques in C.
y C supports both implicit and explicit type conversion. y In pass by value, a copy of actual arguments is stored in formal arguments. The
y Precedence and associativity play a vital role in expression evaluation. changes in formal argument not reflected back to the actual arguments.
y In pass by reference, the address of actual arguments is passed, and changes in
y C has three control statement categories namely: 1. selection statements: if, if- formal arguments are reflected in actual arguments.
else, switch, 2. iteration statements: for, while, do while, 3. jump statements: y A pointer is a variable which stores the memory address of another variable.
break, continue, goto, return. Pointers are what make C-programming language really powerful.
y C has two type of function: user-defined and library functions. y Pointers help in implementing various data structures such a linked lists, trees,
and graphs, helps in returning one or more values from functions, provide easy
y The library functions are in the header files; hence to use these functions in a access to array elements, and play major role in dynamic memory allocations. Also
program one, needs to include the header files. makes the code compact, modular and efficient.
y Functions that are defined by the programmer are called user-defined functions. y Some of the valid operations on pointer include the addition of integer to a pointer,
y Functions aim at making the code modular, compact, understandable, easy to subtraction of an integer from a pointer, increment and decrement of pointers,
modify, reusability and altogether avoid repetition of code. subtraction of same type pointers.
y Function can be called with some argument; these are actual and formal arguments/ y Pointers are used in function in the call by reference function calling.
parameters. y Structure and unions are user-defined structures which are heterogeneous. Both
y Actual arguments / parameters are the ones in the function calls. can store multiple data types.
y Formal arguments / parameters are the ones in the function definition. y The only difference is that structures reserve memory locations for each
datatype, whereas unions reserve memory location for only the largest number of
Return Function
type name
union(members share the memory location).
y Self-referential structures are used to implement data structures such as linked
lists.

Function call
C LANGUAGE

C LANGUAGE

93 94
2
Array

Chapter 2

Chapter 2
DATA STRUCTURES Linear data structure:
Way of organizing data in computer memory so that the memory can be A Linear relationship between the elements is represented by means of
efficiently used in terms of both time and space. contiguous memory locations. Another way of representing this linear
relationship is by using pointers or linked list.
E.g. Array, stack, queue, linked list

Non–linear data structures:


Non–linear data structures are not arranged sequentially in the memory.
The elements in non–linear data structures cannot be traversed in a single
run. Although they are more memory efficient than linear data structures.
E.g. graphs, trees.
Operations on data structures:
Abstract data type (ADT):
y It is a class or datatype whose objects are defined by a set of values
and a set of operations
y Combining data structure with its operation

1) Traversal: Visiting each element in the list


2) Search: Finding the location of an element with a given value of the
record with a given key.
3) Insertion: Adding a new element to the list.
4) Deletion: Removing an element from the list.
5) Sorting: Arranging the elements.
6) Merging: Combining two lists into one.

Types of data structures:

Previous Years’ Question\


Array

Array

95 96
Chapter 2

Chapter 2
2.1 BASICS OF ARRAY y One disadvantage of arrays is that they have fixed sizes. Once an array
Introduction: is
y An array is a collection of similar types of data items. declared, its size cannot be changed. This is due to static memory allocation.
y Each data item is called an element of an array
y Often referred to as homogeneous collection of data elements.
y The datatype of elements may be any valid data type like char, int or
float.
y The elements of the an array share same variable name, but each
variable has a different index number. This index number is called the
subscript.
Syntax: datatype array_name [Size];
E.g. int arr[10]; //represents an array of 10 elements each of integer datatype.
y The array index starts from 0 in C language; hence arr[0] represents the
first element of the array.

Rack Your Brain

Properties of array:
1) A one-dimensional array is a linear data structure which is used to store
similar types of elements.
2) The two basic operations that access an array are:
  i) Extraction operation: It is a function that accepts an array ‘a’ and an
index ‘i’ and returns an element of the array.
  ii) Storing operation: It accepts an array ‘a’, an index ‘i’ and an element
‘x’.
Array

Array

97 98
Chapter 2

Chapter 2
3) The smallest element of an array’s index is called the lower bound. In C
lower bound is always 0.
4) The highest element is called the upper bound and is usually one less
than the number of elements.
5) The number of elements in the array is called range.

E.g. An array ‘a’ whose lower bound is ‘0’ and the upper bound is 99, then
the range would be
⇒ 99–0+1
⇒ 100
6) Array elements are stored at subsequent memory locations Rack Your Brain
7) 2-D arrays are by default stored in row- major order.
8) In C Programming, the lowest index of a 1-D array is always zero, while
the upper index is the size of the array minus one.
9) Array name represents the base address of the array.
10) Only constants and literal values can be assigned as a value of a number
of elements.

E.g.

E.g. Program to print the average of 100 integers and how much each
deviates from the average.
#include <stdio.h>
#define Num 100
Different types of array: average( )
1–D arrays { int n[Num];
y One–dimensional array is used when it is necessary to keep a large int i ; int total = 0;
number of items in memory and reference all the items in a uniform float avg, diff;
manner. for (i = 0; i < Num ; i++)
Syntax: int b[100]; { scanf(“ %d”, & n[i]);
y This declaration reserves 100 successive memory locations, each total + = n[i];
containing a single integer value. }
y The address of the first location is called the base address and denoted avg = total/Num ;
by base(b). printf(“Number difference”);
y The size of each element be w then address of an element i in an array for (i = 0; i < Num ; i++)
b is given as: {
diff = n[i] – avg;
printf (“\n %d %f”, n[i], diff);
}
Array

Array

99 100
Chapter 2

Chapter 2
printf (“\n average is: %f”, avg)}; y The arrays are, by default stored in row- major order. i.e. the first row
main() of the array occupies the first set of memory locations, the second row
{ average(); occupies the next set so on and so forth.
}

2–D arrays
y A 2–dimensional array is a logical data structure helpful in solving
problems.
Syntax: int a[3][5];
here [3] is the number of rows, and [5] is the number of columns.
y This represents a new array containing 3 elements. Each of these
elements in itself is an array containing five integers.
y The 2–D array has 2–indices called row and column. Accessing element a[i][j] from a 2–D array a[m][n] then,
y The number of rows or columns is called the range of the dimension. (Here, starting index of array is 0)
Row–major order:
a[i][j] = base(a) + (i*n + j) *w
Column–major order:
a[i][j] = base(a) + (i + j * m) *w
Where:
m : no. of rows
n : no. of columns
w : element size
base (a): base address of array a[m][n]
Array

Array

101 102
Chapter 2

Chapter 2
Initialization of 2D arrays Multi–dimensional arrays
y One can initialize a 2–D array in the form of a matrix. y Arrays with more than two dimensions
E.g. int a[2][3] = { {0, 0, 0}, Syntax: int a[3][2][4] ;// 3–D array
{1, 1, 1} };

y When the array is completely initialized with all the values, then we do
not need to specify the size of the first dimension.
E.g. int a[ ][3] = { {0, 0, 3},
{1, 1, 1} };

y The first subscript specifies the plane number, the second specifies the
row number, and the third specifies the column number.
int a[p][m][n] ;
p : Plane number
m : row number
n : column number
E.g. a[3][2][4] ;
no. of plane = 3
no. of rows = 2
no. of columns = 4

Array

Array

103 104
Chapter 2

Chapter 2
2.2 ACCESS ELEMENT
1–D array:

Sol:
Given,
array : A[–7....7]
W = 4 bytes
b = 3000
no. of elements = U.B – L.B + 1
= 7 – (–7) + 1
y Let the base address of an array a be b and the lower bound be = 14 + 1
represented as L.B, and this size of each element be w. = 15
y Then the address of Kth element of an array a is given as: ∴ A[K] = b+[(K–L.B)] *w
A[0] = 3000 + [(0–(–7))] *4
= 3000 + 28
A[0] = 3028
y If array indexing starts from 0 then:
OR
Since: A[–7....7] = A[0.....14]
∴ A[0] = A[0–(–7)]
A[0] = A[7]
∴ A[7] = b + K*w
SOLVED EXAMPLES = 3000 + 7*4
A[7] = 3028

Previous Years’ Question (GATE 2005: 2-Mark)

Sol: Given,
base address (b) = 200
size of each element (w) = 4 bytes

since L.B not given, it is by default 0. Here the fifth element refers to the element
at array index 4
fifth element = a[4]
∴ K=4 Rack Your Brain
a[K] = b+ K *w
a[4] = 200 + 4 * 4
a[4] = 216
Array

Array

105 106
Chapter 2

Chapter 2
2–D array:
Two ways of array implementation

Row major order


Let an array A[m][n], then address of element A[i][j] in row major order is
i.e. A[8][5] = A[8–(5)] [5–(–8)]
given by:
≈ A[3][13]
A[i][j] = b + (i*n + j) *w
A[3][13] = 500 + (3 * 17 + 13) * 3
= 500 + 192
when: A[0.....m–1] [0........n–1]
A[3][13] = 692
or
A[8][5] = b + [(U2–L2 + 1) (i–L1) + (j–L2)] *w
= 500 + [(8–(–8)+1) (8–5) + (5–(–8))] *3
when: A[1.....m] [1......n]
= 500 + [17 × 3 + 13] *3
Similarly:
= 500 + 192
A[L1......U1] [L2.....U2]
A[8][5] = 692
then:
A[i][j] = b+[(U2–L2+1) (i–L1) + (j–L2)] *w
Where:
L1 : lower bound of rows
U1 : upper bound of rows
L2 : lower bound of columns
U2 : upper bound of columns Sol: Given,

SOLVED EXAMPLES

1st Approach:

Sol: given,
A[11][17] which is A[5.....15, –8.....8]
m = 11 //no. of rows A[i][j] = b + [(U2–L2+1) (i–L1) + (j–L2)] *w
n = 17 //no. of columns A[5][6] = 2000 + [(13–3+1) (5–1(–5)) + (6–3)] *4
b = 500, w = 3 bytes = 2000 + [(10×11)+3] *4
or = 2000 + 452
L1 = 5, L2 = –8 A[5][6] = 2452
U1 = 15, U2 = 8 2nd Approach:
Arr[0.....10, 0......10]
Array

Array

107 108
Chapter 2

Chapter 2
A[5][6] ≈ [5–1(–5)] [6–3]
≈ A[10] [3]
Previous Years’ Question (GATE 2015 Set-2 : 2-Marks)
A[10][3] = b + (i*n + j) *w
= 2000 + ((10×11)+3) *4
= 2000 + 452
A[10][3] = 2452

Previous Years’ Question (GATE 2000: 1-Mark)

Previous Years’ Question (GATE 2014 : 1-Mark)


Previous Years’ Question (GATE 1998 : 2-Marks)

Column–major order
Let any array A[m][n] then address of element A[i][j] in column major order
is given by:

when: A[0......m–1] [0......n–1]

when: A[1....m] [1......n]


Array

Array

109 110
Chapter 2

Chapter 2
Similarly: A[L1.....U1] [L2.......U2]
then:

where: Sol: Given,


L1 : Lower bound of rows A[–2....8] [–2.....5]
U1 : Upper bound of rows b = 5000
L2 : Lower bound of columns A[i][j] = b+[(i–L1) + (j–L2) (U1–L1+1)] *w
U2 : Upper bond of columns A[3][2] = 5000 + [(3–(–2)) + (2–(–2)) (8–(–2)+1)] *6
= 5000 + [5 + 44] *6
= 5000 + 294
Rack Your Brain A[3][2] = 5294
or
Convert A[–2....8] [–2.....5] to 0 indexing.
i.e. A[0....10] [0.....7] then A[3][2] also
maps to A[5][4] , m = 11, n = 8
\ A[5][4] = b+ (i + j *m) *w
= 5000 + (5 + 4×11) * 6
SOLVED EXAMPLES = 5000 + 294
A[5][4] = 5294

Multi–dimensional arrays:
y In multi–dimensional structure the terms row and columns cannot be used, since there
are more than 2 dimensions.
Sol: Given, \ Let a N–Dimensional array:
A[20][30] stored in column major order
w = 4 bytes A[L1.....U1][L2.....U2][L3.....U3][L4.....U4]........[LN.....UN]
b = 100 then location of A[i, j, k ......x] is given by:
A[5][15] = ? = b + {(i–L1) [(U2–L2+1) (U3–L3+1) (U4–L4+1)......(UN–LN+1)]
A[i][j] = b+ (i + j * m) *w + (j–L2) [(U3–L3+1) (U2–L4+1)....(UN–LN+1)]
A[5][15] = 100 + (5 + 15×20) *4
+ (k–L3) [(U4–L4+1) ..... (UN–LN+1)] ...... + (x–LN)} *w
A[5][15] = 1320
3–D Array
Let a 3–D array A[L1....U1][L2.....U2] [L3.....U3]
then location of A[i][j][k] is given by:
A[i][j][k] = b + {(i–L1) [(U2–L2+1) (U1–L1+1)]
+ (j–L2) [(U1–L1+1)]
+ (k–L3)} *w
Array

Array

111 112
Chapter 2

Chapter 2
SOLVED EXAMPLES  1 0 0 0  1 5 8 10  1 5 0 0
5 2 0 0  0 2 6 9  8 2 6 0 
     
6 7 3 0  0 0 3 7  0 9 3 7 
     
8 9 10 4  4×4 0 0 0 4  4×4 0 0 10 4  4×4
Lower triangular Upper triangular Tridiagonal
matrix    matrix    matrix
Sol: Given,

then:
A[i][j][k] = b+ {(i–L1) [(U2–L2+1) (U1–L1+1)]
+ (j–L2) [(U1–L1+1)]
Triangular matrices:
+ (k–L3)} *w
A[6][2][5] = 100 + {(6–(2)) [(1–(–4)+1) (8–2+1)]
+ (2–(–4)) [(8–2+1)]
+ (5–6)} *4 1) Lower Triangular Matrix , a[i][j]
= 100 + {(4*6*7) + (6*7) + (–1)} *4
= 100 + 836
A[6][2][5] = 936 Range n (n + 1 )
2
Sparse matrices:
� Matrices with relatively high proportions of entries with zero are called Row Major Order (RMO)  i (i − 1) 
sparse matrices. b + ( j – 1 ) + *w
 2 
y There are two general n–square sparse matrices.

1) Triangular matrix Column Major Order (CMO)   ( j − 1) ( j − 2)  


y For a square 2-D array, if all the elements beneath(above) principal b + (i − j) + ( j − 1) n −  * w
  2  
diagonal are zero, then the matrix is called as triangular matrix.
y If all elements beneath the principal diagonal are zero, then the
matrix is upper triangular.
2) Upper Triangular Matrix , a[i][j]
y While for a square matrix, if all the elements above the main diagonal
are zero then matrix is upper triangular.
Range n (n + 1 )
2) Tridiagonal Matrix
y For a square matrix, if all the elements are zero except the elements 2
on the principal diagonal and all the elements immediately above
and immediately below the principal diagonal, then the matrix is Row Major Order (RMO)   (i − 1) (i − 2)  
b + ( j − i) + (i − 1) n −  * w
tridiagonal.   2  
Array

Array

113 114
Chapter 2

Chapter 2
Column Major Order (CMO)  j ( j − 1) 
b + ( i − 1 ) + *w
 2 

3) Strictly Lower Triangular Matrix [ ]nxn, a[i][j]


Range n (n − 1 )
2 �

Row Major Order (RMO)  (i − 1) (i − 2 ) 
b + ( j − 1 ) + *w
 2 

Column Major Order (CMO)  ( j − 1) ( j − 2 )  �


b + ( i − 1 ) + *w
 2 

Tridiagonal matrix [ ]nxn, a[i][j]

Range 3n–2

Row Major Order (RMO) b+ (2i+j–3) *w

Column Major Order (CMO) b+ (i+2j–3) *w



Rack Your Brain �


Array

Array

115 116
3 Stack & Queues

Chapter 3

Chapter 3
3.1 BASICS OF STACK
y A stack is a linear data structure in which
the items can be added or removed only
at one end is called top of stack.
y Stacks are also called last in first out
(LIFO).
y This means that elements are removed
from the stack in reverse order of the
Grey Matter Alert!
way they were inserted.
y Two basic operations associated with
stacks: y LIFO (Last in first out) is also
a) PUSH or Push: Insert an element referred to as FILO (First In last out)
y Some other names for stacks are Fig. 3.2
into the stack.
b) POP or Pop: Delete an element from “Piles” or “Push-down lists” In the above figure, F is the top of the stack. Any elements inserted after F are
the stack. placed on top of F. The arrow with the word ‘top’ represents the top of stack.
Some of the stack representations can be a) represent the stack with F as the top.
given as: b) Push (G), G is pushed on top of F.
c) Push (H), H is pushed on top of G.
d) and (e) similarly, I and J are pushed onto the stack, respectively.
f) POP (J), the element J is deleted from top of the stack.
g) and (h), POP (I), and POP (H) from the stack, respectively.
3.2 OPERATIONS ON STACK
The basic operations used for manipulation of stack data structure is:
a) PUSH: To insert an item into a stack
b) POP: To delete an item from a stack
Fig. 3.1
Consider the above shown representation Another operation used to know the top element of the stack is PEEK().
that 5 elements (A, B, C, D, E) are pushed Therefore when an item is added to a stack, it is pushed onto the stack, and
onto an empty stack. The only implication when an item is removed, it is popped out from the stack.
is that rightmost element, i.e. E is the top Consider a stack S and an item a then:
element. This is emphasised regardless of
the way the stack is described since it is the Operation Meaning
property of stack that insertion and deletion
can only occur at the top of stack. This PUSH (s, a) Push the item a onto the top of the stack s
means that D cannot be deleted before E.
POP(s) Removes the item from the top of the stack.
Note: Assigns the value of the top of the stack to x and
x=POP(s) removes it from the stack s.
Stacks can either be used to postpone certain decisions or the order
Stack & Queues

Stack & Queues

of the processing of data can be postponed until certain conditions PEEK(s) Return the top of the stack.
get true.
Table 3.1 Stack Operations

117 118
Chapter 3

Chapter 3
3.3 STACK CONDITIONS Push operation on stack:
Note:
y If a stack has no item, this stack is Let, n be the maximum size of stack and p be the element to be inserted
called an empty stack. onto the stack s.
Due to the push operation, the stack is
y However, PUSH operation can be
sometimes referred to as push down Push(s, Top, n, p)
performed on an empty stack but a POP
lists.
operation cannot be applied. { if(Top = = n – 1)
y Hence before applying a POP operation, { printf(“stack overflow”);
one needs to make sure that the stack exit(1);
is not empty.
}
y The two stack conditions to be checked before performing an operation
Top ++;
on stack are:
a) Underflow condition: S[Top] = p; //element p inserted on top of the stack
When a POP operation is tried on an empty stack, it is called underflow }
condition. This condition should be false for successful POP operation
and PEEK operation. Pop operation of stack:
Let n be the maximum size of stack and p be the variable which will contain
the value of top of the stack.
Note: Pop(s, Top, n)
Empty determines whether stack s is empty or not. If the value { int p;
returned by the operation is true then the stack is empty. If(Top = = –1)
{ printf(“under flow condition”);
Operation Return value Meaning exit(1);
}
empty(s) TRUE Stack s is empty p = s[Top];
Top - - ;
empty(s) FALSE Stack s is not return(p);
empty }

Application of stack:
b) Overflow condition:
Some of the most popular applications of stacks are:
An overflow condition checks whether the stack is full or not, i.e.
a) Expressions evaluation
whether memory space is available to push new elements onto the
e) Fibonacci series
stack. It is an undesirable condition where the program requests more
b) Balanced parenthesis check
memory space than what is allocated to a particular stack.
f) Permutation
Underflow condition: (empty (s) == TRUE) c) Recursion
Rack Your Brain
                Or g) Subroutines
            (TOP == -1) d) Tower of hanoi
Which data structure does the UNDO
Overflow condition: TOP == sizeof(s)
Balanced parentheses: function of the test editors use?
Stack & Queues

Stack & Queues

Where s is the stack on which PUSH and POP operations are to be performed. y For checking balanced parentheses for How does the compiler work for
Here TOP is a special pointer which always points to the top of the stack and plays a major every open brace, there should be a recursions?
role in checking the overflow and underflow conditions of the stack. closing brace.

119 120
Chapter 3

Chapter 3
y The types of parenthesis used are: The state of stack at various stages of processing the expression for
a) Simple or common bracket ( ) balanced parenthesis
b) Square bracket [ ]
c) Curly bracket { } {a + (b − [c + d]) * e − [(f + g)]}

Example: {{ } [ ] ( )} Balanced parenthesis

( ( ( (( ) ( )) ) ) ( ))
{[ ] (}{( ) ) Imbalanced parenthesis

[[ ][ ](( )
1) Push the open parenthesis onto the stack
2) Whenever a close parenthesis is encountered then pop the top of the
stack which should be the matching parenthesis. Previous Year’s Question

Note: The best data structure to check whether


The two conditions that must hold if the parenthesis in an expression on arithmetic expression has balanced
forms an admissible pattern: parentheses is a,
1) The parenthesis count at the end of the expression should be 1) Queue 2) Tree
0. This implies that there are as many left parentheses as right 3) Stack 4) List
parentheses. Sol: Option 3) (GATE- 2004)
2) The parenthesis count should be non-negative at each point in
the expression. Fig. 3.4

Also, nesting depth at a particular point in an expression is the number off


3.4 EXPRESSION EVALUATION
scopes that have been
There are three notations for an expression representation:
Considered, ( ( ( ) ( ) ) ( ) )
a) Infix notation: In this type of notation the operator lies between the
operands. Example: sum of A and B can be given as A+B
b) Prefix notation: In this type of Notation the operators come before the
operands.
Example: Sum of A and B can be given as +AB
c) Postfix notation: In this type of notation the operator comes after the
operands.
Example: Sum of A and B can be given as AB+

There are five binary operations:


Stack & Queues

Stack & Queues

a) Addition (+) b) Subtraction (-)


c) Multiplication (*) d) Division (/)
Fig. 3.3 e) Exponentiation ($)

121 122
Chapter 3

Chapter 3
y But in the case of exponentiation, the default order of evaluation is
considered from right to left.
Example:
A + B + C A$B$C
(Left to Right) (Right to Left)
(A + B) + C A $ (B $ C)

Fig. 3.5 Note:


Example: Default precedence can be overridden by using parenthesis.

Infix Prefix Postfix Expression conversion:


y The prefixes ‘pre-’, “post-”, and “in-” refer to the relative position of the
A+B*C +A*BC ABC*+ operator w.r.t. the operand.
� The only rules to always remember while expression conversion to
A+B +AB AB+
any form is that operations with higher precedence are converted first
A-B/(C*D$E) -A/B*C$DE ABCDE$*/– and after a portion of the expression is converted to a form, then it is
considered as a single operand.
(A+B) * (C+D) *+AB + CD AB + CD +* Example: A + B * C (Infix)
Table 3.2 Infix vs Prefix vs Postfix
 (Postfix)         A + (*BC)
A + (BC*)  (Prefix)
↓ ↓
Note: Treat as
single
Treat as
single
operand operand

Prefix notation is also known as polish notation postfix notation is


also known as reverse polish notation. Note:

The expression sum of two variables A and B can be given as A + B where Rules of expression conversion:
A and B are the operands, and ‘+’ is the operator. 1) The operations of higher precedence are converted first.
y While expression evaluation, the precedence of operators plays a vital role. 2) Once an operation in converted to postfix or prefix form it is considered
y Parenthesis are of great help in getting the order of evaluation. as a single operand.

Example:

A + (B * C) (A + B) * C

y Here, parenthesis emphasises y Here, parenthesis emphasises


that multiplication is converted that addition is converted
first and then addition. first and then multiplication.
Table 3.3
Stack & Queues

Stack & Queues

When unparenthesized operators having the same precedence are scanned


then the order by default is considered from left to right.
Fig. 3.6

123 124
Chapter 3

Chapter 3
(operator_stack), symbol))
Note: Grey Matter Alert! { temp = pop (operator_stack);
The order of the operators in the postfix add temp to the postfix string;
expression determines the actual order of The prefix form of an expression is not } //end of while
operations in evaluating the expression, the mirror image of the postfix form. Push (operator_stack, symbol);
hence making the use of parenthesis in Example:   } //end of else
postfix notation is unnecessary. } //end of while
while(!empty(operator stack))
Infix Postfix
{ temp = pop (operator_stack);
Add temp to the postfix string;
A + (B * C) ABC * +
} //output all remaining operators
(A + B) * C AB + C * The algorithm given can be understood in step as:
1) The operator-stack is the stack which has been initialized, to push
Infix to postfix conversion: operators from the input string.
1) Check for parenthesis;, the operations enclosed within parenthesis are 2) Symbol is the variable which reads the next input symbol of the infix
to be converted first. expression (input string).
2) The other operations in the expression are converted according to the 3) The if condition checks whether the input symbol is an operand or
operator precedence. operator.

Example: A + (B * C)
1) Parentheses (B * C) are converted first.
A + (BC *)
4) If the precedence of the operator on top of the stack is greater, then
2) Now (BC *) is treated as a single operand, and then the entire expression
the operator in the input string (symbol) then pops the top of the stack
is converted.
and add to postfix expression, then PUSH the symbol onto the stack.
Otherwise, simply push the symbol onto the stack.
5) If the input string is completely scanned, then pop all operators from
3) The postfix expression obtained: ABC * + the operator-stack and add to the postfix string.
Example: A + B * C
Infix to postfix conversion using stack:
An algorithm than can be designed to convert an infix expression to postfix S.no Symbol Postfix string Operator-stack
express is given as:
Operator_stack = empty stack; 1) A A +
while(!End of input)
2) + A +
{ symbol = next input character;
if (symbol == operand) 3) B AB +
Stack & Queues

Stack & Queues

add symbol to the postfix string;


4) * AB +*
else
{ while (!empty (operator_stack) & precedence (Top 5) C ABC +*

125 126
Chapter 3

Chapter 3
S.no Symbol Postfix string Operator-stack S. no Symbol Postfix string Operator-stack

6) ABC* + 12) D ABC + – D (/

7) ABC*+ 13) ) ABC + – D/


Table 3.5
14) $ ABC + – D/ $
Postfix string: ABC * +
15) ( ABC + – D/ $(
Note:
16) E ABC + – D/E $(
The operator-stack has operators that work according to right associativity
of operators: 17) + ABC + – D/E $( +

18) F ABC + – D/EF $( +

Example: ((A – (B + C))/D) $ (E + F) 19) ) ABC + – D/EF + $

20) ABC + – D/EF + $


S. no Symbol Postfix string Operator-stack Table 3.5
Postfix string: ABC + – D/EF + $
1) ( (
Evaluating postfix expression using stack: Rack Your Brain
2) ( (( The following algorithm evaluates an
expression in postfix notation: Convert these postfix expressions to
3) A A (( infix.
operand_stack = empty stack; XY – P + QRS – + $
4) – A ((– while (! end of input)
{symbol = next input character; PQRST – + $ * AB * –
5) ( A ((–(
if (symbol == operand)
6) B AB ((–( PUSH (operand_stack, symbol);
else {
7) + AB ((–(+
operand 2 = pop (operand_stack);
8) C ABC ((–(+ operand 1 = pop (operand_stack);
value = result of apply symbol on operand 1 and operand
9) ) ABC + ((– 2.
/* value = operand 1 symbol operand 2 */
10) ) ABC + – ( PUSH (operand_stack, value);
Stack & Queues

Stack & Queues

11) / ABC + – (/ }
} return (pop(operand_stack)));

127 128
Chapter 3

Chapter 3
The algorithm given for postfix expression evaluation can be understood
as: S. no Symbol Operand 1 Operand 2 Value Operand_stack
1) The operand_stack is an empty stack where the operands are pushed
into. 5) – 26 25 1 1
2) Symbol is the variable which reads the next input symbol of the postfix
6) 3 26 25 1 13
expression.
3) The if condition checks whether the input symbol is an operand or an 7) 18 26 25 1 1 3 18
operator.
8) 2 26 25 1 1 3 18 2

9) / 18 2 9 139

10) + 3 9 12 1 12
Fig. 3.8
11) * 1 12 12 12
�K
 eep the value of the top of stack in operand 2 and the next value in
the operand 1 then apply operator: 12) 2 1 12 12 12 2
  Value = operand 1 operator operand 2
13) $ 12 2 144 144
Example:
14) 3 12 2 144 144 3

15) + 144 3 147 147

Table 3.6
Fig. 3.9
The value of the expression (26 12 13 + – 3 18 2 / + * 2 $ 3 +) is 147.
Value = 6 + 8
Infix to prefix conversion:
Value = 14
Some of the steps to follow to convert a
4) Then, push the value onto the operand stack. complex infix expression into prefix form: Rack Your Brain
5) Once the entire string is traversed or read, then pop the top of the stack 1) Read the expression in reverse from
which returns the value of the expression. right to left. Convex the open braces as Why infix to postfix conversion is
closed and vice versa. required?
Example: 26 12 13 + – 3 18 2 / + * 2 $ 3 +
2) Now, the reversed expression obtained
is converted into postfix notation using stack.
S. no Symbol Operand 1 Operand 2 Value Operand_stack
3) Now, for the final step, reverse the postfix notation i.e. the postfix
1) 26 26 expression read from right to left results in a prefix notation expression.
Example: (4 + (6 * 2) * 8)
2) 12 26 12 Step I: Reverse the infix expression
(4 + (6 * 2) * 8) ← Reading from right to left
Stack & Queues

Stack & Queues

3) 13 26 12 13
(8 * (2 * 6) + 4)
4) + 12 13 25 26 25 Step II: Convert (8 * (2 * 6) + 4) into postfix.
Postfix: 826 ** 4 +

129 130
Chapter 3

Chapter 3
Step III: Reverse the postfix notation.
826 ** 4 + ← Read from right to left. Previous Years’ Question
Prefix: +4 ** 628

Evaluation of prefix expression: Assume that the operators +, –, ×, are left associative, and ^ is right
y The prefix expression evaluation uses Rack Your Brain associative. The order of precedence (from highest to lowest) is ^, ×,
the same steps as the postfix expression +, –. The postfix expression corresponding to the infix expression a +
evaluation just that the input expression Convert the following prefix expression b×c–d^e^f is
is read from right to left. to infix + – $ ABC * D **EFG 1) abc×+def^^– 2) abc×+de^f^–
3) ab+c×d–e^f^ 4) – + a×bc^^def
Example: +4 * * 628 ← Read Sol: 1) (GATE CSE- 2004)

S. no Symbol Operand 1 Operand 2 Value Operand stack 3.5 IMPLEMENTATION OF STACK

1) 8 8

2) 2 82

3) 6 826

4) * 2 6 12 8 12

5) * 8 12 96 96 PUSH (S, Top, x) PUSH (Top, x)


1) Top = Top + 1; 1) Temp = x;
6) 4 8 12 96 96 4 2) S[Top] = x; 2) Temp → next = Top;
3) Top = temp;
7) + 96 4 100 100 POP(S) POP (Top)
1) Temp = S[Top]; 1) Temp = Top;
Table 3.7
2) Top = Top – 1; 2) Top = Top → Next;
The value of prefix expression (+4 * * 628) is 100.
3) Return temp; 3) a = temp → info;
4) Free (temp);
Previous Years’ Question 5) Return (a);
� Push & pop both take 0(1) time. � Push & pop both take 0(1) time.
The following postfix expression with single digit operands are Overflow condition Overflow condition
evaluated using a stack: 8 2 3 ^ / 2 3 * + 5 1 * - Note that ^ is the ⇒ (Top == size of array) ⇒ (AVAIL == NULL) linked list have dynamic
exponentiation operator. The top two elements of the stack after the memory allocation
first * is evaluated are:
Stack & Queues

Stack & Queues

Underflow condition ∴ No limit till memory is available.


1) 6, 1 2) 5, 7 3) 3, 2 4) 1, 5 ⇒ (Top == –1) Underflow condition
Sol: 1) (GATE CSE- 2007)
⇒ (Top == NULL)

131 132
Chapter 3

Chapter 3
3.6 APPLICATION OF STACK: TOWER OF HANOI Generalized Solution for n
The tower of Hanoi problem is described as follows: 1) Move the top (n – 1) disks from peg A to peg B
Suppose there are three pegs labeled A, B and C. On peg A a finite number 2) Move the top disk from peg A to peg C
of n disks are placed in order of decreasing diameter. The objective of 3) Move the top (n – 1) disks from peg B to peg C
the problem is to move the disks from peg A to peg C using peg B as an
auxiliary. The rules are as follows: Let Peg A referred to as BEG, Peg C as END and Peg B as AUX then:
1) Only one disk can be moved at a time. 1) TOH (N – 1, BEG, END, AUX) 2) TOH (1, BEG, AUX, END)
2) Only the top disk on any peg can be moved to any other peg. 3) TOH (N – 1, AUX, BEG, END)
3) At no time can a larger disk be placed on a smaller disk.
The tower of Hanoi (TOH) can be divided into three sub problems and can
The solution to a Tower of Hanoi problem for n = 3. be solved recursively as:
n = 3: Move top disk from peg A to peg C.
Move top disk from peg A to peg B.
Move top disk from peg C to peg B.
Move top disk from peg A to peg C.
Move top disk from peg B to peg A.
Move top disk from peg B to peg C.
Move top disk from peg A to peg C.

Fig. 3.11 The Steps of Tower’s of Hanoi


Fig. 3.12 Recursion Tree for Tower’s of Hanoi
Tower of hanoi solution:
Factorial:
n Steps int n;
factorial(n)
1) A→C { int a, b;
if(n == 0)
2) A → B, A → C, B → C return(1);
Stack & Queues

Stack & Queues

a = n – 1;
3) A → C, A → B, C → B, A → C, B → A, B → C, A → C,
b = factorial(a);
return(n * b);
}

133 134
Chapter 3

Chapter 3
Evaluating the above function by taking n = 4, by using recursive tree Fibonacci number:
evaluation of the function code: int n;
fibonacci(n)

{ int a, b;
if(n < = 1)
return(n);
a = fibonacci(n – 1);
b = fibonacci(n – 2);
return(a + b);
}

Evaluating the above function by taking n = 4 by using recursive tree


evaluation of function code:

Fig. 3.14 Recursion Tree for Fibonacci(N)


Stack & Queues

Stack & Queues

Sol: 3)
Fig. 3.13

135 136
Chapter 3

Chapter 3
3.7 BASICS OF QUEUE Note:
y Queue is a data structure in which we insert and delete data items.
y The fashion in which we insert/delete data items is: ‘One side insertion Queue–data structure is used in breadth first search traversal of graphs.
and other side deletion’.
y Queue is represented as:
SOLVED EXAMPLES

Q1 Consider a queue of size ‘7’. In this queue, five insertions and two deletions
have been performed. Find the values of ‘Front’ and ‘Rear’.
Fig. 3.15
Front: It is a variable that contains position of the element to be deleted.
Sol:
y
Queue size is given as ‘7’.
y Rear: It is a variable that contains position of the newly inserted element.
y For the above representation, Let’s find the front and rear: Let’s plot the queue and perform 5 insertions and 2 deletions ;
1) Front is 0 2) Rear is 3
y Property of queue:
FIFO: First In First Out
OR
LILO: Last In Last Out
In the given queue below, Let’s understand FIFO or LILO.
Consider a queue of size ‘6’.

Fig. 3.15
Steps for insertion:
1) First, a is inserted in the queue at position ‘0’.
2) b is inserted at position 1.
3) c is inserted at position 2.
4) d is inserted at position 3.
Positions 4 and 5 are vacant.
Steps for deletion:
1) The first deletion from queue would be at position 0
2) Second deletion be at position 1
3) Third deletion be at position 2
4) Fourth deletion be at position 3
Clearly, ‘a’ was inserted first and deleted first and so on.
Stack & Queues

Stack & Queues

Note:

If we want to delete b at first from above queue, then it is not possible.


Hence, queue works according to the property FIFO or LILO.

137 138
Chapter 3

Chapter 3
3.8 OPERATIONS ON QUEUE
The operations we can perform on queue is
called ADT of queue. Rack Your Brain
(ADT: Abstract data type)
You are given a queue of size ‘n’. It’s
needed to perform ‘n+3’ insertions.
What would be the result?
y ADT of queue:
1) Enqueue ( )
2) Dequeue ( )
We perform two operations on queue, and they are enqueue and
dequeue.

1) Enqueue ( ):
� Using enqueue, an element is inserted into the queue.
� To insert ‘n’ elements there are ‘n’ enqueue() operations needed.

2) Dequeue ( ):
� Using dequeue an element is deleted from the queue.
� To delete ‘n’ elements from the queue, ‘n’ dequeue() operations
needed.

For Example:
Consider an empty queue of size 6, Fig. 3.18

3.9 TYPES OF QUEUE


There are different types of queue; Let’s understand them one by one.

Fig. 3.17 Circular queue:


� Linear queue is not that efficient because the rear can’t go back once
Enqueue a): Will insert ‘a’ at 0. it reaches end of queue.
For example: Suppose a situation as shown in diagram
Enqueue b): Will insert b at 1 and so on.
Dequeue ( ): Will delete a by default because it was inserted first and so on.

Fig. 3.19
Stack & Queues

Stack & Queues

and we need to insert an element to the above queue, but we can’t. Even
though lot of space is free in the queue. We can’t insert any new element
in queue because rear is at the last position.

139 140
Chapter 3

Chapter 3
y Circular queue is upgraded form of linear queue in which ‘Front’ and Deletion of elements follow as:
‘Rear’ can be represented on a circular structure keeping first position
and last position connected.
y Circular queue is represented as:

Fig. 3.22

Thus, by ascending the priority queue, we get items from the queue
in ascending order.

Fig. 3.20 Circular Queue 2) Descending priority queue:


� Insertion of items on queue follows an arbitrary order.
Priority queue:
� Deletion follows as: On first deletion, it gives the largest item of all
In Priority queue, every element is associated
and so on.
with some priority and according to priority Rack Your Brain
For example: Items are 3, 9, 2, 0, 11, 10, 20.
an element in the queue is processed,
� Insertion of items follows as:
especially on deletion time. Consider a queue of size ‘5’. Find the
So, while inserting elements on the queue, value of ‘Front’ and ‘Rear’ after – 5
we can follow any order but, while deleting, insertions, 2 deletions and 2 insertions
we consider the requirement. respectively.

Types of priority queue:


1) Ascending priority queue Fig. 3.23
2) Descending priority queue Deletion of elements/items follow as:

1) Ascending priority queue:


We insert items arbitrarily in the queue.
While deleting, on the first deletion, it gives the smallest item of all,
and so on.
For example: items are 3, 9, 2, 0, 11, 10, 20
� Insertion and elements follow as:
Stack & Queues

Stack & Queues

Fig. 3.21 Fig. 3.24

141 142
Chapter 3

Chapter 3
Thus, by descending the priority queue, we get items from the queue in
descending order.
Rack Your Brain

A doubly ended queue is given below. Calculate the value of ‘Front’


Rack Your Brain
and ‘Rear’ after the following sequence of operations :
1) Two–deletion at ‘Front’
There is ascending priority queue of size 10. We are given ten numerals 2) Three–deletions at ‘Rear’
to insert into the queue. After insertion, we performed deletion till 5th 3) Two–insertions at Rear’.
position. What is the result? (Take positions of items, starting from 0,
1 and so on).
Input restricted queue:
Input restricted queue is a kind of queue in which insertion operations are
performed only at ‘Rear’ as in regular queue, but deletion operations are
Previous Years’ Question performed at ‘Front’ and ‘Rear’ both the positions.

Let’s understand it with a diagrammatic view:


A priority queue Q is used to implement a stack that stores characters.
Consider the queue, in which ‘F’ represents ‘Front’ and ‘R’ represents ‘Rear’.
PUSH (C) is implemented INSERT (Q, C, K) where K is an appropriate
interger key chosen by the implementation. POP is implemented as
DELETEMIN(Q). For a sequence of operations, the keys chosen are in
a) non–increasing order b) non–decreasing order
c) strictly increasing order d) strictly decreasing order
Sol: d) (GATE 1997 - 2 Marks)
Fig. 3.26
In the above diagram, it can be seen that items can be deleted from ‘Rear’
Doubly ended queue (Dequeue): also.
A doubly ended queue is a kind of queue in which insertion, and deletion
operations are performed at both places of ‘Front’ and ‘Rear’. Output restricted queue:
Output restricted queue is a kind of queue; deletion operations are
y Let’s understand it with a diagrammatic view: performed only at ‘Front’ as in regular queues, but insertion operations can
Consider a queue of size ‘7’, where ‘F’ represents ‘Front’ and ‘R’ represents be performed at ‘Front’ and ‘Rear’ both the positions.
‘Rear’.
A diagrammatic representation can be shown as:
Consider the queue, in which ‘F’ represents ‘Front’ and ‘R’ represents ‘Rear’.
Stack & Queues

Stack & Queues

Fig. 3.25
As shown in the diagram, clearly, we can insert or delete items on both ‘F’
and ‘R’. Fig. 3.27

In above diagram, It can be seen that items can be inserted at ‘Front’ also.
143 144
Chapter 3

Chapter 3
Dequeue operation:
Note:
int remove ( )
Priority queue, input restricted queue, output restricted queue, {
doubly ended queue, all these kinds of queue are not guaranteed to
satisfy FIFO property. int I ;

3.10 IMPLEMENTATION OF QUEUE {


We can implement queue by using other data structures like using array, printf (“q is underflow”) ;
stack or linked list etc. exit (1) ;
Implementation of queue using array: }
y Implementing queue means implementing basic operations of queue else
by using array. {
y So, let’s take an array first ;
y Array is denoted with ‘q’, which represents a queue.
y ‘S’ is the size of array, ‘q’.
y Front is represented with ‘F’.
y Rear is represented with ‘R’.
Enqueue operation:

}
{ }
printf (“q is overflow”) ;
exit(1) ; Assumptions:
} y Array positions start from zero i.e.
else
{ y Initially, when there is no item in queue, F = –1 and R = –1.
y If at any stage F = R, there is only one element in the queue.
y In general, ‘R’ increments by one for each insertion.
y In general, ‘F’ increments by one for
each deletion.
y So, queue can be represented by using Rack Your Brain
an array as:
There is queue ‘Q’ implemented with an
array. In this queue front is represented
with ‘F’, and rear is represented with
Stack & Queues

Stack & Queues

‘R’. size of ‘Q’ is S. Write the condition


for (i) overflow of queue ‘Q’.
} (ii) Underflow of queue ‘Q’.
} Here, S = 8 i.e. size of queue.

145 146
Chapter 3

Chapter 3
Implementation of circular queue using array:
Previous Years’ Question y Implementing a circular queue means implementing basic operations of
the circular queue by using array.
y Let’s take an array ‘q’.
A queue is implemented using an array such that ENQUEUE and DEQUEUE
y ‘S’ is the size of a circular queue i.e., ‘q’.
operations are performed efficiently. Which one of the following statements
y Front is represented with ‘F’.
is correct (n refers to the number of items in the queue)?
y Rear is represented with ‘R’.
a) Both operations can be performed in O(1) time
b) At most one operation can be performed in O(1) time but the worst case 1) Enqueue operation: (To insert items)
time for the other operation will be W(n)
c) The worst case time complexity for both operations will be W(n)
d) Worst case time complexity for both operations will be W(logn) {
Sol: a) (GATE CSE 2016 - SET 1) if ((R+1) mod S == F)
{


exit(1) ;
Previous Years’ Question }
else
Suppose you are given an implementation of a queue of integers. The {
operations that can be performed on the queue are : if (R == –1)
i) isEmpty (Q) – returns true if the queue is empty, false otherwise. {
ii) delete (Q) – deletes the element at the front of the queue and F++ ;
returns its value. R++ ;
iii) insert (Q, i) – insert the integer i at the rear of the queue. }
Consider the following function : else
void f (queue Q) { R = (R+1) mod S ; // This condition will use the
int i ; array in a circular manner, to
if (! isEmpty(Q) ) { insert an item on the queue.
i = delete(Q) ; }
f (Q) ; q [R] = x ;
insert (Q, i) ; }
}
The Above code will insert items in a circular manner by the testing status
}
of a queue in circular manner.
What operation is performed by the above function f?
a) Leaves the queue Q unchanged
b) Reverses the order of the elements in the queue Q
c) Deletes the element at the front of the queue Q and inserts it at
Stack & Queues

Stack & Queues

the rear keeping the other elements in the same order


d) Empties the queue Q
Sol: b) (GATE CSE - 2007)

147 148
Chapter 3

Chapter 3
2) Dequeue operation: (To remove items)
int C_remove( ) Previous Years’ Question
{
int I ; Suppose a circular queue of capacity (n–1) elements is implemented
if (F == –1) with an array of n elements. Assume that the insertion and deletion
{ operations are carried out using REAR and FRONT as array index
variables, respectively. Initially, REAR = FRONT = 0. The conditions to
printf (“Underflow”) ;
detect queue full and queue empty are
exit(1) ; a) full : (REAR+1)
} mod n == FRONT
else empty : REAR == FRONT
{ b) full : (REAR+1)
mod n == FRONT
I = q [F] ;
empty : (FRONT+1)
if (F == R) c) full : REAR == FRONT
F = R = –1 ; empty : (REAR+1)
else mod n == FRONT
d) full : (FRONT+1)
F = (F+1) mod S ; // This condition will use the array to
mod n == REAR
remove items in a circular manner.
empty : REAR == FRONT
return I ; Sol: a) (GATE CSE - 2012)
}
}
Implementation of queue using stack:
Assumptions: y A brief note on stack:
y Array position starts from zero i.e. 0, 1, 2 ............ S–1. A stack is a data structure which functions according to property as last in
y Initially when there is no any item in C_queue F = –1, R = –1. first out. Items are pushed onto stack and can be popped out of it.
y If at any stage F = R, there is only one item in the C_queue.
Stack has two basic operations:
1) Push( ): To push items on stack
2) Pop( ): To pop items from stack.
y Implementation of queue using stack means, performing basic
operations of queues by using basic operations of a stack and satisfying
the property of queue.

y Basic operations of queue are:


1) Enqueue (to insert)
2) Dequeue (to remove)
Stack & Queues

Stack & Queues

y Property that should be satisfied for queue is first in first out (FIFO).
y So, we have to satisfy the property FIFO by using a push–pop operation.

149 150
Chapter 3

Chapter 3
For example: y We need to pop all items from ST and pushed onto S’T. Thus we will get the first item
� We want a queue whose items are P, Q, R, S, T, U, V. inserted onto ST as top of stack_S’T, and we can get the first item deleted easily.
* Let’s implement this queue by using stack.
Operations:
1) Enqueue operation: (To insert items on queue)

Fig. 3.29
Fig. 3.31
By performing above operations, we enqueued all the items successfully.
So, enqueue operation can be given as:

2) Dequeue operation: (To delete items from queue)


y So,

Fig. 3.30 Fig. 3.32

We need to delete first item, second item and so on from stack which
Stack & Queues

Stack & Queues

y
Now, pop (P,S’T) 1st dequeue
we had pushed. For that, we need two stacks. The first stack is ST, and
second stack is S’T.

151 152
Chapter 3

Chapter 3
y So, the dequeue operation can be given as:
Remove( ) Previous Years’ Question
{
if (S’T has some elements/items)
An implementation of a queue Q, using two stacks S1 and S2, is given
return (pop (x, S’T))
below :
else
{
void insert (Q, X) {
while (ST has some elements/items) // While loop
push (S1, X) ;
used for
}
{ continuously
void delete (Q) {
popping an item
if(stack_empty(S1)) then {
I = Pop (x, ST); from ST
print (“Q is empty”) ;
which are supposed
return ;
to be pushed
} else while (!(stack_empty(S1))) {
onto S’T.
X = pop(S1) ;
}
push(S2, X);
return (Pop (x, S’T)) ;
}
}
X = pop(S2) ;
}
}
Thus, it’s successfully done using stack. i.e. queue is implemented by using
Let, n insert and m(≤ n) delete operations to be performed in an
stack.
arbitrary order on an empty queue Q.
Let, x and y be the number of push and pop operations performed
respectively in the process.
Rack Your Brain Which one of the following is true for all m and n?
a) n + m ≤ x ≤ 2n and 2m ≤ y ≤ n + m
b) n + m ≤ x ≤ 2n and 2m ≤ y ≤ 2n
A queue is considered to be implemented by using stack. No. of items
c) 2m ≤ x ≤ 2n and 2m ≤ y ≤ n + m
is 5 as shown in the diagram
d) 2m ≤ x ≤ 2n and 2m ≤ y ≤ 2n
Sol: a) (GATE CSE - 2006)

Element–D has to be removed. How many push–pop operations are Implementation of queue using linked list
needed? y A brief note on linked list:
Linked list is a data structure which is in the form of list of nodes as shown in the diagram
below:
Stack & Queues

Stack & Queues

Fig. 3.33

153 154
Chapter 3

Chapter 3
y A node contains two fields: {
1) An information field (we can use an integer data or char data etc.) printf (“overflow”) ;
2) Next address field
exit(1) ;
y An external pointer ‘V’ that points to the first node, and we can access
}
the entire list by this external pointer. ‘V’ is a pointer variable which
contains the address of the first node of the linked list.

y Implementing queue using linked list means performing enqueue and
dequeue operations on the linked list to satisfy the condition of first in
first out, which is of queue’s property.
y Definition of two operations of queue using linked list:
1) Enqueue/insert operation:
   It means we need to add a node at the end of a given linked list.

2) Dequeue/remove operation:
   It means we need to delete a node from starting of a given linked list. }
}
� Let’s understand with an example:
Consider a linked list: 2) Dequeue/remove operation:
Steps: I – Just remove the first node, second node and so on.

Fig. 3.34

1) Enqueue/insert operation: Fig. 3.36


Steps: I) – Create a node
� Code for dequeue operation:
II) – Attach it at the end of the linked list and so on Remove( )
{
int I ;

Fig. 3.35
� Code for enqueue operation:

V = V → next ;
{
Stack & Queues

Stack & Queues

Removed_node = Null ;
return (I) ;
}

155 156
Chapter 3

Chapter 3
So, now list will appear as:
Chapter Summary

y Stack is a linear data structure in which the items can be added or removed only
at one end called top of stack. Stack satisfied LIFO property.
Fig. 3.37
y Basic operations of stack : 1) PUSH
2) POP
Thus, we can perform insert/remove operation of the queue using linked
y Stack conditions: a) Underflow condition
list.
b) Overflow condition
y Expression evaluation:
We need to convert infix expression to postfix expression for evaluation.
Previous Years’ Question
→ Stack is only used in both conversion and evaluation.
y Stack can be implemented with array as well as linked list.
A queue is implemented using a non–circular singly linked list. The queue has a head y Tower of Hanoi is an application of stack.
pointer and tail pointer, as shown in the figure. Let n denote the number of nodes in the y Queue is a data structure which follows the first in first out property.
queue. Let enqueue be implemented by inserting a new node at the head and dequeue y Operations of queue: 1) Enqueue/insert operation
be implemented by deletion of a node from the tail. 2) Dequeue/remove/delete operation
y Types of queue:
1) Circular queue: A circular queue in which the first position and last
position is connected.
2) Priority queue:  While inserting elements into the queue we can follow
any order, but while deleting, we consider the priority.
Types: Ascending priority queue and
Descending priority queue.
3) Doubly ended queue:
Which one of the following is the time complexity of the most time–efficient
It is a kind of queue in which insertion and deletion operations are performed
implementation of enqueue and dequeue, respectively, for this data structure?
at front and rear both places.
a) q(1), q(1) b) q(1), q(n)
4) Input restricted queue:
c) q(n), q(1) d) q(n), q(n)
Insertion operations are performed only at the rear.
Sol: b) (GATE CSE - 2018)
5) Output restricted queue:
Deletion operations are performed only at the front.
y Implementation of queue:
1) Using array:
It means implementing basic operations of queue to satisfy FIFO using array.
2) Using stack:
This means implementing queue using Push–Pop operations to satisfy FIFO.
3) Using linked list:
It means implementing queue by adding or deleting nodes to satisfy FIFO.
Stack & Queues

Stack & Queues

157 158
4 Linked List

Chapter 4

Chapter 4
4.1 BASICS OF LINKED LIST 4.2 OPERATIONS ON LINKED LIST
Introduction: Insertion operation:
� A brief about why linked list came into the picture: 1) Insertion of a node to the front of a linked list:
Basically, there are two reasons behind taking the linked list into the picture. Consider a Linked List:

1) Insertion and deletion is easier:


In Linked List, insertion and deletion take O(1).
Fig. 4.2
2) Size:
When a conventional array is used, the size of the array is fixed once Now we need to insert a node having info as ‘4’ to the front of the above list.
declared. We cannot increase or decrease the array size based on the
Steps to insert:
elements inserted or deleted.
� Linked list is a data structure which is in the form of list of nodes,
as shown in the diagram below:

Fig. 4.1

� A node contains two fields:


 1) An information field (Head)
 2) Next address field (Tail)
� Information field contains actual data, and the next address field
contains the address of the next node of the list.
� An external pointer ‘V’, is a pointer variable which contains an
address for the first node of the list, and points to the first node.
� We can access the entire list with this external pointer ‘V’.
� Linked list is a flexible data structure as we can insert any number
of nodes and delete nodes from the given linked list.
� Next field of last node of linked list contains null, thus null pointer
is used to signal the end of a list.

Note:
The list with no node is called the empty list or the null list.

Fig. 4.3
Note:
The value of external pointer ‘V’ to an empty list is the null pointer. I) We create an empty node ; , which is pointed by a variable
P, means the address of the new node is set to variable P.
Linked List

Linked List

159 160
Chapter 4

Chapter 4
II) Integer ‘4’ is inserted into the info field of the newly allocated node. Generalised algorithm to add any object ‘x’ to the front of a linked list:
III) Next field of the new node is updated with the address of the first node of the given
list i.e. V.
IV) P–value is assigned to V–value so that V is pointing now to the newly attached node.
V) We get the final list after inserting a node with an info field having integer ‘4’.

* Algorithm for insertion of a node to the front of a linked list:


2) Insertion of a node to the end of a list:
Consider the previous list only:

Fig. 3.4
Meaning:
Now, we have to insert a node having info as 4 to the end of the above list.
1) P = getnode( ) ; // This operation obtains an empty node and sets the content
Steps to insert:
of a variable–‘P’ to the address of that node.
2) info(P) = 4 ; // By this operation, integer–‘4’ is inserted into the info field of
new node. info(P) implies the info field of a node pointed by
P).
3) next(P) = V ; // next(P) implies next field of a node pointed by P, and the
given statement of code implies node pointed by P be added
to the front of the list.
4) V = P ; // the address contained in P is now assigned to the variable
V ; hence previous address residing in V is replaced by the
address residing in P.
5) return (V) ; // It will return the linked list, pointed by ‘V’.

Note:
getnode( )
{
Struct node
{
int info ;
Struct node* next ;
A;
return &A ;
}

(We will see detailed code in the section–4.4 Implementation of Linked


Linked List

Linked List

List.)
Fig. 4.5

161 162
Chapter 4

Chapter 4
I) An empty node is created, which is pointed by a variable ‘P’. Deletion operation:
II) Integer ‘4’ is inserted into the info field of the newly allocated node. 1) Deletion of a node from the front of a linked list:
III) The value of ‘V’ is assigned to a new pointer variable ‘S’. Consider a linked list:
IV) ‘S’ traverses the linked list till the end of the linked list.
V) Last node of the given linked list now points to the newly created
node pointed by P.
VI) next(P) is set to ‘null’, which implies it is last node of the given linked Fig. 4.6
list.
We have to delete the first node of the given linked list.
Steps to delete:
* Algorithm for insertion of a node to the end of a given linked list:

Generalised algorithm to add any object ‘x’ to the end of a linked list:

Fig. 4.7

I) The first node of the given linked list, which is pointed by ‘V’, is pointed
Time complexity:
by one more variable, ‘P’.
1) Insertion at beginning = O(1).
II) Next field of ‘P’ is now assigned to ‘V’.
2) Insertion at the end when we have only head pointer = O(n).
III) Info field is copied to a variable ‘x’.
3) Insertion at the end when we have both head and tail pointer = O(1).
IV) Node pointed by ‘P’ is now free, which means deleted.
Linked List

Linked List

V) Final linked list after deleting the first node is returned.

163 164
Chapter 4

Chapter 4
* Algorithm for deletion of a node from the front of a linked list: I) First node of the given linked list, which is pointed by ‘V’, is pointed by
one more variable, ‘S’.
II) ‘S’ traverses the given linked list till the second last node, and points
it.
III) One variable, ‘P’, points to last node of the linked list.
IV) info field of the node pointed by P is copied to a variable ‘x’.
V) next(S) is set to null, to make it the last node.
VI) node pointed by P is deallocated.
VII) Final linked list after deleting the last node is returned.

* Algorithm for deletion of a node from the end of a linked list:


Note:
freenode(P) ; means the node pointed by ‘P’ is now not allocated. The
corresponding memory space is ready for reuse.

2) Deletion of a node from the end of a linked list:


Rack Your Brain
Consider the previous linked list:

Write an algorithm to perform each of


the following operations:
Fig. 4.8 a) Append an element to the end of a
list.
We have to delete the last node from this linked list.
b) Concatenate two lists.
Steps to delete: Time complexity: c) Delete the last element from a list.
1) Deletion from beginning = O(1). d) Delete the nth element from a list.
2) Deletion of given node = O(n).
3) Deletion of node at end = O(n).

Previous Years’ Question Previous Years’ Question

Q. Let P be a singly linked list, Let Q be Q. 


What is the worst case the time
the pointer to an intermediate node complexity of inserting n elements
x in the list. What is the worst–case into an empty linked list, if the
time complexity of the best known linked list needs to be maintained
algorithm to delete the node x from in sorted order?
the list? a) Q (n2) b) Q (n)
a) O(n) b) O(log2n) c) Q (nlogn) d) Q (1)
b) O(log n) d) O(1) Sol: a) (GATE CSE: 2020)
Sol: d) (GATE CSE: 2004)
Linked List

Linked List

Fig. 4.9

165 166
Chapter 4

Chapter 4
Searching operation:
Linear search on a linked list: Sol:
Consider a linked list, pointed by V, we need to find the address of the node having value ‘4’.

Fig. 4.10

Finding integer ‘4’:


I) Start traversing nodes one by one.
II) Compare the info field of nodes with integer ‘4’.
III) Return the address of the node containing integer ‘4’.

Time complexity:
Linear search on a single linked list will take O(n) [worst case] because at the worst case,
we need to traverse the whole linked list with unsorted elements of it, to find the desired
element.

Note:
Binary search on linked list is possible but not efficient.

PRACTICE QUESTIONS
Previous Years’ Question
Q1 Consider the given linked list below: Previous Years’ Question

Find the result of the code given below: Q. 


In the worst case, the number of Q. 
Linked lists are not suitable data
Struct node * S ; comparisons needed to search a structures of which one of the
S = next(next(next(V))) singly linked list of length n for a following problems?
next(S) = V given element is a) Insertion sort
printf(next(next(next(next(next(V)))))(data)) a) log2n b) n/2 b) Binary search
c) log2n–1 d) n c) Radix sort
Sol: d) (GATE CSE: 2002) d) Polynomial manipulation
Sol: b) (GATE CSE: 1994)
Linked List

Linked List

167 168
Chapter 4

Chapter 4
4.3 TYPES OF LINKED LIST
Singly linked list: Previous Years’ Question
y Singly linked list is a type of linked list in which there is one next field
which points to the next node of the linked list.
Q. 
N items are stored in a sorted doubly linked list. For a delete
y Representation of singly linked list.
operation, a pointer is provided to the record to be deleted. For
a decrease–key operation, a pointer is provided to the record on
which the operation is to be performed.
Fig. 4.11 An algorithm performs the following operations on the list in this
y Each node contains two fields order :
1) Information field: Contains objects Q(N), delete, O(logN) insert,
2) Next Field: Contains the address of the next node means points to O(log N) fund, and Q(N) decrease–key.
the next node. What is the time complexity of all these operations put together?
a) O(log2N) b) O(N)
y Last node next field contains null, which implies that it is the last node.
c) O(N2) d) Q(N2logN)
y It is flexible in size means, we can insert any number of nodes to the
Sol: c) (GATE CSE: 2016 (Set–2))
existing linked list, and also can delete nodes.
Doubly linked list:
y Doubly linked list is a type of linked list in which two fields are reserved Circular singly linked list:
for addresses other than the information field, i.e. one for the previous y Circular single linked list is a type of linked list in which the last node
node address, and one for the next node address. next field contains first node address.
y Representation of circular single linked list.
Representation of doubly linked list:

Fig. 4.12
Fig. 4.13
y Each node contains three fields
1) Previous field: Contains the address of the previous node means points y Each node similar to a single linked list
to previous node. contains two fields
Previous Years’ Question
2) Information field: Contains objects 1) information field
3) Next field: Contains the address of the next node means points to 2) next field
Q. In a circular linked list organization,
the next node. y Last node next field contains the address
insertion of a record involves
of the first node of the linked list, which
y The first node, previous address field posses null value. If some node modification of:
makes it circular.
contains the previous field with a null value, it means it is the first node a) One pointer
y Similar to a single linked list, it is also
of a doubly linked list. b) Two pointer
flexible in size means we can add any
y Last node, next address field posses a null value. If some node contains c) Multiple pointers
no. of nodes to the existing linked list,
the next field with null value, it means it is the last node of a Doubly d) No pointer
which is circular and also can remove
linked list. Sol: b) (GATE CSE: 1987)
nodes by changing node links.
Doubly linked list is also flexible in size means we can add any number
Linked List

Linked List

y
of nodes to the existing linked list and also can remove nodes.

169 170
Chapter 4

Chapter 4
y Advantage: We can go back in the list, wherever we want to reach in the y Each node similar to doubly linked list contains three fields:
circular single linked list. 1) Previous field
y We can go back in the circular single linked list which takes O(n) worst 2) Information field
case time complexity. ‘n’ is no. of elements. 3) Next field

y Similar to doubly linked list, it is also flexible in size means we can add
Previous Years’ Question any no. of nodes to the existing circular doubly linked list and also can
remove nodes by changing node links.
Q. A circularly linked list is used to represent a queue. A single variable
4.4 IMPLEMENTATION OF LINKED LIST
p is used to access the queue. To which node should point p such
Implementation of linked list using structures and pointers:
that both the operations enqueue and dequeue can be performed
in constant time?
* A brief note on structures and pointers:
1) Structure:
It is a group of items in which each item is identified by its own identifier
and its datatype.
� Each of the items is known as a member of the structure.
� Consider the following declaration:

Struct {
int a ;
char b ;
a) rear node b) front node float c ;
c) not possible with a single pointer d) node next to front } A, B, C ;
Sol: a) (GATE CSE: 2004)
� ‘Struct’ it a keyword used to declare a structure.
� There are three members of the structure they are:
Circular doubly linked list:    i) a)
y Circular doubly linked list follows two conditions on doubly linked list,    ii) b)
which are:   iii) c)
1) Last node next field contains first node address. y A, B, and C are structure variables, each of A, B, and C contains three
2) First node previous field contains last node address. members a, b, c.
y Using structure, one can define a datatype, which can be referred a
Above two conditions on the doubly linked list make it circular doubly
user defined data type.
linked list.
y Representation of circular doubly linked list:
2) Pointer:
In C–language, programmers are allowed to reference the location of
objects as well as the objects themselves.
� For example: Suppose there is a variable x of int type or float type or
any type. Then, & x refers to the location which contains x. Here x is
called a pointer.
Linked List

Linked List

Fig. 4.14

171 172
Chapter 4

Chapter 4
Creating linked list: Creating doubly linked list:
Consider a linked list that we have to create,

Fig. 4.16
Fig. 4.15
Code:
Struct node
Code: {
Struct node* prev ;
Struct node Char info ;
{ Struct node * next ;
Char info ; } A1, B1, C1, D1, E1 ;
Struct node * next ;
} A1, B1, C1, D1, E1 ; A1 = { Null, ‘A’, Null} ;
B1 = { Null, ‘B’, Null} ;
C1 = { Null, ‘C’, Null} ;
A1 = {‘A’, Null} ; D1 = { Null, ‘D’, Null} ;
B1 = {‘B’, Null} ; E1 = { Null, ‘E’, Null} ;
C1 = {‘C’, Null} ;
D1 = {‘D’, Null} ; A1. next = & B1 ;
E1 = {‘E’, Null} ;
B1. prev = & A1 ;
B1. next = & C1 ;
A1. next = & B1 ;
B1. next = & C1 ; C1. prev = & B1 ;
C1. next = & D1 ; C1. next = & D1 ;
D1. next = & E1 ;
D1. prev = & C1 ;
D1. next = & E1 ;
Struct node * V = & A1 ;
E1. prev = & D1 ;

Struct node * V = & A1 ;


return (V) ; return (V) ;

Creating circular singly linked list:

Note:
We can create linked lists according to our needs means, whatever type
of object, we are required to keep in the info field of a node, we can keep
Linked List

Linked List

it; for example – int data, char data, float data etc. Fig. 4.17

173 174
Chapter 4

Chapter 4
Code: E1 = {Null, ‘E’, Null} ;

Struct node
{
Char info ; A1. next = & B1 ;
Struct node *next ;
B1. prev = & A1 ;
} A1, B1, C1, D1, E1 ;
B1. next = & C1 ;
A1 = {‘A’, Null} ;
B1 = {‘B’, Null} ; C1. prev = & B1 ;
C1 = {‘C’, Null} ; C1. next = & D1 ;
D1 = {‘D’, Null} ; D1. prev = & C1 ;
E1 = {‘E’, Null} ;
D1. next = & E1 ;
A1. next = & B1 ;
B1. next = & C1 ; E1. prev = & D1 ;
C1. next = & D1 ;
D1. next = & E1 ;
;
Struct node *V = & A1 ;
return (V) ;
Struct node * V = & A1 ;
return (V) ;

Creating circular doubly linked list: Q2 Write a function for the given linked list to delete a node which contains the
data ‘x’.

Fig. 4.18
Sol: Void delete_data_x( )
{ while (V ® data ! = x && V ® next ! = Null)
Code: {
Struct node Q=V;
{ P=V; // P is a new pointer variable.
Struct node * prev ; V = V ® next;
Char info ; }
Struct node *next ; if (V ® data == x)
} A1, B1, C1, D1, E1 ; {
A1 = {Null, ‘A’, Null} ; P ® next = V ® next ;
B1 = {Null, ‘B’, Null} ;
freenode (V);
C1 = {Null, ‘C’, Null} ;
Linked List

Linked List

D1 = {Null, ‘D’, Null} ;

175 176
Chapter 4

Chapter 4
V = Null ; 4.4 USES OF LINKED LIST
return (Q) ; y Different uses of linked list are as follows:
} 1) Implementation of stacks:
We can implement stacks by using a linked list, to satisfy the LIFO
property by adding and removing nodes.
2) Implementation of queues:
Queues can be implemented by using a linked list to satisfy the FIFO
property by adding nodes at last or by removing nodes from the start
of the linked list.
3) Representation of graphs:
Graphs are represented by an adjacency list.
Resulted linked list is now: For example:
Let’s take a graph:

Fig. 4.19

Previous Years’ Question


Fig. 4.20
Adjacency list representation of above graph:
Q. Consider the function f defined below.
Struct item {
int data ;
Struct item * next ;
};
int f(struct item * p) {
return ((p == Null) || (p ® next == Null) ||
((p ® data <= p ® next ® data) && f(p ® next))) ;
}

For a given linked list p, the function f returns 1 if and only if


a) the list is empty or has exactly one element
b) the elements in the list are sorted in non–decreasing order of
data value
c) the elements in the list are sorted in non–increasing order of
data value
d) not all elements in the list have the same data value
Sol: b)
Linked List

Linked List

Fig. 4.21

177 178
Chapter 4

Chapter 4
4) Representation of sparse matrices:
To represent or to store sparse matrices, an array is not a good option. III) – Return the address of the node containing the data.
We use linked list to save space. y Types: 1) Singly Linked List
5) Memory allocation: 2) Doubly Linked List
Dynamically, memory is allocated by using linked list by maintaining list 3) Circular Singly Linked List
of free blocks. 4) Circular Doubly Linked List
y Searching time in linked list is O(n) [Worst case]
y Usage: 1) Implementation of Stacks
2) Implementation of Queues
3) Representation of graph by adjacency list.
Chapter Summary 4) For sparse matrices
5) Memory allocation
y Linked List is a data structure which is in the form of list of nodes, and contains two
fields:

1) Information field (Head): Information field contains actual data.


2) Next–address field (Tail): Next–address field contains the address of the next
node of the list.

y Linked list is a flexible data structure as we can insert any number of nodes and
delete nodes from the given linked list.

y Operations on linked list:


1) Insertion of nodes:
Steps: I)– Create an empty node
    II)– Insert data
   III)– Update the next field of the node
   IV)– Update pointer

2) Deletion of nodes:
Steps: I) – Add one more pointer to the first node.
    II) – Next field of the new pointer is updated with the pointer of first node
previously.
   III)– Take data to some variable.
   IV)– Free the node.

3) Searching:
Steps: I)– Start traversing nodes one by one.
II) Compare into the field of nodes with the required data.
Linked List

Linked List

179 180
5 Tree

Chapter 5

Chapter 5
5.1 TREE Level:
y Level = 1+ number of edges between a node and root
Definition =1+ depth of a node
where, depth starts from 0
“Tree is a non-linear data structure. A tree is defined recursively as a Example: In Fig. 5.1, B, C and D are on the same level.
collection of nodes.”

Terminology:
Root:
It is the topmost node of a tree.

Fig. 5.2

Ancestor and descendant:


y A node ‘x’ is an ancestor of a node ‘y’, if there exists a path from the root
to ‘y’, and ‘x’ appears on the path.
y The node ‘y’ is called a descendant of ‘x’. For example A, C and G are
the ancestors for K in Fig. 5.1.
Height:
y Height of a node is the number of edges in the path from that node to
Fig. 5.1 Binary tree its most distant leaf node.
y Height of root node in the below figure = 3.
Edge: y In the below example, the height of B is 2 (B–F–J). Height of tree = 3 as
An edge refers to the link from parents to children (all links in Fig. 5.1). shown in below figure.
Leaf:
A node, which has no children, is known as leaf node.
Example: E, H, J, K and L are all leaf nodes.
Siblings:
Two (or more) children are called to be siblings, if they have same parent
node.
Example:
F and E are siblings.
H and I are siblings.
C, B and D are siblings.
Depth:
Depth of a node is the number of links/edges from root to a particular node. Fig. 5.3
Example: Depth of G = 2.
Tree

Tree

181 182
Chapter 5

Chapter 5
Note:
i) Number in blue represent the depth of the node
ii) Number in black represent the height of the node.

Height of tree:
y Height of tree is the height of root node.
y Height of root node is the number of edges to its the most distant leaf
node.
Fig. 5.6 Right Skew Tree
Skew tree:
It is a binary tree, where every node is having one child except for the leaf Binary trees:
node. y Each node in a binary tree can have either 0, 1 or 2 children.
y A binary tree without any node is also a valid binary tree and called as
an empty or null binary tree.

Fig. 5.4 Skew Tree

Left skew tree:


Fig. 5.7 Conceptual Structure of a Binary Tree
“It is binary tree, where each node will have only one left child except the
leaf node.”
Example:

Fig. 5.8

Fig. 5.5 Left Skew Tree Strict binary tree:


A strict binary tree is a kind of tree, where each node will have either no
Right skew tree: children or exactly 2 children.
“It is binary tree, where each node will have only one right child except the
leaf node.”
Tree

Tree

183 184
Chapter 5

Chapter 5
Example: Properties of complete binary trees:

Fig. 5.9 Strict Binary Tree

Full binary tree:


A full binary tree is a binary tree, in which all the internal nodes have exactly
two children and all the leaf nodes are present at the last level.

In a complete binary tree with n nodes, the number of internal nodes


Fig. 5.10 Full Binary Tree = n/2
The maximum number of nodes in a complete binary tree = 2(h+1)–1.
Complete binary tree:
y A complete binary tree is a kind of tree, where except the last level all Properties of full binary tree:
the other levels are completely filled. y Number of nodes(n) in a full binary tree of height h = 2h+1-1.
at depth = 0, Number of nodes = 20 = 1
at depth = 1, Number of nodes = 21 = 2
at depth = 2, Number of nodes = 22 = 4
at depth h, Number of nodes = 2h
[20 + 21 + 22 + ........... 2h] = 2(h+1)–1

y Number of leaf-nodes in a full binary tree = 2h.


y Number of non-leaf nodes in a full binary tree =(2h)-1.

Note:
The definition of a Strict binary tree, Full binary tree and complete binary
tree varies in different text books but in GATE, the definition will be
specified properly in the question.
Tree

Tree

185 186
Chapter 5

Chapter 5
Traversing Example:
y To see the information present in every node, we visit every node, which
is called traversing.
y The most popular traversing methods which are applied in binary tree,
are given below:

y Generally, they are applicable on the binary tree, but they can be
extended to ternary tree or m–way tree.
y Inorder traversal: Note:
i) Visit left subtree ii) Visit root iii) Visit right subtree
Every node will be visited three times before we finish walking entire tree
y Preorder traversal:
and go back to the root node.
i) Visit root ii) Visit left subtree iii) Visit right subtree
y Postorder traversal:
Inorder
i) Visit left subtree ii) Visit right subtree iii) Visit root
y To get Inorder of any tree whenever we visit any node for the 2nd time,
Example: then print it.

Finding out inorder, preorder and postorder


y For a given tree, if any node (even that node is leaf node) does not have
any children, then give them dummy children.
y After that, start traversing on the tree from the top to down, left to
right.
Preorder
y To get a preorder of any tree, whenever we visit any node for the 1st time,
we print it.
y BAC is an order of nodes, in which nodes are visited for the 2nd time. So,
BAC is the inorder traversal of tree.

Postorder
y To get postorder of any tree whenever we visit any node for the 3rd time,
then print it while walking the tree.
Tree

Tree

187 188
Chapter 5

Chapter 5
Implementation of inorder traversal
Struct node
{
char data;
struct node *left , *right;
};
void Inorder (struct node *t)
{
if (t)
{
Inorder (t → left);
printf (“%c”, t → data);
Inorder (t → right);
y BCA is an order of nodes, in which nodes are visited for 3rd time. So, BCA
}
® postorder–traversal of tree.
}
Example:

Example:
Let the binary tree and address are as shown below:

So, the output is dbeafcg.

Time complexity:
y The best way to analyse the code is to see how many times the codes
are visiting the node and time taken by each node at each time.

Space complexity:
y Space complexity depends on the number of levels in the tree, and
depending on the level of tree, the stack grows.
y In the worst case, if a tree has ‘n’ nodes, then n level can be possible,
because in worst case, a tree can be a skew–tree.
Tree

Tree

189 190
Chapter 5

Chapter 5
y So, for every node, there will be a data in the stack. void postorder (struct node *t)
Therefore, {
if (t)
{
postorder (t → left);
postorder (t → right);
Preorder traversal printf (“%c”, t → data);
y Code: }
Struct node }
{ Similarly, in Postorder traversal also, we have to visit each node once. Thus,
char data;
struct node *left , *right;
};
In worst case, Tree can be skewed one. So in the worst case, height of the
void preorder (struct node *t)
tree can be n. Therefore,
{
if (t)
{
printf (“%c”, t → data);
preorder (t → left); Note:
preorder (t → right); Space and Time complexity of Inorder, Preorder and Postorder traversals
} are O(n) only.
}
Similar to Inorder Traversal, in Preorder traversal also, we have to visit each Level order traversal:
node once. Thus, y The root of the tree as input is given to the algorithm.
y The while loop in the algorithm dequeues the root, prints it and
enqueues it’s left and right nodes.
y The procedure is repeated until the queue is not empty.
In worst case, Tree can be a skewed tree. So, in the worst case, the height
of the tree can be n. Therefore, Example:

Postorder traversal
y The code of postorder traversal is shown below:
Struct node
{
char data;
struct node *left , *right; The order, in which the nodes need to be visited = 1,2,3,4,5,6,7
};
Tree

Tree

191 192
Chapter 5

Chapter 5
Note: Triple order traversal:
y Triple order traversal means printing every node three times based on
Level order traversal of a tree is same as breadth-first traversal for the below code.
tree.
Code:
Double order struct node
y 1) In double order traversal, we print each node two times based {
on code given. char data;
struct node *left;
Recursive code: struct node *right;
struct node };
{ void TO(struct node *t)
char data; {
struct node *left; if(t)
struct node *right; {
}; printf (“ %c”, t → data);
TO(t → left);
void DO (struct node *t) printf (“%c”, t → data);
{ TO (t → right);
if(t) printf (“ %c”, t → data);
{ }
printf (“%c”, t → data); }
DO (t → left);
printf (“%c”, t → data); Example:
DO (t → right);
}
}

Example:

y Triple order traversal of the above tree is abdddbbacceeeca.

Unlabeled tree:
In an unlabeled tree, the nodes do not have a specific name.

Labeled tree:
In a labeled tree, the nodes have a specific name.

Number of binary tree:


y With one node, the number of unlabeled binary trees possible is one,
The double order traversal of the above tree is abbdffdaceeggc.
and the node itself is a root.
Tree

Tree

193 194
Chapter 5

Chapter 5
y Every unlabelled tree with three nodes can be labelled in 6 different
ways.
y So, the total number of labelled binary trees possible = 5 * 6 = 30.
y With two nodes, the number of unlabeled binary trees possible is 2 and y Given n nodes, the number of structured (i.e., unlabelled) binary trees
are given below: possible are 2n Cn / (n + 1) , and for any given structure, we can find one
tree, which is having the particular preorder or inorder or postorder.

SOLVED EXAMPLES

Q1 Give all binary trees with three nodes P, Q and R, which have preorder as
PQR.
y With three nodes, the number of unlabeled binary trees possible is 5
and are shown below:
2n
Sol: With three nodes, the number of structures =
Cn
(n + 1 )
6! 6.5.4
6
C3 3=
! 3! 3.2
= = = 5.
4 4 4
So, five structures are possible, as shown below:

Note:
If we name the node, then the number of trees possible will be even
more.

y Number of binary trees possible with ‘n’ unlabelled nodes is 2n


Cn / (n + 1) .
And each structure will represent one particular preorder, i.e., PQR, as shown below:
Also known as catlan number.
y Number of binary trees possible with ‘n’ labelled nodes is
 2n C / (n + 1)  × n! .
 n 
y In case of a binary tree having three nodes, each unlabelled tree can be
represented in 6 ways in a labelled tree as shown below:
(Let a, b, c are the names of nodes)
Tree

Tree

195 196
Chapter 5

Chapter 5
Q2 What are the number of binary trees possible, if preorder is ABC and pos- Note:
torder is CBA. If we apply one more filter as inorder BCA, then we will get only one tree
as given below.

Sol: The number of binary trees possible with three nodes and postorder CBA is also
five, because we have five structures possible.
In each structure, there will be exactly one binary tree with CBA as a postorder.

There will always be only one binary tree satisfying all the conditions given
specified preorder, postorder and inorder.

Q3 Let us say, there are three nodes, and three nodes are labelled as A, B and C.
The binary tree with three nodes A, B and C having preorder as ABC and postorder The preorder is given as ABC and inorder is given as BAC. Construct the bina-
as CBA are given below: ry tree for the specific order.

Sol: In preorder, the root will be on the left most side of the given preorder sequence.
So, A is the root of the binary tree.
In inorder, the root will be at the middle, and the left subtree will be at the left
of the root. Similarly, the right subtree will be at the right of the root.
Hence, the binary tree will look like as shown below:

So, the number of a binary tree, which is having preorder ABC and postorder CBA Note:
are 4. i) If postorder is given then we get root from right to left.
ii) So, if preorder and inorder are given or postorder and inorder is given,
we will be able to construct a unique binary tree.
Tree

Tree

197 198
Chapter 5

Chapter 5
Note:
Q4 Given an inorder as 1, 2, 3, 4, 5, 6, 7, 8 and preorder as 5, 3, 1, 2, 4, 6, 8, 7. Find
postorder = ? If only one order is given, we will not be able to create a unique binary–
tree.
Sol: Preorder: (Root,Left,Right)
Recursive program to count number of nodes
Inorder: (Left,Root,Right) Suppose ‘NN’ represents number of nodes.
Thus, 5 will be the root. The recursive equation to count number of nodes is shown below:
NN(T) = 1 + NN(LST) + NN(RST)
Here, LST and RST represent left–subtree and right-subtree, respectively.
Base condition:
NN(T) = 0; when T = 0 (if T is NULL)

Recursive program to count the number of nodes:


struct node
{
int i;
struct node * left;
struct node * right;
};
int NN(struct Node *t)
{
if(t)
return (1+NN(t → left) + NN(t → right));
else
return 0;
}
Recursive program to count the number of leaves
Let ‘NL’ denotes the number of leaf nodes in the tree.
Using Preorder and Inorder, the binary tree we get is shown below: The recursive equation is given below:
NL(T) = 1; if T is leaf
= NL(LST) + NL(RST), otherwise

Program:
int NL(struct node *t) {
if (t == NULL)
return 0;
else if (t → left == NULL && t → right == NULL)
return 1; /* this condition checks whether ‘t’ is leaf or not */
else
return (NL(t → left) + NL(t → right));
}
The postorder is 2, 1, 4, 3, 7, 8, 6, 5.
Tree

Tree

199 200
Chapter 5

Chapter 5
Recursive program to count number of non-leaf Leaf nodes are: B, F, G, H
Let ‘NNL’ denotes the number of non-leaf. Non–leaf nodes are: A, C, D, E
The recursive equation is given below: Full nodes are: A, C, D
NNL(T) = 0, if T is leaf or T is NULL
= 1 + NNL(LST) + NNL(RST), otherwise Note:

Recursive program Full nodes will always be a subset of non-leaf nodes.


int NNL(struct node *t) /* Here, root of the tree is passed */
{ The recursive equation of the full node is given below:
if(t == NULL) Let FN denotes full node.
return 0; FN(T) = 0; T = NULL
if (t → left == NULL && t → right == NULL) = 0; T is a leaf
return 0; = FN(T → LST) + FN(T → RST); if T has only one child.
else = FN(T → LST) + FN(T → RST) +1; if T is a full node.
return (1 + NNL(t → left) + NNL(t → right)); where LST means Left-Subtree and RST means Right-Subtree
}
Program:
Time complexity: int FN(struct node *t)
y Here, every time we visit the node, we are doing constant work. {
y Here, three times we are visiting the each node.
if (!t)
return 0;
if (!t → left && ! t → right)
Space complexity return 0;
Space complexity depends upon the tree’s height, and in the worst-case return (FN (t → left) + FN (t → right) + (t → left && t → right) ? 1: 0);
scenario, tree will be skewed.
}
Space complexity= O(n)
}
Full node
y A node having all children is called a full node.
y In a binary tree, a node having exactly two children is called a full node. Recursive program to find height of a tree
y Every full node is non-leaf node, but all non-leaf nodes are not a full Let us say, the height of the tree pointed by the pointer T is H(T).
node.
The recursive equation is:
Ex:
 0 ;if T is empty

( )
H T =  0 ;if T isleaf

( ( ) ( ))
1 + max H LST ,H RST ; otherwise
Tree

Tree

201 202
Chapter 5

Chapter 5
Program: Note:
int H(struct node *t)
Previous Years’ Question i) The above-mentioned property needs to be satisfied by every
{
node present in the tree.
if (!t && (!t → left && !t → right))
Which of the following sequences ii) Both the left-subtree and right-subtree have to be BSTs.
return 0; denotes the post order traversal
else sequence of the below tree?
return(1+ ((H(t → left) > H(t → right)) ?
H(t → left): H (t → right);
}
}

a) fegcdba b) gcbdafe
c) gcdbfea d) fedgcba Example:
Sol. c) (GATE: 1996) In the below diagram, the tree on left is a BST, but the right one is not a BST.
As node 2 is smaller than 3, but is present in the right subtree of node 3.
Thus, it is violating the property of BST.

Previous Years’ Question

An array X of n distinct integers is interpreted as a complete binary tree.


The index of the first element of the array is 0. The index of the parent
of element X[i], i ≠ 0, is?

i i – 1  i  i
a) b)  2  c)  2  d) 2 − 1 Declaration of BST
2      
y BST is a binary tree whose in-order traversal gives a sorted order of
Sol: d) (GATE: 2006) elements.
struct BST_Node
{
Binary search trees (BSTs) int data;
y BSTs are useful for searching. struct BST_Node *left;
y Binary Search Tree Property:- For all nodes, the value of a node should struct BST_Node *right;
be greater than all nodes in left subtree and smaller than all nodes in };
right subtree.
Tree

Tree

203 204
Chapter 5

Chapter 5
Operation on binary search trees Finding a minimum element in binary search trees
Some of the operations supported by BSTs are y In BSTs, the minimum element is the leftmost node, which is either a
y Find Minimum element leaf node or a node with a right child but not with a left child.
y Find Maximum element y In the below example, the minimum element is 3.
y Insertion in BST
y Deletion in BST
y Searching an element

Note:
y Since root value is always in between left subtree data’s and right
subtree data’s, performing an Inorder traversal on BST produces a
sorted list in an increasing order.
y Searching of a key element:
if key_element < root ® data, then search only in the left subtree.
if key_element > root ® data, then search only in the right subtree.
if key_element == root ® data then key_element is found
In worst case, searching time in Binary Search Tree will be O(n).

Finding an element in binary search trees


y First, start traversing from root node, BST-property is used to find any
element in BST.
y If the value of the element, which we want to search is less than the
value of root-node, then search only in the left sub-tree.
y If the value of the element, which we want to search is greater than the
y Non-recursive way to explain the above algorithm:
value of root-node, then search only in the right sub-tree.
struct BST_Node *Find(struct BST_Node * root, int data) struct Node *Minimum(struct Node *Root)
{ {
if(root == NULL) if(Root==NULL)
return NULL; return NULL;
if(data < root → data) while(Root → l_child !=NULL)
return (Find(root → left, data)); Root=Root → l_child;
else if(data > root → data) return Root; }
return (Find (root → right, data));
else if(root->data ==data)
return root;
}
Tree

Tree

205 206
Chapter 5

Chapter 5
Finding a maximum element in binary search trees eg:
y The maximum element in a BST is the rightmost node, i.e., the right
most leaf node in right subtree.
(OR)
A node in the right subtree having a left child but not having a right child.
y The maximum element in the below shown BST = 19.

Insertion in BST
y We have to first search for the location to insert an element (node) into
a Binary-Search-Tree.
y Find operation is used to find location of data to insert.
y eg: Insert an element 12 into the BST.

struct Node *Maximum(struct Node *Root)


{
if(Root==NULL)
return NULL;
else if(Root → r_child==NULL)
return root;
else Search for element y = 12
return Maximum(root → r_child); 12 < 17, go to the left subtree of 17
} 12 > 9, go to the right subtree of 9
12 > 11, go to the right subtree of 11
12 < 14, go to the left side of 14 and insert 12.

Inorder predecessor:
In BST, Inorder predecessor is the greatest element in the left subtree Deletion in BST
of a node. y If a node to be deleted is a leaf node, then simply delete it. Deleting
a non-leaf node is a complex task as other nodes may need to be
Inorder successor:
shuffled after deletion. The process of deletion can be seen below:
In BST, Inorder successor is the smallest element in the right subtree
y First search the location of the node to be deleted.
of a node.
Tree

Tree

207 208
Chapter 5

Chapter 5
y Following are the cases to delete a node in BST:
For leaf node:
y To delete the node having no child, simply deallocate the memory and
add the NULL pointer to its parent. Previous Years’ Question
Example:
y If we have to delete a node 5, just delete it. A program takes as input a balanced binary search tree with n leaf
nodes and computes the value of a function g(x) for each node x. If the
cost of computing g(x) is:

 number of leaf – nodes number of leaf – nodes 


min  
 in left – subtree of x , in right – subtree of x 
Then the worst–case time complexity of the program is?
a) θ (n) b) θ (nlog n) c) θ n2 ( ) (
d) θ n2 log n )
Node having one child:
Sol: b) (GATE: 2004)
y When we have to delete the node having only one child.
y Delete that node and point the node’s parent-pointer to its children.

Previous Years’ Question

Suppose that we have numbers between 1 and 100 in a binary search


tree and want to search for the number 55. Which of the following
sequences CANNOT be the sequence of nodes examined?
a) {10, 75, 64, 43, 60, 57, 55}
Node having two children: b) {90, 12, 68, 34, 62, 45, 55}
When we have to delete a node having two children, delete that node and c) {9, 85, 47, 68, 43, 57, 55}
replace that with either Inorder successor (OR) Inorder predecessor. d) {79, 14, 72, 56, 16, 53, 55}
Sol: c) (GATE: 2006)
Tree

Tree

209 210
Chapter 5

Chapter 5
AVL (Adelson–Velskil and Landis) trees
Previous Years’ Question
Definition

 data structure is required for storing a set of integers such that each
A It is a height balanced binary search tree.
of the following operations can be done in O(logn) time, where n is the For each node K, the height of the left and right subtrees of K differ
number of elements in the set. by at most 1. It means the balance factor of an AVL tree should be
i) Deletion of the smallest element (-1,0,1).
II) Insertion of an element if it is not already present in the set
What is balance factor?
Which of the following data structures can be used for this purpose?
It is the difference between heights of left sub tree and right sub tree. It
a) A heap can be used but not a balanced binary search tree.
means Balance factor(K) = Height of LST(K)-Height of RST(K).
b) A balanced binary search tree can be used but not a heap.
c) Both balanced binary search tree and heap can be used.
d) Neither balanced search tree nor heap can be used.
Sol: b) (GATE: 2003)

Balanced binary search trees


y Worst case time required for searching in a BST in O(n).
y It occurs when BST is either left-skewed or right-skewed.
y The worst-case time complexity can be reduced to O(logn) using the
concept of balanced binary search tree.
y Suppose H(K) represents the height of a balanced-tree.
Here, K=balance factor
where Balance factor=Height of left subtree-Height of right subtree.

Full balanced binary search trees


y A binary search tree is known to be full balanced BSTs, if balance factor
of each node = 0.
For example: Note:
Height of a Null Tree is –1.

Minimum/maximum number of nodes in AVL tree


y Suppose N(h) denotes the number of nodes present in the AVL tree,
where h denotes the height of an AVL tree.
y For minimum number of nodes having height=h, left subtree of an AVL
tree should be of height (h-1) and right subtree should be of height (h-2).
(OR)
y The left subtree of an AVL tree should be of height (h-2) and right
subtree should be of height (h-1).
Tree

Tree

211 212
Chapter 5

Chapter 5
y Therefore, the recursive equation for minimum number of nodes of Note:
height h:
All above cases will lead to height of an AVL = O(logn), where n denotes
the number of nodes.
y Where N(h-1)= minimum number of nodes of height (h-1)
y N(h-2)= minimum number of nodes of height (h-2) AVL tree declaration
struct AT
From the above equation: {
int d;
struct AT *Lt ,*Rt;
int h;
};
In the above AVL Tree Declaration, AT represents AVL Tree, d represents data,
*Lt represents left pointer, *Rt represents right pointer and h represents
height.

Height of an AVL tree


int h(struct AT *root)
{
if(root==Null)
return -1;
else
return root → h;
}

Rotation:
y When we insert an element to an AVL tree or delete an element from an
AVL tree, the structure of tree will likely to get changed.
y As insertion/deletion of an element can result in an increase/decrease
of the height of subtree by 1.
y To correct it, we need to check the balance factor of each node after
every insertion/deletion of an element in an AVL-Tree and then rotation
of tree is required.
So, the maximum height of an AVL tree = O(logn)
y There are mainly two types of rotations to restore the properties of
For maximum number of nodes, the recursive equation, we get:
AVL-Tree:
N(h)=N(h-1)+N(h-1) +1
Single Rotation and Double Rotation.
=2N(h-1) +1
It is a case of full binary tree. Observation
After solving the above recurrence relation, we will get: y When we insert a new node in an AVL tree, balance factor of those
height h = O(log n) nodes get affected, which are present in the path from root to the
insertion-point.
Tree

Tree

213 214
Chapter 5

Chapter 5
y So, after insertion of a new node, we need to check the balance factor
of 1st node present in the path from insertion-point to root and so on.
y We need to restore the AVL-Tree property according to the type of
violation.
Types of violation
y What type of violation can occur?
y Suppose K is the node, where the imbalance occurs due to different
type of insertion.
y There are four cases possible:

Time complexity: O(1)


Space complexity: O(1)

Right right rotation (RR rotation)


y When a new node 45 is inserted into the given AVL tree, an imbalance
occurs, as balance factor of some nodes get changed.
Given AVL Tree

Balancing of a tree using rotation


Single rotations Example:
Left left rotation (LL rotation):
y Suppose a new node 9 is inserted into the given AVL tree, an imbalance
occurs as balance factor of some nodes get changed. We need to
perform LL-Rotation in this case.
y Given AVL tree


Time complexity: O(1)
Space complexity: O(1)
Tree

Tree

215 216
Chapter 5

Chapter 5
Double rotation
Sometimes, Double rotation is required to restore the AVL-
tree properties as single rotation is not enough to solve it.

Left right rotation (LR rotation)


y LR Rotation is required when insertion occurs at the right subtree of
the left child of a node.

Example
When a new node 15 is inserted, Imbalancing of AVL-tree
occurs.
Here, we have to perform LR Rotation(double rotation) to
balance the given AVL-tree.

Right left rotation (RL rotation)


y RL Rotation is required when insertion occurs at the left subtree of the
right child of a node.
Example:
y When a new node 15 is inserted, Imbalancing of AVL-tree occurs.
y Here, we have to perform RL Rotation(double rotation) to balance the
given AVL-tree

Time and space complexity of insertion in an AVL-tree:


Tree

Tree

217 218
Chapter 5

Chapter 5
Note:
i) In red-black tree and 2-3 tree an element can be searched in O
(log n) time
ii) If “I” is the number of internal nodes of a complete n-ary tree,
Then, the number of leaves present in it can be represented by
I(n–1) + 1.
iii) So, if we have ‘n’ internal nodes of degree 2 in a binary tree, then
we have (n+1) leaf nodes. Step 5: Insert 5

SOLVED EXAMPLES

Q1 Construct an AVL tree for the given sequence


25, 26, 32, 8, 5, 13, 29, 17, 15

Sol: Step 1: Insert 25

Step 6: Insert 13
Step 2: Insert 26

Step 3: Insert 32

Step 4: Insert 8

Step 7: Insert 29
Tree

Tree

219 220
Chapter 5

Chapter 5
Previous Years’ Question

Which of the following is TRUE?


a) The cost of searching an AVL tree is q(logn), but that of a binary
search tree is O(n).
b) The cost of searching an AVL tree is q(logn), but that of a complete
binary tree is q(nlogn).
c) The cost of searching a binary search tree is O(logn), but that of an
Step 8 : Insert 17 AVL tree is q(n).
d) The cost of searching an AVL tree is q(nlogn), but that of a binary
search tree is O(n).
Sol: a) (GATE: 2008)

Heap
y Heap is a nearly complete binary tree.
y It means, all the leaf nodes must be either at level h or (h-1).
Step 9 : Insert 15 y The main property of a heap is that the value of each parent node should be either:
less than equal to its children(min-heap)
OR
greater than equal to the value of its children(max-heap).
y Heap takes less time for inserting an element, for finding minimum in case of min-heap,
for finding maximum element in case of max-heap, for deleting an element etc.
y The time complexity details of different data-structures for different operations are as
shown below:

Data structure Insertion Search Find Min. Delete Min.

Unsorted array O(1) O(n) O(n) O(n)

Sorted array (increasing order) O(n) O(log n) O(1) O(n)

Unsorted linked list O(1) O(n) O(n) O(n)

Min-heap O(log n) O(n) O(1) O(log n)


Tree

Tree

221 222
Chapter 5

Chapter 5
Unsorted array: y In min heap, first element (root) is the smallest element; thus, the time
y Insertion: In an Unsorted array, insertion can happen at the end of the taken by min-heap to find a minimum element = O(1)
array, so it will take O(1) as time complexity. y In the worst case, searching an element in a heap requires to visit all
y Seaching: In an unsorted array, to search for an element, we need to the nodes once.
visit all the elements in the array, i.e., linear search. Hence, its time So, time complexity= O(n)
complexity is O(n).
y Finding minimum element: Similarly, to find the minimum element in Max-heap:
an unsorted array, we might need to visit all the elements in the array in Definition: Max-heap is a complete binary tree, where the root value is
worst case, i.e., linear search. Hence, its time complexity is O(n). maximum. It means, the root element must be greater than all the elements
y Deletion of minimum element: To delete a minimum element in an present in the left subtree and right subtree.
unsorted array, we first have to find the minimum element, which we
want to delete and then after deleting that element, remaining (n-1)
elements need to be moved in worst case given n number of elements
in an unsorted array.
Total complexity = O(n) +O(n) =O(n).
Sorted array (Let ‘n’ be number of elements in increasing order)
y Insertion: Insertion takes O(n) time. Since it is a sorted array, we need
to place the element in its correct position. After that, in the worst
case, we need to move the remaining (n–1) elements, so, the overall
time complexity is O(n).
y Seaching: Since it is a sorted array, we can apply binary search, and it
Min-heap:
will take time complexity as O(logn).
Definition: Min–heap is a complete binary tree, where the root value must
y Finding minimum element: Since the sorted array is in an increasing
be smallest.
order the 1st element is the minimum element. Hence, O(1) is the time
complexity to find a minimum element. y It means the root element should be smaller than all the elements
y Deletion of minimum element: Now, if we want to delete the minimum present in the left subtree and right subtree.
element, we need to move remaining (n–1) element ahead. Hence the
Example:
time complexity is O(n).

Unsorted linked list (having ‘n’ element)


y Insertion: Insertion can happen in O(1), because it is an unsorted linked
list.
y Searching: To search an element, we need to visit every element of a
list. So, In worst case its time complexity is O(n).
y Finding minimum element: To find the minimum element, we need to
visit every element of the list in worst case, as linked list is unsorted.
Hence, its time complexity is O(n).
y Deletion of minimum element: To delete minimum, finding minimum Array representation of a complete binary tree
takes O(n), and deleting it will take O(1) time. y Heap can be represented using the arrays.
y As we know, Heap is nearly a complete binary tree. This method to
Min-heap, (having ‘n’ element)
represent the heap does not waste any space(location).
y The time taken by min heap to insert and delete an element in min
heap =O(log n).
Tree

Tree

223 224
Chapter 5

Chapter 5
y Suppose all the elements are stored in array starting from index 0. It is a binary max-heap, because all the nodes are satisfying the heap property. Here,
y In this case, the representation of the previous given heap can be given as: A.heapsize is 5.
b)

y The index of a parent for a child at index ‘i’ is i / 2 –1.


y Left child of the ith element is at 2*i + 1. This is also a binary max-heap, because all the nodes are satisfying the heap property.
y Right child of the ith element is at 2(i+1). Here, A.heapsize is 5.
y Suppose the index of the array starts at 1, then c)
i
y The index of a parent for a child at index ‘i’ is .
2
y Left child of the ith element is 2*i.
y Right child of the ith element is 2*i+1.

Note:
Here, multiplication with 2 indicates the left shift, and division with 2 This is also a binary max-heap, because all the nodes are satisfying the heap property.
implies the right shift. It is easy to implement like this. Here, A.heapsize is 5.
d)

SOLVED EXAMPLES

Q1 Given the array of element check whether it is binary max–heap or not?


a) 14, 13, 12, 10, 8
This is also a binary max-heap, because all the nodes are satisfying the heap property.
b) 14, 12, 13, 8, 10
Here, A.heapsize is 5.
c) 14, 13, 8, 12, 10
e)
d) 14, 13, 12, 8, 10
e) 89, 19, 40, 17, 12, 10, 2, 5, 7, 11, 6, 9, 70

Sol: a)

This is not a max-heap, because 70 is larger than the parent node, which is 10.
Tree

Tree

225 226
Chapter 5

Chapter 5
Note: y Heapifying is the process of maintaining the heap property (i.e., every
node satisfies the property of the heap according to the max(min) heap).
y Every leaf is a heap. y For example, to heapify an element in a max-heap:
y For a given set of numbers, there may be more than one heap. y First, find its child with maximum value and swap it with the current node.
y If an array is in decreasing order, then it is always a max–heap. y Continue this process as long as all the nodes satisfy the heap-property.
y If the array is in ascending order, then it is always a min–heap. y MAX-HEAPIFY(A, i), where A is the heap and ‘i’ is the index of element,
y The starting index of leaf nodes of a complete binary tree having ‘n’ where insertion happens.
 n   n  y This function can be applied, only if the left subtree and the right
nodes is,  + 1 to n  and the number of non–leaf is  1 to . subtree tree are to be max-heap.
  
 2   2 
Max-Heapify (A, i)
{
l = 2i; // ‘l’ indicates left child index
Note: r = 2i + 1; // ‘r’ indicates right child index
Maximum number of nodes at a given level of height ‘h’ of complete if (l ≤ A.heapsize and A[l] > A[i])
n largest = l;
binary tree (having n number of nodes) is . else
2h+ 1
largest = i;
if (r <= A.heapsize and A[r] > A[largest])
Example: largest = r;
if (largest ≠ i)
exchange A[i] with A[largest]
Max-Heapify (A, largest)
}
y At each step, the largest of the elements A[i], A[LEFT(i)] and A[RIGHT(i)]
are determined, and its index is stored in largest.
y If A[i] is largest, then the subtree rooted at node i is already a max-
heap, and the procedure terminates.
y Otherwise, one of the two children has the largest element, and A[i] is
Maximum number of nodes at height = 0 swapped with A[largest], which causes node i and its children to satisfy
the max–heap property.
n
= y The node indexed by largest, however, now has the original value A[i],
2h+ 1 and thus the subtree rooted at largest might violate the max-heap
property.
15 15 y Consequently, we call MAX-HEAPIFY recursively on that subtree.
= == =8
20+ 1 2 y The running time of MAX-HEAPIFY on a subtree of size n rooted at
So, at most eight nodes will be at height ‘0’ in this particular tree. a given node i is the q(1) time to fix up the relationships among the
elements A[i], A[LEFT(i)], and A[RIGHT(i)], plus the time to run MAX-
Heapifying an element HEAPIFY on a subtree rooted at one of the children of node i (assuming
y Sometimes, when we insert an element into a heap, it might violate the that the recursive call occurs).
property of heap.
Tree

Tree

227 228
Chapter 5

Chapter 5
y The children’s subtrees have a size at most 2n/3. The worst case occurs Step: 3
when the bottom level of the tree is exactly half full, and therefore, we
can describe the running time of MAX-HEAPIFY by the recurrence

T (n) ≤ T ( 2n / 3 ) + θ ( 1)

y The solution to this recurrence, by case 2 of the master theorem, is T(n)


= O(logn).
y Alternatively, we can characterise the running time of MAX-HEAPIFY on
a node of height “h” as O(h). Previous Years’ Question

Example: Step: 4 What is the maximum height of any


Let us assume that node 1 is at the root as shown below: AVL tree with 7 nodes? Assume that
the height of a tree with a single node
Step: 1 is 0.
a) 2 b) 3
c) 4 d) 5
Sol: b) (GATE: 2017)

Space complexity:
y Since, it is a recursive function and for worst case (log n), a recursive call can be made.
y So, the function is called the number of level times.
Step: 2 y Number of levels will be logn in worst case.
y So, space complexity = O(log n)

Note:
i) In worst case, the number of recursive call = number of levels.
ii) Average space complexity = q(log n)
iii) Best case space complexity = W(1)

Build max-heap
y To convert a given array A[1....n] into a max heap, Max-Heapify procedure is used in
bottom to up fashion.
BUILD-MAX-HEAP(A)
1) A.heap–size = A.length
2) for i = A.length / 2 downto 1
3) MAX-HEAPIFY (A, i)
Tree

Tree

229 230
Chapter 5

Chapter 5
Note:
As all the leaf nodes are by default heap.
So, we have to start the iteration from last non-leaf node to node index
1(initial).

y As we can see in the above figures that index i points to node 5 first
and then call Max-Heapify.
y In the next iteration, index i will point to 4 and so on.
y Final result we get is Figure (f) which is final max-heap.

Time complexity:
y Time taken by the Max-Heapify procedure when called on a node having
height ‘h’ = O(h)
y Therefore, the total time taken by Build-Max-Heap is as:
log n
n  Here, 'n' is the total number of nodes in 
∑ 2h+ 1
O h()  a tree, where h is height of node. 
h= 0  

log n
n

h= 0 2h.2
(ch)

log n
cn h 
=
2 ∑  2
h= 0
h 

.......... ( 1)

 cn ∞
h 
( ) ( ) ()
eq. 2 is greater than eq. 1 , 
= O
 2 ∑2  .......... 2
h 
 
 that ' s why we are putting Big Oh 
 h= 0   

 ∞ h 
= O(n) ∑
 h=0 2
h
= 2 ( this is a harmonic propagation) .


So, time complexity is O(n).


Tree

Tree

231 232
Chapter 5

Chapter 5
Space complexity: Space complexity:
y Build max heap space complexity will be same as space complexity y Heap_Increase_Key(A, i, key) does not require any extra space. Hence,
taken by max-heapify (A,i). its space complexity is O(1).
y It will take maximum, when it is called from root.
y That’s why the space complexity is O(log n). Note:

Deletion of Maximum element from max-heap y For Heap_decrease_Key( ) on max–heap, we have to just call the max–
Heap_Delete_Max(A) heapify at that index, where decrease key happen, and after that we
{ will get a heap.
if(A.heap–size < 1) y Space Complexity of Heap_decrease_Key( ) is O(1).
printf (“Heap Underflow”); y Time Complexity of Heap_decrease_Key( ) is O(log n).
max = A[1];
A[1] = A[A.heap-size];
Insert an element in max-heap:
A.heap–size = A.heap–size – 1;
y Whenever we want to insert an element in max–heap, then insert the
MAX–HEAPIFY (A, 1);
element at last index (position).
return max;
y After insertion, compare it with parent, and if parent is lesser than new
}
element, then swap it.
y Here, space complexity and time complexity will be for MAX-HEAPIFY
y We will continue this procedure, until either we reach at root, or if
(A,1) only.
parent is greater than the child.
y So, time and space complexity are O(log n).
Example:
Increase Key in max-heap
Let ‘A’ be the array, and ‘i’ be the index, whose value we want to increase
to key.
Heap_Increase_Key (A, i, key)
{
if (key < A[i])
printf (“error”);
A[i] = key;
while (i > 1 && A[i/2] < A[i])
{
exchange A[i/2] and A[i]
i=i/2;
}
}

Time complexity:
y In worst case, the leaf node will get increased, and then Heap_Increase_
Key (A, i, Key) will get called from leaf.
y That’s why it has to go till the root node. So, it will take O(log n).
Tree

Tree

233 234
Chapter 5

Chapter 5
 n 
y So, minimum element will be in from  + 1  to n.
 2 
 
n
y So, at most we need to search element to find minimum. Hence, the
2
n
time complexity is O   = O (n) .
2
Search an element in Heap
y To search an element in Heap, we may need to visit every element.
y Let us assume number of elements in heap are n. Then, time complexity
to search an element is O(n).

Delete an element in Heap


y To delete an element, first we need to search an element.
y To search an element, we require O(n) as time complexity.
y After deletion of an element, the tree may violate the Heap condition.
So, again we need to call heapify function.
y Hence, its time complexity is O(n).

Time complexity of max-heap with different operations

Operations in max-heap Time complexity


y So, in worst case, the newly inserted element will have to traverse from
the leaf node to the root. Find Maximum O(1)
y Hence, time complexity is O(log n).
y We can even use the max_heap_increase_key (A, i, Key) to implement Delete Maximum O(log n)
the insertion into max-heap.
Insert an Element O(log n)
Procedure:
i) At new node we will insert an element –∞. (–∞ is some element, which Key Increase O(log n)
could be very very smaller than all the elements).
ii) Since-infinity is there, then increasing that node from -infinity to Decrease Key O(log n)
newly inserted node and to do that we called increased key and we
Search a random element O(n)
got the max-heap.
Find minimum O(n)
Finding minimum element in max-heap (having ‘n’ elements)
y Finding a minimum element will take O(n), because minimum will be Delete an element O(n)
present at leaf.
y The starting index of leaf node of heap having n nodes is
y Similarly, we can have all these operations on min-heap.
 n  
 + 1  to n  .
 2  
  
Tree

Tree

235 236
Chapter 5

Chapter 5
Heapsort
y In this algorithm, we are swapping the root element with the last
element in the heap, and then reducing the heapsize by 1. After that, we
are applying the Max-heapify in the root.
y In this way, we can sort the Heap.
Pseudocode:
Heap_Sort (A) /* A is the array having ‘n’ elements.
{
Build-Max-Heap(A)
for (i = A.length down to 2)
{ a) b)
exchange A[1] with A[i]
A.heapsize = A.heapsize – 1
Max-Heapify (A, 1)
}
}

Time complexity:
In Heapsort,
Step 1: Building max heap takes O(n) time.

Step 2:
Swapping of A[1] with A[n]
A.heapsize = A.heapsize – 1
Max-Heapify(A,1)

All these lines will take time as O(log n) in one iteration due to Max-Heapify.
For n nodes(elements), it will take overall O(n log n) time.

Overall time complexity


=O(n) + O(n log n)
=O(n log n)

Note: In the below example of heapsort, we have already built the heap.
Tree

Tree

237 238
Chapter 5

Chapter 5
Previous Years’ Question

 onsider the process of inserting an element into a Max Heap, where


C
the Max Heap is represented by an array. Suppose we perform a binary
search on the path from the new leaf to the root to find the position
for the newly inserted element, the number of comparisons performed
is:
a) ( )
θ log 2 n (
b) θ log 2 log 2 n )
c) θ (n) d) θ nlog 2 n ( )
Sol: b) (GATE: 2007)

Previous Years’ Question

We have a binary heap on n elements and wish to insert n more


elements (not necessarily one after another) into this heap. The total
time required for this is
a) θ (log n) b) θ (n )
c) θ (nlog n) d) ( )
θ n2

Sol: b)

SOLVED EXAMPLES

Q1 Draw binary max-heap for the following given sequence:


25, 78, 99, 98, 82, 100, 102
y In the above figures, Figure(a) shows the max-heap after the call of
Build-Max-Heap.
y Figures (b) to (j) show swapping of A[1] with A[i] and decrement of Sol: Step 1: Insert 25
heapsize each time and the position of i on the max-heap after each
call of Max-Heapify.
Step 2: Insert 78
Tree

Tree

239 240
Chapter 5

Chapter 5
Step 3: Insert 99 Step 7: Insert 102

Step 4: Insert 98

Q2 Which of the following sequence represents ternary max-heap:


a) 11,16,18,19,20,25
b) 15,17,25,30,35,5
c) 20,4,2,5,7,1
d) 40,10,25,30,9,2
Step 5: Insert 82

Sol: d)
Explanation: Since, it is a ternary max-heap. So, it will have 3 children.
Option a):

Step 6: insert 100

It is not a max-heap. It is a min-heap.


Option b):

Here, node 25 is greater than node 15. Therefore, it is not a max-heap.


Tree

Tree

241 242
Chapter 5

Chapter 5
Option c): Step 2: Insert 2

Here, node 7 is greater than node 4. Step 3: Insert 10


\ It is not a max-heap.
Option d):

It is a max-heap.

Q3 Insert 7, 2, 10, 4 in that order in the ternary max heap as shown below:

Step 4: Insert 4

Sol. Step 1: Insert 7

Q4 Number of binary tree with 4 nodes A, B, C and D which is having preorder as


ABCD.
Tree

Tree

243 244
Chapter 5

Chapter 5
4, 12, 16, 27, 29, 34, 44, 50, 52, 65, 77, 88, 92, 93
Sol:
2n
Cn
Number of structure with ‘n’ nodes = Preorder: (Root, Left, Right)
(n + 1)
50, 27, 16, 4, 12, 34, 29, 44, 88, 65, 52, 77, 93, 92
8
C4
So, with 4 nodes, number of structure =
5
8!
= 4!4!
5

8 × 7 × 6 × 5 × 4!
=
4 !× 4 !× 5

8×7×6×5
= = 14
4 × 3 × 2 × 1× 5

Since, each structure will represent one particular preorder, i.e, ABCD.
So, 14 is the number of binary tree with 4 nodes A, B, C and D which is having
preorder as ABCD.

Q5 Given an inorder as 4, 12, 16, 27, 29, 34, 44, 50, 52, 65, 77, 88, 92, 93 and pre-
order as 50, 27, 16, 4, 12, 34, 29, 44, 88, 65, 52, 77, 93, 92 of binary tree. What
is postorder?
a) 12, 4, 16, 29, 44, 34, 27, 52, 77, 65, 92, 93, 88, 50
b) 29, 44, 16, 4, 12, 34, 27, 52, 77, 65, 92, 93, 50, 88
c) 12, 4, 16, 29, 34, 44, 27, 52, 77, 65, 92, 93, 88, 50
d) 12, 4, 16, 29, 44, 34, 27, 52, 77, 65, 92, 93, 50, 88

Sol: a)
Explanation:
Preorder: (Root,Left,Right)
Visit Root, Visit Left subtree, Visit Right subtree

Inorder: (Left,Root,Right)
Visit Left subtree, Visit Root, Visit Right subtree So, the postorder is 12, 4, 16, 29, 44, 34, 27, 52, 77, 65, 92, 93, 88, 50.
So, according to preorder, 50 is the root node. We just traversed the tree from top to down, left to right.
Whenever we visit a node for the last time, we print it.
Inorder: (Left, Root, Right)
Tree

Tree

245 246
Chapter 5

Chapter 5
Q6 Suppose the nodes in BST are given as:
Chapter Summary
50,30,80,20,48,60,90,10,15
What will be the Inorder traversal of BST:
a) 10,15,20,30,48,50,60,80,90 y Tree: It is an example of non-linear data structures.
b) 15,20,30,10,50,48,80,90,60 y Binary trees: A tree in every node is having atmost 2 children (i.e. either 0 child or
c) 10,15,20,30,50,48,60,80,90 1 child or 2 children).
d) None of above y Tree traversal:

Sol: a)
Explanation:
Inorder traversal of a binary search tree is in ascending order (Sorted order) of
key value of nodes.
So, a) is the correct option. y Operations on binary tree:

y Binary seach tree: It is used for searching.


y In BSTs, the element present in the left subtree should be less than the root
element, and the element present in the right subtree should be greater than the
root element.
y Operations on BSTs:

y AVL-tree: It is a height balanced-tree.


y Balance factor of each node in AVL tree must be either -1 or 0 or 1.
y Rotation in AVL tree:
Tree

Tree

247 248
6 Hashing

Chapter 5

Chapter 6
y Heap: It is a complete binary-tree, which takes O(log n) time to insert an element. HASHING TECHNIQUES
Introduction:
y Hashing is a searching technique.
y Hashing is an efficient searching technique in which the number of keys
that must be inspected before obtaining the desired one is minimised.
y Worst case, time complexity in case of hashing is expected as O(1).
y Basically, the motive behind hashing is to retrieve each key stored in
y HeapSort: the table in a single access.
y First swap the root element with the last element in the heap. y In hashing, the location of the record within the table depends only on
y After that decrease the heapsize by 1 the key resulting in retrieving each record in a single access. It does not
y Then perform max-heapify on the root. depend on the locations of other keys, as in a tree.

Hash table and hash function:


1) Hash table:
y Hash table is a table used to maintain keys in it.
y The most efficient way to organize such a table is as array.
y Each key is stored in the hash table at a specific offset from the
base address of the table.
y If the record keys are integers, the keys themselves can serve as
indices to the array, which is used as a table.
y An example of a hash table using an array:

Table 6.1

Hashing
Tree

249 PB 251
Chapter 6

Chapter 6
2) Hash function: (1, 3, 5, and 100058), a table of size 1,00,058 is needed, by doing so, a lot of
� A function which transforms a key into a table index is called as a space is wasted.
hash function. y To overcome the above situation, the hash function is used.
� Consider the situation where keys are supposed to be stored in a y Basically, the hash function is used to compress the keys which are to
hash table directly by their values, i.e., key ‘k‘ will be stored in the kth be stored in the hash table.
position only. y If ‘h’ is a hash function and the key is ‘k’, then h(k) is called the ‘hash
value of key’.
For example: y Let’s understand the hash function by taking h(k) = k%10, where ‘%’ is
⇒ keys are 1, 3, 5, and 8, so we need a table of size at least 8. let’s take it the modulo operator.
of size 10.
⇒ Each key will go to its corresponding location as shown below: Example:
Keys are 105, 110, 202, 403, 509
Since the hash function used is “key modulo 10”, so table size should be 10.

Table 6.2
The above hash table is called the direct address table.
y But suppose, if one key value is large enough, Table 6.3

For example:
Table size is 10 because after applying hash function h(k) = k%10, the last
keys are 1, 3, 5, and 100058.
digit of the key will decide the location/position where the key is to be
So, the minimum table size required is 1,00,058. Thus, for storing only 4 keys
stored. Thus, 0 to 9 digits are sufficient to store all the keys.
Hashing

Hashing
252 252 253 253
Chapter 6

Chapter 6
Detailed discussion on each of the hash
function types: Previous Years’ Question
1) Division modulo method:
y Division Modulo Method is used to
Given the following input (4322, 1334,
compress the key.
1471, 9679, 1989, 6171, 6173, 4199) and
y It uses function as, h(k) = k mod
the hash function x mod 10, which of
(hash_table_size).
the following statements are true?
y Obtained mod value of key with
i) 9679, 1989, 4199 hash to the same
hash_table_size gives the position
value
for the particular key to be stored at.
ii) 1471, 6171 hash to the same value
y For example: iii) All elements hash to the same
Hash table size is S = 1000 and hash index value
varies from 0 to 999. iv) Each element hashes to a different
Key = 5231119265 value
Now, the key is to be stored at? a) i only b) ii only
Mod value for the key: c) i and ii only d) iii or iv
h(k) = k mod (hash_table_size) Sol: c) (GATE CSE: 2004)
= (5231119265) mod 1000
= 265; It is the position where the key
will be stored.

Table 6.4

So, this table size is minimized by Table 6.5


compressing the given keys. Rack Your Brain
y It is the easiest method to create a hash function.
Types of hash functions: A hash function is given as h(k) = K%69, 2) Digit extraction method:
Different types of hash functions are: where K indicates a key and h(k) indicates y Digit Extraction Method is also used to compress the key.
1) Division Modulo Method hash function of the key. What should y In this method, selected digits are extracted from the key to be
2) Digit Extraction Method be the minimum size of hash table and stored and used as the position where key has to be stored.
3) Mid Square Method indices of a hash table? y For example:
4) Folding Method Suppose hash table addresses are from 0 to 999, means of size 1000
Hashing

Hashing
254 254 255 255
Chapter 6

Chapter 6
if keys are of 7 digits and are as follows: 5231119, Note:
4290386,
Digits can be extracted from any desired
1296043, position in the keys.

5930532, 3) Mid square method:


y It is used to compress the key, which
6080239, is supposed to be stored in the hash Grey Matter Alert!
According to the digit extraction method, we could extract digits from positions 2, 3 and 7 table.
from the keys. y In this method, we perform a square The Digit extraction method is also
operation on the key and take the called Truncation Method.
middle digits as addresses for keys.

y For example:
Suppose the key is 32165 and the size of
the hash table is 1000.
So, h(k) = (32165)2 plus take middle digits.

Table 6.7

Table 6.6
Hashing

Hashing
256 256 257 257
Chapter 6

Chapter 6
4) Folding method: Retrieval of keys from hash table:
y In this method, we select a number of digits from the key and Retrieval of keys means we need to find the location Previous Years’ Question
perform addition on digits and make it concise. of the key exactly where it is stored in the hash
y The value resulting from doing the above changes, acts as the table.
Which one of the following
address/position for the key to be stored in the hash table. So, depending on the type of hash functions, we
hash functions on integers will
search the keys. Let’s check one by one:
y For example: distribute keys most uniformly
Suppose the key to be stored is 321653533 and the size of the hash table 1) Division modulo method: over 10 buckets numbered 0 to
is 1000. The hash table size is 1000 (0 to 999). 9 for i ranging from 0 to 2020?
Since table indices vary from 0 to 999, we will take the number of digits The key is 5231119265 and we need to find a) h(i) = i2 mod 10
equal to 3 to fold from the key. its location. So, applying the division modulo b) h(i) = i3 mod 10
method, c) h(i) = (11*i2) mod 10
h (5231119265) = (5231119265) % (1000) d) h(i) = (12*i) mod 10
= 265; 265 is the address, for Sol: b)
the key.  (GATE CSE: 2015 (Set–2))
Thus, it just takes constant time to search a
key.
means O(1) in Every Case.

2) Digit extraction method:


Hash table size is 1000 (0 to 999). Key is 5231119, and we need to find
its location.
So, applying the digit extraction method,
Extract the digits from positions exactly which are used to store.

3) Mid square method:


Hash table size is 1000 (0 to 999)
The key is 32165, and we need to find its location.
So, applying mid square method,
h(32165) = (32165)2 plus taking middle digits

Table 6.8

Note:
4) Folding method:
The folding method and mid square method are better among Hash table size is 1000 (0 to 999)
all the methods. Since every digit is participating, it is difficult to The key is 321653533, and we need to find its location.
find other counter keys which generate the same address. So, applying the folding method in the same way,
Hashing

Hashing
258 258 259 259
Chapter 6

Chapter 6
SOLVED EXAMPLES

Q1 Given a hash table with 39 slots that store 250 keys. Calculate the load fac-

tor ‘µ’. (up to two decimal places)

Sol: 6.41
Given,
Number of slots = 39
Note: Number of keys = 250
Previous Years’ Question
For retrieval of keys by using any of the Number of keys 250

= µ = = 6.41 Ans.
above methods, constant time is needed Number of slots 39
Consider the following two statements:
i.e. O(1) Every Case.
i) A hash function (these are often
used for computing digital
signatures) is an injective function. Collision:
Load factor and collisions:
ii) encryption technique such as DES y After applying the hash function on keys
Load factor:
performs a permutation on the to map them to some address of the hash Previous Years’ Question
y The number of keys per slot of the hash
elements of its input alphabet. table, if more than one key are mapped to
table is called the load factor.
Which one of the following options the same address of the hash table, then Consider a hash function that distributes
y Load factor is denoted by ‘µ’.
is valid for the above two this phenomenon results in a collision. keys uniformly. The hash table size is
y Suppose the number of slots in the
statements? y In simple words, if one slot of hash table 20. After hashing of how many keys will
hash table equals to N and the number
a) Both are false is getting more than one key to be stored the probability that any new key hashed
of keys to be stored in the hash table
b) Statement (i) is true, and the other at it, it is called a collision. collides with an existing one exceed 0.5.
is x.
is false y Let’s understand it with an example: a) 5 b) 6
x c) Statement (ii) is true, and the other
y \ Number of keys per slot = c) 7 d) 10
N is false Sol: d) (GATE CSE: 2007)
This means the average number of keys per d) Both are true
x Sol: c) (GATE CSE: 2007)
slot =
N

∴ x
µ=
N Calculating hash indices for each key,
1) h(49) = 49 mod 10 = 9; this key will go to hash index 9 and so on.
2) h(26) = 26 mod 10 = 6
3) h(32) = 32 mod 10 = 2
4) h(59) = 59 mod 10 = 9
5) h(60) = 60 mod 10 = 0
6) h(29) = 29 mod 10 = 9
7) h(41) = 41 mod 10 = 1
Hashing

Hashing
260 260 261 261
Chapter 6

Chapter 6
Mapping each key to its location in the hash table, h(539) = 539 mod 10 = 9
h(510) = 510 mod 10 = 0
h(201) = 201 mod 10 = 1
h(202) = 202 mod 10 = 2
h(503) = 503 mod 10 = 3
h(405) = 405 mod 10 = 5
h(406) = 406 mod 10 = 6
h(911) = 911 mod 10 = 1
h(922) = 922 mod 10 = 2
h(906) = 906 mod 10 = 6

Mapping each key to its location in hash table,

Grey Matter Alert!

Hash collision is also termed a hash


clash.

Table 6.9

Q2 There is a hash table of size 10. Keys are given as 539, 510, 201, 202, 503,
405, 406, 911, 922, 906. Find if there is any collision present in the system
and if yes, find the number of collisions encountered. The division modulo
method is used as the hash function.

Sol: Yes, 3 collisions.


Calculating hash indices for each key:
Given the hash function used is the division modulo method. i) Yes, At index no. 1, 2, 6, collisions are present.
ii) Calculating no. of collisions:
Index 1 → One collision
Index 2 → One collision
Index 6 → One collision
\ Total no. of collisions = 3 Ans.
Hashing

Hashing
262 262 263 263
Chapter 6

Chapter 6
COLLISION RESOLUTION TECHNIQUES Mapping each key to its location in the hash table,
y Ideally, no two keys should be compressed into the same integer.
Unfortunately, such an ideal technique doesn’t exist.
So, here we will discuss some techniques which will resolve when
collisions are resulted, and leads us close to ideal.

y Basically, there are two techniques of dealing with hash collisions.


1) Chaining
2) Open addressing

1) Chaining:
y Chaining is a collision resolution technique.
y This technique resolves collision by building a linked list of all keys
hashed to the same hash index.
y Hash table is in the form of the array only, but it is the array of
pointers in this case.
y Keys are stored in nodes of a linked list.
y At a specific hash index, one address is stored that is of the first
node of the chain in the form of linked list.
y Applying chaining for some keys like:
Example: Keys are given as 49, 26, 32, 59, 69, 79, 60, 50, 30, 41, 61, 29
Table size is given as 10 (0 to 9).
Hash function, h(k) = k mod 10. Table 6.10

The collision resolution technique is chaining. y Array positions which don’t have any node to point to are set to Null pointer.

Calculating hash indices for each key, Advantages of chaining:


h(49) = 49 mod 10 = 9 y By applying chaining, a hash index can handle many keys in it. We can
h(26) = 26 mod 10 = 6 deal with the infinite number of collisions.
h(32) = 32 mod 10 = 2 y Any insertion of a key in hash table will take constant time, i.e. O(1)
h(59) = 59 mod 10 = 9 every case.
y Deletion in chaining is easy because we store keys in an unsorted
h(69) = 69 mod 10 = 9
manner, so deletion of one key doesn’t create trouble for other keys.
h(79) = 79 mod 10 = 9
h(60) = 60 mod 10 = 0 Disadvantages of chaining:
h(50) = 50 mod 10 = 0 y Deletion and searching in chaining takes O(n) time complexity.
y Consider the previous instance,
h(30) = 30 mod 10 = 0
h(41) = 41 mod 10 = 1
h(61) = 61 mod 10 = 1
h(29) = 29 mod 10 = 9
Hashing

Hashing
264 264 265 265
Chapter 6

Chapter 6
h(10) = 10 mod 10 = 0
h(53) = 53 mod 10 = 3
h(62) = 62 mod 10 = 2
h(57) = 57 mod 10 = 7
h(50) = 50 mod 10 = 0
h(11) = 11 mod 10 = 1

Table 6.11

we can analyse clearly that even though many slots are empty but we can’t
store keys in them, and forcefully, we need to take space outside the array.
Chaining has a drawback in terms of space consumption.

Load factor:
Load factor for chaining; µ ≥ 0
It means keys per slot is 0 or any positive integer.
One slot can point to 0–key, 1–key or more than one key.

SOLVED EXAMPLES

Q3 Consider a hash table with 10 slots. The hash function used is h(k) = k mod 10.
The collisions are resolved by using chaining. Keys 25, 68, 29, 35, 10, 53, 62,
57, 50, 11 are inserted in the given order. The maximum, minimum and average
chain lengths for the hash table, respectively are: __________

Sol: 2, 0, 1.
Calculating hash indices for each key.
h(25) = 25 mod 10 = 5
h(68) = 68 mod 10 = 8 \ Maximum chain length = 2
h(29) = 29 mod 10 = 9 Minimum chain length = 0
h(35) = 35 mod 10 = 5 2+1+1+1+0+2+0+1+1+1 10
Average chain length = = = 1
10 10
Hashing

Hashing
So, 2, 0, 1 Ans.
266 266 267 267
Chapter 6

Chapter 6
1 1 9 1 1 1
Q4 Consider a hash table with 10 slots which uses chaining for collision reso- =3 × × ×
10 10 10
× 10 + 1 × × ×
10 10 10
× 10
lution. Assuming the table is initially empty and considering simple uniform Previous Years’ Question
hashing. What would be the probability that after 3 keys are inserted, a
3×9 1
chain of size at least 2 results? = + An advantage of a chained hash table
10 × 10 10 × 10
(external hashing) over the open
27 1 addressing scheme is
Sol: 0.28 = +
100 100 a) Worst-case complexity of search
y We have given Table size = 10 operations are less
y Collision resolution technique – chaining 28 b) Space used is less
Table is empty initially =
y 100 c) Deletion is easier
y Simple uniform hashing d) None of the above
y 3 keys are inserted = 0.28 Ans. Sol: c) (GATE CSE: 1996)
y Simple uniform hashing – It evenly distributes elements into the slots of a
hash table.
Probability of a chain of size atleast 2
= [Probability of a chain of size exactly 2]
+ [Probability of a chain of size exactly 3]
2) Open addressing:
 1 1 9  1 1 1  y Open addressing is also a collision resolution technique like chaining.
= 10 ×  3C2 × × × + 10 ×  3C3 × × ×
 10 10 10   10 10 10  y Only a hash table is used to store keys, unlike chaining.
y In open addressing, at any point, the size of the table must be
greater than or equal to the total number of keys.

yThere are three types of open addressing, considering the way the
collisions are resolved:
   i) Linear Probing
   ii) Quadratic Probing
   iii) Double Hashing

Let’s understand each of the above three types of open addressing one by
one:

i) Linear probing:
y In this method, if some collision arises, then to resolve it, we probe
next slot to store the key linear.
y Linear_Probing(k, i) = (h(k) + i) mod S
∀ i ∈ {0, 1, 2, 3, ........, S–1}
Where k is a key
h(k) is the hash of key ‘k’.
i is iteration number or collision number.
S is the size of the hash table.
Hashing

Hashing
268 268 269 269
Chapter 6

Chapter 6
y When any collision is encountered for some key, the above definition
1) 1)
is applied to resolve the collision according to Linear Probing.
2) 2)
y Let’s understand linear probing with an example: 3) 3)

Insert keys one by one into the hash table.


Initially, table is empty:



Table 6.13 Table 6.13

So, applying So,


linear
applying
probing,
linear probing,
h(k) = 36 modh(k)
10 == 36
6 mod 10 = 6
Linear probing
Linear
(k, i)probing
= (h(k) +(k,i) i)mod
= (h(k)
S + i) mod S

         

Now, key–36Now,
will go
key–36
to position
will go‘7’.
to position ‘7’.

4) 4)
5) 5)

6) 6)

7) h(70) = 707)mod
h(70)
10 == 70
0→ mod
1 – 10
Collision
= 0 → 1 – Collision
Table 6.12 Linear Probing
Linear
(k, 1)Probing
= (0 + (k,
1) mod
1) = 10
(0 + 1) mod 10
Hashing

Hashing
270 270 271 271
Chapter 6

Chapter 6
= 1 mod 10 = 1 → 1 – Collision again y Because of primary clustering, the
Linear probing (k, 2) = (0 + 2) mod 10 average searching time increases. Grey Matter Alert!
= 2 mod 10 = 2 → 1 – Collision again y Linear probing is suffering from primary
Linear probing (k, 3) = (0 + 3) mod 10 clustering. Linear probing is also called closed
hashing.

8) h(80) = 80 mod 10 = 0 → 1 – Collision


Linear probing (k, 1) = (0 + 1) mod 10
= 1 mod 10 = 1 → 1 – Collision again
Q5 Consider a hash table with 8 buckets which use linear probing to resolve col-
lisions. The key values are integers, and hash function used is key mod 8. If
Linear probing (k, 2) = (0 + 2) mod 10 the keys 55, 133, 50, 123, 62 are inserted in the hash table in order then what
= 2 mod 10 = 2 → 1 – Collision again is the index of the key 62?
Linear probing (k, 3) = (0 + 3) mod 10
= 3 mod 10 = 3 → 1 – Collision again
Linear probing (k, 4) = (0 + 4) mod 10 Sol: 0th position of the hash table.
Given, Number of buckets/slots = 8
Means hash table size is 8. (0 to 7)
Now, all keys are stored by following linear probing. Hash function = key% 8, (% = mod)
Collision resolution technique = Linear probing.
Advantages of linear probing:
The main advantage of linear probing is, the each position/the hash index Mapping keys to their location one by one:
of the hash table can be used to store keys. 1) h(55) = 55% 8 = 7 2) h(133) = 133% 8 = 5
3) h(50) = 50% 8 = 2 4) h(123) = 123% 8 = 3
Disadvantages of linear probing: 5) h(63) = 63% 8 = 7 – 1 collision
i) While probing, each slot is being checked to get to store some key at
Applying linear probing,
it, so it will take more time by probing slowly.
ii) In linear probing, we can probe only in the manner of one slot after h(63) = 63% 8 = 7
another. Linear probing (k, i) = (h(k) + i) mod 8
iii) Deletion is difficult because once we delete some key, it creates ⇒ Linear probing (k, 1) = (7 + 1) mod 8 = 8 mod 8 = 0
trouble for other keys while any search is performed. Resulting hash table is:

Time complexity:
i) Searching : O(1) [Best case]
O(S) [Worst case], [Average Case]
ii) Insertion : O(1) [Best case]
O(S) [Worst case], [Average Case]
iii) Deletion : O(1) [Best case]
O(S) [Worst case], [Average Case]

Primary clustering:
y In case two keys have the same start of the hash index, then while
performing linear probing to resolve the collision, both will follow the
same path in a linear manner, which is unnecessary. It is called primary
Hashing

Hashing
clustering.

272 272 273 273


Chapter 6

Chapter 6
So, key–63 would be inserted at location ‘0’ 3) Double hashing:
of the hash table. Previous Years’ Question y In this method, if some collision arises then to resolve the collision,
a second hash function to key is applied.
2) Quadratic probing:
y Double_hashing(k, i) = (hash_1(k) + i × hash_2(k)) mod S
y In this method, if some collision Consider a hash table of size seven,
∀ i ∈ {0, 1, 2, 3, ........, S–1}
arises, then to resolve the collision, with starting index zero, and a hash
we probe the next slot to store the function (3x + 4) mod 7. Assuming the Where k is a key.
key in a quadratic manner. hash table is initially empty, which of hash 1 (k) is first hash function which is applied to the key.
y Quadratic_probing(k, i) = (h(k) + i2) the following are the contents of the hash 2 (k) is second hash function which is applied to the key.
mod S table when the sequence 1, 3, 8, 10 is i is the iteration number or collision number.
inserted into the table using closed S is the size of the hash table.
∀ i ∈ {0, 1, 2, 3, ........, S–1}
hashing? Note that ‘_’ denotes an y First hash function is: hash1 (k) = k mod S.
Where k is a key. y Second hash function can be taken in many ways by keeping in
empty location in the table.
h(k) is the hash of key ‘k’. mind the goodness of the hash function.
a) 8, _, _, _, _, _ , 10
i is iteration number or collision number. y Good second hash function must never evaluate to zero and must
b) 1, 8, 10, _, _, _, 3
S is the size of the hash table. make sure all cells should be covered while probing.
c) 1, _, _, _, _, _, 3
y When any collision is encountered y When any collision is encountered for some key, the above definition
d) 1, 10, 8, _, _, _, 3
for some key, the above definition is applied to resolve the collisions according to the double hashing
Sol: b) (GATE CSE: 2007)
is applied to resolve the collision definition.
according to quadratic probing. y In double hashing, we can probe every slot of the hash table for
y In quadratic probing, we probe some key randomly, but every slot should be covered.
one slot after another in quadratic y Double_hashing doesn’t have the problem of primary clustering or
manner. secondary clustering.
Time complexity: Previous Years’ Question
i) Searching : O(1) [Best case] Comparison between open addressing and chaining:
O(S) [Worst case], [Average Case]
Consider a hash table of size 11 that uses
ii) Insertion : O(1) [Best case] i) Open addressing scheme is
open addressing with linear probing. Let
O(S) [Worst case], [Average Case] complex in consideration of i) Chaining is simple.
h(k) = k mod 11 be the hash function
iii) Deletion : O(1) [Best case] computational work.
used. A sequence of records with keys
O(S) [Worst case], [Average Case]
43 36 92 87 11 4 71 13 14 is inserted ii) We need extra space outside
into an initially empty hash table, ii) All the keys are stored only
y Quadratic probing faces the problem the hash table to store the
in the table.
of secondary clustering. the bins of which are indexed from keys.
zero to ten. What is the index of
Secondary clustering: iii) In open addressing, a key iii) When keys are not mapped
the bin into which the last record
y In case two keys have the same start of can be sent to a slot which to a slot, then the slot space
is inserted?
the hash index of the hash table while is not mapped for the key. is wasted.
a) 2 b) 4
performing quadratic probing to resolve the
c) 6 d) 7
collision, both will follow the same path in iv) Open addressing schemes
Sol: d) (GATE CSE: 2008) have problems like primary iv) There is nothing like
quadratic manner, which is unnecessary.
It is called secondary clustering. clustering and secondary clustering in chaining.
clustering.
y Because of secondary clustering average
searching time for a key increases.
Hashing

Hashing
274 274 275 275
Chapter 6

v) In chaining, hash table never


v) Hash table may become full. fill up, we can only add keys
to chain.

vi) It provides better cache vi) Cache performing in


performance. chaining is not good.

y Hashing is a searching technique.


y Basically, the motive behind hashing is to retrieve each key stored in
hash table in single access.
y Worst case searching time in case of hashing is expected O(1) [Every
Case].
y Hash table is used to maintain keys in it.

Chapter Summary

y A function which transforms a key into a table index is called as hash function.
y Types of hash functions:
1) Division modulo method
2) Digit extraction method
3) Mid square method
4) Folding method
y Load Factor: Number of keys per slot of the hash table is called load factor.
x
(µ ) ; µ = , x: Number of keys, N: Number of slots.
N
y Collision: In simple words, if one slot of hash table is getting more than one key to
be stored in it, is called as collision.
y Collision Resolution Techniques:
Techniques which can resolve collision, basically are of two kinds.
i) Chaining:
This technique resolves collision by building a linked list of all keys hashed to
the same hash index.
ii) Open addressing:
Open addressing scheme works in three ways.
a) Linear Probing: Probes the slots lineary to store the keys.
b) Quadratic Probing: Probes the slots quadratically to store the keys.
c) Double Hashing: Covers all the slots randomly to store a key and so on.
Hashing

276 PB

You might also like