Professional Documents
Culture Documents
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
struct tree_node
{
int data;
tree_node* left;
tree_node* right;
};
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
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
BinarySearchTree();
bool isEmpty();
void insertItem(int);
void removeItem(int);
bool searchItem(int);
int findHeight();
int countTotalNodes();
int countLeafNodes();
};
• 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
User-Defined Data
Types
item = Pop()
Push(M) item = M
M
D D D
C C C
B B B
A A A
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)
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
• 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
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
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
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