You are on page 1of 84

CSEE2123: OOP and Data Structures

Fall 2018
Binary Trees
Revision
Lecture 32
M. Tahir Awan
(mtahir@cust.edu.pk)
Capital University of Science & Technology (CUST),
Islamabad
Final-Term Exam (Tentative)
• Final-Term Date & Time
–Date : January 19, 2019 (Saturday)
–Time : 01:30 pm

–Finalterm Percentage 40%


–Finalterm will be Comprehensive

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 2


Assignment # 4 :
Programming Assignment
• Due Date :
– Thursday, January 10th , 2019
– Due at : 05:00 p.m.
• Late Submission
– Not Allowed
• Assignment Submission
– Separate *.cpp file for each Question
– Submit in zip format
– File name : “Name_RollNo_AssignNo.zip”
– Submission Path
» \\fs\assignments$\mTahir\OOP\Assignment4

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 3


Advanced Data Structures
• Linear Data Structures
–Arrays
–Linked Lists
–Stacks
–Queues
• Non-Linear Data Structures
–Trees
–Binary Trees

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 4


Binary Tree
• Binary Tree is a special tree where each parent
node has at maximum two child nodes
• Tree starts at the root node i.e. node at the top
• Leaf node in the tree does not have any child
nodes
root
• Root Node 1 has two
child nodes (Left Node
:2 , Right Node:3) 1
• Nodes are connected
through edges 2 3
• Each tree has one root
node 4 5 6 7
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 5
Binary Tree : Implementation
• Binary tree can be dynamically implemented as
sequence of nodes
• Each node contain one data field and left & right
node pointers

struct tree_node
{
int data;
tree_node* left;
tree_node* right;
};

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 6


Binary Tree Implementation : Class
• A C++ Class to struct tree_node {
tree_node* left;
implement Binary tree_node* right;
Tree int data;
};
• Tree Attributes class BinaryTree {

• Tree Operations private:


tree_node* root;
public:

BinaryTree();
bool isEmpty();
int findHeight();
void inOrder(tree_node*);
void preOrder(tree_node*);
void postOrder(tree_node*);

int countTotalNodes();
int countLeafNodes();
};
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 7
Binary Tree : Traversal
• Traversal is the process to visit all the nodes of a
binary tree. Traversal starts from the root node
• Tree traversal is used in insertion, deletion &
searching operations on trees
• There are three methods to traverse a binary tree
Root
–In-order Traversal
–Pre-order Traversal 17
–Post-order Traversal
41 9

29 6 81 40
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 8
Binary Tree : Breadth First Traversal
• Traverse all the nodes at one Root
level before moving to the next
level, starting from level 0
17

41 9

29 6 81 40

• Breadth First Traversal: {17 41 9 29 6 81 40}

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 9


Binary Tree Implementation : Class
• A C++ Class to struct tree_node {
tree_node* left;
implement Binary tree_node* right;
Tree int data;
};
• Tree Attributes class BinaryTree {

• Tree Operations private:


tree_node* root;
public:

BinaryTree();
bool isEmpty();
int findHeight();
void inOrder(tree_node*);
void preOrder(tree_node*);
void postOrder(tree_node*);

int countTotalNodes();
int countLeafNodes();
};
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 10
Binary Search Tree (BST)
• Binary Search trees are binary trees that exhibit
a special behavior.
– Value of the root node is larger than all the
nodes in left sub-tree
– Value of the root node is smaller than all the
nodes in right sub-tree

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 11


Binary Search Tree Implementation :
Class
• A C++ Class to struct tree_node {
tree_node* left;
implement Binary tree_node* right;
Search Tree int data;
};
• Tree Attributes class BinarySearchTree {

• Tree Operations private:


tree_node* root;
public:

BinarySearchTree();
bool isEmpty();
void insertItem(int);
void removeItem(int);
bool searchItem(int);
int findHeight();
int countTotalNodes();
int countLeafNodes();
};

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 12


Binary Search Tree : Searching
• Algorithm for searching in a
binary search tree
– Compare the data to be
searched with root node ,
if found we are done
– If data to be searched is
less than root node ,
search in the left sub-
tree
– If data to be searched is
greater than root node,
search in the right sub-
tree
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 13
Traversal in Binary Search Trees
• Three Traversal Methods Root
• in-order
• pre-order 25
• post-order
• In order traversal in binary 15 45
search trees generates a sorted
sequence (ascending order)
9 21 33 77

