Unacademy - Programming & Data Structures
Unacademy - Programming & Data Structures
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
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
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:
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”
C LANGUAGE
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
Table 1.5
1)
2)
3)
4)
5)
E 6)
7)
8)
9)
10)
11)
Table 1.6
C LANGUAGE
C LANGUAGE
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.
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.
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
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
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
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
19 20
Chapter 1
Chapter 1
AND operator (&&): Compound assignment operator:
y Binary operator represented as &&. y Combination of arithmetic operator with assignment operator.
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 ––.
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
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
23 24
Chapter 1
Chapter 1
Rack Your Brain
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
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
Increase the
readability of
code
Fig. 1.21
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.
}
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
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
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.
while(1) for (; ;) do
{...................... {...................... {......................
..................... ..................... .....................
} } } while (1);
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:
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.
Example:
Void Function Non-void Function
void func() return_datatype func()
{ { return value;
} }
C LANGUAGE
3) Character variable
4) Function call returning a value.
45 46
Chapter 1
Chapter 1
Types of functions:
B
F
F
F
include
a)
b)
c)
Fig. 1.33 Types of Functions
d)
Sol: c) ( )
1) M
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
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
C LANGUAGE
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
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); }
1)
2)
3)
4)
5)
6)
7)
C LANGUAGE
C LANGUAGE
8)
Table 1.23
57 58
Chapter 1
Chapter 1
!
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)
61 62
Chapter 1
Chapter 1
a)
b)
c)
d)
Sol: d) ( )
Sol:
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
!
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
C LANGUAGE
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
Table 1.27
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:
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
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
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.
Sol: ( )
Example:
int *fun()
{int x = 5;
int *p = &x;
return p;
}
main()
{int *ptr;
ptr = fun();
–––––
–––––
–––––
}
C LANGUAGE
C LANGUAGE
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
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:
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: , .
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
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;
a) b) c) d)
Sol: a) ( )
C LANGUAGE
C LANGUAGE
89 90
Chapter 1
Chapter 1
Chapter Summary
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
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.
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
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
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
Column–major order
Let any array A[m][n] then address of element A[i][j] in column major order
is given by:
Array
109 110
Chapter 2
Chapter 2
Similarly: A[L1.....U1] [L2.......U2]
then:
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.
Array
113 114
Chapter 2
Chapter 2
Column Major Order (CMO) j ( j − 1)
b + ( i − 1 ) + *w
2
�
Range n (n − 1 )
2 �
�
Row Major Order (RMO) (i − 1) (i − 2 )
b + ( j − 1 ) + *w
2
Range 3n–2
�
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
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
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)]}
( ( ( (( ) ( )) ) ) ( ))
{[ ] (}{( ) ) 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
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)
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
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
125 126
Chapter 3
Chapter 3
S.no Symbol Postfix string Operator-stack S. no Symbol Postfix string Operator-stack
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
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
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)
1) 8 8
2) 2 82
3) 6 826
4) * 2 6 12 8 12
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.
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);
}
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
Note:
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
Fig. 3.19
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.
141 142
Chapter 3
Chapter 3
Thus, by descending the priority queue, we get items from the queue in
descending order.
Rack Your Brain
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 ;
}
{ }
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
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
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 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:
We need to delete first item, second item and so on from stack which
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
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
Fig. 3.35
� Code for enqueue operation:
V = V → next ;
{
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
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:
Fig. 4.1
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’.
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 ;
}
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
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.
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
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
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 ;
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
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
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:
y Linked list is a flexible data structure as we can insert any number of nodes and
delete nodes from the given linked list.
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
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.8
Tree
183 184
Chapter 5
Chapter 5
Example: Properties of complete binary trees:
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.
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:
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:
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.
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.
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)
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:
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.
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).
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.
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:
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)
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.
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)
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.
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.
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
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
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:
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).
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)
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
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)
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) .
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).
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.
Note: In the below example of heapsort, we have already built the heap.
Tree
Tree
237 238
Chapter 5
Chapter 5
Previous Years’ Question
Sol: b)
SOLVED EXAMPLES
Tree
239 240
Chapter 5
Chapter 5
Step 3: Insert 99 Step 7: Insert 102
Step 4: Insert 98
Sol: d)
Explanation: Since, it is a ternary max-heap. So, it will have 3 children.
Option a):
Tree
241 242
Chapter 5
Chapter 5
Option c): Step 2: Insert 2
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
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:
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.
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
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.
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.
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-
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
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.
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.
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.
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)
Table 6.13 Table 6.13
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.
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.
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
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