0% found this document useful (0 votes)
7 views

DSA TillL21 Merged

Uploaded by

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

DSA TillL21 Merged

Uploaded by

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

BITS F232: FOUNDATIONS OF DATA STRUCTURES

& ALGORITHMS Chittaranjan Hota, PhD


Professor of Computer Sc.
(1 ST SEMESTER 2022-23) BITS-Pilani Hyderabad Campus
hota[AT]hyderabad.bits-pilani.ac.in
INTRODUCTION
COURSE CONTENT & COURSE ADMINISTRATION
•Introduction to OOP and C++
•Implementing elementary and common data structures
•Implementing advanced data structures
•Understanding algorithmic techniques to solve complex
computational problems

Consultation Hour: Thursday (5 to 6pm) Course Material: Google class


WHY STUDY DSA?
BITS F232: FOUNDATIONS OF DATA STRUCTURES
& ALGORITHMS Chittaranjan Hota, PhD
Professor of Computer Sc.
(1 ST SEMESTER 2022-23) BITS-Pilani Hyderabad Campus
hota[AT]hyderabad.bits-pilani.ac.in
THE PROBLEM SOLVING PROCESS
JOURNEY OF A DSA ENGINEER

Mathematical Abstract Data


Data Structures AB,AC,AD,BA,DC,ED
Model Types BC,BD,EA
DA,DB
EB,EC
Informal Algorithm Pseudo-language
C++ Program
Program
OBJECT-ORIENTED DESIGN GOALS

What is Object-Oriented Design?


•Style of writing computer programs using objects, and their interactions. (Minor
degree admissions at BITS, Hyderabad)
BITS F232: FOUNDATIONS OF DATA STRUCTURES
& ALGORITHMS Chittaranjan Hota, PhD
Professor of Computer Sc.
(1 ST SEMESTER 2022-23) BITS-Pilani Hyderabad Campus
hota[AT]hyderabad.bits-pilani.ac.in
OOP FUNDAMENTALS OVERVIEW
INHERITANCE EXS.

Source: https://www.geeksforgeeks.org/
POLYMORPHISM: FUNCTION OVERLOADING
OPERATOR OVERLOADING
#include <iostream> #include <iostream>
using namespace std;
using namespace std;
class employee {
int main() { public:
int a = 45; int empno;
float salary;
int b;
};
b = a; //a variable to another int main() {
cout << “value of b: “ <<b; employee e1= {123, 60000.50}, e2;
e2 = e1; // assign an object to another
return 0;
cout << e1.empno << ‘\t’ << e2.salary;
} return 0;
}
FUNCTION OVERRIDING
It is the redefinition of base class function in its derived class with same signature.

A C++ virtual function is a member function in the base class that you redefine in a derived
class.
FRIEND CLASS
 A friend class is a class whose members have
access to the private members of another
class.

 Rectangle is a friend of Square allowing


Rectangle’s member functions to access
private members of Square.

 Friendship is not transitive.


BITS F232: FOUNDATIONS OF DATA STRUCTURES
& ALGORITHMS Chittaranjan Hota, PhD
Professor of Computer Sc.
(1 ST SEMESTER 2022-23) BITS-Pilani Hyderabad Campus
hota[AT]hyderabad.bits-pilani.ac.in
OOP FUNDAMENTALS CONTINUED…
PROTECTED ACCESS
SPECIFIER
ABSTRACT CLASSES IN C++
TEMPLATES IN C++
Class template
Function template

https://www.geeksforgeeks.org/
STANDARD TEMPLATE LIBRARY (STL) IN C++
•A set of powerful template classes that implement many
popular and commonly used data structures and
algorithms like vector, list, queue, deque, priority_queue,
stacks, substr, replace, sorting, searching etc.
•Alternatively, it is a library of container classes,
algorithms, and iterators.

STL on strings:
insert, append, swap, size,
resize, reverse etc.

Output

What are the STL functions used in this code?


BITS F232: FOUNDATIONS OF DATA STRUCTURES
& ALGORITHMS Chittaranjan Hota, PhD

(1 ST SEMESTER 2022-23) Professor of Computer Sc.


BITS-Pilani Hyderabad Campus

ELEMENTARY DATA STRUCTURES: ARRAYS hota[AT]hyderabad.bits-pilani.ac.in

© Chittaranjan Hota, BITS Hyderabad


ARRAYS: AN EXAMPLE
Lab-3 next week

deleteItemFromIndex(Index)
shrink()
IMPLEMENTATION: STORING GAME ENTRIES
class GameEntry { class Scores {
public: public:
GameEntry ( const string &n = " ", int s = 0); Scores(int maxEnt = 10);
string getName() const; ~Scores();
int getScore() const; void add(const GameEntry &e);
private: GameEntry remove(int i) ;
string name; void printAllScores();
int score; private:
}; int maxEntries; //maximum number of entries
int numEntries; //actual number of entries
( A Class representing a Game entry)
GameEntry *entries;
}; ( A Class for storing Game scores)
GameEntry::GameEntry(const string &n, int s) : name(n),
score(s) { } Scores::Scores(int maxEnt) {
string GameEntry::getName() const { return name; } maxEntries = maxEnt; // save the max size
int GameEntry::getScore() const { return score; } entries = new GameEntry[maxEntries];
numEntries = 0;
( Constructor and member functions) } Scores::~Scores() { delete[] entries; }
INSERTING INTO AND DELETING FROM ARRAY
void Scores::add(const GameEntry &e) { GameEntry Scores::remove(int i)
int newScore = e.getScore(); // score to add {
if (numEntries == maxEntries) { // the array is full if ((i < 0) || (i >= numEntries)) //
if (newScore <= entries[maxEntries - 1].getScore()) invalid index
return; // not high enough - ignore throw("IndexOutOfBounds -
} Invalid index");
else numEntries++; // if not full, one more entry GameEntry e = entries[i]; // save
the removed object
int i = numEntries - 2; // start with the next to last
for (int j = i + 1; j < numEntries; j++)
while (i >= 0 && newScore > entries[i].getScore() ) {
entries[j - 1] = entries[j]; // shift
entries[i + 1] = entries[i]; // shift right if smaller
entries left
i--;
} numEntries--; // one fewer
entries[i + 1] = e; // put e in the empty spot entry
} return e; // return the
removed object
}
(Inserting a Game entry object) (Removing a Game entry object)
More sorting & searching algos later…

SORTING & SEARCHING IN AN ARRAY


void Dynamic1DArray ::sort() int Dynamic1DArray
{ ::binarySearch(const int item)
for (int j = 1; j < size; j++) {
{ int low = 0, high = size - 1;
while (low <= high){
int key = arr[j]; int mid = low + ((high –
int i = j - 1; low) >> 1);
while (i > -1 && arr[i]>key) if (item == arr[mid])
{ return mid;
arr[i + 1] = arr[i]; if (item < arr[mid])
i = i - 1; high = mid - 1;
} else
arr[i + 1] = key; low = mid + 1;
} }
return -1; }
}
(Insertion Sort) (Binary Search)
MULTI-DIMENSIONAL ARRAYS
Month
0 1 2 3 4 5 6 7 8 9 10 11 Arrays in C++ are one-dimensional.
0 30 40 75 95 130 220 210 185 135 80 40 45 However, we can define a 2D array
1 25 25 80 75 115 270 200 165 85 5 10 16 as “an array of arrays”.
Year

2 35 45 90 80 100 205 135 140 170 75 60 95