• In-order Sequence: {9 15 21 25 33 45 77}


• Pre-order Sequence: {25 15 9 21 45 33 77}
• Post-order Sequence: {9 21 15 33 77 45 25}
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 14
Binary Search Tree : Insert Item
• In order to insert data in a
binary search tree, first find
the position, where data
should be inserted using
search algorithm
• New item inserted will
always be a leaf node
• Tree should be binary
search tree before and after
insertion of new item
• Binary search tree does not
contain duplicate data

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 15


Binary Search Tree :
Practice Question
• Draw a binary search tree, if nodes are inserted
in the order
• 35, 66, 12, 25, 44, 51, 19, 9, 81

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 16


Binary Search Tree : Remove Item
• In order to remove data
from a binary search tree,
first locate the node
• 4 Cases :
• Case 1: Node to be removed
is a leaf node
• Case 2: Node to be removed
has only left child
• Case 3: Node to be removed
has only right child
• Case 4: Node to be removed
has both left and right
Childs
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 17
Binary Search Tree : Remove Item
• Case 1 : Remove Leaf Node : 45
Before After

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 18


Binary Search Tree : Remove Item
• Case 2 : Remove Node with right child only : 30
Before After

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 19


Binary Search Tree : Remove Item
• Case 3 : Remove Node with left child only : 80
Before After

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 20


Binary Search Tree : Remove Item
• Case 4 : Remove Node with both Childs : 50
Before After

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 21


Binary Search Tree : Remove Item
• Case 4 : Remove Node with both Childs : 50

• Algorithm
• Find position of the node to be removed
• Find a minimum value in the right sub-tree OR
maximum value in the left sub-tree
• Replace value of the node to be removed with
found minimum/maximum.
• To remove duplicate value, it becomes Case 2 or
Case 3 i.e node with one child. Remove the
duplicate node accordingly

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 22


Binary Search Tree Implementation :
Class
• A C++ Class to struct tree_node {
tree_node* left;
implement Binary tree_node* right;
Search Tree int data;
};
• Complete the class BinarySearchTree {
following functions private:
tree_node* root;
• void removeItem(int); public:
• int countTotalNodes();
BinarySearchTree();
• int countLeafNodes();
bool isEmpty();
void insertItem(int);
void removeItem(int);
bool searchItem(int);
int findHeight();
int countTotalNodes();
int countLeafNodes();
};

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 23


Revision
C++ : User-Defined Data Types

User-Defined Data
Types

enum struct union class


Object Oriented Paradigm
• Fundamental idea in
object oriented
programming is to
model entities like
real world objects
• An OOP program can
be viewed as a
collection of
cooperating objects

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 26


Definition of a „class‟
• Definition of a class is composed of
• 1. Data Members
• The data items within a class are
called data members or data fields
that define its attributes
• 2. Function Members
• Member functions are included
within a class to define its behavior
• All computations on an object are
done using member functions

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 27


C++ : Class Definition
Definition of a class starts with the keyword „class‟
followed by class name
Class Definition :
Member functions Out-side class
• Member functions of class can be defined out-
side of class definition
float Circle::getArea() {
return radius* radius* 3.14159;
}

• Scope resolution operator ::


• Class Name :: function Name

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 29


Class Constructor : Definitions
• Class Constructor can be defined in one of many
ways to initialize class attributes
Circle() : radius(0) {}
Circle()
{ radius = 0; }
Circle (int r) : radius(r)
{}
Circle (int r)
{ radius = r; }
Circle (int r = 1)
{ radius = r; }
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 30
The „This‟ Pointer
• Every object in C++ has access to its own
address through an implicit pointer
called this pointer
• The this pointer is an implicit parameter to all
member functions and constructor of the class

Complex( float real, float void setReal( float real)


imag) {
{ this->real = real;
this->real = real; }
this->imag = imag;
}

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 31


C++ : Structures vs. Classes
• In C++ Structures can almost be used exactly the
same way as classes
• Only difference between the two is
– In structures members are public by default
– In classes members are private by default
class noName { struct noName {
int data1; void func();
public: private:
void func(); int data1;
}; };

• Structures are used to group data and classes


are used to group data and functions

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 32


