2. A C++ program
#include <iostream.h>
int main() {
//variable declaration
//read values input from user
//computation and print output to user
return 0;
}
After you write a C++ program you compile it; that is, you run a
program called compiler that checks whether the program follows the
C++ syntax
– if it finds errors, it lists them
– If there are no errors, it translates the C++ program into a program
in machine language which you can execute
2
G.JAYABHARATHI,Assistant
professor
3. Hello world program
#include <iostream.h>
int main() {
cout << “Hello world!”;
return 0;
}
3
G.JAYABHARATHI,Assistant
professor
4. Variable declaration
type variable-name;
Meaning: variable <variable-name> will be a variable of type <type>
Where type can be:
– int //integer
– double //real number
– char //character
Example:
int a, b, c;
double x;
int sum;
char my-character;
4
G.JAYABHARATHI,Assistant
professor
5. Input statements
cin >> variable-name;
Meaning: read the value of the variable called <variable-name> from the
user
Example:
cin >> a;
cin >> b >> c;
cin >> x;
cin >> my-character;
5
G.JAYABHARATHI,Assistant
professor
6. Output statements
cout << variable-name;
Meaning: print the value of variable <variable-name> to the user
cout << “any message “;
Meaning: print the message within quotes to the user
cout << endl;
Meaning: print a new line
Example:
cout << a;
cout << b << c;
cout << “This is my character: “ << my-character << “ he he he”
<< endl;
6
G.JAYABHARATHI,Assistant
professor
7. If statements
if (condition) {
S1;
}
else {
S2;
}
S3;
condition
S1 S2
S3
True False
7
G.JAYABHARATHI,Assistant
professor
8. Boolean conditions
• Comparison operators
== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal
• Boolean operators
&& and
|| or
! not
8
G.JAYABHARATHI,Assistant
professor
9. Examples
Assume we declared the following variables:
int a = 2, b=5, c=10;
Here are some examples of boolean conditions we can use:
• if (a == b) …
• if (a != b) …
• if (a <= b+c) …
• if(a <= b) && (b <= c) …
• if !((a < b) && (b<c)) …
9
G.JAYABHARATHI,Assistant
professor
10. If example
#include <iostream.h>
void main() {
int a,b,c;
cin >> a >> b >> c;
if (a <=b) {
cout << “min is “ << a << endl;
}
else {
cout << “ min is “ << b << endl;
}
cout << “happy now?” << endl;
}
10
G.JAYABHARATHI,Assistant
professor
12. While example
//read 100 numbers from the user and output their sum
#include <iostream.h>
void main() {
int i, sum, x;
sum=0;
i=1;
while (i <= 100) {
cin >> x;
sum = sum + x;
i = i+1;
}
cout << “sum is “ << sum << endl;
}
12
G.JAYABHARATHI,Assistant
professor
13. Data Structures
data object
•set or collection of instances
•integer = {0, +1, -1, +2, -2, +3, -3, …}
•daysOfWeek = {S,M,T,W,Th,F,Sa}
13
G.JAYABHARATHI,Asistant
professor
14. The relationships are usually specified by
specifying operations on one or more instances.
add, subtract, predecessor, multiply
Data Structure
14
G.JAYABHARATHI,Asistant
professor
15. Linear (or Ordered) Lists
instances are of the form
(e0, e1, e2, …, en-1)
where ei denotes a list element
n >= 0 is finite
list size is n
15
G.JAYABHARATHI,Asistant
professor
16. Linear Lists
L = (e0, e1, e2, e3, …, en-1)
relationships
e0 is the zero’th (or front) element
en-1 is the last element
ei immediately precedes ei+1
16
G.JAYABHARATHI,Asistant
professor
17. Linear List Abstract Data Type
AbstractDataType LinearList
{
instances
ordered finite collections of zero or more elements
operations
empty(): return true iff the list is empty, false otherwise
size(): return the list size (i.e., number of elements in the list)
get(index): return the indexth element of the list
indexO f(x): return the index of the first occurrence of x in
the list, return -1 if x is not in the list
erase(index): remove the indexth element,
elements with higher index have their index reduced by 1
insert(theIndex, x): insert x as the indexth element, elements
with theIndex >= index have their index increased by 1
output(): output the list elements from left to right
}
17
G.JAYABHARATHI,Asistant
professor
18. Linear List As C++ Abstract Class
template<class T>
class linearList
{
public:
virtual ~linearList() {};
virtual bool empty() const = 0;
virtual int size() const = 0;
virtual T& get(int theIndex) const = 0;
virtual int indexOf(const T& theElement)const = 0;
virtual void erase(int theIndex) = 0;
virtual void insert(int theIndex,
const T& theElement) = 0;
virtual void output(ostream& out) const = 0;
};
18
G.JAYABHARATHI,Asistant
professor
20. Fundamental Concepts
Some fundamental concepts that you should know:
– Dynamic memory allocation.
– Recursion.
– Performance analysis.
20
G.JAYABHARATHI,Asistant
professor
21. Performance Analysis
• There are problems and algorithms to solve them.
• Problems and problem instances.
• Example: Sorting data in ascending order.
– Problem: Sorting
– Problem Instance: e.g. sorting data (2 3 9 5 6 8)
– Algorithms: Bubble sort, Merge sort, Quick sort, Selection sort,
etc.
• Which is the best algorithm for the problem? How do we judge?
21
G.JAYABHARATHI,Asistant
professor
22. Performance Analysis
Two criteria are used to judge algorithms:
(i) time complexity
(ii) space complexity.
• Space Complexity of an algorithm is the amount of memory it needs to
run to completion.
• Time Complexity of an algorithm is the amount of CPU time it needs
to run to completion.
22
G.JAYABHARATHI,Asistant
professor
23. Space Complexity
• Memory space S(P) needed by a program P, consists of two
components:
– A fixed part: needed for instruction space (byte code), simple
variable space, constants space etc. c
– A variable part: dependent on a particular instance of input and
output data. Sp(instance)
• S(P) = c + Sp(instance)
23
G.JAYABHARATHI,Asistant
professor
24. Time Complexity
• Time required T(P) to run a program P also consists of two
components:
– A fixed part: compile time which is independent of the problem
instance c.
– A variable part: run time which depends on the problem instance
tp(instance)
• T(P) = c + tp(instance)
24
G.JAYABHARATHI,Asistant
professor
25. Time Complexity
• How to measure T(P)?
– Measure experimentally, using a “stop watch”
T(P) obtained in secs, msecs.
– Count program steps T(P) obtained as a step count.
• Fixed part is usually ignored; only the variable part tp() is measured.
25
G.JAYABHARATHI,Asistant
professor
26. Time Complexity
• What is a program step?
– a+b+b*c+(a+b)/(a-b) one step;
– comments zero steps;
– while (<expr>) do step count equal to the number of times
<expr> is executed.
– for i=<expr> to <expr1> do step count equal to number of times
<expr1> is checked.
26
G.JAYABHARATHI,Asistant
professor
27. Time Complexity: Example 1
Statements S/E Freq. Total
1 Algorithm Sum(a[],n) 0 - 0
2 { 0 - 0
3 S = 0.0; 1 1 1
4 for i=1 to n do 1 n+1 n+1
5 s = s+a[i]; 1 n n
6 return s; 1 1 1
7 } 0 - 0
2n+3 27
G.JAYABHARATHI,Asistant
professor
28. Time Complexity: Example 2
Statements S/E Freq. Total
1 Algorithm Sum(a[],n,m) 0 - 0
2 { 0 - 0
3 for i=1 to n do; 1 n+1 n+1
4 for j=1 to m do 1 n(m+1) n(m+1)
5 s = s+a[i][j]; 1 nm nm
6 return s; 1 1 1
7 } 0 - 0
2nm+2n+2 28
G.JAYABHARATHI,Asistant
professor
29. Performance Measurement
• Which is better?
– T(P1) = (n+1) or T(P2) = (n2 + 5).
– T(P1) = log (n2 + 1)/n! or T(P2) = nn(nlogn)/n2.
• Complex step count functions are difficult to compare.
• For comparing, ‘rate of growth’ of time and space complexity
functions is easy and sufficient.
29
G.JAYABHARATHI,Asistant
professor
30. Big O Notation
• Big O of a function gives us ‘rate of growth’ of the step count function
f(n), in terms of a simple function g(n), which is easy to compare.
Definition:
[Big O] The function f(n) = O(g(n)) (big ‘oh’ of g of n) iff there
exist positive constants c and n0 such that f(n) <= c*g(n) for all n,
n>=n0. See graph on next slide.
Example: f(n) = 3n+2 is O(n) because 3n+2 <= 4n for all n >= 2. c = 4, n0
= 2. Here g(n) = n.
30
G.JAYABHARATHI,Asistant
professor
32. FRIEND FUNCTIONS
•The access privileges in C++ such as
•Public
•Protected
•Private
32
G.JAYABHARATHI,Asistant
professor
33. Private
• The private member functions can be accessed only by the members of the
class.
• The class is considered as private by the C++ compiler, if no specifies is
declared for the member.
Public
• The member functions with public access specifies can be accessed outside
of the class.
• This kind of members is accessed by creating instance of the class.
33
G.JAYABHARATHI,Asistant
professor
34. Protected
• Protected members are accessible by the class itself and it's sub-classes.
• The members with protected specified act exactly like private as long as they
are referenced within the class or from the instance of the class.
• The protected members become private of a child class in case of private
inheritance, public in case of public inheritance, and stay protected in case of
protected inheritance.
34
G.JAYABHARATHI,Asistant
professor
35. Access specifier
Visible to own
class members
Visible to
objects of
same/other
class
public Yes Yes
private Yes No
protected Yes No
36. When we want our private data to be shared by a non member function
Then:
• Basically, we declare something as a friend, you give it access to your
private data members.
• Single functions or entire classes may be declared as friends of a class.
• Function definition must not use keyword friend.
36
G.JAYABHARATHI,Asistant
professor
38. Friend function characteristics
• It is not in scope of class.
• It cannot be called using object of that class.
• It can be invoked like a normal function.
• It should use a dot operator for accessing members.
• It can be public or private.
• It has objects as arguments.
• Perhaps the most common use of friend functions is overloading << and >> for
I/O.
38
G.JAYABHARATHI,Asistant
professor
39. Example
class demo
{
int x;
public:
demo(int a)
{
x=a;
}
friend void display(demo);
};
void display(demo d1)
{
cout<<d1.x;
}
void main()
{
demo d2(5);
display(d2);
}
39
G.JAYABHARATHI,Asistant
professor
40. class sample
Class sample
{
int a;
int b;
public:
void setval(){ a=25,b=40}
friend float mean(sample s);
};
float mean(sample s)
{ return (s.a+s.b)/2.0;
}
void main()
{ sample X;
cout<<mean(X);
}
40
G.JAYABHARATHI,Asistant
professor
41. Friend class
• It is possible that all member of the one class can be friend of another class.
• Friendship is not inherited, transitive, or reciprocal.
• Derived classes don’t receive the privileges of friendship.
• The privileges of friendship aren’t transitive.
41
G.JAYABHARATHI,Asistant
professor
42. Example
class demo
{
private:
int x,y;
public:
demo(int a,int b)
{
x=a;
y=b;
}
friend class frnd;
};
class frnd
{
public:
void display(demo d1)
{
cout<<“x is=”d1.x;
cout<<“y is=”d1.y;
}
};
void main()
{
demo d2(10,40);
frnd f1;
f1.display(d2);
getch();
}
42
G.JAYABHARATHI,Asistant
professor
43. Linked Representation
• list elements are stored, in memory, in an arbitrary order
• explicit information (called a link) is used to go from
one element to the next
G. JAYABHARATHI,Assistant
professor
43
44. Memory Layout
a b c d e
c a e d b
A linked representation uses an arbitrary layout.
Layout of L = (a,b,c,d,e) using an array representation.
G. JAYABHARATHI,Assistant
professor
44
45. Linked Representation
pointer (or link) in e is NULL
c a e d b
use a variable firstNode to get to the
first element a
firstNode
G. JAYABHARATHI,Assistant
professor
45
46. Normal Way To Draw A Linked List
link or pointer field of node
data field of node
a b c d e
NULL
firstNode
G. JAYABHARATHI,Assistant
professor
46
47. Chain
•A chain is a linked list in which each node represents one
element.
• There is a link or pointer from one element to the next.
• The last node has a NULL pointer.
a b c d e
NULL
firstNode
G. JAYABHARATHI,Assistant
professor
47
48. Node Representation
template <class T>
struct chainNode
{
// data members
T element;
chainNode<T> *next;
// constructors come here
};
next
element
G. JAYABHARATHI,Assistant
professor
48
49. Constructors Of chainNode
chainNode() {}
?
?
?
element
next
element
chainNode(const T& element)
{this->element = element;}
chainNode(const T& element, chainNode<T>* next)
{this->element = element;
this->next = next;}
G. JAYABHARATHI,Assistant
professor
49
53. get(5)
checkIndex(5); // throws exception
desiredNode = firstNode->next->next->next->next->next;
// desiredNode = NULL
return desiredNode->element; // NULL.element
a b c d e
NULL
firstNode
G. JAYABHARATHI,Assistant
professor
53
54. Erase An Element
erase(0)
a b c d e
NULL
firstNode
deleteNode = firstNode;
firstNode = firstNode->next;
delete deleteNode;
G. JAYABHARATHI,Assistant
professor
54
55. a b d e
NULL
firstNode
c
erase(2)
first get to node just before node to be removed
c
c
beforeNode = firstNode->next;
b
beforeNode
G. JAYABHARATHI,Assistant
professor
55
56. erase(2)
save pointer to node that will be deleted
deleteNode = beforeNode->next;
beforeNode
a b c d e
nul
l
firstNode
G. JAYABHARATHI,Assistant
professor
56
57. erase(2)
now change pointer in beforeNode
beforeNode->next = beforeNode->next->next;
delete deleteNode;
beforeNode
a b c d e
nul
l
firstNode
G. JAYABHARATHI,Assistant
professor
57
58. insert(0,’f’)
a b c d e
NULL
firstNode
f
newNode
Step 1: get a node, set its data and link fields
newNode = new chainNode<char>(theElement,
firstNode);
G. JAYABHARATHI,Assistant
professor
58
59. insert(0,’f’)
a b c d e
NULL
firstNode
f
newNode
Step 2: update firstNode
firstNode = newNode;
G. JAYABHARATHI,Assistant
professor
59
60. One-Step insert(0,’f’)
a b c d e
NULL
firstNode
f
newNode
firstNode = new chainNode<char>(‘f’, firstNode);
G. JAYABHARATHI,Assistant
professor
60
61. insert(3,’f’)
• first find node whose index is 2
a b c d e
NULL
firstNode
f
newNode
beforeNode
c
• next create a node and set its data and link fields
chainNode<char>* newNode = new chainNode<char>( ‘f’,
beforeNode->next);
• finally link beforeNode to newNode
beforeNode->next = newNode;
G. JAYABHARATHI,Assistant
professor
61
62. Two-Step insert(3,’f’)
beforeNode = firstNode->next->next;
beforeNode->next = new chainNode<char>
(‘f’, beforeNode->next);
a b c d e
NULL
firstNode
f
newNode
beforeNode
c
G. JAYABHARATHI,Assistant
professor
62
63. 0 1 2 3 4 5 6
Linear List Array Representation
use a one-dimensional array element[]
a b c d e
L = (a, b, c, d, e)
Store element i of list in element[i].
63
G.JAYABHARATHI,Assistant
professor
64. Right To Left Mapping
a
b
c
d
e
64
G.JAYABHARATHI,Assistant
professor
65. Mapping That Skips Every Other Position
a b c d e
65
G.JAYABHARATHI,Assistant
professor
67. Representation Used In Text
put element i of list in element[i]
use a variable listSize to record current number of elements
0 1 2 3 4 5 6
a b c d e
listSize = 5
67
G.JAYABHARATHI,Assistant
professor
68. Insert/Erase An Element
a b c d e
listSize = 5
a g b c d e
listSize = 6
insert(1,g)
68
G.JAYABHARATHI,Assistant
professor
69. Increasing Array Length
Length of array element[] is 6.
a b c d e f
T* newArray = new T[15];
First create a new and larger array
69
G.JAYABHARATHI,Assistant
professor
70. Increasing Array Length
Now copy elements from old array to new one.
a b c d e f
a b c d e f
copy(element, element + 6, newArray);
70
G.JAYABHARATHI,Assistant
professor
71. Increasing Array Length
Finally, delete old array and rename new array.
delete [] element;
element = newArray;
arrayLength = 15;
a b c d e f
element[0]
71
G.JAYABHARATHI,Assistant
professor
72. template<class T>
void changeLength1D(T*& a, int oldLength,
int newLength)
{
if (newLength < 0)
throw illegalParameterValue();
T* temp = new T[newLength];
// new array
int number = min(oldLength, newLength);
// number to copy
copy(a, a + number, temp);
delete [] a;
// deallocate old memory
a = temp;
}
72
G.JAYABHARATHI,Assistant
professor
73. Space Complexity
newArray = new char[7];
space needed = 2 * newLength – 1
= 2 * maxListSize – 1
element[6] a b c d e f
73
G.JAYABHARATHI,Assistant
professor
74. Array Doubling
Double the array length.
a b c d e f
newArray = new char[12];
a b c d e f
Time for n inserts goes up by Q(n).
Space needed = 1.5*newLength.
Space needed <= 3*maxListSize – 3
74
G.JAYABHARATHI,Assistant
professor
75. The Class chain
next (datatype chainNode<T>*)
element (datatype T)
Use chainNode
a b c d e
NULL
firstNode
listSize = number of elements
G. JAYABHARATHI, Assistant
professor
75
76. The Class chain
template<class T>
class chain : public linearList<T>
{
public:
// constructors and destructor defined here
// ADT methods
bool empty() const {return listSize == 0;}
int size() const {return listSize;}
// other ADT methods defined here
protected:
void checkIndex(int theIndex) const;
chainNode<T>* firstNode;
int listSize;
};
G. JAYABHARATHI, Assistant
professor
76
77. Constructor
template<class T>
chain<T>::chain(int initialCapacity = 10)
{// Constructor.
if (initialCapacity < 1)
{ostringstream s;
s << "Initial capacity = "
<< initialCapacity << " Must be > 0";
throw illegalParameterValue(s.str());
}
firstNode = NULL;
listSize = 0;
}
G. JAYABHARATHI, Assistant
professor
77
78. The Destructor
template<class T>
chain<T>::~chain()
{// Chain destructor. Delete all nodes
// in chain.
while (firstNode != NULL)
{// delete firstNode
chainNode<T>* nextNode = firstNode->next;
delete firstNode;
firstNode = nextNode;
}
}
G. JAYABHARATHI, Assistant
professor
78
79. The Method get
template<class T>
T& chain<T>::get(int theIndex) const
{// Return element whose index is theIndex.
checkIndex(theIndex);
// move to desired node
chainNode<T>* currentNode = firstNode;
for (int i = 0; i < theIndex; i++)
currentNode = currentNode->next;
return currentNode->element;
}
a b c d e
NULL
firstNode
G. JAYABHARATHI, Assistant
professor
79
80. The Method indexOf
template<class T>
int chain<T>::indexOf(const T& theElement) const
{
// search the chain for theElement
chainNode<T>* currentNode = firstNode;
int index = 0; // index of currentNode
while (currentNode != NULL &&
currentNode->element != theElement)
{
// move to next node
currentNode = currentNode->next;
index++;
}
G. JAYABHARATHI, Assistant
professor
80
81. Erase An Element
erase(0)
a b c d e
NULL
firstNode
deleteNode = firstNode;
firstNode = firstNode->next;
delete deleteNode;
G. JAYABHARATHI, Assistant
professor
81
82. Remove An Element
template<class T>
void chain<T>::erase(int theIndex)
{
checkIndex(theIndex);
chainNode<T>* deleteNode;
if (theIndex == 0)
{// remove first node from chain
deleteNode = firstNode;
firstNode = firstNode->next;
}
G. JAYABHARATHI, Assistant
professor
82
83. erase(2)
Find & change pointer in beforeNode
beforeNode->next = beforeNode->next->next;
delete deleteNode;
beforeNode
a b c d e
nul
l
firstNode
G. JAYABHARATHI, Assistant
professor
83
84. Remove An Element
else
{ // use p to get to beforeNode
chainNode<T>* p = firstNode;
for (int i = 0; i < theIndex - 1; i++)
p = p->next;
deleteNode = p->next;
p->next = p->next->next;
}
listSize--;
delete deleteNode;
}
G. JAYABHARATHI, Assistant
professor
84
85. One-Step insert(0,’f’)
a b c d e
NULL
firstNode
f
newNode
firstNode = new chainNode<char>(‘f’, firstNode);
G. JAYABHARATHI, Assistant
professor
85
86. Insert An Element
template<class T>
void chain<T>::insert(int theIndex,
const T& theElement)
{
if (theIndex < 0 || theIndex > listSize)
{// Throw illegalIndex exception
}
if (theIndex == 0)
// insert at front
firstNode = new chainNode<T>
(theElement, firstNode);
G. JAYABHARATHI, Assistant
professor
86
87. Two-Step insert(3,’f’)
beforeNode = firstNode->next->next;
beforeNode->next = new chainNode<char>
(‘f’, beforeNode->next);
a b c d e
NULL
firstNode
f
newNode
beforeNode
c
G. JAYABHARATHI, Assistant
professor
87
88. Inserting An Element
else
{ // find predecessor of new element
chainNode<T>* p = firstNode;
for (int i = 0; i < theIndex - 1; i++)
p = p->next;
// insert after p
p->next = new chainNode<T>
(theElement, p->next);
}
listSize++;
}
G. JAYABHARATHI, Assistant
professor
88
89. Performance
50,000 operations of each type
Operation FastArrayLinearList Chain
get 1.0ms 13.2sec
best-case inserts 2.1ms 45.1ms
average inserts 1.5sec 49.3sec
worst-case inserts 2.5sec 12.9sec
best-case removes 2.0ms 2.1ms
average removes 1.5sec 68.8sec
worst-case removes 2.5sec 12.9sec
G. JAYABHARATHI, Assistant
professor
89
90. Chain With Header Node
a b c d e
NULL
headerNode
G. JAYABHARATHI, Assistant
professor
90
91. Empty Chain With Header Node
headerNode
NULL
G. JAYABHARATHI, Assistant
professor
91
92. Circular List
a b c d e
firstNode
G. JAYABHARATHI, Assistant
professor
92
93. Doubly Linked List
a b c d e
NULL
firstNode
NULL
lastNode
G. JAYABHARATHI, Assistant
professor
93
95. Doubly Linked Circular List With Header Node
a b c e
headerNode
d
G. JAYABHARATHI, Assistant
professor
95
96. Empty Doubly Linked Circular List With Header Node
headerNode
G. JAYABHARATHI, Assistant
professor
96
97. Stacks
• Linear list.
• One end is called top.
• Other end is called bottom.
• Additions to and removals from the top end only.
97
G.JAYABHARATHI,Asistant
professor
98. Stack Of Cups
• Add a cup to the stack.
bottom
top
C
A
B
D
E
F
• Remove a cup from new stack.
• A stack is a LIFO list.
bottom
top
C
A
B
D
E
98
G.JAYABHARATHI,Asistant
professor
99. The Abstract Class stack
template<class T>
class stack
{
public:
virtual ~stack() {}
virtual bool empty() const = 0;
virtual int size() const = 0;
virtual T& top() = 0;
virtual void pop() = 0;
virtual void push(const T& theElement) = 0;
};
99
G.JAYABHARATHI,Asistant
professor
100. Parentheses Matching
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
– Output pairs (u,v) such that the left parenthesis at position u is matched with
the right parenthesis at v.
• (2,6) (1,13) (15,19) (21,25) (27,31) (0,32) (34,38)
• (a+b))*((c+d)
– (0,4)
– right parenthesis at 5 has no matching left parenthesis
– (8,12)
– left parenthesis at 7 has no matching right parenthesis
100
G.JAYABHARATHI,Asistant
professor
101. Parentheses Matching
• scan expression from left to right
• when a left parenthesis is encountered, add its position to the stack
• when a right parenthesis is encountered, remove matching position from stack
101
G.JAYABHARATHI,Asistant
professor
107. Towers Of Hanoi/Brahma
A B C
1
2
3
4
• 64 gold disks to be moved from tower A to tower C
• each tower operates as a stack
• cannot place big disk on top of a smaller one 107
G.JAYABHARATHI,Asistant
professor
108. Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/Brahma
A B C
1
2
3
108
G.JAYABHARATHI,Asistant
professor
109. Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/Brahma
A B C
1
2
3
109
G.JAYABHARATHI,Asistant
professor
110. Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/Brahma
A B C
1 2 3
110
G.JAYABHARATHI,Asistant
professor
111. Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/Brahma
A B C
1 2
3
111
G.JAYABHARATHI,Asistant
professor
112. Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/Brahma
A B C
1
2
3
112
G.JAYABHARATHI,Asistant
professor
113. Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/Brahma
A B C
1
2
3
113
G.JAYABHARATHI,Asistant
professor
114. Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/Brahma
A B C
1
2
3
114
G.JAYABHARATHI,Asistant
professor
115. Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/Brahma
A B C
1
2
3
• 7 disk moves 115
G.JAYABHARATHI,Asistant
professor
116. Recursive Solution
A B C
1
• n > 0 gold disks to be moved from A to C using B
• move top n-1 disks from A to B using C 116
G.JAYABHARATHI,Asistant
professor
117. Recursive Solution
A B C
1
• move top disk from A to C
117
G.JAYABHARATHI,Asistant
professor
118. Recursive Solution
A B C
1
• move top n-1 disks from B to C using A
118
G.JAYABHARATHI,Asistant
professor
119. Recursive Solution
A B C
1
• moves(n) = 0 when n = 0
• moves(n) = 2*moves(n-1) + 1 = 2n-1 when n > 0
119
G.JAYABHARATHI,Asistant
professor
120. Towers Of Hanoi/Brahma
• moves(64) = 1.8 * 1019 (approximately)
• Performing 109 moves/second, a computer would take
about 570 years to complete.
• At 1 disk move/min, the monks will take about 3.4 * 1013
years.
120
G.JAYABHARATHI,Asistant
professor
121. Chess Story
• 1 grain of rice on the first square, 2 for next, 4 for
next, 8 for next, and so on.
• Surface area needed exceeds surface area of earth.
121
G.JAYABHARATHI,Asistant
professor
122. Chess Story
• 1 penny for the first square, 2 for next, 4 for next,
8 for next, and so on.
• $3.6 * 1017 (federal budget ~ $2 * 1012) .
122
G.JAYABHARATHI,Asistant
professor
124. 17
Routing A 2-pin Net
1 2 3 4 5 6 7 8 9 10
30 29 28 27 26 25 24 23 22 21
11
12
13
14
15
16
18
19
20
40
39
38
37
36
35
34
33
32
31
4
17
Routing
for pins
5
through
16 is
confined
to upper
right
region.
Routing
for pins
1-3 and
18-40 is
confined
to lower
left
region.
124
G.JAYABHARATHI,Asistant
professor
125. 17
Routing A 2-pin Net
1 2 3 4 5 6 7 8 9 10
30 29 28 27 26 25 24 23 22 21
11
12
13
14
15
16
18
19
20
40
39
38
37
36
35
34
33
32
31
4
17
(u,v),
u<v is a
2-pin
net.
u is start
pin.
v is end
pin.
Examine
pins in
clock-
wise
order
beginn-
ing with
pin 1.
125
G.JAYABHARATHI,Asistant
professor
126. 17
Routing A 2-pin Net
1 2 3 4 5 6 7 8 9 10
30 29 28 27 26 25 24 23 22 21
11
12
13
14
15
16
18
19
20
40
39
38
37
36
35
34
33
32
31
4
17
Start pin
=> push
onto
stack.
End pin
=> start
pin must
be at top
of stack.
126
G.JAYABHARATHI,Asistant
professor
127. Method Invocation And Return
public void a()
{ …; b(); …}
public void b()
{ …; c(); …}
public void c()
{ …; d(); …}
public void d()
{ …; e(); …}
public void e()
{ …; c(); …}
return address in a()
return address in b()
return address in c()
return address in d()
return address in e()
return address in c()
return address in d()
127
G.JAYABHARATHI,Asistant
professor
128. Try-Throw-Catch
• When you enter a try block, push the address of this block on a stack.
• When an exception is thrown, pop the try block that is at the top of the stack (if
the stack is empty, terminate).
• If the popped try block has no matching catch block, go back to the preceding
step.
• If the popped try block has a matching catch block, execute the matching catch
block.
128
G.JAYABHARATHI,Asistant
professor
129. Rat In A Maze
129
G.JAYABHARATHI,Asistant
professor
130. Rat In A Maze
• Move order is: right, down, left, up
• Block positions to avoid revisit. 130
G.JAYABHARATHI,Asistant
professor
131. Rat In A Maze
• Move order is: right, down, left, up
• Block positions to avoid revisit. 131
G.JAYABHARATHI,Asistant
professor
132. Rat In A Maze
• Move backward until we reach a square from which
a forward move is possible. 132
G.JAYABHARATHI,Asistant
professor
133. Rat In A Maze
• Move down.
133
G.JAYABHARATHI,Asistant
professor
134. Rat In A Maze
• Move left.
134
G.JAYABHARATHI,Asistant
professor
135. Rat In A Maze
• Move down.
135
G.JAYABHARATHI,Asistant
professor
136. Rat In A Maze
• Move backward until we reach a square from which
a forward move is possible. 136
G.JAYABHARATHI,Asistant
professor
137. Rat In A Maze
• Move backward until we reach a square from which
a forward move is possible.
• Move downward.
137
G.JAYABHARATHI,Asistant
professor
138. Rat In A Maze
• Move right.
• Backtrack. 138
G.JAYABHARATHI,Asistant
professor
139. Rat In A Maze
• Move downward.
139
G.JAYABHARATHI,Asistant
professor
140. Rat In A Maze
• Move right.
140
G.JAYABHARATHI,Asistant
professor
141. Rat In A Maze
• Move one down and then right.
141
G.JAYABHARATHI,Asistant
professor
142. Rat In A Maze
• Move one up and then right.
142
G.JAYABHARATHI,Asistant
professor
143. Rat In A Maze
• Move down to exit and eat cheese.
• Path from maze entry to current position operates as a stack.
143
G.JAYABHARATHI,Asistant
professor
144. Queues
• Linear list.
• One end is called front.
• Other end is called rear.
• Additions are done at the rear only.
• Removals are made from the front only.
144
G.JAYABHARATHI,Asistant
professor
150. Revisit Of Stack Applications
• Applications in which the stack cannot be
replaced with a queue.
– Parentheses matching.
– Towers of Hanoi.
– Switchbox routing.
– Method invocation and return.
– Try-catch-throw implementation.
• Application in which the stack may be
replaced with a queue.
– Rat in a maze.
• Results in finding shortest path to exit.
150
G.JAYABHARATHI,Asistant
professor
160. Derive From arrayList
when front is left end of list and rear is right end
• empty() => queue::empty()
– O(1) time
• size() => queue::size(0)
– O(1) time
• front() => get(0)
– O(1) time
• back() => get(size() - 1)
– O(1) time
0 1 2 3 4 5 6
a b c d e
160
G.JAYABHARATHI,Asistant
professor
161. Derive From arrayList
• pop() => erase(0)
– O(size) time
• push(theElement) => insert(size(), theElement)
– O(1) time
0 1 2 3 4 5 6
a b c d e
161
G.JAYABHARATHI,Asistant
professor
162. Derive From arrayList
when front is right end of list and rear is left end
• empty() => queue::empty()
– O(1) time
• size() => queue::size(0)
– O(1) time
• front() => get(size() - 1)
– O(1) time
• back() => get(0)
– O(1) time
0 1 2 3 4 5 6
a b c d e
162
G.JAYABHARATHI,Asistant
professor
163. Derive From arrayList
• pop() => erase(size() - 1)
– O(1) time
• push(theElement) => insert(0, theElement)
– O(size) time
0 1 2 3 4 5 6
a b c d e
163
G.JAYABHARATHI,Asistant
professor
164. Derive From arrayList
– to perform each opertion in O(1) time (excluding
array doubling), we need a customized array
representation.
164
G.JAYABHARATHI,Asistant
professor
165. Derive From extendedChain
a b c d e
NULL
firstNode lastNode
front rear
when front is left end of list and rear is right end
• empty() => extendedChain::empty()
– O(1) time
• size() => extendedChain::size()
– O(1) time
165
G.JAYABHARATHI,Asistant
professor
166. Derive From ExtendedChain
a b c d e
NULL
firstNode lastNode
front rear
• front() => get (0)
– O(1) time
• back() => getLast() … new method
166
G.JAYABHARATHI,Asistant
professor
167. Derive From ExtendedChain
a b c d e
NULL
firstNode lastNode
front rear
• push(theElement) => push_back(theElement)
– O(1) time
• pop() => erase(0)
– O(1) time
167
G.JAYABHARATHI,Asistant
professor
168. Derive From extendedChain
e d c b a
NULL
firstNode lastNode
rear front
when front is right end of list and rear is left end
• empty() => extendedChain::empty()
– O(1) time
• size() => extendedChain::size()
– O(1) time
168
G.JAYABHARATHI,Asistant
professor
169. Derive From extendedChain
a b c d e
NULL
firstNode lastNode
rear front
• front() => getLast()
– O(1) time
• back() => get(0)
– O(1) time
169
G.JAYABHARATHI,Asistant
professor
170. Derive From extendedChain
a b c d e
NULL
firstNode lastNode
rear front
• push(theElement) => insert(0, theElement)
– O(1) time
• pop() => erase(size() - 1)
– O(size) time
170
G.JAYABHARATHI,Asistant
professor
171. Custom Linked Code
• Develop a linked class for queue from scratch to
get better preformance than obtainable by
deriving from extendedChain.
171
G.JAYABHARATHI,Asistant
professor
172. Custom Array Queue
• Use a 1D array queue.
queue[]
• Circular view of array.
[0]
[1]
[2] [3]
[4]
[5] 172
G.JAYABHARATHI,Asistant
professor
173. Custom Array Queue
• Possible configuration with 3 elements.
[0]
[1]
[2] [3]
[4]
[5]
A B
C
173
G.JAYABHARATHI,Asistant
professor
174. Custom Array Queue
• Another possible configuration with 3 elements.
[0]
[1]
[2] [3]
[4]
[5]
A
B
C
174
G.JAYABHARATHI,Asistant
professor
175. Custom Array Queue
• Use integer variables theFront and theBack.
– theFront is one position counterclockwise from
first element
– theBack gives position of last element
– use front and rear in figures
[0]
[1]
[2] [3]
[4]
[5]
A B
C
front rear
[0]
[1]
[2] [3]
[4]
[5]
A
B
C
front
rear
175
G.JAYABHARATHI,Asistant
professor
176. Push An Element
[0]
[1]
[2] [3]
[4]
[5]
A B
C
front
rear
• Move rear one clockwise.
176
G.JAYABHARATHI,Asistant
professor
177. Push An Element
• Move rear one clockwise.
[0]
[1]
[2] [3]
[4]
[5]
A B
C
front
rear
• Then put into queue[rear].
D
177
G.JAYABHARATHI,Asistant
professor
178. Pop An Element
[0]
[1]
[2] [3]
[4]
[5]
A B
C
front
rear
• Move front one clockwise.
178
G.JAYABHARATHI,Asistant
professor
179. Pop An Element
[0]
[1]
[2] [3]
[4]
[5]
A B
C
front
rear
• Move front one clockwise.
• Then extract from queue[front].
179
G.JAYABHARATHI,Asistant
professor
180. Moving rear Clockwise
[0]
[1]
[2] [3]
[4]
[5]
A B
C
front rear
• rear++;
if (rear = = arrayLength) rear = 0;
• rear = (rear + 1) % arrayLength;
180
G.JAYABHARATHI,Asistant
professor
184. Empty That Queue
• When a series of removes causes the queue to
become empty, front = rear.
• When a queue is constructed, it is empty.
• So initialize front = rear = 0.
[0]
[1]
[2] [3]
[4]
[5]
front
rear
184
G.JAYABHARATHI,Asistant
professor
185. A Full Tank Please
[0]
[1]
[2] [3]
[4]
[5]
A
B
C
front
rear
185
G.JAYABHARATHI,Asistant
professor
186. A Full Tank Please
[0]
[1]
[2] [3]
[4]
[5]
A
B
C
front
rear
D
186
G.JAYABHARATHI,Asistant
professor
187. A Full Tank Please
[0]
[1]
[2] [3]
[4]
[5]
A
B
C
front
rear
D E
187
G.JAYABHARATHI,Asistant
professor
188. A Full Tank Please
[0]
[1]
[2] [3]
[4]
[5]
A
B
C
front
rear
D E
F
• When a series of adds causes the queue to become full,
front = rear.
• So we cannot distinguish between a full queue and an
empty queue!
188
G.JAYABHARATHI,Asistant
professor
189. Priority Queues
Two kinds of priority queues:
• Min priority queue.
• Max priority queue.
189
G.JAYABHARATHI,Asistant
professor
190. Min Priority Queue
• Collection of elements.
• Each element has a priority or key.
• Supports following operations:
empty
size
insert an element into the priority queue (push)
get element with min priority (top)
remove element with min priority (pop)
190
G.JAYABHARATHI,Asistant
professor
191. Max Priority Queue
• Collection of elements.
• Each element has a priority or key.
• Supports following operations:
empty
size
insert an element into the priority queue (push)
get element with max priority (top)
remove element with max priority (pop)
191
G.JAYABHARATHI,Asistant
professor
192. Complexity Of Operations
Two good implementations are heaps and leftist trees.
empty, size, and top => O(1) time
insert (push) and remove (pop) => O(log n) time where n is the size of
the priority queue
192
G.JAYABHARATHI,Asistant
professor
193. Applications
Sorting
• use element key as priority
• insert elements to be sorted into a priority queue
• remove/pop elements in priority order
if a min priority queue is used, elements are extracted in ascending order of
priority (or key)
if a max priority queue is used, elements are extracted in descending order
of priority (or key)
193
G.JAYABHARATHI,Asistant
professor
194. Sorting Example
Sort five elements whose keys are 6, 8, 2, 4, 1 using a max priority queue.
Insert the five elements into a max priority queue.
Do five remove max operations placing removed elements into the
sorted array from right to left.
194
G.JAYABHARATHI,Asistant
professor
195. After Inserting Into Max Priority Queue
Sorted Array
6
8
2
4
1
Max Priority
Queue
195
G.JAYABHARATHI,Asistant
professor
196. After First Remove Max Operation
Sorted Array
6
2
4
1
8
Max Priority
Queue
196
G.JAYABHARATHI,Asistant
professor
197. After Second Remove Max Operation
Sorted Array
2
4
1
8
6
Max Priority
Queue
197
G.JAYABHARATHI,Asistant
professor
198. After Third Remove Max Operation
Sorted Array
2
1
8
6
4
Max Priority
Queue
198
G.JAYABHARATHI,Asistant
professor
199. After Fourth Remove Max Operation
Sorted Array
1
8
6
4
2
Max Priority
Queue
199
G.JAYABHARATHI,Asistant
professor
200. After Fifth Remove Max Operation
Sorted Array
8
6
4
2
1
Max Priority
Queue
200
G.JAYABHARATHI,Asistant
professor
201. Complexity Of Sorting
Sort n elements.
n insert operations => O(n log n) time.
n remove max operations => O(n log n) time.
total time is O(n log n).
compare with O(n2) for sort methods
201
G.JAYABHARATHI,Asistant
professor
202. Heap Sort
Uses a max priority queue that is implemented as a heap.
Initial insert operations are replaced by a heap initialization step that takes
O(n) time.
202
G.JAYABHARATHI,Asistant
professor
203. Machine Scheduling
m identical machines (drill press, cutter, sander, etc.)
n jobs/tasks to be performed
assign jobs to machines so that the time at which the last job completes
is minimum
203
G.JAYABHARATHI,Asistant
professor
204. Machine Scheduling Example
3 machines and 7 jobs
job times are [6, 2, 3, 5, 10, 7, 14]
possible schedule
A
B
C
time ----------->
6
2
3
7
13
13
21
204
G.JAYABHARATHI,Asistant
professor
205. Machine Scheduling Example
Finish time = 21
Objective: Find schedules with minimum finish time.
A
B
C
time ----------->
6
2
3
7
13
13
21
205
G.JAYABHARATHI,Asistant
professor
206. LPT Schedules
Longest Processing Time first.
Jobs are scheduled in the order
14, 10, 7, 6, 5, 3, 2
Each job is scheduled on the machine on which it finishes
earliest.
206
G.JAYABHARATHI,Asistant
professor
207. LPT Schedule
[14, 10, 7, 6, 5, 3, 2]
A
B
C
14
10
7 13
15
16
16
Finish time is 16! 207
G.JAYABHARATHI,Asistant
professor
208. LPT Schedule
• LPT rule does not guarantee minimum finish time schedules.
• (LPT Finish Time)/(Minimum Finish Time) <= 4/3 - 1/(3m) where m is
number of machines.
• Usually LPT finish time is much closer to minimum finish time.
• Minimum finish time scheduling is NP-hard.
208
G.JAYABHARATHI,Asistant
professor
209. NP-hard Problems
• Infamous class of problems for which no one has developed a polynomial
time algorithm.
• That is, no algorithm whose complexity is O(nk) for any constant k is known
for any NP-hard problem.
• The class includes thousands of real-world problems.
• Highly unlikely that any NP-hard problem can be solved by a polynomial
time algorithm.
209
G.JAYABHARATHI,Asistant
professor
210. NP-hard Problems
• Since even polynomial time algorithms with degree k > 3 (say) are not
practical for large n, we must change our expectations of the algorithm that is
used.
• Usually develop fast heuristics for NP-hard problems.
Algorithm that gives a solution close to best.
Runs in acceptable amount of time.
• LPT rule is good heuristic for minimum finish time scheduling.
210
G.JAYABHARATHI,Asistant
professor
211. Complexity Of LPT Scheduling
• Sort jobs into decreasing order of task time.
O(n log n) time (n is number of jobs)
• Schedule jobs in this order.
assign job to machine that becomes available first
must find minimum of m (m is number of machines) finish times
takes O(m) time using simple strategy
so need O(mn) time to schedule all n jobs.
211
G.JAYABHARATHI,Asistant
professor
212. Using A Min Priority Queue
• Min priority queue has the finish times of the m machines.
• Initial finish times are all 0.
• To schedule a job remove machine with minimum finish time from the
priority queue.
• Update the finish time of the selected machine and insert the machine back
into the priority queue.
• m put operations to initialize priority queue
• 1 remove min and 1 insert to schedule each job
• each insert and remove min operation takes O(log m) time
• time to schedule is O(n log m)
• overall time is
O(n log n + n log m) = O(n log (mn))
212
G.JAYABHARATHI,Asistant
professor
213. Min Tree Example
2
4 9 3
4 8 7
9 9
Root has minimum element.
213
G.JAYABHARATHI,Asistant
professor
214. Max Tree Example
9
4 9 8
4 2 7
3 1
Root has maximum element.
214
G.JAYABHARATHI,Asistant
professor
215. Min Heap Definition
• complete binary tree
• min tree
215
G.JAYABHARATHI,Asistant
professor
216. Min Heap With 9 Nodes
Complete binary tree with 9 nodes.
216
G.JAYABHARATHI,Asistant
professor
217. Min Heap With 9 Nodes
Complete binary tree with 9 nodes that is also a min tree.
2
4
6 7 9 3
8 6
3
217
G.JAYABHARATHI,Asistant
professor
218. Max Heap With 9 Nodes
Complete binary tree with 9 nodes
that is also a max tree.
9
8
6 7 2 6
5 1
7
218
G.JAYABHARATHI,Asistant
professor
219. 9 8 7 6 7 2 6 5 1
1 2 3 4 5 6 7 8 9 10
0
A Heap Is Efficiently Represented As An Array
9
8
6 7 2 6
5 1
7
219
G.JAYABHARATHI,Asistant
professor
220. Moving Up And Down A Heap
9
8
6 7 2 6
5 1
7
1
2 3
4 5 6 7
8 9
220
G.JAYABHARATHI,Asistant
professor
221. Inserting An Element Into A Max Heap
Complete binary tree with 10 nodes.
9
8
6 7 2 6
5 1
7
7
221
G.JAYABHARATHI,Asistant
professor
222. Inserting An Element Into A Max Heap
New element is 5.
9
8
6 7 2 6
5 1
7
7
5
222
G.JAYABHARATHI,Asistant
professor
223. Inserting An Element Into A Max Heap
New element is 20.
9
8
6
7
2 6
5 1
7
7
7
223
G.JAYABHARATHI,Asistant
professor
224. Inserting An Element Into A Max Heap
New element is 20.
9
8
6
7
2 6
5 1
7
7
7
224
G.JAYABHARATHI,Asistant
professor
225. Inserting An Element Into A Max Heap
New element is 20.
9
8
6
7
2 6
5 1
7
7
7
225
G.JAYABHARATHI,Asistant
professor
226. Inserting An Element Into A Max Heap
New element is 20.
9
8
6
7
2 6
5 1
7
7
7
20
226
G.JAYABHARATHI,Asistant
professor
227. Inserting An Element Into A Max Heap
Complete binary tree with 11 nodes.
9
8
6
7
2 6
5 1
7
7
7
20
227
G.JAYABHARATHI,Asistant
professor
228. Inserting An Element Into A Max Heap
New element is 15.
9
8
6
7
2 6
5 1
7
7
7
20
228
G.JAYABHARATHI,Asistant
professor
229. Inserting An Element Into A Max Heap
New element is 15.
9
8
6
7
2 6
5 1
7
7
7
20
8
229
G.JAYABHARATHI,Asistant
professor
230. Inserting An Element Into A Max Heap
New element is 15.
8
6
7
2 6
5 1
7
7
7
20
8
9
15
230
G.JAYABHARATHI,Asistant
professor
231. Complexity Of Insert
Complexity is O(log n), where n is
heap size.
8
6
7
2 6
5 1
7
7
7
20
8
9
15
231
G.JAYABHARATHI,Asistant
professor
232. Removing The Max Element
Max element is in the root.
8
6
7
2 6
5 1
7
7
7
20
8
9
15
232
G.JAYABHARATHI,Asistant
professor
233. Removing The Max Element
After max element is removed.
8
6
7
2 6
5 1
7
7
7 8
9
15
233
G.JAYABHARATHI,Asistant
professor
234. Removing The Max Element
Heap with 10 nodes.
8
6
7
2 6
5 1
7
7
7 8
9
15
Reinsert 8 into the heap. 234
G.JAYABHARATHI,Asistant
professor
235. Removing The Max Element
Reinsert 8 into the heap.
6
7
2 6
5 1
7
7
7
9
15
235
G.JAYABHARATHI,Asistant
professor
236. Removing The Max Element
Reinsert 8 into the heap.
6
7
2 6
5 1
7
7
7
9
15
236
G.JAYABHARATHI,Asistant
professor
237. Removing The Max Element
Reinsert 8 into the heap.
6
7
2 6
5 1
7
7
7
9
15
8
237
G.JAYABHARATHI,Asistant
professor
238. Removing The Max Element
Max element is 15.
6
7
2 6
5 1
7
7
7
9
15
8
238
G.JAYABHARATHI,Asistant
professor
239. Removing The Max Element
After max element is removed.
6
7
2 6
5 1
7
7
7
9
8
239
G.JAYABHARATHI,Asistant
professor
240. Removing The Max Element
Heap with 9 nodes.
6
7
2 6
5 1
7
7
7
9
8
240
G.JAYABHARATHI,Asistant
professor
241. Removing The Max Element
Reinsert 7.
6 2 6
5 1
7
9
8
241
G.JAYABHARATHI,Asistant
professor
242. Removing The Max Element
Reinsert 7.
6 2 6
5 1
7
9
8
242
G.JAYABHARATHI,Asistant
professor
243. Removing The Max Element
Reinsert 7.
6 2 6
5 1
7
9
8
7
243
G.JAYABHARATHI,Asistant
professor
244. Complexity Of Remove Max Element
Complexity is O(log n).
6 2 6
5 1
7
9
8
7
244
G.JAYABHARATHI,Asistant
professor
248. Linear Lists And Trees
• Linear lists are useful for serially ordered data.
– (e0, e1, e2, …, en-1)
– Days of week.
– Months in a year.
– Students in this class.
• Trees are useful for hierarchically ordered data.
– Employees of a corporation.
• President, vice presidents, managers, and so on.
248
G.JAYABHARATHI, Asistant
professor
249. Hierarchical Data And Trees
• The element at the top of the hierarchy is the root.
• Elements next in the hierarchy are the children of the
root.
• Elements next in the hierarchy are the grandchildren of
the root, and so on.
• Elements that have no children are leaves.
249
G.JAYABHARATHI, Asistant
professor
250. great grand child of root
grand children of root
children of root
Example Tree
President
VP1 VP2 VP3
Manager1 Manager2 Manager Manager
Worker Bee
root
250
G.JAYABHARATHI, Asistant
professor
251. Definition
• A tree t is a finite nonempty set of elements.
• One of these elements is called the root.
• The remaining elements, if any, are
partitioned into trees, which are called the
subtrees of t.
251
G.JAYABHARATHI, Asistant
professor
256. Caution
• Some texts start level numbers at 0 rather than
at 1.
• Root is at level 0.
• Its children are at level 1.
• The grand children of the root are at level 2.
• And so on.
• We shall number levels with the root at level 1.
256
G.JAYABHARATHI, Asistant
professor
257. height = depth = number of levels
Level 4
Level 3
Level 2
President
VP1 VP2 VP3
Manager1 Manager2 Manager Manager
Worker Bee
Level 1
257
G.JAYABHARATHI, Asistant
professor
258. Node Degree = Number Of Children
President
VP1 VP2 VP3
Manager1 Manager2 Manager Manager
Worker Bee
3
2 1 1
0 0 1 0
0
258
G.JAYABHARATHI, Asistant
professor
259. Tree Degree = Max Node Degree
Degree of tree = 3.
President
VP1 VP2 VP3
Manager1 Manager2 Manager Manager
Worker Bee
3
2 1 1
0 0 1 0
0
259
G.JAYABHARATHI, Asistant
professor
260. Binary Tree
• Finite (possibly empty) collection of elements.
• A nonempty binary tree has a root element.
• The remaining elements (if any) are partitioned into two binary
trees.
• These are called the left and right subtrees of the binary tree.
260
G.JAYABHARATHI, Asistant
professor
261. Differences Between A Tree & A Binary Tree
• No node in a binary tree may have a degree more
than 2, whereas there is no limit on the degree of a
node in a tree.
• A binary tree may be empty; a tree cannot be
empty.
261
G.JAYABHARATHI, Asistant
professor
262. Differences Between A Tree & A Binary Tree
• The subtrees of a binary tree are ordered; those of a tree
are not ordered.
a
b
a
b
• Are different when viewed as binary trees.
• Are the same when viewed as trees.
262
G.JAYABHARATHI, Asistant
professor
263. Arithmetic Expressions
• (a + b) * (c + d) + e – f/g*h + 3.25
• Expressions comprise three kinds of entities.
– Operators (+, -, /, *).
– Operands (a, b, c, d, e, f, g, h, 3.25, (a + b), (c + d), etc.).
– Delimiters ((, )).
263
G.JAYABHARATHI, Asistant
professor
264. Operator Degree
• Number of operands that the operator requires.
• Binary operator requires two operands.
– a + b
– c / d
– e - f
• Unary operator requires one operand.
– + g
– - h
264
G.JAYABHARATHI, Asistant
professor
265. Infix Form
• Normal way to write an expression.
• Binary operators come in between their left and right
operands.
– a * b
– a + b * c
– a * b / c
– (a + b) * (c + d) + e – f/g*h + 3.25
265
G.JAYABHARATHI, Asistant
professor
266. Infix Expression Is Hard To Parse
• Need operator priorities, tie breaker, and delimiters.
• This makes computer evaluation more difficult than is necessary.
• Postfix and prefix expression forms do not rely on operator priorities, a
tie breaker, or delimiters.
• So it is easier for a computer to evaluate expressions that are in these
forms.
266
G.JAYABHARATHI, Asistant
professor
267. Postfix Form
• The postfix form of a variable or constant is the same as its infix form.
– a, b, 3.25
• The relative order of operands is the same in infix and postfix forms.
• Operators come immediately after the postfix form of their operands.
– Infix = a + b
– Postfix = ab+
267
G.JAYABHARATHI, Asistant
professor
268. Postfix Examples
• Infix = a + b * c
– Postfix = a b c * +
• Infix = a * b + c
Postfix = a b * c +
• Infix = (a + b) * (c – d) / (e + f)
Postfix = a b + c d - * e f + /
268
G.JAYABHARATHI, Asistant
professor
269. Unary Operators
• Replace with new symbols.
– + a => a @
– + a + b => a @ b +
– - a => a ?
– - a-b => a ? b -
269
G.JAYABHARATHI, Asistant
professor
270. Postfix Evaluation
• Scan postfix expression from left to right pushing operands
on to a stack.
• When an operator is encountered, pop as many operands as
this operator needs; evaluate the operator; push the result
on to the stack.
• This works because, in postfix, operators come
immediately after their operands.
270
G.JAYABHARATHI, Asistant
professor
271. Postfix Evaluation
• (a + b) * (c – d) / (e + f)
• a b + c d - * e f + /
• a b + c d - * e f + /
stack
a
• a b + c d - * e f + /
b
• a b + c d - * e f + /
271
G.JAYABHARATHI, Asistant
professor
272. Postfix Evaluation
• (a + b) * (c – d) / (e + f)
• a b + c d - * e f + /
• a b + c d - * e f + /
stack
(a + b)
• a b + c d - * e f + /
• a b + c d - * e f + /
• a b + c d - * e f + /
c
• a b + c d - * e f + /
d
• a b + c d - * e f + /
272
G.JAYABHARATHI, Asistant
professor
273. Postfix Evaluation
• (a + b) * (c – d) / (e + f)
• a b + c d - * e f + /
stack
(a + b)
• a b + c d - * e f + /
(c – d)
273
G.JAYABHARATHI, Asistant
professor
274. Postfix Evaluation
• (a + b) * (c – d) / (e + f)
• a b + c d - * e f + /
stack
(a + b)*(c – d)
• a b + c d - * e f + /
e
• a b + c d - * e f + /
• a b + c d - * e f + / f
• a b + c d - * e f + /
274
G.JAYABHARATHI, Asistant
professor
275. Postfix Evaluation
• (a + b) * (c – d) / (e + f)
• a b + c d - * e f + /
stack
(a + b)*(c – d)
• a b + c d - * e f + /
(e + f)
• a b + c d - * e f + /
• a b + c d - * e f + /
• a b + c d - * e f + /
• a b + c d - * e f + /
275
G.JAYABHARATHI, Asistant
professor
276. Prefix Form
• The prefix form of a variable or constant is the same as its
infix form.
– a, b, 3.25
• The relative order of operands is the same in infix and
prefix forms.
• Operators come immediately before the prefix form of
their operands.
– Infix = a + b
– Postfix = ab+
– Prefix = +ab
276
G.JAYABHARATHI, Asistant
professor
277. Binary Tree Form
• a + b
+
a b
• - a
-
a
277
G.JAYABHARATHI, Asistant
professor
278. Binary Tree Form
• (a + b) * (c – d) / (e + f)
/
+
a b
-
c d
+
e f
*
/
278
G.JAYABHARATHI, Asistant
professor
279. Merits Of Binary Tree Form
• Left and right operands are easy to visualize.
• Code optimization algorithms work with the binary tree form
of an expression.
• Simple recursive evaluation of expression.
+
a b
-
c d
+
e f
*
/
279
G.JAYABHARATHI, Asistant
professor
280. Binary Tree Traversal Methods
• In a traversal of a binary tree, each element of the binary tree is
visited exactly once.
• During the visit of an element, all action (make a clone,
display, evaluate the operator, etc.) with respect to this element
is taken.
280
G.JAYABHARATHI,Asistant
professor
281. Binary Tree Traversal Methods
• Preorder
• Inorder
• Postorder
• Level order
281
G.JAYABHARATHI,Asistant
professor
288. Inorder Example (visit = print)
a
b c
d e
f
g h i j
g d h b e i a f j c
288
G.JAYABHARATHI,Asistant
professor
289. Inorder By Projection (Squishing)
a
b c
d e
f
g h i j
g d h b e i a f j c
289
G.JAYABHARATHI,Asistant
professor
290. Inorder Of Expression Tree
+
a b
-
c d
+
e f
*
/
Gives infix form of expression (sans parentheses)!
e
a + b * c d / + f
-
290
G.JAYABHARATHI,Asistant
professor
293. Postorder Example (visit = print)
a
b c
d e
f
g h i j
g h d i e b j f c a
293
G.JAYABHARATHI,Asistant
professor
294. Postorder Of Expression Tree
+
a b
-
c d
+
e f
*
/
Gives postfix form of expression!
a b + c d - * e f + /
294
G.JAYABHARATHI,Asistant
professor
295. Traversal Applications
a
b c
d e
f
g h i j
• Make a clone.
• Determine height.
•Determine number of nodes.
295
G.JAYABHARATHI,Asistant
professor
296. Level Order
Let t be the tree root.
while (t != NULL)
{
visit t and put its children on a FIFO queue;
if FIFO queue is empty, set t = NULL;
otherwise, pop a node from the FIFO queue and call it t;
}
296
G.JAYABHARATHI,Asistant
professor
297. Level-Order Example (visit = print)
a
b c
d e
f
g h i j
a b c d e f g h i j
297
G.JAYABHARATHI,Asistant
professor
298. Binary Tree Construction
• Suppose that the elements in a binary tree are distinct.
• Can you construct the binary tree from which a given
traversal sequence came?
• When a traversal sequence has more than one element, the
binary tree is not uniquely defined.
• Therefore, the tree from which the sequence was obtained
cannot be reconstructed uniquely.
298
G.JAYABHARATHI,Asistant
professor
300. Preorder And Postorder
preorder = ab a
b
a
b
postorder = ba
• Preorder and postorder do not uniquely define a binary tree.
• Nor do preorder and level order (same example).
• Nor do postorder and level order (same example).
300
G.JAYABHARATHI,Asistant
professor
301. Inorder And Preorder
• inorder = g d h b e i a f j c
• preorder = a b d g h e i c f j
• Scan the preorder left to right using the
inorder to separate left and right subtrees.
• a is the root of the tree; gdhbei are in the left
subtree; fjc are in the right subtree.
a
gdhbei fjc
301
G.JAYABHARATHI,Asistant
professor
302. Inorder And Preorder
• preorder = a b d g h e i c f j
• b is the next root; gdh are in the left
subtree; ei are in the right subtree.
a
gdhbei fjc
a
gdh
fjc
b
ei 302
G.JAYABHARATHI,Asistant
professor
303. Inorder And Preorder
• preorder = a b d g h e i c f j
• d is the next root; g is in the left
subtree; h is in the right subtree.
a
gdh
fjc
b
ei
a
g
fjc
b
ei
d
h 303
G.JAYABHARATHI,Asistant
professor
304. Inorder And Postorder
• Scan postorder from right to left using
inorder to separate left and right subtrees.
• inorder = g d h b e i a f j c
• postorder = g h d i e b j f c a
• Tree root is a; gdhbei are in left subtree; fjc
are in right subtree.
304
G.JAYABHARATHI,Asistant
professor
305. Inorder And Level Order
• Scan level order from left to right using
inorder to separate left and right subtrees.
• inorder = g d h b e i a f j c
• level order = a b c d e f g h i j
• Tree root is a; gdhbei are in left subtree; fjc
are in right subtree.
305
G.JAYABHARATHI,Asistant
professor
307. Initializing A Max Heap
Start at rightmost array position that has a child.
8
4
7
6 7
8 9
3
7
10
1
11
5
2
Index is n/2. 307
G.JAYABHARATHI,Asistant
professor
308. Initializing A Max Heap
Move to next lower array position.
8
4
7
6 7
8 9
3
7
10
1
5
11
2
308
G.JAYABHARATHI,Asistant
professor
309. Initializing A Max Heap
8
4
7
6 7
8 9
3
7
10
1
5
11
2
309
G.JAYABHARATHI,Asistant
professor
310. Initializing A Max Heap
8
9
7
6 7
8 4
3
7
10
1
5
11
2
310
G.JAYABHARATHI,Asistant
professor
311. Initializing A Max Heap
8
9
7
6 7
8 4
3
7
10
1
5
11
2
311
G.JAYABHARATHI,Asistant
professor
312. Initializing A Max Heap
8
9
7
6 3
8 4
7
7
10
1
5
11
2
312
G.JAYABHARATHI,Asistant
professor
313. Initializing A Max Heap
8
9
7
6 3
8 4
7
7
10
1
5
11
2
313
G.JAYABHARATHI,Asistant
professor
314. Initializing A Max Heap
8
9
7
6 3
8 4
7
7
10
1
5
11
Find a home for 2.
314
G.JAYABHARATHI,Asistant
professor
315. Initializing A Max Heap
8
9
7
6 3
8 4
7
7
5
1
11
Find a home for 2.
10
315
G.JAYABHARATHI,Asistant
professor
316. Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
1
11
Done, move to next lower array position.
10
5
316
G.JAYABHARATHI,Asistant
professor
317. Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
1
11
10
5
Find home for 1.
317
G.JAYABHARATHI,Asistant
professor
318. 11
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
10
5
Find home for 1.
318
G.JAYABHARATHI,Asistant
professor
319. Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
11
10
5
Find home for 1.
319
G.JAYABHARATHI,Asistant
professor
320. Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
11
10
5
Find home for 1.
320
G.JAYABHARATHI,Asistant
professor
321. Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
11
10
5
Done.
1
321
G.JAYABHARATHI,Asistant
professor
323. Complexity
Time for level j subtrees is <= 2j-1(h-j+1) = t(j).
Total time is t(1) + t(2) + … + t(h-1) = O(n).
323
G.JAYABHARATHI,Asistant
professor
324. Leftist Trees
Linked binary tree.
Can do everything a heap can do and in the
same asymptotic complexity.
Can meld two leftist tree priority queues in
O(log n) time.
324
G.JAYABHARATHI,Asistant
professor
325. Extended Binary Trees
Start with any binary tree and add an
external node wherever there is an
empty subtree.
Result is an extended binary tree.
325
G.JAYABHARATHI,Asistant
professor
327. An Extended Binary Tree
number of external nodes is n+1 327
G.JAYABHARATHI,Asistant
professor
328. The Function s()
For any node x in an extended binary tree,
let s(x) be the length of a shortest path
from x to an external node in the subtree
rooted at x.
328
G.JAYABHARATHI,Asistant
professor
331. Properties Of s()
If x is an external node, then s(x) = 0.
Otherwise,
s(x) = min {s(leftChild(x)),
s(rightChild(x))} + 1
331
G.JAYABHARATHI,Asistant
professor
332. Height Biased Leftist Trees
A binary tree is a (height biased) leftist tree
iff for every internal node x,
s(leftChild(x)) >= s(rightChild(x))
332
G.JAYABHARATHI,Asistant
professor
333. A Leftist Tree
0 0 0 0
0 0
0 0
0
0
1 1 1
2 1 1
2 1
2
333
G.JAYABHARATHI,Asistant
professor
334. Leftist Trees--Property 1
In a leftist tree, the rightmost path is a
shortest root to external node path and
the length of this path is s(root).
334
G.JAYABHARATHI,Asistant
professor
335. A Leftist Tree
0 0 0 0
0 0
0 0
0
0
1 1 1
2 1 1
2 1
2
Length of rightmost path is 2. 335
G.JAYABHARATHI,Asistant
professor
336. Leftist Trees—Property 2
The number of internal nodes is at least
2s(root) - 1
Because levels 1 through s(root) have no
external nodes.
So, s(root) <= log(n+1)
336
G.JAYABHARATHI,Asistant
professor
337. A Leftist Tree
0 0 0 0
0 0
0 0
0
0
1 1 1
2 1 1
2 1
2
Levels 1 and 2 have no external nodes. 337
G.JAYABHARATHI,Asistant
professor
338. Leftist Trees—Property 3
Length of rightmost path is O(log n), where
n is the number of nodes in a leftist tree.
Follows from Properties 1 and 2.
338
G.JAYABHARATHI,Asistant
professor
339. Leftist Trees As Priority Queues
Min leftist tree … leftist tree that is a min tree.
Used as a min priority queue.
Max leftist tree … leftist tree that is a max tree.
Used as a max priority queue.
339
G.JAYABHARATHI,Asistant
professor
340. A Min Leftist Tree
8 6 9
6 8 5
4 3
2
340
G.JAYABHARATHI,Asistant
professor
341. Some Min Leftist Tree Operations
empty()
size()
top()
push()
pop()
meld()
initialize()
push() and pop() use meld().
341
G.JAYABHARATHI,Asistant
professor
343. Push Operation
push(7)
8 6 9
6 8 5
4 3
2
Create a single node min leftist tree. 7
343
G.JAYABHARATHI,Asistant
professor
344. Push Operation
push(7)
8 6 9
6 8 5
4 3
2
Create a single node min leftist tree.
Meld the two min leftist trees.
7
344
G.JAYABHARATHI,Asistant
professor
345. Remove Min (pop)
8 6 9
6 8 5
4 3
2
345
G.JAYABHARATHI,Asistant
professor
346. Remove Min (pop)
8 6 9
6 8 5
4 3
2
Remove the root.
346
G.JAYABHARATHI,Asistant
professor
347. Remove Min (pop)
8 6 9
6 8 5
4 3
2
Remove the root.
Meld the two subtrees. 347
G.JAYABHARATHI,Asistant
professor
348. Meld Two Min Leftist Trees
8 6 9
6 8 5
4 3
6
Traverse only the rightmost paths so as to get
logarithmic performance. 348
G.JAYABHARATHI,Asistant
professor
349. Meld Two Min Leftist Trees
8 6 9
6 8 5
4 3
6
Meld right subtree of tree with smaller root and
all of other tree.
349
G.JAYABHARATHI,Asistant
professor
350. Meld Two Min Leftist Trees
8 6 9
6 8 5
4 3
6
Meld right subtree of tree with smaller root and all of
other tree.
350
G.JAYABHARATHI,Asistant
professor
351. Meld Two Min Leftist Trees
8 6
6 8
4 6
Meld right subtree of tree with smaller root and all of
other tree.
351
G.JAYABHARATHI,Asistant
professor
352. Meld Two Min Leftist Trees
8 6
Meld right subtree of tree with smaller root and all of
other tree.
Right subtree of 6 is empty. So, result of melding right
subtree of tree with smaller root and other tree is the
other tree.
352
G.JAYABHARATHI,Asistant
professor
353. Meld Two Min Leftist Trees
Swap left and right subtree if s(left) < s(right).
Make melded subtree right subtree of smaller root.
8 6
6
8
6
8 353
G.JAYABHARATHI,Asistant
professor
354. Meld Two Min Leftist Trees
8 6
6 6
4
8
8 6
6
4
6
8
Make melded subtree right subtree of smaller root.
Swap left and right subtree if s(left) < s(right).
354
G.JAYABHARATHI,Asistant
professor
355. Meld Two Min Leftist Trees
9
5
3
Swap left and right subtree if s(left) < s(right).
Make melded subtree right subtree of smaller root.
8 6
6 6
4
8
355
G.JAYABHARATHI,Asistant
professor
356. Meld Two Min Leftist Trees
9
5
3
8 6
6 6
4
8
356
G.JAYABHARATHI,Asistant
professor
357. Initializing In O(n) Time
• create n single node min leftist trees
and place them in a FIFO queue
• repeatedly remove two min leftist trees
from the FIFO queue, meld them, and
put the resulting min leftist tree into the
FIFO queue
• the process terminates when only 1 min
leftist tree remains in the FIFO queue
• analysis is the same as for heap
initialization 357
G.JAYABHARATHI,Asistant
professor
359. Divide and Conquer
• Recursive in structure
– Divide the problem into sub-problems that are similar to the
original but smaller in size
– Conquer the sub-problems by solving them recursively. If they
are small enough, just solve them in a straightforward manner.
– Combine the solutions to create a solution to the original problem
Intro 359
G.JAYABHARATHI,Asistant
professor
360. An Example: Merge Sort
Sorting Problem: Sort a sequence of n elements into non-decreasing
order.
• Divide: Divide the n-element sequence to be sorted into two
subsequences of n/2 elements each
• Conquer: Sort the two subsequences recursively using merge sort.
• Combine: Merge the two sorted subsequences to produce the sorted
answer.
Intro 360
G.JAYABHARATHI,Asistant
professor
362. Merge-Sort (A, p, r)
INPUT: a sequence of n numbers stored in array A
OUTPUT: an ordered sequence of n numbers
MergeSort (A, p, r) // sort A[p..r] by divide & conquer
1 if p < r
2 then q (p+r)/2
3 MergeSort (A, p, q)
4 MergeSort (A, q+1, r)
5 Merge (A, p, q, r) // merges A[p..q] with A[q+1..r]
Initial Call: MergeSort(A, 1, n)
Intro 363
G.JAYABHARATHI,Asistant
professor
363. Procedure Merge
Merge(A, p, q, r)
1 n1 q – p + 1
2 n2 r – q
3 for i 1 to n1
4 do L[i] A[p + i – 1]
5 for j 1 to n2
6 do R[j] A[q + j]
7 L[n1+1]
8 R[n2+1]
9 i 1
10 j 1
11 for k p to r
12 do if L[i] R[j]
13 then A[k] L[i]
14 i i + 1
15 else A[k] R[j]
16 j j + 1
Sentinels, to avoid having to
check if either subarray is
fully copied at each step.
Input: Array containing
sorted subarrays A[p..q]
and A[q+1..r].
Output: Merged sorted
subarray in A[p..r].
Intro 364
G.JAYABHARATHI,Asistant
professor
364. j
Merge – Example
6 8 26 32 1 9 42 43
… …
A
k
6 8 26 32 1 9 42 43
k k k k k k k
i i i i
i j j j j
6 8 26 32 1 9 42 43
1 6 8 9 26 32 42 43
k
L R
Intro 365
G.JAYABHARATHI,Asistant
professor
365. Correctness of Merge
Merge(A, p, q, r)
1 n1 q – p + 1
2 n2 r – q
3 for i 1 to n1
4 do L[i] A[p + i – 1]
5 for j 1 to n2
6 do R[j] A[q + j]
7 L[n1+1]
8 R[n2+1]
9 i 1
10 j 1
11 for k p to r
12 do if L[i] R[j]
13 then A[k] L[i]
14 i i + 1
15 else A[k] R[j]
16 j j + 1
Loop Invariant for the for loop
At the start of each iteration of the
for loop:
Subarray A[p..k – 1]
contains the k – p smallest elements
of L and R in sorted order.
L[i] and R[j] are the smallest elements of
L and R that have not been copied back into
A.
Initialization:
Before the first iteration:
•A[p..k – 1] is empty.
•i = j = 1.
•L[1] and R[1] are the smallest
elements of L and R not copied to A.
Intro 366
G.JAYABHARATHI,Asistant
professor
366. Correctness of Merge
Merge(A, p, q, r)
1 n1 q – p + 1
2 n2 r – q
3 for i 1 to n1
4 do L[i] A[p + i – 1]
5 for j 1 to n2
6 do R[j] A[q + j]
7 L[n1+1]
8 R[n2+1]
9 i 1
10 j 1
11 for k p to r
12 do if L[i] R[j]
13 then A[k] L[i]
14 i i + 1
15 else A[k] R[j]
16 j j + 1
Maintenance:
Case 1: L[i] R[j]
•By LI, A contains p – k smallest elements
of L and R in sorted order.
•By LI, L[i] and R[j] are the smallest
elements of L and R not yet copied into A.
•Line 13 results in A containing p – k + 1
smallest elements (again in sorted order).
Incrementing i and k reestablishes the LI
for the next iteration.
Similarly for L[i] > R[j].
Termination:
•On termination, k = r + 1.
•By LI, A contains r – p + 1 smallest
elements of L and R in sorted order.
•L and R together contain r – p + 3 elements.
All but the two sentinels have been copied
back into A.
Intro 367
G.JAYABHARATHI,Asistant
professor
367. Analysis of Merge Sort
• Running time T(n) of Merge Sort:
• Divide: computing the middle takes (1)
• Conquer: solving 2 subproblems takes 2T(n/2)
• Combine: merging n elements takes (n)
• Total:
T(n) = (1) if n = 1
T(n) = 2T(n/2) + (n) if n > 1
T(n) = (n lg n) (CLRS, Chapter 4)
Intro 368
G.JAYABHARATHI,Asistant
professor