3 30 40 70 70 90 180 180 210 145 35 85 80
4 30 35 40 90 150 230 305 295 60 95 80 30
int x[2][3][4]={ { {0,5,2,4}, {4,5,6,7}, {8,9,1,4} },
Average Yearly Rainfall (in mm) { {10,13,1,15}, {6,7,8,9}, {2,2,13,12} } };

3-dimensional

Img. Source: https://www.geeksforgeeks.org/


BITS F232: FOUNDATIONS OF DATA STRUCTURES
& ALGORITHMS Chittaranjan Hota, PhD

(1 ST SEMESTER 2022-23) Professor of Computer Sc.


BITS-Pilani Hyderabad Campus

LINKED LISTS hota[AT]hyderabad.bits-pilani.ac.in

© Chittaranjan Hota, BITS Hyderabad


SINGLY LINKED LISTS
next
•Recap: What are the drawbacks of Arrays?
•What is a linked list?
element
(A node)

Head Tail

68.5 40.0 82.5


(A linked list with mid-sem scores)

Applications: Implementation of Stacks, queues, Graphs (Adj list), Keep track OS process states, Free memory blocks
IMPLEMENTING A SINGLY LINKED LIST
Step 1: Define a class for the Node Step 3: Define a set of member functions for the
class StringNode { Linked list class defined in Step 2
//let us write it up…
StringLinkedList::StringLinkedList() : head(NULL){ }
StringLinkedList::~StringLinkedList() {
};
Step 2: Define a class for the Linked list while(!empty())
class StringLinkedList { // let us write it up...
public: StringLinkedList(); }
~StringLinkedList(); bool StringLinkedList::empty() const {
bool empty() const; return ???;
const string& front() const; }
void addFront(const string& e);
void removeFront(); const string& StringLinkedList::front() const {
private: StringNode* head; return ???;
}; }
INSERTING & REMOVING AT THE HEAD OF LINKED
LIST
1. Create a new node
2. Store data into this node
3. Have new node point to old head X Goa
Dubai
4. Update head to point to new node
void StringLinkedList::addFront(const string& e)
{ Pilani
StringNode* v = ??? StringNode; Inserting at the head
v->elem = e;
v->??? = head; void StringLinkedList::removeFront()
head = ???; {
} StringNode* old = head;
head = old->next;
1. Save old head
delete old; 2. Advance head to the next
} node
Deleting at the head
3. Delete the old head node
INSERTING AT THE TAIL & INSIDE A LINKED LIST
1. Allocate a new node
2. Insert new element (Hyd)
Pilani Dubai Goa 
3. Have new node point to null (v->next
= NULL)
4. Have old last node point to new node 

(last_node -> next = v)

Head Hyd 
Pilani Dubai Goa 

void insertAfter(Node* prev_node, int new_data)


Node* new_node = new Node();
new_node->data = new_data;
Head Hyd new_node->next = prev_node->next;
 prev_node->next = new_node;
DELETING THE LAST NODE
Algorithm:
1. If (headNode == null) //if the first node is null
then return null
2. If (headNode.next == null) //if there is only one node
then free head and return null
3. while secondLast.next.next != null //traverse till secondLast
secondLast = secondLast.nextNode
4. Delete last node and set the pointer of secondLast to null.

Img. Source: https://www.geeksforgeeks.org/


STACK & QUEUE AS SINGLY LINKED LISTS
?
nodes nodes
? ?
 
elements elements

Stack: How can you implement a stack Queue: How can you implement a queue as a
as a linked list? linked list?

Implementation in later chapters…


GENERIC SINGLY LINKED LISTS: USING TEMPLATES
BITS F232: FOUNDATIONS OF DATA STRUCTURES
& ALGORITHMS Chittaranjan Hota, PhD

(1 ST SEMESTER 2022-23) Professor of Computer Sc.


BITS-Pilani Hyderabad Campus

LINKED LISTS CONTINUED… hota[AT]hyderabad.bits-pilani.ac.in

© Chittaranjan Hota, BITS Hyderabad


DOUBLY LINKED LIST
•Deleting the last node in a singly linked list is not efficient. Applications:
Why? (rather any node other than first one or two) • Used by browsers for
•What is a doubly linked list? what functionality?
• Used to implement MRU,
•Insertions and deletions are more efficient. and LRU caches?
• Undo/ Redo functionality
typedef string Elem; in Word.
class DNode { • Used to implement hash
private: Elem elem;
tables, stacks, binary tree
DNode* prev;
DNode* next;
etc.
friend class DLinkedList;
};
(Implementation of DLL Node)
INSERTING INTO DOUBLY-LINKED LIST
header u p trailer
X
X

Algorithm insert(p, e): //insert e


before p
Let us write the pseudo code in
v
parallel…
REMOVING A NODE IN DOUBLY-LINKED LIST

Algorithm remove (p: position ) {


header p trailer
if (p->previous != nil) // not first X
X
p->previous->next = ???;
if (p->next != nil) //not the last
p->next->previous = ???;
}
REVERSING A DOUBLY-LINKED LIST
void listReverse(DLinkedList& L) {

DLinkedList T; // temporary list


while (!L.empty()) { // reverse L into T
string s = L.front(); L.removeFront();
T.addFront(s);
}
while (!T.empty()) { // copy T back to L
string s = T.front();
T.removeFront();
L.addBack(s);
}
} Lab 4 Next Week }
MIDDLE NODE OF A LINKED LIST

Lab 4 Next Week

Can you not find out a


loop using this algo?
BITS F232: FOUNDATIONS OF DATA STRUCTURES
& ALGORITHMS Chittaranjan Hota, PhD

(1 ST SEMESTER 2022-23) Professor of Computer Sc.


BITS-Pilani Hyderabad Campus

LINKED LISTS CONTINUED… hota[AT]hyderabad.bits-pilani.ac.in

© Chittaranjan Hota, BITS Hyderabad


SWAPPING TWO NODES IN A LINKED LIST

Lab 4 Next
Week
CIRCULAR LINKED LISTS
•A circular linked list is a singly-linked list except for the last element
of the list pointing to the first. Without starting over we can go
back to the first.
•What is the need of cursor node?

What are the front and the back elements?


RECURSION: ELEGANT WAY FOR REPETITIVE TASKS
Recursion: When a function or a method calls itself. A set of problems can be solved easily using
recursion (a powerful programming tool).

Source: https://www.byte-by-byte.com/ Quick sort https://abetterscientist.wordpress.com/


WHY USE RECURSION? • How do you find length of a Linked list?
• How do you search an element in a linked
do { list?

int i, counter = 0;
Amit
for (i=0; i<6; i++) {
if (person.manager == employeeName) {
counter ++;
counter=counter+countEmployeeUnder(person.name) Raj Deb
}
}
return counter;
} Ragini Rohit Sachin

(Recursion to find out how many people work under, say Amit) (Business Organization Chart)
RECURSION: ENGLISH RULER EXAMPLE

drawTicks (length-1)

Draw tick of given length

drawTicks (length-1)
• An interval with a central tick length L >1 consists of:
• An interval with a central tick length L-1
• An single tick of length L
• An interval with a central tick length L-1
BITS F232: FOUNDATIONS OF DATA STRUCTURES
& ALGORITHMS Chittaranjan Hota, PhD

(1 ST SEMESTER 2022-23) Professor of Computer Sc.


BITS-Pilani Hyderabad Campus