C++ : Objects Memory Allocation
• sizeof()
function can be
used to get
memory
allocated to
class objects
Distance d1;
sizeof(d1)

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 33


Classes and const
• Const keyword can be used with classes in
following ways
• member function arguments as const
• Function arguments of type const cannot be
modified inside function
• const member functions
• Const member functions cannot change any of
the object data members
• const objects
• Const objects cannot modify any of the object
data
• All functions in Const objects should also be
constant
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 34
Function Overloading
• C++ allows to specify more than one definition of
a function, known as function overloading
• Multiple definitions of the function must differ in
terms of types and/or the number of arguments
in the argument list
• Compiler selects the proper function to call
based upon type, number and order of
arguments
• Function definitions that only differ in return type
can not be overload

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 35


Distance Class : „+‟ Operator
Overloading
• Argument on the left of operator is the object it-
self while object on the right is argument to
operator member function

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 36


Concept of Inheritance
• Derived class inherits all the features (attributes,
behavior) of the base class

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 37


Classes : Access Specifiers
• Public, private and protected Access Specifiers

Access public protected private

Same class YES YES YES

Derived classes YES YES NO

Outside classes YES NO NO

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 38


Types of Inheritance
• Different Inheritance structures

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 39


Types of Inheritance specified by Access
Specifiers
• Class B and C are derived from class A

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 40


Function Template
• One function to handle 3 different data types

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 41


Class Templates & Objects
• Datatype will
be specified
while
instantiating
class objects

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 42


Non-Virtual Functions : Static Binding
• Compiler
ignores
contents of the
pointer ptr and
chooses the
member function
that matches the
type of the
pointer ptr

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 43


Virtual Functions : Dynamic Binding
• For Virtual
functions,
compiler selects
the function
based on the
contents of the
pointer ptr, not
on the type of
the pointer

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 44


Polymorphism
• Polymorphism means having many forms.
• Polymorphism means that a call to a member
function will cause a different function to be
executed depending on the type of object that
invokes the function
• In C++ polymorphism is achieved through Virtual
Functions

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 45


Pure Virtual Function & Abstract
Classes
• If a virtual function in base class has no
meaningful definition, it is called pure virtual
function
• Pure virtual function is defined as:
virtual void Display() = 0;

• If base class contain pure virtual function, all


derived classes must override the virtual
function
• Classes that contain at least one pure virtual
function are known as abstract base classes

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 46


