Searching: Introduction

Similar documents
CS 361, Lecture 21. Outline. Things you can do. Things I will do. Evaluation Results

Binary Search Trees. Nearest Neighbor Binary Search Trees Insertion Predecessor and Successor Deletion Algorithms on Trees.

Binary Search Trees. Binary Search Trees. Nearest Neighbor. Nearest Neighbor

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

Lecture 6: Analysis of Algorithms (CS )

Augmenting Data Structures

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Binary Trees. Recursive definition. Is this a binary tree?

TREES. Trees - Introduction

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

Announcements. Problem Set 2 is out today! Due Tuesday (Oct 13) More challenging so start early!

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

COMP Analysis of Algorithms & Data Structures

Lecture: Analysis of Algorithms (CS )

Programming II (CS300)

Algorithms. AVL Tree

Binary Search Trees > = 2014 Goodrich, Tamassia, Goldwasser. Binary Search Trees 1

A set of nodes (or vertices) with a single starting point

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Friday Four Square! 4:15PM, Outside Gates

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Binary Trees, Binary Search Trees

CS102 Binary Search Trees

Binary search trees (BST) Binary search trees (BST)

Trees. (Trees) Data Structures and Programming Spring / 28

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

CMSC 441: Algorithms. Hillol Kargupta, Professor.

CSC 172 Data Structures and Algorithms. Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

x-fast and y-fast Tries

Algorithms. Deleting from Red-Black Trees B-Trees

Binary search trees :

CSE 502 Class 16. Jeremy Buhler Steve Cole. March A while back, we introduced the idea of collections to put hash tables in context.

12 Binary Search Tree

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Binary Search Trees, etc.

SFU CMPT Lecture: Week 9

Binary Search Trees. Motivation. Binary search tree. Tirgul 7

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Binary Search Trees (10.1) Dictionary ADT (9.5.1)

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

(2,4) Trees Goodrich, Tamassia. (2,4) Trees 1

Data Structures in Java

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

COMP Analysis of Algorithms & Data Structures

Search Trees - 1 Venkatanatha Sarma Y

COMP Analysis of Algorithms & Data Structures

Predecessor. Predecessor Problem van Emde Boas Tries. Philip Bille

CS350: Data Structures Binary Search Trees

Binary Search Tree (3A) Young Won Lim 6/6/18

CS24 Week 8 Lecture 1

Week 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.

Week 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.

Binary Search Trees. 1. Inorder tree walk visit the left subtree, the root, and right subtree.

Binary Search Trees. Antonio Carzaniga. Faculty of Informatics University of Lugano. October 22, Antonio Carzaniga 1

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Self-Balancing Search Trees. Chapter 11

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Outline for Today. How can we speed up operations that work on integer data? A simple data structure for ordered dictionaries.

Dictionaries. Priority Queues

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

CS 350 : Data Structures Binary Search Trees

CS350: Data Structures Red-Black Trees

Predecessor. Predecessor. Predecessors. Predecessors. Predecessor Problem van Emde Boas Tries. Predecessor Problem van Emde Boas Tries.

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Search Trees (Ch. 9) > = Binary Search Trees 1

Dictionaries. 2/17/2006 Dictionaries 1

Data Structures and Algorithms for Engineers

INF2220: algorithms and data structures Series 1

A Binary Search Tree Implementation

Trees. Truong Tuan Anh CSE-HCMUT

Binary Search Trees. Analysis of Algorithms

Practical session No. 6. AVL Tree

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

CSCI Trees. Mark Redekopp David Kempe

Multi-Way Search Tree

Associate Professor Dr. Raed Ibraheem Hamed

Chapter 2: Basic Data Structures

Multiway Search Trees. Multiway-Search Trees (cont d)

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Advanced Set Representation Methods

Data Structures and Algorithms

Binary Search Trees. Antonio Carzaniga. Faculty of Informatics University of Lugano. October 21, Antonio Carzaniga 1

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

We assume uniform hashing (UH):

Lecture 3: B-Trees. October Lecture 3: B-Trees

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Today s Outline. CS 561, Lecture 8. Direct Addressing Problem. Hash Tables. Hash Tables Trees. Jared Saia University of New Mexico

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

ץע A. B C D E F G H E, B ( -.) - F I J K ) ( A :. : ע.)..., H, G E (. י : י.)... C,A,, F B ( 2

CMSC 341. Binary Search Trees CMSC 341 BST

Week 2. CS 400 Programming III. Read: Module 2 readings before lecture

Transcription:

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