ADS Notes
ADS Notes
Objectives:
1. To introduce and practice advanced algorithms and programming techniques necessary for developing sophisticated
computer application programs
2. To learn new techniques for solving specific problems more efficiently and for analyzing space and time requirements.
3. To write programs in C to solve problems using data structures such as arrays, linked lists, stacks, queues, Trees, graphs,
hash tables, search Trees.
Unit I: The Fundamentals of Object-Oriented Programming: Necessity for OOP, Data Hiding, Data Abstraction, Encapsulation,
Procedural Abstraction, Class and Object, Scope of Class and Scope Resolution Operator, Member Function of a Class,
private, protected, and public Access Specifier.
Unit II Priority Queues: Definition, realizing a Priority Queue using Heaps, Insertion, Deletion, Heap sort, External Sorting.
Dictionaries: Linear List Representation, Skip List Representation, Operations- Insertion, Deletion and Searching.
Unit III: Hashing: Hash Table Representation, Hash Functions, Collision Resolution Separate
Chaining, Open Addressing-Linear Probing, Quadratic Probing, Double Hashing, Rehashing,
Extendible Hashing, Comparison of Hashing and Skip Lists.
Unit IV: Trees: Basic Terminology, Binary Tree, Array and Linked List Representations, Traversals, Threaded Binary Trees.
Search Trees (Part I): Binary Search Trees- Definition (ADT), Implementation, Operations- Searching, Insertion and Deletion,
AVL Trees, Definition, Operations – Insertion and Searching. Search Trees (Part II): B-Trees- Definition, B-Tree of Order m,
Insertion, Deletion and Searching, Red black Trees and Splay Trees.
Unit V: Graphs: Basic Terminology, Representations of Graphs, Graph Search Methods: DFS, BFS. Text Processing Pattern
Matching Algorithms: Brute Force, The Knuth-Morris-Pratt Algorithm, Tries: Standard Tries, Compressed Tries, Suffix Tries.
Course Outcomes: Upon the successful completion of the course, the student will be able:
1. Analyze performance of algorithms with respect to complexities, trace and code recursive functions, understanding of
various searching algorithms.
2. Understand operations on linear data structures such as stacks, queues to solve various computing problems and
Implement these data structures in more than one manner.
3. Classify different linked lists and their operations, Compare different implementations to recognize the advantages and
disadvantages.
4. Understand the linked implementation use in non-linear data structures such as Binary Tree and Binary Search Tree and
Tree traversals such as in, pre, post.
5. Understand graph types and their representations. Know the usage of graph traversal algorithms.
Textbooks:
1.Data Structures, Algorithms and Applications in C++, S.Sahni, University Press (India) Pvt.Ltd, Second edition, Universities
Press Orient Longman Pvt. Ltd.
2. Data Structures and Algorithm Analysis in C++, Mark Allen Weiss, Pearson Education. Ltd., Second Edition.
Why OOP?
Suppose that you want to assemble your own PC, you go to a hardware store and pick up a motherboard,
a processor, some RAMs, a hard disk, a casing, a power supply, and put them together. You turn on the
power, and the PC runs. You need not worry whether the motherboard is a 4-layer or 6-layer board,
whether the hard disk has 4 or 6 plates; 3 inches or 5 inches in diameter, whether the RAM is made in
DS Using C++ Page 1
Japan or Korea, and so on. You simply put the hardware components together and expect the machine to
run. Of course, you have to make sure that you have the correct interfaces, i.e., you pick an IDE hard
disk rather than a SCSI hard disk, if your motherboard supports only IDE; you have to select RAMs with
the correct speed rating, and so on. Nevertheless, it is not difficult to set up a machine from
hardware components.
Similarly, a car is assembled from parts and components, such as chassis, doors, engine, wheels, brake,
and transmission. The components are reusable, e.g., a wheel can be used in many cars (of the same
specifications).
Hardware, such as computers and cars, are assembled from parts, which are reusable components.
How about software? Can you "assemble" a software application by picking a routine here, a routine
there, and expect the program to run? The answer is obviously no! Unlike hardware, it is very difficult to
"assemble" an application from software components. Since the advent of computer 60 years ago, we
have written tons and tons of programs. However, for each new application, we have to re-invent the
wheels and write the program from scratch.
Why re-invent the wheels?
1.1 Traditional Procedural-Oriented languages
Unified Modeling Language (UML) Class and Instance Diagrams: The above class
diagrams are drawn according to the UML notations. A class is represented as a 3-compartment box,
containing name, data members (variables), and member functions, respectively. classname is shown in
bold and centralized. An instance (object) is also represented as a 3-compartment box, with instance
name shown as instanceName:Classname and underlined.
Brief Summary
1. A class is a programmer-defined, abstract, self-contained, reusable software entity that mimics a
real-world thing.
2. A class is a 3-compartment box containing the name, data members (variables) and the member
functions.
3. A class encapsulates the data structures (in data members) and algorithms (member functions).
The values of the data members constitute its state. The member functions constitute its behaviors.
4. An instance is an instantiation (or realization) of a particular item of a class.
2.3 Class Definition
In C++, we use the keyword class to define a class. There are two sections in the class
declaration: private and public, which will be explained later. For examples,
Alternatively, you can invoke the constructor explicitly using the following syntax:
A class called Circle is to be defined as illustrated in the class diagram. It contains two data
members: radius (of type double) and color (of type String); and three member
functions: getRadius(), getColor(), and getArea().
Three instances of Circles called c1, c2, and c3 shall then be constructed with their respective data
members, as shown in the instance diagrams.
In this example, we shall keep all the codes in a single source file called CircleAIO.cpp.
CircleAIO.cpp
1/* The Circle class (All source codes in one file) (CircleAIO.cpp) */
2#include <iostream> // using IO functions
3#include <string> // using string
4using namespace std;
5
6class Circle {
7private:
8 double radius; // Data member (Variable)
9 string color; // Data member (Variable)
10
11public:
12 // Constructor with default values for data members
13 Circle(double r = 1.0, string c = "red") {
14 radius = r;
15 color = c;
16 }
17
To compile and run the program (with GNU GCC under Windows):
> CircleAIO
Radius=1.2 Area=4.5239 Color=blue
Radius=3.4 Area=36.3169 Color=red
Radius=1 Area=3.1416 Color=red
2.9 Constructors
A constructor is a special function that has the function name same as the classname. In the
above Circle class, we define a constructor as follows:
// Constructor has the same name as the class
Circle(double r = 1.0, string c = "red") {
radius = r;
color = c;
}
Try moving radius to the public section, and re-run the statement.
On the other hand, the function getRadius() is declared public in the Circle class. Hence, it can be
invoked in the main().
UML Notation: In UML notation, public members are denoted with a " +", while private members
with a "-" in the class diagram.
2.12 Information Hiding and Encapsulation
A class encapsulates the static attributes and the dynamic behaviors into a "3-compartment box". Once a
class is defined, you can seal up the "box" and put the "box" on the shelve for others to use and reuse.
Anyone can pick up the "box" and use it in their application. This cannot be done in the traditional
procedural-oriented language like C, as the static attributes (or variables) are scattered over the entire
program and header files. You cannot "cut" out a portion of C program, plug into another program and
expect the program to run without extensive changes.
Data member of a class are typically hidden from the outside word, with private access control modifier.
Access to the private data members are provided via public assessor functions,
e.g., getRadius() and getColor().
This follows the principle of information hiding. That is, objects communicate with each others using well-
defined interfaces (public functions). Objects are not allowed to know the implementation details of
others. The implementation details are hidden or encapsulated within the class. Information hiding
facilitates reuse of the class.
Rule of Thumb: Do not make any data member public, unless you have a good reason.
2.13 Getters and Setters
To allow other to read the value of a private data member says xxx, you shall provide a get
function (or getter or accessor function) called getXxx(). A getter need not expose the data in raw
format. It can process the data and limit the view of the data others will see. Getters shall not modify the
data member.
To allow other classes to modify the value of a private data member says xxx, you shall provide a set
function (or setter or mutator function) called setXxx(). A setter could provide data validation (such as
range checking), and transform the raw data into the internal representation.
For example, in our Circle class, the data members radius and color are declared private. That is to
say, they are only available within the Circle class and not visible outside the Circle class -
including main(). You cannot access the private data members radius and color from
the main() directly - via says c1.radius or c1.color. The Circle class provides two public accessor
functions, namely, getRadius() and getColor(). These functions are declared public. The main() can
invoke these public accessor functions to retrieve the radius and color of a Circle object, via
says c1.getRadius() and c1.getColor().
There is no way you can change the radius or color of a Circle object, after it is constructed in main().
You cannot issue statements such as c1.radius = 5.0 to change the radius of instance c1, as radius is
declared as private in the Circle class and is not visible to other including main().
If the designer of the Circle class permits the change the radius and color after a Circle object is
constructed, he has to provide the appropriate setter, e.g.,
// Setter for color
void setColor(string c) {
color = c;
}
class Circle {
private:
double radius; // Member variable called "radius"
......
public:
void setRadius(double radius) { // Function's argument also called "radius"
this->radius = radius;
// "this.radius" refers to this instance's member variable
// "radius" resolved to the function's argument.
}
......
}
In the above codes, there are two identifiers called radius - a data member and the function parameter.
This causes naming conflict. To resolve the naming conflict, you could name the function
parameter r instead of radius. However, radius is more approximate and meaningful in this context. You
can use keyword this to resolve this naming conflict. " this->radius" refers to the data member; while
"radius" resolves to the function parameter.
"this" is actually a pointer to this object. I will explain pointer and the meaning of " ->" operator later.
Alternatively, you could use a prefix (such as m_) or suffix (such as _) to name the data members to avoid
name crashes. For example,
class Circle {
private:
double m_radius; // or radius_
......
public:
void setRadius(double radius) {
m_radius = radius; // or radius_ = radius
}
......
}
C++ Compiler internally names their data members beginning with a leading underscore ( e.g., _xxx)
and local variables with 2 leading underscores (e.g., __xxx). Hence, avoid name beginning with
underscore in your program.
2.15 "const" Member Functions
A const member function, identified by a const keyword at the end of the member function's header,
cannot modifies any data member of this object. For example,
double getRadius() const { // const member function
radius = 0;
// error: assignment of data-member 'Circle::radius' in read-only structure
return radius;
}
// A getter for variable xxx of type T receives no argument and return a value of type T
T getXxx() const { return xxx; }
// A setter for variable xxx of type T receives a parameter of type T and return void
void setXxx(T x) { xxx = x; }
// OR
void setXxx(T xxx) { this->xxx = xxx; }
}
For a bool variable xxx, the getter shall be named isXxx(), instead of getXxx(), as follows:
private:
// Private boolean variable
bool xxx;
public:
// Getter
bool isXxx() const { return xxx; }
// Setter
void setXxx(bool x) { xxx = x; }
// OR
void setXxx(bool xxx) { this->xxx = xxx; }
If C++, if you did not provide ANY constructor, the compiler automatically provides a default constructor
that does nothing. That is,
class MyClass {
public:
// The default destructor that does nothing
~MyClass() { }
......
}
Advanced Notes
If your class contains data member which is dynamically allocated (via new or new[] operator), you
need to free the storage via delete or delete[].
2.20 *Copy Constructor
A copy constructor constructs a new object by copying an existing object of the same type. In other
words, a copy constructor takes an argument, which is an object of the same class.
If you do not define a copy constructor, the compiler provides a default which copies all the data
members of the given object. For example,
class MyClass {
private:
T1 member1;
T2 member2;
public:
// The default copy constructor which constructs an object via memberwise copy
MyClass(const MyClass & rhs) {
member1 = rhs.member1;
member2 = rhs.member2;
}
......
}
The default copy constructor performs shadow copy. It does not copy the dynamically allocated data
members created via new or new[] operator.
2.21 *Copy Assignment Operator (=)
The compiler also provides a default assignment operator ( =), which can be used to assign one object to
another object of the same class via memberwise copy. For example, using the Circle class defined
earlier,
Circle c6(5.6, "orange"), c7;
cout << "Radius=" << c6.getRadius() << " Area=" << c6.getArea()
<< " Color=" << c6.getColor() << endl;
// Radius=5.6 Area=98.5206 Color=orange
cout << "Radius=" << c7.getRadius() << " Area=" << c7.getArea()
<< " Color=" << c7.getColor() << endl;
// Radius=1 Area=3.1416 Color=red (default constructor)
Circle c8 = c6; // Invoke the copy constructor, NOT copy assignment operator
}
The copy assignment operator differs from the copy constructor in that it must release the
dynamically allocated contents of the target and prevent self assignment. The assignment operator
shall return a reference of this object to allow chaining operation (such as x = y = z).
The default constructor, default destructor, default copy constructor, default copy assignment
operators are known as special member functions, in which the compiler will automatically generate
a copy if they are used in the program and not explicitly defined.
3. Separating Header and Implementation
For better software engineering, it is recommended that the class declaration and implementation be
kept in 2 separate files: declaration is a header file " .h"; while implementation in a " .cpp". This is known
as separating the public interface (header declaration) and the implementation. Interface is defined by
the designer, implementation can be supplied by others. While the interface is fixed, different vendors
can provide different implementations. Furthermore, only the header files are exposed to the users, the
implementation can be provided in an object file " .o" (or in a library). The source code needs not given to
the users.
I shall illustrate with the following examples.
Instead of putting all the codes in a single file. We shall "separate the interface and implementation" by
placing the codes in 3 files.
1. Circle.h: defines the public interface of the Circle class.
2. Circle.cpp: provides the implementation of the Circle class.
3. TestCircle.cpp: A test driver program for the Circle class.
DS Using C++ Page 17
Circle.h - Header
1/* The Circle class Header (Circle.h) */
2#include <string> // using string
3using namespace std;
4
5// Circle class declaration
6class Circle {
7private: // Accessible by members of this class only
8 // private data members (variables)
9 double radius;
10 string color;
11
12public: // Accessible by ALL
13 // Declare prototype of member functions
14 // Constructor with default values
15 Circle(double radius = 1.0, string color = "red");
16
17 // Public getters & setters for private data members
18 double getRadius() const;
19 void setRadius(double radius);
20 string getColor() const;
21 void setColor(string color);
22
23 // Public member Function
24 double getArea() const;
25};
Program Notes:
The header file contains declaration statements, that tell the compiler about the names and types,
and function prototypes without the implementation details.
C++98/03 does NOT allow you to assign an initial value to a data member
(except const static members). Date members are to be initialized via the constructor. For example,
double radius = 1.0;
// error: ISO C++ forbids in-class initialization of non-const static member 'radius'
C++11 allows in-class initialization of data members.
You can provide default value to function's arguments in the header. For example,
Header contains function prototype, the parameter names are ignored by the compiler, but good to
serve as documentation. For example, you can leave out the parameter names in the prototype as
follows:
Program Notes:
The implementation file provides the definition of the functions, which are omitted from
the declaration in the header file.
#include "Circle.h"
The compiler searches the headers in double quotes (such as "Circle.h") in the current
directory first, then the system's include directories. For header in angle bracket (such
as <iostream>), the compiler does NOT searches the current directory, but only the system's include
directories. Hence, use double quotes for user-defined headers.
Circle::Circle(double r, string c) {
You need to include the className:: (called class scope resolution operator) in front of all the
members names, so as to inform the compiler this member belong to a particular class.
(Class Scope: Names defined inside a class have so-called class scope. They are visible within the
class only. Hence, you can use the same name in two different classes. To use these names outside
the class, the class scope resolution operator className:: is needed.)
DS Using C++ Page 19
You CANNOT place the default arguments in the implementation (they shall be placed in the header).
For example,
Let's write a class called Time, which models a specific instance of time with hour, minute and second
values, as shown in the class diagram.
The class Time contains the following members:
Three private data members: hour (0-23), minute (0-59) and second (0-59), with default values of
0.
A public constructor Time(), which initializes the data members hour, minute and second with the
values provided by the caller.
public getters and setters for private data
members: getHour(), getMinute(), getSecond(), setHour(), setMinute(), and setSecond().
A public member function setTime() to set the values of hour, minute and second given by the
caller.
A public member function print() to print this Time instance in the format " hh:mm:ss", zero-filled,
e.g., 01:30:04.
A public member function nextSecond(), which increase this instance by one
second. nextSecond() of 23:59:59 shall be 00:00:00.
Let's write the code for the Time class, with the header and implementation separated in two
files: Time.h and Time.cpp.
Header - Time.h
1/* Header for the Time class (Time.h) */
2#ifndef TIME_H // Include this "block" only if TIME_H is NOT defined
3#define TIME_H // Upon the first inclusion, define TIME_H so that
4 // this header will not get included more than once
5class Time {
6private: // private section
7 // private data members
8 int hour; // 0 - 23
9 int minute; // 0 - 59
10 int second; // 0 - 59
11
12public: // public section
13 // public member function prototypes
A class called Account, which models a bank account, is designed as shown in the class diagram. It
contains:
Two private data members: accountNumber (int) and balance (double), which maintains the
current account balance.
Public functions credit() and debit(), which adds or subtracts the given amount from the
balance, respectively. The debit() function shall print "amount withdrawn exceeds the current
balance!" if amount is more than balance.
A public function print(), which shall print "A/C no: xxx Balance=xxx" (e.g., A/C no: 991234
Balance=$88.88), with balance rounded to two decimal places.
Header file - Account.h
1/* Header for Account class (Account.h) */
2#ifndef ACCOUNT_H
3#define ACCOUNT_H
A Ball class models a moving ball, designed as shown in the class diagram, contains the following
members:
Four private data members x, y, xSpeed and ySpeed to maintain the position and speed of the ball.
A constructor, and public getters and setters for the private data members.
A function setXY(), which sets the position of the ball and setXYSpeed() to set the speed of the
ball.
A function move(), which increases x and y by xSpeed and ySpeed, respectively.
A function print(), which prints "Ball @ (x,y) with speed (xSpeed,ySpeed)", to 2 decimal
places.
Header File - Ball.h
1/* Header for the Ball class (Ball.h) */
2#ifndef BALL_H
3#define BALL_H
4
5class Ball {
6private:
7 double x, y; // Position of the ball
8 double xSpeed, ySpeed; // Speed of the ball
9
10public:
11 Ball(double x = 0.0, double y = 0.0, // Constructor with default values
12 double xSpeed = 0.0, double ySpeed = 0.0);
13 double getX() const;
Let's begin with a class called Author, designed as shown in the class diagram. It contains:
Three private data members: name (string), email (string),
and gender (char of 'm', 'f' or 'u' for unknown).
A constructor to initialize the name, email and gender with the given values. There are no default
values for data members.
Getters for name, email and gender, and setter for email. There is no setter for name and gender as
we assume that these attributes cannot be changed.
A print() member function that prints "name (gender) at email", e.g., "Peter Jones (m) at
[email protected]".
Header File - Author.h
1/* Header for the Author class (Author.h) */
2#ifndef AUTHOR_H
3#define AUTHOR_H
4
5#include <string>
6using namespace std;
7
8class Author {
9private:
10 string name;
11 string email;
12 char gender; // 'm', 'f', or 'u' for unknown
13
14public:
15 Author(string name, string email, char gender);
16 string getName() const;
17 string getEmail() const;
18 void setEmail(string email);
19 char getGender() const;
20 void print() const;
21};
22
Let's design a Book class. Assume that a book is written by one and only one author. The Book class (as
shown in the class diagram) contains the following members:
Four private data members: name (string), author (an instance of the class Author that we have
created earlier), price (double), and qtyInStock (int, with default value of 0). The price shall be
positive and the qtyInStock shall be zero or positive.
Take note that data member author is an instance (object) of the class Author, instead of a
fundamental types (such as int, double). In fact, name is an object of the class string too.
The public getters and setters for the private data members. Take note that getAuthor() returns
an object (an instance of class Author).
A public member function print(), which prints "'book-name' by author-name (gender) @ email".
A public member function getAuthorName(), which returns the name of the author of
this Book instance.
The hallow diamond shape in the class diagram denotes aggregation (or has-a) association relationship.
That is, a Book instance has one (and only one) Author instance as its component.
Header File - Book.h
1/* Header for the class Book (Book.h) */
2#ifndef BOOK_H
3#define BOOK_H
4
5#include <string>
6#include "Author.h" // Use the Author class
7using namespace std;
8
9class Book {
10private:
11 string name;
12 Author author; // data member author is an instance of class Author
13 double price;
14 int qtyInStock;
DS Using C++ Page 39
15
16public:
17 Book(string name, Author author, double price, int qtyInStock = 0);
18 // To recieve an instance of class Author as argument
19 string getName() const;
20 Author getAuthor() const; // Returns an instance of the class Author
21 double getPrice() const;
22 void setPrice(double price);
23 int getQtyInStock() const;
24 void setQtyInStock(int qtyInStock);
25 void print() const;
26 string getAuthorName() const;
27};
28
29#endif
#include "Author.h"
We need to include the "Author.h" header, as we use the Author class in this class Book.
private:
Author author;
We declare a private data member author as an instance of class Author, defined earlier.
Implementation File - Book.cpp
1/* Implementation for the class Book (Book.cpp) */
2#include <iostream>
3#include "Book.h"
4using namespace std;
5
6// Constructor, with member initializer list to initialize the
7// component Author instance
8Book::Book(string name, Author author, double price, int qtyInStock)
9 : name(name), author(author) { // Must use member initializer list to construct ob
10 // Call setters to validate price and qtyInStock
11 setPrice(price);
12 setQtyInStock(qtyInStock);
13}
14
15string Book::getName() const {
16 return name;
17}
18
19Author Book::getAuthor() const {
20 return author;
21}
22
23double Book::getPrice() const {
24 return price;
25}
26
27// Validate price, which shall be positive
28void Book::setPrice(double price) {
DS Using C++ Page 40
29 if (price > 0) {
30 this->price = price;
31 } else {
32 cout << "price should be positive! Set to 0" << endl;
33 this->price = 0;
34 }
35}
36
37int Book::getQtyInStock() const {
38 return qtyInStock;
39}
40
41// Validate qtyInStock, which cannot be negative
42void Book::setQtyInStock(int qtyInStock) {
43 if (qtyInStock >= 0) {
44 this->qtyInStock = qtyInStock;
45 } else {
46 cout << "qtyInStock cannot be negative! Set to 0" << endl;
47 this->qtyInStock = 0;
48 }
49}
50
51// print in the format ""Book-name" by author-name (gender) at email"
52void Book::print() const {
53 cout << "'" << name << "' by ";
54 author.print();
55}
56
57// Return the author' name for this Book
58string Book::getAuthorName() const {
59 return author.getName(); // invoke the getName() on instance author
60}
Book::Book(string name, Author author, double price, int qtyInStock)
: name(name), author(author) {
setPrice(price);
setQtyInStock(qtyInStock);
}
In the constructor, the caller is supposed to create an instance of Author, and pass the instance into the
constructor. We use member initializer list to initialize data members name and author. We call setters in
the body, which perform input validation to set the price and qtyInStock. The body is run after the
member initializer list. The author(author) invokes the default copy constructor of the Author class,
which performs memberwise copy for all the data members. Object data member shall be constructed via
the member initializer list, not in the body. Otherwise, the default constructor will be invoked to construct
the object.
void Book::setPrice(double price) {
if (price > 0) {
this->price = price;
} else {
cout << "price should be positive! Set to 0" << endl;
this->price = 0;
}
DS Using C++ Page 41
}
The setter for price validates the given input.
string Book::getAuthorName() const {
return author.getName();
}
Invoke the getName() of the data member author, which returns the author's name of this Book instance.
TestBook.cpp
1/* Test Driver for the Book class (TestBook.cpp) */
2#include <iostream>
3#include "Book.h"
4using namespace std;
5
6int main() {
7 // Declare and construct an instance of Author
8 Author peter("Peter Jones", "[email protected]", 'm');
9 peter.print(); // Peter Jones (m) at [email protected]
10
11 // Declare and construct an instance of Book
12 Book cppDummy("C++ for Dummies", peter, 19.99);
13 cppDummy.setQtyInStock(88);
14 cppDummy.print();
15 // 'C++ for Dummies' by Peter Jones (m) at [email protected]
16
17 cout << cppDummy.getQtyInStock() << endl; // 88
18 cout << cppDummy.getPrice() << endl; // 19.99
19 cout << cppDummy.getAuthor().getName() << endl; // "Peter Jones"
20 cout << cppDummy.getAuthor().getEmail() << endl; // "[email protected]"
21 cout << cppDummy.getAuthorName() << endl; // "Peter Jones"
22
23 Book moreCpp("More C++ for Dummies", peter, -19.99);
24 // price should be positive! Set to 0
25 cout << moreCpp.getPrice() << endl; // 0
26}
The Default Copy Constructor
The initializer author(author) in the constructor invokes the so-called copy constructor. A copy
constructor creates a new instance by copying the given instance of the same class. If you do not provide
a copy constructor in your class, C++ provides a default copy constructor, which construct a new object
via memberwise copy. For example, for Author class, the default copy constructor provided by the
compiler is as follows:
// Default copy constructor of Author class provided by C++
Author::Author(const Author& other)
: name(other.name), email(other.email), gender(other.gender) { } // memberwise copy
Program Notes:
In C++, string is a class in the standard library (in header <string>, belonging to namespace std),
just like Point, Circle classes that we have defined.
Instead of including "using namespace std;", which is a poor practice as this statement will be
included in all the files using this header, we use the fully-qualified name std::string.
Instead of passing string objects by value into function, which affects performance as a clone copy
needs to be made. We pass the string objects by reference (indicated by &).
However, in pass-by-reference, changes inside the function will affect the caller's copy outside the
function.
If we do not intend to change the object inside the function, we could use keyword const to indicate
immutability. If the object is inadvertently changed inside the function, compiler would issue an error.
Author.cpp
1/* Implementation for the Author class (Author.cpp) */
2#include <iostream>
3#include "Author.h"
4using namespace std;
Program Notes:
Author::Author(const string & name, const string & email, char gender)
{ ...... }
In the constructor, the string objects are passed by reference. This improves the performance as it
eliminates the need of creating a temporary (clone) object. The constructor then invokes the copy
constructor of the string class to memberwise copy the arguments into its data
members name and email.
We make the parameters const to prevent them from modifying inside the function (with side effect
to the original copies).
Program Notes:
Book(const string & name, const Author & author, double price, int qtyInStock
= 0);
string and Author objects are passed into the constructor via reference. This improves performance
as it eliminates the creation of a temporary clone copy in pass-by-value. The parameters are
marked const as we do not intend to modify them inside the function (with side effect to the original
copies).
Author getAuthor() const;
The getter returns a copy of the data member author.
Book.cpp
1/* Implementation for the class Book (Book.cpp) */
2#include <iostream>
3#include "Book.h"
4using namespace std;
5
6// Constructor, with member initializer list to initialize the
7// component Author instance
8Book::Book(const string & name, const Author & author, double price, int qtyInStock)
9 : name(name), author(author) { // Init object reference in member initializer list
Unit II
Priority Queues: Definition, realizing a Priority Queue using Heaps, Insertion, Deletion, Heap sort, External Sorting
Min priority queue: Collection of elements in which the items can be inserted arbitrarily, but only smallest
element can be removed.
Max priority queue: Collection of elements in which insertion of items can be in any order but only largest
element can be removed.
In priority queue, the elements are arranged in any order and out of which only the smallest or
largest element allowed to delete each time.
The implementation of priority queue can be done using arrays or linked list. The data structure heap
is used to implement the priority queue effectively.
APPLICATIONS:
1. The typical example of priority queue is scheduling the jobs in operating system. Typically OS allocates
priority to jobs. The jobs are placed in the queue and position of the job in priority queue determines
their priority. In OS there are 3 jobs- real time jobs, foreground jobs and background jobs. The OS always
schedules the real time jobs first. If there is no real time jobs pending then it schedules foreground jobs.
Lastly if no real time and foreground jobs are pending then OS schedules the background jobs.
2. In network communication, the manage limited bandwidth for transmission the priority queue is used.
3. In simulation modeling to manage the discrete events the priority queue is
used. Various operations that can be performed on priority queue are-
1. Find an element
2. Insert a new element
3. Remove or delete an element
The abstract data type specification for a max priority queue is given below. The specification for a min
priority queue is the same as ordinary queue except while deletion, find and remove the element with
minimum priority
Heap is a tree data structure denoted by either a max heap or a min heap.
A max heap is a tree in which value of each node is greater than or equal to value of its children nodes. A
min heap is a tree in which value of each node is less than or equal to value of its children nodes.
18 4
12 4 12 14
11 10 18 20
Now if we want to insert 7. We cannot insert 7 as left child of 4. This is because the max heap has a property that
value of any node is always greater than the parent nodes. Hence 7 will bubble up 4 will be left child of 7.
18
12 7 inserted!
11
10 4
If we want to insert node 25, then as 25 is greatest element it should be the root. Hence 25 will bubble up and
18 will move down.
25 inserted!
12 18
11
10 4
The insertion strategy just outlined makes a single bubbling pass from a leaf toward the root. At each level
we do (1) work, so we should be able to implement the strategy to have complexity O(height) = O(log n).
For deletion operation always the maximum element is deleted from heap. In Max heap the maximum
element is always present at root. And if root element is deleted then we need to reheapify the tree.
25
12 18
11
10 4
Delete root element:25, Now we cannot put either 12 or 18 as root node and that should be greater than all its
children elements.
18
12 4
11 10
Now we cannot put 4 at the root as it will not satisfy the heap property. Hence we will bubble up 18 and place 18 at
root, and 4 at position of 18.
If 18 gets deleted then 12 becomes root and 11 becomes parent node of 10.
Thus deletion operation can be performed. The time complexity of deletion operation is O(log n).
1. Remove the maximum element which is present at the root. Then a hole is created at the root.
2. Now reheapify the tree. Start moving from root to children nodes. If any maximum element is found
then place it at root. Ensure that the tree is satisfying the heap property or not.
3. Repeat the step 1 and 2 if any more elements are to be deleted.
For deletion operation always the maximum element is deleted from heap. In Max heap the maximum
element is always present at root. And if root element is deleted then we need to reheapify the tree.
25
12 18
11
10 4
Delete root element:25, Now we cannot put either 12 or 18 as root node and that should be greater than all its
children elements.
18
12 4
11 10
Now we cannot put 4 at the root as it will not satisfy the heap property. Hence we will bubble up 18 and place 18 at
root, and 4 at position of 18.
If 18 gets deleted then 12 becomes root and 11 becomes parent node of 10.
HEAP SORT
Heap sort is a method in which a binary tree is used. In this method first the heap is created using binary tree and
then heap is sorted using priority queue.
25 57 48 38 10 91 84 33
In the heap sort method we first take all these elements in the array ―A‖
Now start building the heap structure. In forming the heap the key point is build heap in such a way
that the highest value in the array will always be a root.
Insert 25
swap(&arr[i], &arr[largest]);
heapify(arr, n, i);
All the algorithms require that the input fit into main memory. There are, some applications
where the input is much too large to fit into memory.
To do so, external sorting algorithms are designed to handle very large inputs.
Internal sorting deals with the ordering of records of a file in the ascending or descending
order when the whole file or list is compact enough to be accommodate in the internal
memory of the computer.
In many applications and problems it is quite common to encounter huge files comprising
millions of records which need to be sorted for their effective use in the application concerned.
The application domains of e-governance, digital library, search engines, on-line telephone
directory and electoral system, to list a few, deal with voluminous files of records.
Majority of the internal sorting techniques are virtually incapable of sorting large files since they require the whole
file in the internal memory of the computer, which is impossible. Hence the need for external sorting methods
which are exclusive strategies to sort huge files.
External sorting is a term for a class of sorting algorithms that can handle massive amounts of data. External
sorting is required when the data being sorted do not fit into the main memory of a computing device (usually
RAM) and instead they must reside in the slower external memory (usually a hard drive). External sorting typically
uses a hybrid sort-merge strategy. In the sorting phase, chunks of data small enough to fit in main memory are read,
sorted, and written out to a temporary file. In the merge phase, the sorted sub-files are combined into a single
larger file.
One example of external sorting is the external merge sort algorithm, which sorts chunks that each fit
in RAM, then merges the sorted chunks together. We first divide the file into runs such that the size of a run is small
enough to fit into main memory. Then sort each run in main memory using merge sort sorting algorithm. Finally
merge the resulting runs together into successively bigger runs, until the file is sorted.
Due to their large volume, the files are stored in external storage devices such as tapes, disks
or drums.
The external sorting strategies therefore need to take into consideration the kind of medium on
which the files reside, since these influence their work strategy.
A common principal behind most popular external sorting methods is outlined below:
1. Internally sort batches of records from the source file to generate runs. Write out the runs as
and when they are generated on to the external storage devices.
2. Merge the runs generated in the earlier phase, to obtain larger but fewer runs, and write them
out onto the external storage devices.
3. Repeat the run generated and merge, until in the final phase only one run gets generated, on which
Block of
Intermed
Second
iate fies
ary
Main
DICTIONARIES:
Dictionary is a collection of pairs of key and value where every value is associated with the
corresponding key.
Basic operations that can be performed on dictionary are:
1. Insertion of value in the dictionary
2. Deletion of particular value from dictionary
3. Searching of a specific value with the help of key
The dictionary can be represented as a linear list. The linear list is a collection of pair and
value. There are two method of representing linear list.
1. Sorted Array- An array data structure is used to implement the dictionary.
2. Sorted Chain- A linked list data structure is used to implement the dictionary
class dictionary
private:
int k,data;
struct
node
{
public: int key;
int value;
public: struct node *next;
} *head;
dictionary();
}; void insert_d( );
void delete_d( );
v id display_d( );
o void length();
New/head/curr/prev
1 10 NULL
New
4 20 NULL
Compare the key value of ‗curr‘ and ‗New‘ node. If New->key > Curr->key then attach New
node to ‗curr‘ node.
prev=curr
1 10 20 NULL
4
Add a new node <7,80> then
If we insert <3,15> then we have to search for it proper position by comparing key
value. (curr->key < New->key) is false. Hence else part will get executed.
1 10 4 20
7 80 NULL
3 15
The delete operation:
Case 1: Initially assign ‗head‘ node as ‗curr‘ node.Then ask for a key value of the node which is
to be deleted. Then starting from head node key value of each jode is cked and compared with
the desired node‘s key value. We will get node which is to be deleted in variable ‗curr‘. The
node given by variable ‗prev‘ keeps track of previous node of ‗cuu‘ node. For eg, delete node
with key value 4 then
cur
1 10 3 15 4 20 7 80 NULL
se 2:
curr hea
d
1 10 4 20 7 80 NULL
3 15
head
3 15 4 20 7 80 NULL
1 2 3 4 5 6 7
head tail
node node
The skip list is an efficient implementation of dictionary using sorted chain. This is because
in skip list each node consists of forward references of more than one node at a time.
Eg:
null
Now to search any node from above given sorted chain we have to search the sorted chain
from head node by visiting each node. But this searching time can be reduced if we add one
level in every alternate node. This extra level contains the forward pointer of some
node. That means in sorted chain come nodes can holds pointers to more than one node.
NULL
If we want to search node 40 from above chain there we will require comparatively less
time. This search again can be made efficient if we add few more pointers forward
NULL
references.
skip list
Element *next
Searching:
The desired node is searched with the help of a key value.
Searching for a key within a skip list begins with starting at header at the overall list level
and moving forward in the list comparing node keys to the key_val. If the node key is less than
the key_val, the search continues moving forward at the same level. If o the other hand, the
node key is equal to or greater than the key_val, the search drops one level and continues
forward. This process continues until the desired key_val has been found if it is present in the
skip list. If it is not, the search will either continue at the end of the list or until the first key
with a value greater than the search key is found.
Insertion:
There are two tasks that should be done before insertion operation:
1. Before insertion of any node the place for this new node in the skip list is searched.
Hence before any insertion to take place the search routine executes. The last[] array in
the search routine is used to keep track of the references to the nodes where the
search, drops down one level.
2. The level for the new node is retrieved by the routine randomelevel()
UNIT III
HASH TABLE REPRESENTATION
Hash table is a data structure used for storing and retrieving data very quickly. Insertion
of data in the hash table is based on the key value. Hence every entry in the hash table
is associated with some key.
Using the hash key the required piece of data can be searched in the hash table by few
or more key comparisons. The searching time is then dependent upon the size of the
hash table.
The effective representation of dictionary can be done using hash table. We can place
the dictionary entries in the hash table using hash function.
HASH FUNCTION
Hash function is a function which is used to put the data in the hash table. Hence one
can use the same hash function to retrieve the data from the hash table. Thus hash
function is used to implement the hash table.
For example: Consider that we want place some employee records in the hash table The
record of employee is placed with the help of key: employee ID. The employee ID is a 7 digit
number for placing the record in the hash table. To place the record 7 digit number is
converted into 3 digits by taking only last three digits of the key.
th
If the key is 496700 it can be stored at 0 position. The second key 8421002, the record of
nd
those key is placed at 2 position in the array.
Hence the hash function will be- H(key) = key%1000
Where key%1000 is a hash function and key obtained by hash function is called hash key.
Bucket and Home bucket: The hash function H(key) is used to map several dictionary
entries in the hash table. Each position of the hash table is called bucket.
The function H(key) is home bucket for the dictionary with pair whose value is key.
There are various types of hash functions that are used to place the record in the hash table-
1 72
54%10=4 2 54
72%10=2 3
89%10=9 4
37%10=7 5
8
9
2. Mid Square:
In the mid square method, the key is squared and the middle or mid part of the result is used
as the index. If the key is a string, it has to be preprocessed to produce a number. Consider
that if we want to place a record 3111 then
2
3111 = 9678321
for the hash table of size 1000
H(3111) = 783 (the middle 3 digits)
H(key) = floor(p *(fractional part of key*A)) where p is integer constant and A is constant real
number.
H(key) = floor(50*(107*0.61803398987))
= floor(3306.4818458045)
= 3306
At 3306 location in the hash table the record 107 will be placed.
4. Digit Folding:
The key is divided into separate parts and using some simple operation these parts
are combined to produce the hash key.
For eg; consider a record 12365412 then it is divided into separate parts as 123 654 12
and these are added together
H(key) = 123+654+12
= 789
The record will be placed at location 789
5. Digit Analysis:
The digit analysis is used in a situation when all the identifiers are known in advance.
We first transform the identifiers into numbers using some radix, r. Then examine the digits of
each identifier. Some digits having most skewed distributions are deleted. This deleting of
digits is continued until the number of remaining digits is small enough to give an address in
the range of the hash table. Then these digits are used to calculate the hash address.
COLLISION
the hash function is a function that returns the key value using which the record can be placed
in the hash table. Thus this function helps us in placing the record in the hash table at
appropriate position and due to this we can retrieve the record directly from that location. This
function need to be designed very carefully and it should not return the same hash key
address for two different records. This is an undesirable situation in hashing.
Definition: The situation in which the hash function returns the same hash key (home bucket)
for more than one record is called collision and two same hash keys returned for different
records is called synonym.
Similarly when there is no room for a new pair in the hash table then such a situation is
called overflow. Sometimes when we handle collision it may lead to overflow conditions.
Collision and overflow show the poor hash functions.
0
For example,
1 131
In collision handling method chaining is a concept which introduces an additional field with
data i.e. chain. A separate chain table is maintained for colliding data. When collision occurs
then a linked list(chain) is maintained at the home bucket.
For eg;
Here D = 10
0
131 21 61 NULL
1
3 NULL
131 61
NULL
7 97 NULL
A chain is maintained for colliding elements. for instance 131 has a home bucket (key) 1.
similarly key 21 and 61 demand for home bucket 1. Hence a chain is maintained at index 1.
OPEN ADDRESSING – LINEAR PROBING
This is the easiest method of handling collision. When collision occurs i.e. when two records
demand for the same home bucket in the hash table then collision can be solved by placing the
second record linearly down whenever the empty bucket is found. When use linear probing
(open addressing), the hash table is represented as a one-dimensional array with indices that
range from 0 to the desired table size-1. Before inserting any elements into this table, we must
initialize the table to represent the situation where all slots are empty. This allows us to detect
overflows and collisions when we inset elements into the table. Then using some suitable hash
function the element can be inserted into the hash table.
For example:
We will use Division hash function. That means the keys are placed using the formula
at H(key) = 131 % 10
=1
Index 1 will be the home bucket for 131. Continuing in this fashion we will place 4, 8, 7.
Now the next key to be inserted is 21. According to the hash function
H(key)=21%10
H(key) = 1
But the index 1 location is already occupied by 131 i.e. collision occurs. To resolve this collision
we will linearly move down and at the next empty location we will prob the element.
Therefore 21 will be placed at the index 2. If the next element is 5 then we get the home
bucket for 5 as index 5 and this bucket is empty so we will put the element 5 at index 5.
Index 0
Key Key Key
39
19%10 = 9 cluster is formed
29
18%10 = 8 8
39%10 = 9
29%10 = 9
8%10 = 8
19
empty this cluster problem can be solved by quadratic probing.
QUADRATIC PROBING:
Quadratic probing operates by taking the original hash value and adding successive values of
an arbitrary quadratic polynomial to the starting value. This method uses following formula.
2
H(key) = (Hash(key) + i ) % m)
where m can be table size or any prime number.
for eg; If we have to insert following elements in the hash table with table size 10: 37, 90, 55, 22,
17, 49, 87 0 90
11
1 22
37 % 10 = 7 2
55
90 % 10 = 0 3
55 % 10 = 5 4 37
22 % 10 = 2 5
11 % 10 = 1 6
7
Now if we want to place 17 a collision will occur as 17%10 = 7 and 8
bucket 7 has already an element 37. Hence we will apply 9
quadratic probing to insert this record in the hash
2
table. Hi (key) = (Hash(key) + i ) % m
Consider i = 0 then
2
(17 + 0 ) % 10 = 7
2
(17 + 1 ) % 10 = 8, when i =1
49 % 10 = 9 3
55
4
37
5 49
6
7
9
Now to place 87 we will use quadratic probing.
0 90
(87 + 0) % 10 = 7 1 11
22
(87 + 1) % 10 = 8… but already occupied 2
2
(87 + 2 ) % 10 = 1.. already occupied 3 55
2
(87 + 3 ) % 10 = 6 4 87
37
5 49
It is observed that if we want place all the necessary elements 6
in the hash table the size of divisor (m) should be twice as 7
large as total number of elements. 8
9
DOUBLE HASHING
Double hashing is technique in which a second hash function is applied to the key when a
collision occurs. By applying the second hash function we will get the number of positions from
the point of collision to insert.
There are two important rules to be followed for the second
function: it must never evaluate to zero.
must make sure that all cells can be probed.
The formula to be used for double hashing is
90
Consider the following elements to be placed in the hash table of size 10 37, 90, 45,
22, 17, 49, 55
45
Initially insert the elements using the formula for H1(key). Insert 37, 90,
45, 22
H1(37) = 37 % 10 = 7 37
H1(90) = 90 % 10 = 0
49
H1(45) = 45 % 10 = 5
H1(22) = 22 % 10 = 2
H1(49) = 49 % 10 = 9
Now if 17 to be inserted then
Key
90
H1(17) = 17 % 10 = 7
17
H2(key) = M – (key % M) 22
Here M is prime number smaller than the size of the table. Prime number
smaller than table size 10 is 7
45
Hence M = 7 H2(17)
= 7-(17 % 7) 37
ave to take
=7–3=4
49
That means we have to insert the element 17 at 4 places from 37. In short we
h 4 jumps. Therefore the 17 will be placed at index 1.
H1(55) = 55 % 10 =5 Collision 90
17
H2(55) = 7-(55 % 7) 22
=7–6=1
That means we have to take one jump from index 5 to place 55.
45
Finally the hash table will be - 55
Comparison of Quadratic Probing & Double Hashing
The double hashing requires another hash function whose probing efficiency is same
as some another hash function required when handling random collision.
The double hashing is more complex to implement than quadratic probing. The
quadratic probing is fast technique than double hashing.
REHASHING
Rehashing is a technique in which the table is resized, i.e., the size of table is doubled by
creating a new table. It is preferable is the total size of table is a prime number. There are
situations in which the rehashing is required.
Consider we have to insert the elements 37, 90, 55, 22, 17, 49, and 87. the table size is 10
and will use hash function.,
37 % 10 = 7
90 % 10= 0
55 % 10 = 5
22 % 10 = 2
17 % 10 = 7 Collision solved by linear probing
49 % 10 = 9
Now this table is almost full and if we try to insert more elements collisions will occur and
eventually further insertions will fail. Hence we will rehash by doubling the table size. The old
table size is 10 then we should double this size for new table, that becomes 20. But 20 is not a
prime number, we will prefer to make the table size as 23. And new hash function will be
H(key) key mod 23 0
90
1 11
22
37 % 23 = 14 2
90 % 23 = 21 3 55
55 % 23 = 9 4 87
37
22 % 23 = 22 5 49
17 % 23 = 17 6
49 % 23 = 3 7
87 % 23 = 18 8
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1. This technique provides the programmer a flexibility to enlarge the table size if required.
2. Only the space gets doubled with simple hash function which avoids occurrence
of collisions.
EXTENSIBLE HASHING
Extensible hashing is a technique which handles a large amount of data. The data to
be placed in the hash table is by extracting certain number of bits.
Extensible hashing grow and shrink similar to B-trees.
In extensible hashing referring the size of directory the elements are to be placed
in buckets. The levels are indicated in parenthesis.
0
1
Levels
0) (1)
001
111
010 data to be
placed in
bucket
The bucket can hold the data of its global depth. If data in bucket is more than
global depth then, split the bucket and double the directory.
Consider we have to insert 1, 4, 5, 7, 8, 10. Assume each page can hold 2 data entries (2 is
the depth).
Step 1: Insert 1, 4
1 = 001
4 = 100
010
Insert 5. The bucket is full. Hence double the directory.
Thus the data is inserted using extensible hashing.
Deletion Operation:
00 01 10 11
Delete 7.
00 01 10 11
(1) (1)
100 001 Note that the level was increased
1000 101 when we insert 7. Now on deletion
of 7, the level should get decremented.
00 00 10 11
(1) (1)
100 001
101
Applications of hashing:
This method is used to carry out dictionary Skip lists are used to implement dictionary
operations using randomized processes. operations using randomized process.
If the sorted data is given then hashing is The sorted data improves the performance
not an effective method to implement of skip list.
dictionary.
The space requirement in hashing is for The forward pointers are required for every
hash table and a forward pointer is required level of skip list.
per node.
Hashing is an efficient method than skip The skip lists are not that mush efficient.
lists.
Skip lists are more versatile than hash Worst case space requirement is larger
table. for skip list than hashing.
Unit IV
TREES
A Tree is a data structure in which each element is attached to one or more elements directly beneath it.
Level 0
1
B
C D
E F G
H I J
2
K L
3
Terminology
1. Root: This is the unique node in the tree to which further sub trees are attached. Eg: A
Degree of the node: The total number of sub-trees attached to the node is called the degree of
the node.Eg: For node A degree is 3. For node K degree is 0
3. Leaves: These are the terminal nodes of the tree. The nodes with degree 0 are always the leaf nodes.
Eg: E, F, K, L,H, I, J
4. Internal nodes: The nodes other than the root node and the leaves are called the internal nodes.
Eg: B, C, D, G
5. Parent nodes: The node which is having further sub-trees(branches) is called the parent node
of those sub-trees. Eg: B is the parent node of E and F.
6. Predecessor: While displaying the tree, if some particular node occurs previous to some other
node then that node is called the predecessor of the other node. Eg: E is the predecessor of the
node B.
7. Successor: The node which occurs next to some other node is a successor node. Eg: B is
the successor of E and F.
8. Level of the tree: The root node is always considered at level 0, then its adjacent children are
supposed to be at level 1 and so on. Eg: A is at level 0, B,C,D are at level 1, E,F,G,H,I,J are at level 2, K,L
are at level 3.
9. Height of the tree: The maximum level is the height of the tree. Here height of the tree is 3.
The height if the tree is also called depth of the tree.
10. Degree of tree: The maximum degree of the node is called the degree of the tree.
BINARY TREES
Binary tree is a tree in which each node has at most two children, a left child and a right child. Thus
the order of binary tree is 2.
A binary tree is a finite set of nodes which is either empty or consists of a root and two disjoint
trees called left sub-tree and right sub- tree.
In binary tree each node will have one data field and two pointer fields for representing
the sub-branches. The degree of each node in the binary tree will be at the most two.
1. Left skewed binary tree: If the right sub-tree is missing in every node of a tree we call it as left skewed
tree.
A
C
2. Right skewed binary tree: If the left sub-tree is missing in every node of a tree we call it is right
sub-tree.
B C
D E F G
Note:
n
1. A binary tree of depth n will have maximum 2 -1 nodes.
2. A complete binary tree of level l will have maximum 2l nodes at each level, where l starts from 0.
3. Any binary tree with n nodes will have at the most n+1 null branches.
4. The total number of edges in a complete binary tree with n terminal nodes are 2(n-1).
a) Sequential Representation
b) Linked Representation
a) Sequential Representation
The simplest way to represent binary trees in memory is the sequential representation that
uses one-dimensional array.
1) The root of binary tree is stored in the 1 st location of array
th
2) If a node is in the j location of array, then its left child is in the location 2J+1 and its right
child in the location 2J+2
d+1
The maximum size that is required for an array to store a tree is 2 -1, where d is the depth of the tree.
2. Insertions and deletions which are the most common operations can be done
without moving the nodes.
Disadvantages of linked representation:
1. This representation does not provide direct access to a node and special algorithms
are required.
2. This representation needs additional space in each node for storing the left and right
sub- trees.
Traversing a tree means that processing it so that each node is visited exactly once. A
binary tree can be
traversed a number of ways.The most common tree traversals are
In-order
Pre-order
and Post-
order
Pre-order 1.Visit the root Root | Left | Right
2.Traverse the left sub tree in pre-order
3.Traverse the right sub tree in pre-order.
In-order 1.Traverse the left sub tree in in-order Left | Root | Right
2. Visit the root
3. Traverse the right sub tree in in-order.
Post-order 1.Traverse the left sub tree in post-order Left | Right | Root
2.Traverse the right sub tree in post-order.
3.Visit the root
B C
D E F G
H I J
nd th
Print 2 Print 4
B D
C Print this
s at the last E
t
Print 1
C-B-A-D-E is the inorder traversal i.e. first we go towards the leftmost node. i.e. C so print that node
C. Then go back to the node B and print B. Then root node A then move towards the right sub-
tree print D and finally E. Thus we are following the tracing sequence of Left|Root|Right. This type of
traversal is called inorder traversal. The basic principle is to traverse left sub-tree then root and then
the right sub-tree.
Pseudo Code:
A-B-C-D-E is the preorder traversal of the above fig. We are following Root|Left|Right path
i.e. data at the root node will be printed first then we move on the left sub-tree and go on
printing the data till we reach to the left most node. Print the data at that node and then move
to the right sub- tree. Follow the same principle at each sub-tree and go on printing the data
accordingly.
Before Insertion
In the above fig, if we wan to insert 23. Then we will start comparing 23 with value of root node
i.e. 10. As 23 is greater than 10, we will move on right sub-tree. Now we will compare 23 with 20
and move right, compare 23 with 22 and move right. Now compare 23 with 24 but it is less than
24. We will move on left branch of 24. But as there is node as left child of 24, we can attach 23
as left child of 24.
Deletion of a node from binary search tree.
For deletion of any node from binary search tree there are three which are possible.
i. Deletion of leaf node.
ii. Deletion of a node having one child.
iii. Deletion of a node having two children.
Deletion of leaf node.
This is the simplest deletion, in which we set the left or right pointer of parent node as NULL.
10
7 15
Before deletion
5 9 12 18
From the above fig, we want to delete the node having value 5 then we will set left pointer of its
parent node as NULL. That is left pointer of node having value 7 is set to NULL.
Deletion of a node having one child.
AVL TREES
Adelsion Velski and Lendis in 1962 introduced binary tree structure that is balanced
with respect to height of sub trees. The tree can be made balanced and because of this
retrieval
of any node can be done in Ο(log n) times, where n is total number of nodes. From the name of these
scientists the tree is called AVL tree.
Definition:
An empty tree is height balanced if T is a non empty binary tree with TL and TR as
its left and right sub trees. The T is height balanced if and only if
i. TL and TR are height balanced.
ii. hL-hR <= 1 where hL and hR are heights of TL and TR.
The idea of balancing a tree is obtained by calculating the balance factor of a tree.
The balance factor BF(T) of a node in binary tree is defined to be hL-hR where hL and
hR are heights of left and right sub trees of T.
For any node in AVL tree the balance factor i.e. BF(T) is -1, 0 or +1.
Height of AVL Tree:
Theorem: The height of AVL tree with n elements (nodes) is O(log n).
Proof: Let an AVL tree with n nodes in it. Nh be the minimum number of nodes in an AVL tree
of height h.
In worst case, one sub tree may have height h-1 and other sub tree may have height h-2. And both
these sub trees are AVL trees. Since for every node in AVL tree the height of left and right sub trees
differ by at most 1.
Hence
N =N +N
h h-1
+1
h-2
h. N0=0 N1=2
N > Nh = Nh-1+Nh-2+1
> 2Nh-2
> 4Nh-4
.
.
> 2iNh-2i
N > 2h/2-1N2
= O(log N)
This proves that height of AVL tree is always O(log N). Hence search, insertion and deletion can
be carried out in logarithmic time.
Representation of AVL Tree
The AVL tree follows the property of binary search tree. In fact AVL trees are
basically binary search trees with balance factors as -1, 0, or +1.
After insertion of any node in an AVL tree if the balance factor of any node
becomes other than -1, 0, or +1 then it is said that AVL property is violated. Then
we have to restore the destroyed balance condition. The balance factor is denoted
at
After insertion of a new node if balance condition gets destroyed, then the nodes on that path(new
node insertion point to root) needs to be readjusted. That means only the affected sub tree is to be
rebalanced.
The rebalancing should be such that entire tree should satisfy AVL property.
There are four different cases when rebalancing is required after insertion of new node.
1. An insertion of new node into left sub tree of left child. (LL).
2. An insertion of new node into right sub tree of left child. (LR).
3. An insertion of new node into left sub tree of right child. (RL).
4. An insertion of new node into right sub tree of right child.(RR).
Some modifications done on AVL tree in order to rebalance it is called rotations of AVL tree
Insertion Algorithm:
1. Insert a new node as new leaf just as an ordinary binary search tree.
2. Now trace the path from insertion point(new node inserted as leaf) towards root. For each node
‗n‘ encountered, check if heights of left (n) and right (n) differ by at most 1. a)If yes,
move towards parent (n).
b)Otherwise restructure by doing either a single rotation or a double rotation.
Thus once we perform a rotation at node ‗n‘ we do not require to perform any rotation at
any ancestor on ‗n‘.
When node ‗1‘ gets inserted as a left child of node ‗C‘ then AVL property gets destroyed
i.e. node A has balance factor +2.
The LL rotation has to be applied to rebalance the nodes.
2. RR rotation:
When node ‗4‘ gets attached as right child of node ‗C‘ then node ‗A‘ gets unbalanced. The
rotation which needs to be applied is RR rotation as shown in fig.
When node ‗3‘ is attached as a right child of node ‗C‘ then unbalancing occurs because of LR.
Hence LR rotation needs to be applied.
When node ‗2‘ is attached as a left child of node ‗C‘ then node ‗A‘ gets unbalanced as its
balance factor becomes -2. Then RL rotation needs to be applied to rebalance the AVL tree.
Example:
To insert node ‗1‘ we have to attach it as a left child of ‗2‘. This will unbalance the tree as
follows. We will apply LL rotation to preserve AVL property of it.
Insert 25
We will attach 25 as a right child of 18. No balancing is required as entire tree preserves the AVL
property
Insert 28
The node ‗28‘ is attached as a right child of 25. RR rotation is required to rebalance.
Insert 12
Even after deletion of any particular node from AVL tree, the tree has to be restructured in order
to preserve AVL property. And thereby various rotations need to be applied.
2. a) If the node to be deleted is a leaf node then simply make it NULL to remove.
b) If the node to be deleted is not a leaf node i.e. node may have one or two children, then the
node must be swapped with its in order successor. Once the node is swapped, we can remove
this node.
3. Now we have to traverse back up the path towards root, checking the balance factor of
every node along the path. If we encounter unbalancing in some sub tree
then balance that sub tree using appropriate single or double rotations. The deletion
algorithm takes O(log n) time to delete any node.
The tree becomes
Searching:
The searching of a node in an AVL tree is very simple. As AVL tree is basically binary search tree, the
algorithm used for searching a node from binary search tree is the same one is used to search a node
from AVL tree.
PART II
BTREES
Multi-way trees are tree data structures with more than two branches at a node. The data
structures of m-way search trees, B trees and Tries belong to this category of tree
structures.
AVL search trees are height balanced versions of binary search trees, provide efficient
retrievals and storage operations. The complexity of insert, delete and search operations on
AVL search trees id O(log n).
Applications such as File indexing where the entries in an index may be very large,
maintaining the index as m-way search trees provides a better option than AVL search trees
which are but only balanced binary search trees.
While binary search trees are two-way search trees, m-way search trees are extended binary
search trees and hence provide efficient retrievals.
B trees are height balanced versions of m-way search trees and they do not recommend
representation of keys with varying sizes.
Tries are tree based data structures that support keys with varying sizes.
Definition:
A B tree of order m is an m-way search tree and hence may be empty. If non empty, then the
following properties are satisfied on its extended tree representation:
i. The root node must have at least two child nodes and at most m child nodes.
ii. All internal nodes other than the root node must have at least |m/2 | non empty child nodes and at
most m non empty child nodes.
iii. The number of keys in each internal node is one less than its number of child nodes and these
keys partition the keys of the tree into sub trees.
iv. All external nodes are at the same
level. v.
Example:
F K O B tree of order 4
Level 1
C D G M N P Q W
S T X Y Z
Level
3
Insertion
For example construct a B-tree of order 5 using following numbers. 3, 14, 7, 1, 8, 5, 11, 17, 13, 6, 23, 12,
20, 26, 4, 16, 18, 24, 25, 19
The order 5 means at the most 4 keys are allowed. The internal node should have at least 3 non empty
children and each leaf node must contain at least 2 keys.
1 3 7 14
.
Step 2: Insert 8, Since the node is full split the node at medium 1, 3, 7, 8, 14
1 3 8 14
1 3 5
8 11 14 17
Step 4: Now insert 13. But if we insert 13 then the leaf node will have 5 keys which is not allowed.
Hence 8,
11, 13, 14, 17 is split and medium node 13 is moved up.
7 13
1 3 5 8 11 14 17
Step 5: Now insert 6, 23, 12, 20 without any split.
7 13
1 3 5 6 8 11 12 14 17 20 23
Step 6: The 26 is inserted to the right most leaf node. Hence 14, 17, 20, 23, 26 the node is split and 20
will be moved up.
7 13 20
1 3 5 6 8 11 12 14 17 23 26
Step 7: Insertion of node 4 causes left most node to split. The 1, 3, 4, 5, 6 causes key 4 to move up.
4 7 13 20
1 3 5 6 8 11 12 14 16 17 18 23 24 25 26
Step 8: Finally insert 19. Then 4, 7, 13, 19, 20 needs to be split. The median 13 will be moved up
to from a root node.
The tree then will be -
13
4 7 17 20
1 3 5 6 8 11 12 14 16 18 19 23 24 25 26
Deletion
Consider a B-tree
4 7 17 20
1 3 5 6 8 11 12 14 16 18 19 23 24 25 26
Delete 8, then it is very simple.
13
4 7 17 20
1 3 5 6 11 12 14 16 18 19 23 24 25 26
Now we will delete 20, the 20 is not in a leaf node so we will find its successor which is 23, Hence
23 will be moved up to replace 20.
13
4 7 17 23
1 3 5 6 11 12 14 16 18 19 24 25 26
Next we will delete 18. Deletion of 18 from the corresponding node causes the node with only
one key, which is not desired (as per rule 4) in B-tree of order 5. The sibling node to immediate
right has an extra key. In such a case we can borrow a key from parent and move spare key of
sibling up.
4 7 24
17
1 3 5 6 11 12 14 16 19 23 25 26
Now delete 5. But deletion of 5 is not easy. The first thing is 5 is from leaf node. Secondly this leaf
node has no extra keys nor siblings to immediate left or right. In such a situation we can combine
this node with one of the siblings. That means remove 5 and combine 6 with the node 1, 3. To make
the tree balanced we have to move parent‘s key down. Hence we will move 4 down as 4 is between
1, 3, and 6. The tree will be-
13
7 17 24
1 3 4 6 11 12 14 16 19 23 25 26
But again internal node of 7 contains only one key which not allowed in B-tree. We then will try to borrow
a key from sibling. But sibling 17, 24 has no spare key. Hence we can do is that, combine 7 with 13 and
17, 24. Hence the B-tree will be
1 3 4 6 12 16 23 26
11 14 19 25
Searching
The search operation on B-tree is similar to a search to a search on binary search tree. Instead of choosing
between a left and right child as in binary tree, B-tree makes an m-way choice. Consider a B-tree as given
below.
13
4 7
17
20
1 3 5 6 8 11 12 14 16 18 19 23 24 25 26
The running time of search operation depends upon the height of the tree. It is O(log n).
Height of B-tree
The maximum height of B-tree gives an upper bound on number of disk access. The maximum
number of keys in a B-tree of order 2m and depth h is
2 h-1
1 + 2m + 2m(m+1) + 2m(m+1) + . . .+ 2m(m+1)
h
i-1
The maximum height of B-tree with n keys
UNIT V
Terminology of Graph
Graphs:-
A graph G is a discrete structure consisting of nodes (called vertices) and lines joining the
nodes (called edges). Two vertices are adjacent to each other if they are joint by an edge. The
edge joining the two vertices is said to be an edge incident with them. We use V (G) and E(G)
to denote the set of vertices and edges of G respectively.
Incidence Matrix
In this representation, graph can be represented using a matrix of size total
number of vertices by total number of edges. That means if a graph with 4
vertices and 6 edges can be represented using a matrix of 4X6 class. In this matrix,
rows represents vertices and columns represents edges. This matrix is filled with
either 0 or 1 or -1. Here, 0 represents row edge is not connected to column
vertex, 1 represents row edge is connected as outgoing edge to column vertex
and -1 represents row edge is connected as incoming edge to column vertex.
Graph traversals
Graph traversal means visiting every vertex and edge exactly once in a well-defined order.
While using certain graph algorithms, you must ensure that each vertex of the graph is visited
exactly once. The order in which the vertices are visited are important and may
depend upon the algorithm or question that you are solving.
During a traversal, it is important that you track which vertices have been visited. The
most common way of tracking vertices is to mark them.
This recursive nature of DFS can be implemented using stacks. The basic idea is
as follows: Pick a starting node and push all its adjacent nodes into a stack.
Pop a node from stack to select the next node to visit and push all its adjacent nodes into a
stack.
Repeat this process until the stack is empty. However, ensure that the nodes that are
visited are marked. This will prevent you from visiting the same node more than
once. If you do not mark the nodes that are visited and you visit the same node more
than once, you may end up in an infinite loop.