tutorial 8 sorting (2 nd part) & binary search tree

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

Upload: karina-doland

Post on 16-Dec-2015

228 views

Category:

Documents


0 download

TRANSCRIPT

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

Tutorial 8

Sorting (2nd part) &

Binary Search Tree

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

Merge Sort

• Key ideas:– 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!

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

Student Presentation

• Gr31. Lim Wei Hong or Chia Jie Shen

2. David Seo and Li Huan (or Tanvir Islam or Zhang Jianfei)

3. Jacob Pang (or Hema Kumar or Robin Teh)

• Gr41. Cynthia Tan or Sherilyn Ng

2. Tan Peck Luan and Jasmine Choy

3. Hanyenkno Afi or Wang Kang

Overview of the questions:1. Trace Merge Sort (1 student)

2. Binary Search Tree (2 students)2a-b and 2c-d

3. Convert a Binary Search Tree into Double Linked Circular List (1 student)

• Gr51. Stephanie Teo

2. Joyeeta Biswas

3. Zhang Denan

• Gr61. Laura Chua or Zhang Chao

2. Brenda Koh and Gerard Lou

3. Rasheilla or Chow Jian Ann

3

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

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, 9 2 6 1 3, 7 8 0 5

4 9 1, 6 3 7 0, 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

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

Quick Sort

• Key ideas:– 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

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

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≤x >x

x

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

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

• Start from root, at each step, determine whether to go to left or right• 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– http://www.cs.jhu.edu/~goodrich/dsa/trees/btree.html

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

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 successoris taken for deletionof node with twochildren)

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

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)

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

Q3: BST to a Sorted DLCL

• Give pseudocodeto convert a BSTinto a sorted DLCL!

// Modify recursive inorder traversal!

makeCircularDLL(root, doubleLinkedList){

if(root.left != NULL)

makeCircularDLL(root.left, doubleLinkedList);

doubleLinkedList.insertTail(root.item); // assume ADT linked list is ready

if(root.right != NULL)

makeCircularDLL(root.right, doubleLinkedList);

}

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

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, 2-3-4 Tree, Red Black Tree, etc…– Java has TreeMap ADT

• It is a balanced BST (Red-Black Tree)• http://java.sun.com/j2se/1.5.0/docs/api/java/util/TreeMap.html

• The height of Balanced BST is expected to be O(log2 n)

• Next week: something that probably faster than BST: Hash Table

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

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)

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

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.

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

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)

• D(3)

– I(8) I(-2) I(1)• D(5) D(9)

– I(-1) I(0)• D(1)