# Sorting (2nd part) & Binary Search Tree

## Presentation on theme: "Sorting (2nd part) & Binary Search Tree"— Presentation transcript:

Sorting (2nd part) & Binary Search Tree
Tutorial 8 Sorting (2nd part) & Binary Search Tree

Merge Sort Key ideas: Now, let us see an example in Q1!
Chop (unsorted) list into exactly half, recursively! Do it until size = 1 (base case, by default 1 item is sorted) When n is odd, the middle one can go to left or right sub list, just be consistent! When the recursion is winding up, do an efficient O(n) MERGING process. Simply compare front of sub list A and front of sub list B, the smaller is taken first! The overall complexity is O(n log n) At every recursion step we do O(n) merge process And we only do these merging process O(log2 n) times Compare with Tutorial 7 question 3.d part 2 (when g(n) = O(n)) Now, let us see an example in Q1!

Student Presentation Gr3 Gr4 Overview of the questions: Gr5 Gr6
Lim Wei Hong or Chia Jie Shen David Seo and Li Huan (or Tanvir Islam or Zhang Jianfei) Jacob Pang (or Hema Kumar or Robin Teh) Gr4 Cynthia Tan or Sherilyn Ng Tan Peck Luan and Jasmine Choy Hanyenkno Afi or Wang Kang Overview of the questions: Trace Merge Sort (1 student) Binary Search Tree (2 students) 2a-b and 2c-d Convert a Binary Search Tree into Double Linked Circular List (1 student) Gr5 Stephanie Teo Joyeeta Biswas Zhang Denan Gr6 Laura Chua or Zhang Chao Brenda Koh and Gerard Lou Rasheilla or Chow Jian Ann

Q1: Trace Mergesort 4, 9, 2, 6, 1 || 3, 7, 8, 0, 5 4, 9, 2, 6, 1 3, 7, 8, 0, 5 4, 9, 2 6, 1 3, 7, 8 0, 5 4, , , , 5 4, 9 3, 7 2, 4, 9 3, 7, 8 1, 2, 4, 6, 9 0, 3, 5, 7, 8 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 The answer is quite trivial and sometimes mechanical. Ensure that you understand merge sort completely!

Quick Sort Key ideas: Quick Sort will be discussed next week
Partition (unsorted) list in O(n) steps around a reference number (pivot) Left sub list will be smaller than pivot, right sub list will be larger (or equal) than pivot Item equal to pivot can be placed on left or right sub list, just be consistent! After partitioning, pivot will definitely be in the correct place in sorted list. Choosing proper pivot is crucial for Quicksort! People usually take random pivot for better average performance Partitioning algorithm is the most complex part of quick sort. There are several partitioning algorithms out there, all in O(n) Example (as in lecture note) will be shown next week Then, recursively process the left and right sub lists in the same manner. Do it until size = 1 (base case, by default 1 item is sorted) It is on average O(n log n) too, if we use random pivot It can be faster than merge sort due to many reasons not discussed in CS1102 Quick Sort will be discussed next week

Tree, Binary Tree, and BST
Verify that you have understand these: Basic concepts about Trees: Extension of Linked List Node, Edges, Parent, Children, Root, Leaf, Internal Node, Level, Height, Size Basic concepts about Binary Trees: Definition: max 2 children (left and right), full, complete Implementation: reference (linked) or array based Binary tree traversals: Inorder, Preorder, Postorder, Level-order Basic concepts about Binary Search Trees: Definition: Binary Tree where BST property holds. Used in ADT Table (more advanced than List) List: index (position)  data Table: key  data The definition of “Level” and “Height” is not universal and can be differ by 1! ≤x >x x

Binary Search Tree (BST)
Binary Search Tree (assuming a roughly balanced tree): Insert: O(height) ~> O(log2 n) Start from root, at each step, determine whether to go to left or right Insertion will only occur at leaf! Search: O(height) ~> O(log2 n), similar to insertion Stop when item is found or we reach the leaf but item not found Delete, 3 cases: O(height) ~> O(log2 n) Delete leaf (straightforward) Just delete that node Delete internal node with 1 child (either left or right child) Link the node’s only child with the node’s parent Delete internal node with 2 children Pick the inorder successor (or predecessor) to replace the content of the node to be deleted. Delete the actual copy of the replacement node… Demo

Q2: Trace BST Operations (1)
From empty BST, insert: 3, 6, 4, 10, 1, 2, 13, 8, 7, 9, 15, 12, 11. Delete 1 and then 10 (assumption: inorder successor is taken for deletion of node with two children)

Q2: Trace BST Operations (2)
Traversals: Inorder: 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 15 SORTED output! (Remember this) Preorder 3, 2, 6, 4, 11, 8, 7, 9, 13, 12, 15 Postorder 2, 4, 7, 9, 8, 12, 15, 13, 11, 6, 3 Complete Binary Tree? No, many blanks on left side of node 2! Delete 4 nodes (circled)

Next: Balanced BST & Hashing
If your BST is not balanced, its height can be as “tall” as O(n)! It degenerates into another Linked List This is not desirable! There are various proposals of Balanced BST AVL Tree (Adelson-Velski + Landis) Rotate an unbalanced node during insertion/deletion This was inside last year CS1102 syllabus, but not this year. And many others: Splay Tree, Tree, Red Black Tree, etc… Java has TreeMap ADT It is a balanced BST (Red-Black Tree) The height of Balanced BST is expected to be O(log2 n) Next week: something that probably faster than BST: Hash Table

Extra Examples (1) Execute the following sequence of operations on
An empty Binary Search Tree Note: when you delete a node with two children, replace it with the inorder predecessor and delete the inorder predecessor. Show the final tree after the sequence of operations are executed. Insert(10)  I(100)  I(30)  I(80)  I(50), Delete(10), I(60)  I(70)  I(40), D(80), I(90)  I(20), D(30), D(70)

Extra Examples (1) - Solution
The Final BST: If you did not manage to get this tree, re-do your solutions again! Your concept may be still incorrect.

Extra Examples (2-3) Note: when you delete a node with two children, replace it with the inorder predecessor and delete the inorder predecessor. Example 2 Final BST I(1)  I(2)  I(3)  I(4) D(3) I(3)  I(5) D(4) Example 3 Final BST I(5)  I(3)  I(7)  I(9) I(8)  I(-2)  I(1) D(5)  D(9) I(-1)  I(0) D(1)