Searching: Introduction Searching is a major topic in data structures and algorithms Applications: Search for students transcripts from ARR Search for faculty contact email address, office Search for books, journals in the library Search for a word from a dictionary Questions: What kinds of data structures (ADT) are used? What kinds of algorithms are used? Binary Search Trees COMP171/S2002/L1 1
Searching: ADT Elements: sets of records with keys, e.g. student s transcript from ARR, key is ID ; Call no. of a book from the library, key is author ; Word from the dictionary, key is the word. Basic operations: isempty ismember (search or query) insert! search + add delete! search + remove Other possible operations: successor and predecessor maximum and minimum range Binary Search Trees COMP171/S2002/L1 2
Search Trees We introduce a new efficient representation for Table and Dictionary, we call this new ADT Search Trees. Types of Search Trees: Binary search trees (BSTs) Balanced search trees 2-3+ trees and red-black trees Search trees for disks: B+-trees. Binary Search Trees COMP171/S2002/L1 3
Binary Search Trees (BST) These are binary trees with the following properties: For a binary tree of size n, with n distinct keys, A unique key is associated with each internal node; The labeled tree satisfies the Binary-search-tree property: For all internal nodes u, keys in the left subtree of u are less than the key at u, and the key at u is less than the keys in the right subtree of u. Binary Search Trees COMP171/S2002/L1 4
Binary Search Trees: Example 1 Days of the week: We assume the names are strings and use string comparisons for the order. BST (?) Thursday Monday Tuesday Friday Sunday Wednesday Saturday Binary-search-tree condition: keys in the left subtree of u are less than the key at u, and the key at u is less than the keys in the right subtree of u. Binary Search Trees COMP171/S2002/L1 5
Binary Search Trees: Example 2 Days of the week: BST (?) Tuesday Thursday Wednesday Monday Sunday Friday Saturday Binary-search-tree condition: keys in the left subtree of u are less than the key at u, and the key at u is less than the keys in the right subtree of u. Binary Search Trees COMP171/S2002/L1 6
Binary Search Trees: Example 3 Days of the week: BST (?) Thursday Monday Sunday Friday Tuesday Wednesday Saturday Binary-search-tree condition: keys in the left subtree of u are less than the key at u, and the key at u is less than the keys in the right subtree of u. Binary Search Trees COMP171/S2002/L1 7
Binary Search Trees: Example 4 (Cont d) Search for M : start at the root, for each internal node, if equal,!found. if less than key at node, then go left subtree else go right subtree if node is external node,! key is NOT found. C H K T M W Y Similar to Binary search!! Performance: O(h) or O(log n) Binary Search Trees COMP171/S2002/L1 8
Binary Search Trees (Cont d) Operations: (key is x) search(x) minimum maximum successor(x) predecessor(x) insert(x) delete(x) Binary Search Trees COMP171/S2002/L1 9
BST - Search(x) Given Key, x and a BST, T, of the records Search for x in the BST, T Algorithm: T[u] denote the key at node u if x = T[u]! found if x < T[u]! search left subtree of u if x > T[u]! search right subtree of u Discussion: Q: When do we know that x is not found? A: When an external node is reached! unsuccessful search Binary Search Trees COMP171/S2002/L1 10
BST - Search(x) (Cont d) Performance Analysis: Recursive procedure - binary search In the worst case, searching time is O(h); where h is the height of BST! O(log n) where n is the size of the BST. Recurrence relation: T(1) = a T(n) = a + T(n/2)! O(log n) Binary Search Trees COMP171/S2002/L1 11
BST - Minimum Finding the Minimum Key in the BST Q: Where is it? A: The leftmost node whose left child is an external node. (Why?) Q: How do we find the minimum key in a BST? Or how do we find the minimum key in a subtree rooted at a node u? A: Traverse the left subtree of u, until the node has a left external child. Key at u is the minimum. 52 Performance: O(h) time in the worst case. 20 80 10 38 28 68 90 Binary Search Trees COMP171/S2002/L1 12
BST - Maximum Finding the Maximum Key of a BST: Q: Where is it? A: The rightmost node whose right child is an external node. Q: How do we find the maximum key in a BST? Or the maximum key of a subtree rooted at a node u? A: (exercise) 52 Performance: O(?) 20 80 10 38 28 68 90 Binary Search Trees COMP171/S2002/L1 13
BST - Successor(x) Successor(x) is the key immediately following (or right after ) x Idea: successor(x) ==> Search(x) +? (3 Phases) search(x) Search will be successful, i.e. end up at an internal node, u Right-child(u) Go to the right child of u, say r minimum(r) The minimum key of the subtree rooted at r is the successor of x. Q: What if node r is an external node? (see example) Performance: O(h) time in the worst case. 52 20 80 10 38 68 90 Binary Search Trees COMP171/S2002/L1 14 28
BST - Predecessor(x) Predecessor(x) is the key immediately preceding (or right before ) x. Idea: predecessor(x) ==> search(x) +? (3 Phases) search(x) Search will be successful, i.e. end up at an internal node, u Left-child(u) Go to the left child of u, say r maximum(r) The maximum key of the subtree rooted at node r is the predecessor of x. 52 Q: What if node r is an external node? 20 Performance: O(h) time in the worst case. 10 38 Binary Search Trees COMP171/S2002/L1 15 28 68 80 90
minimum!? Binary Search Trees: Example T minimum at node M!? H W maximum!? C M Y successor(t)!? K [U,V] successor(k)!? predecessor(t)!? [A,B] [D,G] [N,S] [I,J] L predecessor(w)!? X Z Binary Search Trees COMP171/S2002/L1 16
BST - Insert(x) Idea: (basically, adding an internal node to the BST at the right place) Insert(x)! Search(x) + Add(x) (2 Phases) Search(x) Search will be unsuccessful, i.e. end up at an external node (if search successful, no need to add) Found the right place for the new key Add(x) Change external node to an internal node, u, (with x as key) with two external children. 52 Ensure the BST condition is still valid Analysis 20 80 Takes O(h) time in the worst case 10 38 68 Example: Insert(50) Binary Search Trees COMP171/S2002/L1 17 28 90
BST Insertion example Insert keys: 8, 1, 17, 3, 5, 19 and 12 in that order into an initially empty binary search tree: 8 8 8 8 1 17 1 17 1 3 8 1 17 8 1 17 8 1 17 3 3 19 3 12 19 5 5 5 Binary Search Trees COMP171/S2002/L1 18
BST - Delete(x) Idea: delete(x)! Search(x) + Remove(x) (2 Phases) Search(x) Search will be successful, i.e. end up at an internal node, u (if search unsuccessful, no need to remove) Found the right place to remove x Remove(x) Remove the internal node, u. Ensure the BST condition is still valid Binary Search Trees COMP171/S2002/L1 19
BST - Remove(x) (Cont d) Remove(x) (4 scenarios) u has 2 external nodes as children! replace u and its children with a single external node. u has a left external node and a right subtree! replace u and its left child (an external node) with the right subtree of u. u has a right external node and a left subtree! replace u and its right child (an external node) with the left subtree of u. u has 2 internal children (2 subtrees as children)! non-trivial case. Binary Search Trees COMP171/S2002/L1 20
BST Deletion example 8 delete 5 8 delete 1 8 1 17 1 17 3 17 3 5 12? 3 delete 8 12 delete 17 12 8 3 12 Binary Search Trees COMP171/S2002/L1 21
BST - delete(x): non-trivial case (Cont d) Remove(x): (e.g. x = H) H has 2 internal children (2 subtrees as children) How do restore the BST property? T T H W K W C M Y C M Y K Binary Search Trees COMP171/S2002/L1 22
BST - delete(x) T Remove(x): x is at node u H W Idea: Replace x with Its successor! the minimum of u s right subtree; or Its predecessor! the maximum of u s left subtree. C K M Y Performance: O(h) time in the worst case. Example: x = H? Binary Search Trees COMP171/S2002/L1 23
Exercise: BST - delete(x) where x is the root T W Y K W K Y K C M Y C M C M? Binary Search Trees COMP171/S2002/L1 24
Performance of BST Measuring the efficiency of operations on trees: Node visit is a measure that we choose Measures of efficiency for BST: h - height of the tree n - number of elements. (more realistic) Methods: search(x) is O(h) insert(x) is O(h) delete(x) is O(h) successor(x) is O(h) Note: Fat BST has the best worst case; h is approx. log n. Skinny BST has the worst worst case; h is approx. is n. Binary Search Trees COMP171/S2002/L1 25
Depth First Traversal: Pre-order English description Empty tree terminate Non-empty tree: Visit root node Traverse left subtree of root Traverse right subtree of root Summary: Visit parents before children, and left child before right child: N-L-R Level 0 Root Left Child Parent Right Child Binary Search Trees COMP171/S2002/L1 26
Depth First Traversal: Pre-order illustration N-L-R Root Parent Left Child Right Child Binary Search Trees COMP171/S2002/L1 27
N-L-R Depth First Traversal: Pre-order (cont d) B Example: Q Y R E H X G M K T P A Binary Search Trees COMP171/S2002/L1 28
Depth First Traversal: Pre-order (cont d) Algorithm: Preorder(x) Input: x is the root of a subtree. 1. if x is not NULL 2. Then output key(x); 3. preorder(left(x)); 4. preorder(right(x)); Binary Search Trees COMP171/S2002/L1 29
Depth First Traversal: In-order English description: it s also called Symmetric order traversal Empty tree terminate Non-empty tree: Traverse left subtree of root Visit root node Level 0 Root Traverse right subtree of root Summary: Traverse left subtree before visiting root, Visit root before traversing right subtree. L-N-R Left Child Parent Right Child Binary Search Trees COMP171/S2002/L1 30
Depth First Traversal: In-order illustration L-N-R Root Parent Left Child Right Child Binary Search Trees COMP171/S2002/L1 31
L-N-R Depth First Traversal: In-order (cont d) B Example: Q Y R E H X G M K T P A Binary Search Trees COMP171/S2002/L1 32
Depth First Traversal: In-order (cont d) Algorithm: Inorder(x) Input: x is the root of a subtree. 1. if x is not NULL 2. Then Inorder(left(x)); 3. output key(x); 4. Inorder(right(x)); Binary Search Trees COMP171/S2002/L1 33
Depth First Traversal: Post-order English description: Empty tree terminate Non-empty tree: Traverse left subtree of root Traverse right subtree of root Visit root node Summary: Visit Children (left before right) before parent. L-R-N Level 0 Root Left Child Parent Right Child Binary Search Trees COMP171/S2002/L1 34
Depth First Traversal: Post-order illustration L-R-N Root Parent Left Child Right Child Binary Search Trees COMP171/S2002/L1 35
L-R-N Depth First Traversal: Post-order (cont d) B Example: Q Y R E H X G M K T P A Binary Search Trees COMP171/S2002/L1 36
Depth First Traversal: Post-order (cont d) Algorithm: Postorder(x) Input: x is the root of a subtree. 1. if x is not NULL 2. Then Postorder(left(x)); 3. Postorder(right(x)); 4. output key(x); Binary Search Trees COMP171/S2002/L1 37
BST: Traversals Implications of the binary-search-tree condition: inorder traversal (L-N-R) gives sorted order external nodes are associated with gaps between adjacent keys Binary Search Trees COMP171/S2002/L1 38
Binary Search Trees Inorder traversal of internal nodes gives sorted order: C H K M T W Y T H W C M Y K Binary Search Trees COMP171/S2002/L1 39
Binary Search Trees External nodes are associated with gaps between adjacent keys, eg. Range is from A to Z. T H W C M Y K [U,V] [A,B] [D,G] [N,S] X Z [I,J] L Binary Search Trees COMP171/S2002/L1 40