Inheritance : Constructor , Destructor
Call Order
• Call order of default constructor & destructors
#include <iostream>
using namespace std; ~Derived ( ) {
class Base { cout<<"Derived destructor"<<endl;
public: }
Base ( ) { };
cout<<"Base construct"<<endl;
} void main( ) {
~Base ( ) { Derived x;
cout<<"Base destructor"<<endl; }
}
};
class Derived : public Base {
Base Class Constructor is
public:
called first than derived
Derived ( ) { class constructor is called
cout<<"Derived construct"<<endl;
}

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 47


Basic Data Structures
• Arrays
–One Dimensional,
–Multidimensional Arrays
• Structures
• Union
• Class Objects
• User Defined Data Types
–struct
–class
–enum
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 48
Types of Advanced Data Structures
• Array-based Lists
• Linked Lists
– Single
– Double
– Circular
• Queue and Stack
• Implementation with arrays and linked lists
• Trees
• Binary

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 49


Advanced Data Structures: Algorithms
• Algorithm is a step-by-step procedure, in order
to get the desired output from data structures
• Algorithms are used to manipulate data in data
structures
• Algorithms used in Data Structures
–Searching
–Sorting
–Traversal
–Hashing
–Recursion

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 50


Components of Standard Template
Library (STL)
• Main components of standard template library
(STL) are :
1. Containers
– Containers are used to store & manage data items e.g.
vectors, lists, stack
2. Iterators
– Iterators are used to step through the elements of data
collection (Traversal)
– Iterators are a generalization of the concept of pointers
3. Algorithms
– Algorithms are used to process and manipulate data
stored in containers e.g. searching, sorting

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 51


Array based List as a Class
• Array based List can be implemented as a class
in C++
• Array based List class will have following
attributes
– An array (static or dynamic)
– Size or Length
– Maximum Array Size
• Operations on List are
implemented using Class
Functions
• Array based List is an
abstract data type (ADT) for
data storage
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 52
Linked List
• A linked list is a data structure that can store an
indefinite amount of items
• Each item in the Linked List contains data and
reference (Link) to the next item. Last item in the
list points to NULL
• Items in the linked list are called nodes. Nodes
are dynamically allocated
• A linked list can grow or shrink in size at runtime
• Address of the first Node is stored in Head

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 53


Types of Linked List
• Singly Linked List

• Doubly Linked List

• Circular Linked List

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 54


Traversing a Linked List
• HeadPtr points to the first Node
• NextPtr points to the Next Node

• HeadPtr->Data (First Element)


• HeadPtr->NextPtr->Data (Second Element)
• Third Element ?

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 55


Building a Doubly Linked List
• Building a Doubly Linked List Forward
• New node is always inserted at the end of the
Linked List

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 56


Building a Circular Linked List
• Adding nodes to an empty Circular Linked List

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 57


Stack Operations : Push & Pop
• Push() will place data on Top of Stack
• Pop() will remove data from Top of Stack
• Top of Stack points to Empty Location on Stack

item = Pop()
Push(M) item = M
M
D D D
C C C
B B B
A A A

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 58


Stack implementation using Arrays :
Push Operation
• Push operation will place data on Top of Stack
• Tope of Stack will Increment

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 59


Linked List based Stack
• Stack can be implemented using Singly Linked
List
• Each item is stored in dynamically created nodes
• In Linked List implementation, TopPtr will point
to last item pushed onto stack

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 60


Queue Operations : EnQueue &
Dequeue
• In FIFO Queue :
• Data is added at Tail
End. Operation is Called
Enqueue
• Data is removed from
Head End. Operation is
called Dequeue
• Data cannot be
accessed from middle
of the Queue

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 61


Array based FIFO Queue : Operations
• Queue Initialization

• Enqueue : Adding Items to the Queue

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 62


Array based FIFO Queue : Operations
• Dequeue : Removing Items from the Queue

• Making the Queue Circular


• First Array position immediately
follows the last array position

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 63


Linked List based FIFO Queue
• Queue can be implemented using Singly Linked
List
• Each item is stored in dynamically created nodes
• FrontPtr is used to remove items from the Queue
, RearPtr is used to add items to the Queue

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 64


Advanced Data Structures: Algorithms
• Algorithm is a step-by-step procedure, in order
to get the desired output from data structures
• Algorithms are used to manipulate data in data
structures
• Algorithms used in Data Structures
–Searching
–Sorting
–Traversal
–Hashing
–Recursion

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 65


Algorithm Complexity :
Big O Notation
• Common asymptotic notations

Class Big-Oh
constant O(1)
logarithmic O(log2 N)
linear O(N)
log-linear O(N log2 N)
quadratic O(N2)
cubic O(N3)
... ...
exponential O(2N)

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 66


Sequential Search : Example
• Search 33 in the array
• Key = 33

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 67


Binary Search : Example
• Search 31 in the array
• Key = 42
• First = 0 , Last = 16

index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
value -4 2 7 10 15 20 22 25 30 36 42 50 56 68 85 92 103

First Mid Last

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 68


Binary Search : Performance Analysis
• Every iteration of Binary Search Loop cuts size
of array by half and makes two comparisons
• Maximum Iterations = m + 1 , where

• Best Case : Item to search is middle item of the


List
• Worst Case : Maximum Comparisons

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 69


Sorting Algorithms
• Sorting is arranging data in a specific order i.e.
ascending or descending order
• Sorting can be used to make searching efficient
i.e. binary search

• Sorting Algorithms
–Bubble Sort
–Selection Sort
–Insertion Sort
–Merge Sort
–Quick Sort
70
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST
Bubble Sort : Example 2
77 42 35 12 101 5

1 2 3 4 5 6

42 35 12 77 5 101

1 2 3 4 5 6

35 12 42 5 77 101
N – 1 Passes

1 2 3 4 5 6

12 35 5 42 77 101

1 2 3 4 5 6

12 5 35 42 77 101

1 2 3 4 5 6

5 12 35 42 77 101
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 71
Selection Sort : Example
• Initial array:
index 0 1 2 3 4 5 6 7 8 9 10
value 22 18 12 -4 27 30 36 50 91 68 7

• After 1st, 2nd, and 3rd passes:


index 0 1 2 3 4 5 6 7 8 9 10
value -4 18 12 22 27 30 36 50 91 68 7

index 0 1 2 3 4 5 6 7 8 9 10
value -4 7 12 22 27 30 36 50 91 68 18

index 0 1 2 3 4 5 6 7 8 9 10
value -4 7 12 22 27 30 36 50 91 68 18
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 72
Selection Sort : Example
• Initial array:
index 0 1 2 3 4 5 6 7 8 9 10
value 22 18 12 -4 27 30 36 50 91 68 7

• After 4th , 5th , and (N-1) Passes:


index 0 1 2 3 4 5 6 7 8 9 10
value -4 7 12 18 27 30 36 50 91 68 22

index 0 1 2 3 4 5 6 7 8 9 10
value -4 7 12 18 22 30 36 50 91 68 27

index 0 1 2 3 4 5 6 7 8 9 10
value -4 7 12 18 22 27 30 36 50 68 91
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 73
Insertion Sort using Arrays
• One pass of Insertion Sort

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 74


Insertion Sort using Linked List
• Pointer lastInOrder points to last element in
sorted sub-list.
• FirstOutOfOrder points to first element in
unsorted sub-list
• Pointers trailCurrent & current are used to
traverse the sorted list

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 75


Insertion Sort vs Selection Sort:
Performance Analysis
• Worst case complexity of both Selection Sort
and Insertion sort is O(n2)

• Both Selection Sort and Insertion Sort are fast as


compared to Bubble Sort on average

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 76


Recursive Algorithms
• An algorithm that finds the solution to a given
problem by reducing the problem to smaller
versions of itself is called a recursive algorithm
• Recursive algorithms can be used to
– Find factorial of a number
– Compute powers of a number
– Fibonacci Series
– Sort an array
• In computation of recursive algorithms, problem
is recursively reduced to a base case

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 77


Merge Sort using Arrays: Example
index 0 1 2 3 4 5 6 7
value 22 18 12 -4 58 7 31 42
split

22 18 12 -4 58 7 31 42
split split

22 18 12 -4 58 7 31 42
split split split split

22 18 12 -4 58 7 31 42
merge merge merge merge
18 22 -4 12 7 58 31 42
merge merge
-4 12 18 22 7 31 42 58
merge

1/10/2019 -4 and7DS12
CSEE2123:OOP 18 ©22 31 Awan,
M. Tahir 42 58
CUST 78
Merge Sort : Performance Analysis
• At Level k, maximum number of comparisons to
merge the lists

• For m Levels , number of comparisons


where

• Maximum number of Comparisons for Merge


Sort

• Merge sort is quite fast as compared to Bubble,


Selection and Insertion Sorts
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 79
Quick Sort : Partitioning of the Array
• Select the Last value as Pivot
• Use two pointers to traverse rest of the array
• Partition the array in two halves and place pivot
in centre
• Recursively repeat the process

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 80


Quick Sort : Performance Analysis
• Performance Analysis of Quick Sort

• Quick Sort is fast as compared to all the sorting


algorithms that we have studied

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 81


Binary Tree
• Binary Tree is a special tree where each parent
node has at maximum two child nodes
• Tree starts at the root node i.e. node at the top
• Leaf node in the tree does not have any child
nodes
root
• Root Node 1 has two
child nodes (Left Node
:2 , Right Node:3) 1
• Nodes are connected
through edges 2 3
• Each tree has one root
node 4 5 6 7
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 82
Binary Search Tree (BST)
• Binary Search trees are binary trees that exhibit
a special behavior.
– Value of the root node is larger than all the
nodes in left sub-tree
– Value of the root node is smaller than all the
nodes in right sub-tree

1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 83


C/C++ Programming Resources
• Other Programming Courses
– Microprocessors & CA
– ASIC Design & FPGA
– Embedded Systems
• Helpful Websites & Resources
• http://www.tutorialspoint.com/
• https://msdn.microsoft.com/en-us/library/
• http://www.cplusplus.com/doc/tutorial/
• Data Structures & Algorithms : Animations
• http://www.ee.ryerson.ca/~courses/coe428/sorting/inserti
onsort.html
• https://www.cs.usfca.edu/~galles/visualization/Algorithm
s.html
1/10/2019 CSEE2123:OOP and DS © M. Tahir Awan, CUST 84

You might also like