4-Searching1 Searching Dan Barrish-Flood. 4-Searching2 Dynamic Sets Manage a changing set S of elements. Every element x has a key, key[x]. Operations:

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
Introduction to Algorithms Jiafen Liu Sept
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
UNC Chapel Hill Lin/Foskey/Manocha Binary Search Tree Her bir node u bir object olan bir linked data structure ile temsil edilebilir. Her bir node key,
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Binary Search Trees Comp 550.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Binary Search Trees CIS 606 Spring Search trees Data structures that support many dynamic-set operations. – Can be used as both a dictionary and.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
4a-Searching-More1 More Searching Dan Barrish-Flood.
Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
CS 361 – Chapter 3 Sorted dictionary ADT Implementation –Sorted array –Binary search tree.
Binary Search Tree Qamar Abbas.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
October 9, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Aalborg University
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Mudasser Naseer 1 1/25/2016 CS 332: Algorithms Lecture # 10 Medians and Order Statistics Structures for Dynamic Sets.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
Binary Search Trees Lecture 6 Prof. Dr. Aydın Öztürk.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Binary Search Trees What is a binary search tree?
Binary search tree. Removing a node
Binary Search Trees.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
CS200: Algorithms Analysis
Lecture 7 Algorithm Analysis
Elementary Data Structures
Binary Search Trees (13.1/12.1)
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VII
Chapter 12: Binary Search Trees
CS6045: Advanced Algorithms
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search Trees
Topic 6: Binary Search Tree Data structure Operations
Binary SearchTrees [CLRS] – Chap 12.
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Binary Trees, Binary Search Trees
Presentation transcript:

4-Searching1 Searching Dan Barrish-Flood

4-Searching2 Dynamic Sets Manage a changing set S of elements. Every element x has a key, key[x]. Operations: –SEARCH(k): return x, s.t. key[x] = k (or NIL) –INSERT(x): Add x to the set S –DELETE(x): Remove x from the set S (this uses a pointer to X, not a key value). –MINIMUM: Return the element w/the min key. –MAXIMUM: Return the element w/the max key. –SUCCESSOR(x): Return the next largest element. –PREDECESSOR(x):Return the next smallst element.

4-Searching3 Implementing Dynamic Sets with unordered array SEARCH takes Θ(n) INSERT takes Θ(1) if we don’t care about duplicates. DELETE takes Θ(n), so do MINIMUM, MAXIMUM, SUCCESSOR, PREDECESSOR. How to deal with fixed length limitation? Grow the array as needed...

4-Searching4 Growable arrays INSERT(x) 1.if next_free[A] > length[A] then 2. GROW(A) 3.A[next_free[A]] ← x 4.next_free[A] ← next_free[A] + 1 GROW(A) 1.allocate B to be 2·length[A] 2.for i ← to next_free[A] – 1 do 3. B[i] ← A[i] 4.next_free[B] ← next_free[A] 5.free A 6.A ← B

4-Searching5 Running time of INSERT

4-Searching6 Sorted Arrays INSERT takes Θ(n) time, so does DELETE MIN, MAX, SUCC, PRED take Θ(1) time. What about SEARCH? Use binary search: –Guess the middle –If it’s correct, we’re done. –If it’s too high, recurse on the left half –It it’s too low, recurse on the right half

4-Searching7 Running Time for Binary Search T(1) = 1 T(n) = T(n/2) + 1, for n ≥ 2 Do the domain transformation: n = 2 k and k = lgn, etc. telescope, back-substitute, you know the routine: you end with: T(n) = Θ(lgn), worst-case

4-Searching8 Linked Lists for Dynamic Sets Unordered (pretty much same as unordered array, running-time-wise) –INSERT in Θ(1) (stick it at the head) –SEARCH in Θ(n) (linear search) –DELETE in Θ(n) Θ(1) on doubly-linked list (sort of...) –MIN, MAX in Θ(n) –SUCC, PRED in Θ(n)

4-Searching9 Linked Lists (Continued) Sorted –INSERT in Θ(n)...need to find the right place –SEARCH in Θ(n)...no binary search on linked lists! –DELETE in Θ(n) for singly linked list Θ(1) for doubly-linked list, sort of... –MIN in Θ(1) –MAX in Θ(1) with end pointer –SUCC, PRED can take Θ(1) PRED iff doubly-linked

4-Searching10 Can we get all operations to run in time O(lgn)? Yes, pretty much, using trees. A binary search tree is a binary tree with a key at each node x, where: –All the keys in the left subtree of x ≤ key[x] –All the keys in the right subtree of x ≥ key[x]

4-Searching11 Two binary search trees How do we search? Find min, max? insert, delete?

4-Searching12 Here’s a nice BST we’ll refer back to this slide for SEARCH, MIN, MAX, etc.

4-Searching13 Node representation for BST

4-Searching14 Code for BST SEARCH These run in time O(h), where h is the height of the tree.

4-Searching15 Code for BST MIN, MAX ( both run in O(h) ) For MIN, follow left pointers from the root For MAX, follow right pointers from the root.

4-Searching16 BST SUCCESSOR, PREDECESSOR SUCCESSOR is broken into two cases. SUCCESSOR runs in O(h), where h is the height of the tree. The case for PREDECESSOR is symmetric.

4-Searching17 Turning a BST into a sorted list (or printing it) Since the procedure is called (recursively) exactly twice for each node (once for its left child and once for its right child), and does a constant amount of work, the tree walk takes Θ(n) time for a tree of n nodes. (Later, we will see about pre- and post-order tree walks.)

4-Searching18 BST INSERT runs in O(h)

4-Searching19 BST DELETE runs in O(h)

4-Searching20 TREE_DELETE case one

4-Searching21 TREE_DELETE case two

4-Searching22 TREE_DELETE case three