RECURSION CONTINUED… hota[AT]hyderabad.bits-pilani.ac.in

© Chittaranjan Hota, BITS Hyderabad


LINEAR RECURSION & TAIL RECURSION
•What is linear recursion?

tail recursion

Let us see the recursion trace…


BINARY RECURSION
• What is binary recursion?

Problem: add all the numbers in an integer array A:


Algorithm BinarySum(A, i, n):
Input: An array A and integers i and n
Output: The sum of the n integers in A starting at index i
if n = 1 then
return A[i ];
return BinarySum(A, i, n/ 2) + BinarySum(A, i + n/ 2, n/ 2)

• What are those two calls? Let us see the recursion trace…
COMPUTING FIBONACCI NUMBERS
nk denote the number of calls
performed in the execution of fib(k)

Is binary recursion better here? Let us draw the tree too… What is the type of this rec.?
WHAT IS COMPLEXITY & HOW IMPORTANT IS IT?

Img. Source: https://storyweaver.org.in/

• To find out square of a number (n), will you prefer multiplication or repeated addition?

• You want to look for a word in a dictionary that has every word sorted alphabetically. How
many algorithms are there and which one will you prefer?
for ( i=1; i < c; i++) {
FUNCTIONS FOR ALGORITHM ANALYSIS }
a = a + 1;

complexity?
•The Constant function: Output is same i.e. independent of input. It characterizes the number of
steps needed to do a basic operation on a computer like, what types of operations? Constant
algorithm does not depend on the input size.

What about f(w) = w2 ? Is it a constant function? Which ones are not constant
functions?

n = 1,2, 3… y=c f(n) =3


y=f(n) = c

We will use g(n) = 1 as the constant function.


BITS F232: FOUNDATIONS OF DATA STRUCTURES
& ALGORITHMS Chittaranjan Hota, PhD

(1 ST SEMESTER 2022-23) Professor of Computer Sc.


BITS-Pilani Hyderabad Campus

ALGORITHM COMPLEXITY hota[AT]hyderabad.bits-pilani.ac.in

© Chittaranjan Hota, BITS Hyderabad


LOGARITHMIC FUNCTION
•Heavily used in Analysis of algorithms. Is it an inverse function?
• g(n) = log(n) 1. log(nm) = log n + log m
2. log (n/m) = log n – log m

Rules:
3. Log (nr) = r log n (Img. source: wiki)
x = logbn if and only if ? 4. Loga n = logb n/logb a
•The reason why it is used with base 2 most, a common operation in many while (low <= high) {
algorithms is to repeatedly divide the input into half, integers are also
stored in binary etc. mid = (low + high)/ 2;
Many programs require an encoding for a collection of objects. What is if (target < list [mid])
high = mid - 1;
the minimum number of bits needed to represent ‘n’ distinct code values?
else if (target > list [mid])
for (i = n; i > 0; i = i/2 { low = mid + 1;
a = a +1; else break;
1000 codes to store  ?
} complexity? } complexity?
LINEAR AND N-LOG-N FUNCTIONS void quicksort(list[], left, right) {
int pivot = partition(list, left, right);
quicksort(list, left, pivot-1);
quicksort(list, pivot+1, right);
Linear Function: Given an input value ‘n’, the linear function g (n) } complexity? ?
assigns the value ‘n’ itself. g(n) = ?
Used in Algorithm analysis when we have to do a single basic
operation for each of ‘n’ elements. g(n) = n
Ex: Let us see some examples…
N-log-N Function: A function that assigns to an input ‘n’ the value of
‘n’ times logarithm base 2 of ‘n’. g(n) = nlog2n.
This function grows a little ??? than the linear function and a ???
than the quadratic function.
g(n) = n log n
How is it used in Algorithms?
for ( i = 0; i < n; i++) {
a = a+1;
} complexity?
QUADRATIC FUNCTIONS
Quadratic: Given an input value ‘n’,
the function ‘g’ assigns the product V
of ‘n’ with itself. Also, called ‘n I
squared’. S
U
Used in analysing algorithms where A
nested loops are used. L
I
Z
g(n) = n2 A
T
I
O
N
CUBIC AND POLYNOMIAL FUNCTIONS g(n) = n3

Cubic: It assigns to an input value ‘n’ the product of ‘n’ with


itself three times. g(n) = n3. 1E+29
Cubic
1E+27
1E+25
Quadratic
1E+23
1E+21 Linear
1E+19

T(n)
1E+17
1E+15
No of 1E+13
1E+11
multiplications? 1E+9
1E+7
1E+5
1E+3
1E+1
1E-1
1E-1 1E+2 1E+5 1E+8

Interestingly, all the functions that we have listed are part of (In this log-log graph, the slope of the line
large class of functions called, polynomials: corresponds to the growth rate)
EXPONENTIAL FUNCTIONS AND OTHERS
Exponential function: The function g(n) assigns to the input argument ‘n’ the value obtained by
multiplying the base ‘b’ by itself n times. It is: g(n) = bn.
For instance, if we have a loop that starts by performing one operation
and then doubles the number of operations performed with each
iteration, then the number of operations performed in the nth iteration is
?. Ex? g(n) = 2n
The Floor and Ceiling Functions: The value of logarithm is not an integer,
typically. Hence, Floor and Ceiling functions are used to round them.

• ⌊x⌋ = the largest integer less than or equal to x and ⌈x⌉ = the
smallest integer greater than or equal to x.
GROWTH RATES OF SEVEN FUNCTIONS
Idle for what
types of
Infeasible?

operations? Practical?

Idle for what


types of
operations?

[log-log plot with growth rates (running times)as slopes]


BITS F232: FOUNDATIONS OF DATA STRUCTURES
& ALGORITHMS Chittaranjan Hota, PhD

(1 ST SEMESTER 2022-23) Professor of Computer Sc.


BITS-Pilani Hyderabad Campus

ALGO COMPLEXITY hota[AT]hyderabad.bits-pilani.ac.in

© Chittaranjan Hota, BITS Hyderabad


ANALYSIS OF ALGORITHMS: EXPERIMENTAL STUDIES
•Characterizes the running times of algorithms and data structure operations along with space
usage.
•Write a program implementing the algorithm on a hardware
with required system software.
•Run the program with inputs of varying sizes and composition.
•Use a method or a system call like clock() to get an accurate

Running time
measure of the actual running time. Or use a profiler to count
number of instructions executed at run time.
•Plot the results (running time vs. different test inputs of
varying sizes)
Input size
Limitations:
• It is necessary to implement the complete algorithm, which may be difficult.
• Results may not be indicative of the running time on other inputs not included in the experiment.
• In order to compare two algorithms, the same hardware and software environments must be used
NEXT LAB

recursive
EXAMPLE RUN TIME…

Inside main()
CONTINUED…

NEXT LAB
THEORETICAL ANALYSIS
•Uses a high-level description of the algorithm instead of an Primitive
implementation. operations:
CPU Add, sub,
•Characterizes running time as a function of the input size, n. 1
2 cmp,
0 indexing,
•Takes into account all possible inputs RAM branching
•Allows us to evaluate the speed of an algorithm etc.
independent of the hardware/software environment (The RAM Model)

Algorithm arrayMax(A, n) Natural measure of “goodness”


P Input: array A of n integers Easier to analyse. Applications like games, robotics etc. need it.

s Output: maximum element of A


e max  A[0] difficult. Why?

u for i  1 to n  1 do
d The algorithm arrayMax executes
if A[i]  max then
o about 8n - 3 primitive operations
max  A[i]
in the worst case.
return max
BITS F232: FOUNDATIONS OF DATA STRUCTURES
& ALGORITHMS Chittaranjan Hota, PhD

(1 ST SEMESTER 2022-23) Professor of Computer Sc.


BITS-Pilani Hyderabad Campus

BIG-O NOTATION hota[AT]hyderabad.bits-pilani.ac.in

© Chittaranjan Hota, BITS Hyderabad


BIG-O (ASYMPTOTIC) NOTATION
•The Big-O notation, O(g(n)), is used to give an upper
bound (worst-case complexity) on a positive runtime
function f(n) where n is the input size.

Definition of Big-O:
Consider a function f(n) that is non-negative  n  0.
We say that “f(n) is Big-O of g(n)” i.e., f(n)  O(g(n)),
if  n0  0 and a constant c > 0 such that:
?
 n  n0
Dominant Total Lesser Contribution of lesser
term terms terms

ASYMPTOTIC COMPLEXITY n

10

100
n2

100

10,000
n2 + 4n + 4

144

10,404
4n + 4

44

404
Lesser/total * 100

30.55 %

3.88 %

1,000 1,000,000 1,004,004 4004 0.39 %

Consider f(n) = 2n2 + 4n + 4 10,000 100,000,000 100,040,004 40,004 0.039 %

Let us approximate f(n) by g(n) = n2 , why?


Then we need to find a function c *g(n) such that:
f(n) ≤ c*g(n)
 2n2 + 4n + 4 ≤ cn2


There are many pairs of (n0, c) that satisfy above

Let us look at three such pairs:


(n0 = 1, c = 10), (n0 = 2, c = 5), (n0 = 4, c = 3.25)
Source: The Internet
BIG-OH NOTATION EXAMPLES
10,00,000
n^2
10,000 100n
1,00,000
3n
10n

2n+10 10,000 n
1,000

n
1,000
100
100

10
10

1 1
1 10 100 1,000 1 10 100 1,000
n n

Example: 2n + 10 is O(?) Is n2, O(n)?


BIG-OH EXAMPLES
f(n) = 2n+2 What is c and n0 ?

f(n) = 3n3 + 20n2 + 5


3n3 + 20n2 + 5 is O(n3)
f(n) = 2n3 + 10n O (n3)
need c > 0 and n0  1 such that 3n3 + 20n2 + 5  c.n3 for n  n0
this is true for c = 4 and n0 = 21 (2n3 + 10n )  c.n3
For c = 12, and n0 = 2

f(n) = 3 log n + 5
3 log n + 5 is O(log n)
need c > 0 and n0  1 such that 3 log n + 5  c.log n for n  n0
this is true for c = 8 and n0 = 2
BIG-O: IMPLICATION OF THE DEFINITION
For all sufficiently large n, c *g(n) is an upper bound of f(n).
Let us see an example:
f(n) = 3n + 4 is O(n)
i.e., 3n + 4  cn
 3n + 4  cn2  f(n) is also O(n2),
 3n + 4  cn3  f(n) is also O(n3),
...
 3n + 4  cnn  f(n) is also O(nn)
There are many g’s now. Which one should be chosen? As small as possible.
Hence, 3n + 4 is O(n), and not O(n2), or O(n3), etc.
TRY YOURSELF…
Assume that you lost your wedding ring on the beach,
and have no memory of when it came off. Thus, you
decide to do a brute force grid search with your
metal detector, where you divide the beach into strips,
and walk down every strip, scanning the whole beach,
until you find it. For simplicity, assume the beach is a
square of side length ’l’ meters, each of your strips
has a constant width of 1 meter, and it takes 10
seconds to walk 1 meter (it's hard to walk while
searching). Find the big-oh performance of your ring
finding algorithm.
Source: https://brilliant.org/
BIG-O AND GROWTH RATE
Increasing complexity

As the amount of data gets

Increasing problem size


bigger, how much more
resource will my algorithm
require?

Source: The Internet


GROWTH RATE CONTINUED…
As input gets larger,
any algorithm of a
smaller order will be
more efficient than an
algorithm of a larger
order.

Which one is efficient? What about this part?


BIG-OH RULES
1. If an algorithm performs a certain sequence of
steps f(N)times for a function f, it takes O(f(N))
steps.
This algorithm examines each of the N items once,
so it’s performance O(N).

2. If an algorithm performs an operation that takes


f(N) steps and then performs another operation
that takes g(N) steps for function f and g, the
algorithm’s total performance is O(g(N) + f(N)).

The total runtime of the algorithm is O (1+ N + 1).


Using algebraic rules inside of Big O Notation,
final algorithm’s performance is O (N + 2)
CONTINUED…
What is the big-oh bound for 13n4 – 8n2 + log2n ?
 O(13n4 – 8n2 + log2n)  Ignore lower order
terms  O(13n4)  Ignoring leading constant 
O(n4)

3. If an algorithm takes O (f(N) + g(N)) steps and


the function f(N) is bigger than g(N),
algorithm’s performance can be simplified to
O (f(N)).
findBiggestNumber algorithm has O(N+
2) runtime. When N grows very large, the
function N is larger than our constant value 2,
so algorithm’s runtime can be simplified
to O(N).
4. If an algorithm performs an operation that
takes f(N) steps, and every step performs
another operation that takes g(N) steps,
algorithm’s total performance is O(f(N)×g(N)). O(n2)
TRY YOURSELF…

If the second loop goes upto int a = 0, i = N;


N? while (i > 0) {
a += i;
i /= 2;
}
TRY YOURSELF…
int i = 1, j; int sum = 0; for (int j = 0; j < n * n; j++)
while(i <= n) { for(int i = 1; i <= n; i++) sum = sum + j;
j = 1; for(int j = i; j < 0; j++)
while(j <= n) sum += i * j ; for (int k = 0; k < n; k++)
{ sum = sum - l;
statements of O(1) int n = 100;
j = j*2; // . . . print("sum is now ” + sum);
} for(int i = 1; i <= n; i++)
i = i+1; for(int j = 1; j <= n; j++)
} sum += i * j ;
O(nlogn) O(n) O(1) O(n2)
Getting treated for your toothache inside the campus medical center, access dental(Alwal), or at Yashoda
(Secunderabad)…
If you want to access a particular element in an array of ‘n’ elements… Best case

RELATIVES OF BIG-OH ( AND ) T


I
M
Big-Omega Notation () E

•Just like Bio-O provides asymptotic upper-bound, Big-


provides asymptotic lower-bound on the running time.
•f(n) is (g(n)) if there exists a constant c > 0 and an
f(n) = Ω(g(n))
integer constant n0  1 such that f(n)  c.g(n) for all n  n0 Ω(nlog n)
Let, f(n) = 3n.logn + 2n Justification: 3n.logn+2n ≥ 3n.logn, for n ≥ 2 Average
T
case
Big-Theta Notation () I
M
f(n) is Θ(g(n)) , if: f(n) is both O(g(n)) and Ω(g(n)) E
f(n) is (g(n)) if there are constants c1 > 0 and c2 > 0 and an
integer constant n0  1 such that c1.g(n)  f(n)  c2.g(n) for n  n0
f(n) = Θ(g(n))
3nlogn+4n+5logn is Θ(nlog n) 3nlogn  3nlogn+4n+5logn  (3+4+5) nlogn for n  2
ANALYSIS OF RECURSIVE FUNCTIONS
Complexity?

Source: https://dotnettutorials.net/ (Recursion Tree Method) (Using Recurrence Relation)

1. Induction/ Successive sub.


2. Master Theorem
BITS F232: FOUNDATIONS OF DATA STRUCTURES Chittaranjan Hota, PhD
& ALGORITHMS (1 ST SEMESTER 2022-23) Professor of Computer Sc.

STACK ADT BITS-Pilani Hyderabad Campus


hota[AT]hyderabad.bits-pilani.ac.in
(Acknowledgements: Some slides/images are adapted from Sartaj Sahni)
© Chittaranjan Hota, BITS Hyderabad
ASYMPTOTIC ANALYSIS
n Running Running
What is Asymptotic Analysis? time on A time on B

10 2 sec ~ 1 hr

Ex: Searching in a sorted array using Linear 100 20 sec ~ 1.8 hrs
search (on fast computer A) and Binary search (on
slow computer B). 106 ~ 55.5 hrs ~ 5.5 hrs

Source: https://www.geeksforgeeks.org/
109 ~ 6.3 yrs ~ 8.3 hrs

(with linear search running time on A=0.2*n)


(with binary search running time on machine
B = 1000 * log(n))
PREFIX AVERAGE EXAMPLE
35
X
•The i-th prefix average of an array X is average of the 30
first (i + 1) elements of X: A
25
A[i] = (X[0] + X[1] + … + X[i])/(i+1)
20
Algorithm prefixAverages1(X, n) Algorithm prefixAverages2(X, n)
Input array X of n integers Input array X of n integers 15
Output array A of prefix averages of X Output array A of prefix averages of X
Anew array of n integers n A  new array of n integers n 10
for i  0 to n  1 do n s0 1
s0 n for i  0 to n  1 do n
for j  0 to i do 1 + 2 + …+ n 5
s  s + X[i] n
s  s + X[j] 1 + 2 + …+ n A[i]  s / (i + 1) n
A[i]  s / (i + 1) n return A 1 0
return A 1 1 2 3 4 5 6 7

O(n2) O(n)
STACKS
•A stack S is a linear sequence of elements to which elements x can only be inserted and deleted
from the head of the list in the order they appear.
•A stack implements the Last-In-First-Out (LIFO) policy.

top F
top E
E
D
D
C
C
B Usages:
B
String reversal, undo/redo, recursion, dfs,
bottom A backtracking, expression conversion etc.
bottom A
STACK USAGES CONTINUED…
main ()
{
int i = 5;
foo(i);
}
foo (int j)
{
int k; (Runtime Stack)
k = j+1;
bar(k);
}
bar (int m) {

}
(Stock span)
EXAMPLE USAGE2: RAT IN A MAZE
CONTINUED…

Move order is: right, down,


left, up
Block positions to avoid
revisit.
CONTINUED…

Move order is: right, down,


left, up
Block positions to avoid
revisit.
CONTINUED…

Move backward until we


reach a square from which
a forward move is possible.
CONTINUED…

Move down
CONTINUED…

Move left
CONTINUED…

Move down
CONTINUED…

Move backward until we


reach a square from
which a forward move is
possible.
CONTINUED…

Move backward until we


reach a square from which a
forward move is possible.

Move downward
CONTINUED…

Move right
Backtrack
CONTINUED…

Move downward
CONTINUED…

Move right
CONTINUED…

Move one down and


then right
CONTINUED…

Move one up and then


right
CONTINUED…

Move down to exit and eat


cheese.
Path from maze entry to
current position operates as
a stack.
ARRAY-BASED STACK IMPLEMENTATION
•A simple way of implementing
the Stack ADT uses an array. Algorithm push(o) Algorithm pop()
if t = S.size()  1 then if empty() then
•We add elements from left to
throw StackFull throw StackEmpty
right.
else else
•A variable keeps track of the tt+1 tt1
index of the top element. S[t]  o return S[t + 1]

Algorithm size()

S return t + 1
0 1 2 t
CONTINUED…
C
O
M
P
L
E
X
I
T
Y
?
STACK USAGE: PARENTHESIS MATCHING EXAMPLE
Let S be an empty stack
for i=0 to n-1 do
if X[i] is an opening grouping symbol then (Output)
S.push(X[i])
else
if X[i] is a closing grouping symbol then
if S.empty() then
return false {nothing to match with}
if S.pop() does not match the type of X[i] then
return false {wrong type}
if S.empty() then
Task 1 and Task 2 of Lab6
return true {every symbol matched}
What are the demerits of Array implementation?
else
return false {some symbols were never matched}
STACK USAGE: REVERSING A VECTOR EXAMPLE

Non-recursive algo…
BITS F232: FOUNDATIONS OF DATA STRUCTURES Chittaranjan Hota, PhD
& ALGORITHMS (1 ST SEMESTER 2022-23) Professor of Computer Sc.

STACKS AND QUEUES BITS-Pilani Hyderabad Campus


hota[AT]hyderabad.bits-pilani.ac.in

© Chittaranjan Hota, BITS Hyderabad


STACK ADT: LINKED LIST IMPLEMENTATION
IMPLEMENTING A STACK WITH A GENERIC LINKED LIST
STACK USAGE: MATCHING TAGS IN AN HTML DOC
Stdin Inputs

// opening tag?

// repeat until end of string

Lab6

1. Get a vector of HTML tags from the input, and store them in a vector of strings. 2. Check whether HTML tags stored in the vector tags are matched
COMPUTING STOCK SPAN: STACK USAGE
• Stock span can be defined as the number of consecutive days before the current day where the price
of the stock was equal to or less than the current price.

SBI cards, 7th Oct 2022


COMPUTING STOCK SPAN CONTINUED…
X
Algorithm spans2(X, n) Complexity: O(n)
A  new array of n integers n
S  new empty stack 1
for i  0 to n  1 do n
while (S.empty()  X[S.top()]  X[i] ) do n
S.pop() n
if S.empty() then n
A[i]  i + 1 n
else
A[i]  i - S.top() n
S.push(i) n
return A 1
A
S
Lab6
QUEUES
Some Applications of Queues:
1. Network routers
2. Scheduling a single shared resource
(CPU)
3. Handling website traffic
4. Call centers
Is it a linear data structure?
What operations would you like to
see in a queue ADT?
Img. Source: https://www.javascripttutorial.net/
BITS F232: FOUNDATIONS OF DATA STRUCTURES Chittaranjan Hota, PhD
& ALGORITHMS (1 ST SEMESTER 2022-23) Professor of Computer Sc.

QUEUES CONTINUED… BITS-Pilani Hyderabad Campus


hota[AT]hyderabad.bits-pilani.ac.in

© Chittaranjan Hota, BITS Hyderabad


ARRAY-BASED IMPLEMENTATION OF A QUEUE
Approach 1: Similar to stack based implementation where Q[0] be the front of
the queue and have the queue grow from there.
How good is this approach?

Approach 2: Using an Array with three variables to avoid moving objects once
they are placed in the queue.
Use three variables: f, r, and n (front, rear, and number of elements). Let us see
the dequeue and enqueue operations… (What is the complexity?)
If we repeatedly enqueue and dequeue a single element, what problem it might
cause? Out-of-bounds
CONTINUED…
Approach 3: Use a circular array with ‘f’ and ‘r’ indices wrapping around the end of the
queue.

(Normal configuration: f  r) (Wrapped around: r < f)

Which operator in C++ can do this?


OPERATIONS USING CIRCULAR ARRAY & INTERFACE

Algorithm size() Algorithm enqueue (P) { template <typename E>


return n
if size() = N  1 then class Queue {
Algorithm empty() throw QueueFull public:
return (n = 0) int size() const;
else { bool empty() const;
const E& front() const
Algorithm dequeue () Q [r]  P throw(QueueEmpty);
if empty() then r  (r + 1) mod N void enqueue (const E& e);
throw QueueEmpty nn+1 void dequeue()
else } throw(QueueEmpty);
f  (f + 1) mod N } };
nn1
QUEUE ADT USING CIRCULAR LINKED-LIST

How will you enque “Akshya” into the


above queue?

How will you deque “Deb” from the


above queue?
(Class structure for Linked queue)
QUEUE IMPLEMENTATION USING CIRCULAR
LINKED-LIST IN C++

Lab7: Task1 (Write the driver function of this code)


STACK USING TWO QUEUES: MAKING PUSH COSTLY

What is the time complexity?


The STL Queue:

Lab7: Task2
size(), empty(), push(e),
pop(), front(), back()

Alternate way of doing this is by making pop() costly…


A QUEUE USING TWO STACKS
(1) Enqueue: push the elements into the Stack 1.
(2) Dequeue: push all the elements from Stack 1 into Stack 2, and then pop from Stack 2. Complexity?

5
Which operation is costly here?
2 5
3 2
3 Pop Out
3 5
2 2
5 3

Stack 1 Stack 2
DOUBLE-ENDED QUEUE ADT: DEQUE
•A queue-like data structure that supports insertion and
deletion at both the front and the rear of the queue.
•Applications: Ticketing line, Steal-scheduling algorithm in
Intel’s parallel programming, etc.
•insertFront(), front(), eraseFront(), insertBack(), back(),
eraseBack()

size(), empty(),
push_front(e),
push_back(e), pop_front(),
pop_back(), front(), back()
What are some of the scenarios where
Deque operations might be applicable?
(The STL deque)
DEQUE IMPLEMENTATION

(A Doubly linked-list with Sentinels)

Complexity of Operations?
BITS F232: FOUNDATIONS OF DATA STRUCTURES Chittaranjan Hota, PhD
& ALGORITHMS (1 ST SEMESTER 2022-23) Professor of Computer Sc.

VECTORS AND LISTS BITS-Pilani Hyderabad Campus


hota[AT]hyderabad.bits-pilani.ac.in

© Chittaranjan Hota, BITS Hyderabad


ADAPTERS DESIGN PATTERN
•What is an adapter/ a wrapper?

typedef string Elem;


Deque StackDequeStack
Queue{ // stack as a deque
insertFront() - public:
-
DequeStack();
insertBack() Push() Enqueue()
int size() const;
removeFront() - Dequeue()
bool empty() const;
removeBack() Pop() const
- Elem& top(); Next weeks Lab: Lab 7:
void push(const Elem& e); Queue using Deque.
Size() Size() Size()
void pop();
Empty() Empty()private:
Empty() • A stack using a deque?
LinkedDeque D; };
VECTOR OR ARRAY LIST ADT
•The Vector or Array List ADT extends the notion of an array by storing a sequence of objects.
•An element can be accessed, inserted or removed by specifying its index (number of elements
preceding it)
•An exception is thrown if an incorrect index is given (e.g., a negative index)

Main methods:
at(i), set(i, o), insert(i, o), erase(i), size(), empty()

0 ≤ i ≤ size()−1

Applications: Sorted collection of objects (elementary database),


auxiliary ds for algorithms.
(Let us see the output and contents
of the vector)
SIMPLE ARRAY-BASED IMPLEMENTATION
Use an array A of size N
A variable n keeps track of the size of the array list (number
of elements stored)
How will you implement?
at (i)
set (i, o)
insert (i, o)
erase (i, o)

What would be the performance of a vector realized by an


array?
Img. Source: https://codenuclear.com/
COMPARISON OF STRATEGIES:
AMORTIZATION (A DESIGN PATTERN)
•We compare the incremental strategy and the doubling
strategy by analyzing the total time T(n) needed to perform a
series of n insert(o) operations.
Doubling Strategy
•We call amortized time of an insert operation the average
time taken by an insert over the series of operations, i.e., T(n)/n • We replace the array k = log2 n
•We replace the array k = n/c times
times
I
N • The total time T(n) of a series of
C •The total time T(n) of a series of n insert operations is n insert operations is
R proportional to
E proportional to
M
E
n+c+2c+ 3c + 4c +…+ kc = n + c(1 + 2 + 3 +…+ k) = n + n + 1 + 2 + 4 + 8 + …+ 2k
N ck(k + 1)/2 = n + 2k + 1 - 1 = 3n - 1
T
A Since c is a constant, T(n) is O(n + k2), i.e., O(n2) • T(n) is O(n)
L
The amortized time of an insert operation is O(n) • The amortized time of an insert
operation is O(1)
(Increase the size by a constant c)
STL VECTORS WITH ALGORITHMS

(STL Vectors with Algorithms)


BITS F232: FOUNDATIONS OF DATA STRUCTURES Chittaranjan Hota, PhD
& ALGORITHMS (1 ST SEMESTER 2022-23) Professor of Computer Sc.

LIST ADT BITS-Pilani Hyderabad Campus


hota[AT]hyderabad.bits-pilani.ac.in

© Chittaranjan Hota, BITS Hyderabad


POSITION ADT & ITERATORS: LIST ADT
• What is a Position ADT? • List ADT establishes a before/after
relation between positions
•It gives a unified view of diverse ways of storing
data, such as: What would be the
• a cell of an array contents of the list and
• a node of a linked list the output?

•Just one method:


• object p.element(): returns the element at
position
• In C++ it is convenient to implement this as
what?
DOUBLY LINKED LIST
A doubly linked list provides a natural implementation
of the List ADT. Algorithm insert(p, e): {insert e
Nodes implement Position and store: before p}
 element 1. Create a new node v
Complexity? 2. velement = e
 link to the previous node
3. u = pprev
 link to the next node
4. vnext = p; pprev = v
Special trailer and header nodes are used as Sentinels. {link in v before p}
5. vprev = u; unext = v
header nodes/positions trailer {link in v after u}

Algorithm remove(p):
u = pprev
w = pnext
unext = w {linking out p}
elements wprev = u
CONTAINERS AND ITERATORS
•What is a Container?
Let C be a container and p be an iterator for
• Can you give some examples? C:
•Various notions of iterator:
How will you iterate through the container?
• (standard) iterator: allows read-write access to
elements Example: (with an STL vector)
typedef vector<int>::iterator Iterator;
• const iterator: provides read-only access to elements int sum = 0;
• bidirectional iterator: supports both ++p and --p for (Iterator p = V.begin(); p != V.end(); ++p)
sum += *p;
• random-access iterator: supports both p+i and p-i return sum;
STL LISTS & ITERATORS

o/p

Source: https://www.geeksforgeeks.org/
Using Indexing Operator

Using Iterators
INDEX VS POSITION: MORE EXAMPLES
SEQUENCE ADT
•The Sequence ADT generalizes the Vector and List
ADTs
•Elements are accessed by:
 Index, or
 Position
•Generic methods:
 size(), empty()

Direct applications: Generic replacement for stack,


queue, vector, or list.
Small database (e.g., address book).
Indirect applications:
Building block of more complex data structures. (Doubly-linked list Implementation)
SEQUENCE ADT: ARRAY BASED
Operation Arr Li
ay st elements
•We use a circular array storing size, empty 1 1

positions. atIndex, indexOf,


at
1 n

begin, end 1 1
A position object stores: set(p,e) 1 1
 Element set(i,e) 1 n

 Index insert(i,e), erase(i) n n


insertBack, 1 1
eraseBack
•Indices f and l keep track of first insertFront, n 1 0 1 2 3
and last positions. eraseFront

positions
insert(p,e), n 1
erase(p)

If you insert an element between 2nd and 3rd element,


how will the arrangement look like? A
f l
BITS F232: FOUNDATIONS OF DATA STRUCTURES Chittaranjan Hota, PhD
& ALGORITHMS (1 ST SEMESTER 2022-23) Professor of Computer Sc.

TREE ADT BITS-Pilani Hyderabad Campus


hota[AT]hyderabad.bits-pilani.ac.in

© Chittaranjan Hota, BITS Hyderabad


SEQUENCES: EXAMPLE BUBBLE SORT
•Time complexity of bubble sort?

Array based: atIndex takes O(1)  O(n2) for Bubble sort. Iterator increment takes O(1) in either array or node-based
Node based: atIndex takes O(n)  O(n3) for Bubble sort. sequence implementations  O(n2) worst case for Bubble sort.
TREES: NON-LINEAR DATA STRUCTURES
•In computer science, what is a tree?
•A tree consists of nodes with a parent-child relation.
•Applications???

Formally, we define tree T to be a set of nodes storing


elements in a parent-child relationship with the following
properties:
- If T is nonempty, it has a special node, called the
root of T, that has no parent.
- Each node v of T different from the root has a
unique parent node w; every node with parent w is a
child of w.
TREE TERMINOLOGIES AND PROSPERITIES
Root: ???
A
Internal node: ???
External node: ???
B C D
Ancestors of a node: parent, grandparent,
grand-grandparent, etc.
Depth of a node: ???
E F G H
Height of a tree: maximum depth of any node
(???)
Descendant of a node: child, grandchild, I J K What is a sub-tree?
grand-grandchild, etc.
No of edges??? Degree of a node???
Level of a node???
count of subtrees
TRY YOURSELF…
President

VP1 VP2 VP3

Manager1 Manager2 Manager3 Manager4

Worker1
ORDERED TREES
What are Ordered Trees?
TYPES OF TRESS IN DATA STRUCTURES

(An AVL trees)

(A general tree) (A binary tree) (A binary search tree)


TREE ADT
•Generic methods:
 integer size()
 boolean empty()
•Accessor methods:
 position root()
 list<position> positions()
•Position-based methods:
 position p.parent() (An informal interface
 list<position> p.children() (An informal interface for the tree ADT)
for a position in a tree)
• Query methods:
•boolean p.isRoot()
• boolean p.isExternal()
• Additional update methods may be defined by data structures implementing the Tree ADT.
A LINKED STRUCTURE FOR GENERAL TRESS

NULL

(The node structure) (The portion of the data structure associated (Running times of the functions of
with root node and its children) an n-node linked tree structure)
BITS F232: FOUNDATIONS OF DATA STRUCTURES Chittaranjan Hota, PhD
& ALGORITHMS (1 ST SEMESTER 2022-23) Professor of Computer Sc.

TREE TRAVERSALS BITS-Pilani Hyderabad Campus


hota[AT]hyderabad.bits-pilani.ac.in

© Chittaranjan Hota, BITS Hyderabad


DEPTH AND HEIGHT OF A TREE
The height of a node p in a tree T is also defined recursively:
Algorithm depth(T, p): -If p is external, then the height of p is ???.
-Otherwise, the height of p is one plus the ??? height of a
Let us write it out using Recursion… child of p

Algorithm height1(T): Algorithm height2(T, p):


h=0 if p.isExternal() then
The running time of algorithm depth(T, p) is for each p ∈ T.positions() do return 0
O(dp), where dp denotes the depth of the if p.isExternal() then else
node p in the tree T. h = max(h,depth(T, p)) h=0
return h for each q ∈ p.children()
Worst case complexity is ??? where there (The height of a tree is equal to
the maximum depth of its external
do h=max(h,height2(T,q))
are ‘n’ nodes in the tree. nodes) return 1+h
height2 spends O(1+cp) time at
O(n2)
each node p O(n)
TREE TRAVERSAL ALGORITHMS
Traversal Types
Why do we need to traverse a tree?
Level-order Depth First
(Breadth First)
void print_level(Node* root, int level_no)
{
if (!root)
return;
if (level_no == 0) {
printf("%d -> ", root->value);
}
else {
print_level(root->left, level_no - 1);
print_level(root->right, level_no - 1);
BFS on Maze-solving algorithm }
(Source: Wiki)
}
Find out the prefix expression from this How will you implement it (BFS)
expression tree… using a queue? (BFS: using recursion)
TREE TRAVERSAL: DEPTH-FIRST (PREORDER)
•In a preorder traversal, a node is Make Money Fast!
visited before its descendants.
•Applications: print a structured 1. Motivations 2. Methods Purchase house
document, create the copy of a tree,
get prefix expression on an 2.1 Stock 2.2 Ponzi 2.3 Bank
expression tree. 1.1 Greed 1.2 Avidity
Fraud Scheme Robbery

i
Algorithm preOrder(v) { t
e
Let us write it out in a r
recursive way… a
} t
i
v
Lab 8 next week e

Can you draw the binary tree for the preorder traversal output: 1 2 4 5 3?
POSTORDER TRAVERSAL
•In a postorder traversal, a node is visited after its
descendants.
•Application: compute space used by files in a directory Lab 8 next week
and its subdirectories, delete the tree, compute postfix
expression…
csf211/

Algorithm postOrder(v){
for each child w of v notices.txt
homeworks/ programs/
postOrder (w); 1K
visit(v);
}
h1.doc h2.doc p1.cpp p2.cpp p3.cpp
2K 5K 7K 22K 18K
BITS F232: FOUNDATIONS OF DATA STRUCTURES Chittaranjan Hota, PhD
& ALGORITHMS (1 ST SEMESTER 2022-23) Professor of Computer Sc.

BINARY TREES BITS-Pilani Hyderabad Campus


hota[AT]hyderabad.bits-pilani.ac.in

© Chittaranjan Hota, BITS Hyderabad


DEFINITIONS: BINARY TREE
• What is a binary tree?
•What is a full binary tree or proper binary tree?
• All internal nodes have exactly ??? children
•What is a complete binary tree? (what are these?)
• Every level except the last is filled. Last from left.
•Are the children of a binary tree ordered?
•Is it a recursive data structure?
• Applications:
• Problem representation (arithmetic expressions,
decision trees) (what is this?)
• Efficient algorithmic solutions (searching becomes
faster, priority queues via heaps) Every full binary tree is a complete binary
• … tree. What about the reverse?
DIFFERENT TYPES CONTINUED…

Strict or Full Binary trees Complete Binary trees

Perfect Binary tree Skewed Binary tree

Source: https://www.enjoyalgorithms.com/
FULL VS COMPLETE: TRY YOURSELF…

Full (X), or complete (X) Full (X), or complete (Y) Full (Y), or complete (N) Full (Y), or complete (Y)
EXAMPLE USAGES
Binary tree associated with an
arithmetic expression
 internal nodes: ???
 external nodes: ??? Want a quick breakfast?
Example: arithmetic expression tree for Yes No
the expression (4  (a - 4) + (5 - b)) Coffee & burger? Pizza and Pasta
Let us draw it…
Yes No Yes No
Binary tree associated with a decision process:
@CafeCoffeeDay Inst Cafe Bits&bytes Alankrita
- internal nodes: questions with yes/no
answer
- external nodes: ???

Example: Breakfast decision of yours’


PROPERTIES OF A BINARY TREE
Notation:
n: number of nodes, e: number of external nodes, i: number of internal nodes, and h: height

• Minimum number of nodes in a binary tree with height h


=h+1
• Maximum number of nodes in a binary tree with height h h?
= 2h+1 - 1 n?
• Let us draw the tree for h = 3?
• Total number of leaf nodes in a binary tree = total
number of nodes with 2 children + 1
• Maximum number of nodes at any level ‘l’ in a binary
tree = 2l.
• Let us draw it for level 2.
PROPERTIES OF A PROPER BINARY TREE
Notation:
n: number of nodes, e: number of external nodes, i: number of internal nodes, and h: height

• Properties:
• e=i+1
• n = 2e - 1
• hi
• h  (n - 1)/2
• e  2h
• h  log2 e
• h  log2 (n + 1) - 1
BINARY TREE ADT
INORDER TRAVERSAL
•In an inorder traversal a node is visited after its left subtree and before its right subtree
Application: draw a binary tree

Algorithm inOrder(v)
if  v.isExternal()
inOrder(v.left())
visit(v)
if  v.isExternal()
inOrder(v.right())

complete it…
PRINT ARITHMETIC EXPRESSION
Specialization of an inorder traversal
 print operand or operator when visiting node +
 print “(“ before traversing left subtree
 print “)“ after traversing right subtree
 
Algorithm printExpression(v)
if v.isExternal()
print(“(’’) 2 - 5 b
inOrder(v.left())
print(v.element())
if v.isExternal() a 4
inOrder(v.right())
print (“)’’) Let us write it out…
EVALUATE ARITHMETIC EXPRESSION
Specialization of a postorder traversal
 recursive method returning the value of a subtree
 when visiting an internal node, combine the values of the subtrees

+ Algorithm evalExpr(v)
if v.isExternal()
  return v.element()
else
x  evalExpr(v.left())
2 - 3 2
y  evalExpr(v.right())
  operator stored at v
5 1 return x  y
EULER TOUR TRAVERSAL
• Generic traversal of a binary tree
• We can unify the tree-traversal algorithms (in-order,
pre-order, and post-order) into a single framework by
relaxing the requirement that each node be visited
exactly once.
• Walk around the tree and visit each node three times:
 on the left (preorder)
 from below (inorder)
 on the right (postorder)
+: It allows for more general kinds of algorithms to be
expressed easily.
(Source: Wiki)

Applications: finding no. of descendants, classifying advance and retreat edges, level of each node,
lowest common ancestor, etc.
BITS F232: FOUNDATIONS OF DATA STRUCTURES Chittaranjan Hota, PhD
& ALGORITHMS (1 ST SEMESTER 2022-23) Professor of Computer Sc.

BINARY TREES CONTINUED… BITS-Pilani Hyderabad Campus


hota[AT]hyderabad.bits-pilani.ac.in

© Chittaranjan Hota, BITS Hyderabad


BINARY TREE TRAVERSAL: PREORDER (RECAP)
template <class T>
/
void preOrder(binaryTreeNode<T> *t)
{
* +
if (t != NULL)
{ e f
+ -
visit(t);
preOrder(t->leftChild); a b c d

preOrder(t->rightChild);
/ * +a b - c d +e f
}
} Gives prefix form of expression!
BINARY TREE TRAVERSAL: INORDER
template <class T> / Squishing
void inOrder(binaryTreeNode<T> *t)
{ * +
if (t != NULL)
e f
{ + -
inOrder(t->leftChild);
a b c d
visit(t);
inOrder(t->rightChild);
a + b * c - d / e + f
}
} Gives infix form of expression!
BINARY TREE TRAVERSAL: POSTORDER (RECAP)
template <class T>
/
void postOrder(binaryTreeNode<T> *t)
{
if (t != NULL) * +

{ e f
+
postOrder(t->leftChild); -

postOrder(t->rightChild); a b c d
visit(t);
} a b +c d - * e f + /
} Gives postfix form of expression!
EULER TOUR TRAVERSAL (RECAP)
•Generic traversal of a binary tree
•We can unify the tree-traversal
algorithms (in-order, pre-order, and
post-order) into a single framework by
relaxing the requirement that each
node be visited exactly once.
•We visit each node three times
•Euler tour: walk around a binary tree
where each edge is treated as a wall,
which you cannot cross. Each node will Img. Source: https://www.andrew.cmu.edu/
be visited either on the left, or under Applications: printing fully parenthesized expression
the below, or on right. etc.
BINARY TREE CONSTRUCTION FROM TRAVERSAL ORDER
Can you construct the binary tree, given two traversal sequences?

preorder = ab Try: a
Postorder: DEBFCA
postorder = ba Inorder: DBEAFC

gdhbei fjc
do they uniquely define a binary tree?
a
inorder = g d h b e i a f j c
preorder = a b d g h e i c f j
b fjc
preorder = a b d g h e i c f j
Similarly, Scan postorder from right to left using inorder. gdh ei …
IMPLEMENTATION OF TREES: LINKED STRUCT.
B 
A node is represented by an object storing
 Element
 Parent node
 Sequence of children nodes A  D F 
Node objects implement the Position ADT

What tree does it represent?

C  E 
A VECTOR-BASED IMPLEMENTATION OF BINARY
TREE
• A simple structure for representing a binary tree T is based on a way of numbering the nodes
of T.
• If v is the root of T, then f(v) = 1; If v is the left child of node u, then f(v) = 2f(u); If v is the right
child of node u, then f(v) = 2f(u) +1  function f is called level numbering function.

(General scheme) (An Example Expression tree)


TRY YOURSELF…
a1 1
a
2 3 3
b c b
7
4 5 6 7 c
d e f g 15
8 9 10
h i j d

tree[ ] a b c d e f g h i j a - b - - - c - - - - - - - d
0 5 10 0 5 10 15
tree[ ]
BINARY TREE IMPLEMENTATION USING LINKED
STRUCT

7 8
(Lab 8)

3 4
BINARY TREE UPDATE FUNCTIONS

expandExternal(const Position& p)
removeAboveExternal (const Position& p)
CONTINUED…

The vector implementation of a binary tree is a


fast and easy way of realizing the binary-tree
ADT, but it can be very space inefficient if the
height of the tree is large.  O(2n), where ‘n’ is
no. of nodes in T.
BINARY SEARCH TREES

More later

(Searching successfully 36 through blue solid path, and unsuccessfully 71 through dashed path)
THE TEMPLATE FUNCTION PATTERN
•The template function pattern describes a generic computation method that can be
tuned for a particular application by redefining certain steps.

(Member function eulerTour recursively traverses


(Class EulerTour defining a generic Euler tour
the tree and accumulates the results)
of a binary tree. It realizes template function
pattern and must be specialized for use)
CONTINUED…

(Implementation of class EvaluateExpressionTour


which specializes EulerTour to evaluate the expression (A derived class, called PrintExpressionTour that prints the
associated with an arithmetic expression tree) arithmetic expression)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy