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.

Slides:



Advertisements
Similar presentations
David Luebke 1 6/1/2014 CS 332: Algorithms Medians and Order Statistics Structures for Dynamic Sets.
Advertisements

Chapter 12 Binary Search Trees
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)
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
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.
The complexity and correctness of algorithms (with binary trees as an example)
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,
Chapter 12 Binary search trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from.
Binary Search Trees Comp 550.
CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Sorting. How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order.
COMP 171 Data Structures and Algorithms Tutorial 6 Binary Search Trees.
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.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
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.
DAST 2005 Tirgul 7 Binary Search Trees. DAST 2005 Motivation We would like to have a dynamic ADT that efficiently supports the following common operations:
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
CS 2133: Data Structures Binary Search Trees.
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.
CS 3343: Analysis of Algorithms Lecture 16: Binary search trees & red- black trees.
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.
Binary Search Tree Qamar Abbas.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
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.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
October 9, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Aalborg University
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
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.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Lecture 91 Data Structures, Algorithms & Complexity Insertion and Deletion in BST GRIFFITH COLLEGE DUBLIN.
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.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Binary Search Trees What is a binary search tree?
CS 3013 Binary Search Trees.
Binary Search Trees.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
CS200: Algorithms Analysis
CS 583 Analysis of Algorithms
CS 3013 Binary Search Trees.
Lecture 7 Algorithm Analysis
Ch. 12: Binary Search Trees Ming-Te Chi
ძებნის ორობითი ხეები BST (Binary Search Trees)
Ch. 12: Binary Search Trees Ming-Te Chi
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
Topic 6: Binary Search Tree Data structure Operations
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Presentation transcript:

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 data ■ Dynamic sets support queries such as: ○ Search(S, k), Minimum(S), Maximum(S), Successor(S, x), Predecessor(S, x) ■ They may also support modifying operations like: ○ Insert(S, x), Delete(S, x)

David Luebke 3 5/4/2015 Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite data, elements have: ■ key: an identifying field inducing a total ordering ■ left: pointer to a left child (may be NULL) ■ right: pointer to a right child (may be NULL) ■ p: pointer to a parent node (NULL for root)

David Luebke 4 5/4/2015 Review: Binary Search Trees ● BST property: Any element in the left subtree is less than or equal to its root, and any element in the right subtree is greater than or equal to the root. ● Example: F BH KDA

David Luebke 5 5/4/2015 Inorder Tree Walk ● What does the following code do? TreeWalk(x): if x != NULL: TreeWalk(left[x]); print(x); TreeWalk(right[x]); ● A: prints elements in sorted (increasing) order ● This is called an inorder tree walk ■ Preorder tree walk: print root, then left, then right ■ Postorder tree walk: print left, then right, then root

David Luebke 6 5/4/2015 Inorder Tree Walk ● Example: ● How long will a tree walk take? ● Prove that inorder walk prints in monotonically increasing order (sorts!) F BH KDA

David Luebke 7 5/4/2015 Operations on BSTs: Search ● Given a key and a pointer to a node, returns an element with that key or NULL: TreeSearch(x, k) if (x = NULL or k = key[x]) return x; if (k < key[x]) return TreeSearch(left[x], k); else return TreeSearch(right[x], k);

David Luebke 8 5/4/2015 BST Search: Example ● Search for D: F BH KDA

David Luebke 9 5/4/2015 BST Search: Example ● Search for D: F BH KDA D Before or After F?

David Luebke 10 5/4/2015 BST Search: Example ● Search for D: F BH KDA D Before or After B?

David Luebke 11 5/4/2015 BST Search: Example ● Search for D: F BH KDA Found!

David Luebke 12 5/4/2015 BST Search: Example ● Search for C: F BH KDA

David Luebke 13 5/4/2015 Operations on BSTs: Search ● Here’s another function that does the same: TreeSearch(x, k) while (x != NULL and k != key[x]) if (k < key[x]) x = left[x] else x = right[x] return x ● Which of these two functions is more efficient?

David Luebke 14 5/4/2015 Operations of BSTs: Insert ● Adds an element x to the tree so that the binary search tree property continues to hold ● The basic algorithm ■ Like the search procedure above ■ Insert x in place of NULL ■ Use a “trailing pointer” to keep track of where you came from (like inserting into singly linked list)

David Luebke 15 5/4/2015 BST Insert: Example ● Example: Insert C F BH KDA C

David Luebke 16 5/4/2015 Insert TreeInsert(T,z) y <- NULL x <- root(T) while x != NULL: y <- x if key[z] < key[x]: x <- left[x] else: x <- right[x] parent[z] <- y if y = NULL: root(T) <- z else if key[z] < key[y]: left[y]<-z else: right[y]<- z

David Luebke 17 5/4/2015 BST Search/Insert: Running Time ● What is the running time of TreeSearch() or TreeInsert()? ● A: O(h), where h = height of tree ● What is the height of a binary search tree? ● A: worst case: h = O(n) when tree is just a linear string of left or right children

David Luebke 18 5/4/2015 BST Operations: Minimum ● How can we implement a Minimum() query? ● What is the running time?

David Luebke 19 5/4/2015 BST Operations: Successor ● For deletion, we will need a Successor() operation ● Successor of a node is the node that comes next in order. ● What are the general rules for finding the successor of node x? (hint: two cases)

David Luebke 20 5/4/2015 BST Operations: Successor ● Two cases: ■ x has a right subtree: successor is minimum node in right subtree ■ x has no right subtree: successor is first ancestor of x whose left child is also ancestor of x ○ Intuition: As long as you move to the left up the tree, you’re visiting smaller nodes. ● Predecessor: similar algorithm

David Luebke 21 5/4/2015 Successor Example What’s the successor of 15?

David Luebke 22 5/4/2015 Successor Example What’s the successor of 15? The minimum of the right subtree.

David Luebke 23 5/4/2015 Successor Example What’s the successor of 13? It has no right subtree…

David Luebke 24 5/4/2015 Successor Example What’s the successor of 13? Lowest ancestor that has a left node that’s an ancestor.

David Luebke 25 5/4/2015 Successor TreeSuccessor(x) if right[x] != NULL return TreeMinimum(right[x]) y <- parent[x] while y != NULL and x = right[y] x <- y y <- parent[y] return y

David Luebke 26 5/4/2015 BST Operations: Delete ● Deletion is a bit tricky ● 3 cases: ■ x has no children: ○ Remove x F BH KDA C Example: delete K F BH DA C

David Luebke 27 5/4/2015 BST Operations: Delete ● Deletion is a bit tricky ● 3 cases: ■ x has one child: ○ Splice out x F BH KDA C Example: delete H F BK DA C

David Luebke 28 5/4/2015 BST Operations: Delete ● Deletion is a bit tricky ● 3 cases: ● x has two children: ● Replace with successor F BK DA C Example: delete B

David Luebke 29 5/4/2015 BST Operations: Delete F BH DA C Example: delete B F C K DA B’s successor is C Delete C Replace B with C

David Luebke 30 5/4/2015 BST Operations: Delete ● Why will case 2 always go to case 0 or case 1? ● A: because when x has 2 children, its successor is the minimum in its right subtree ● Could we swap x with predecessor instead of successor? ● A: yes. Would it be a good idea? ● A: might be good to alternate

David Luebke 31 5/4/2015 Delete TreeDelete(T,z) if left[z] = NULL or right[z] = NULL y <- z else y <- TreeSuccessor(z) if left[y] != NULL: x <- left[y] else: x <- right[y] if x != NULL: parent[x] <- parent[y] if parent[y] = NULL: root(T) = x else if y = left[parent[y]]: left[parent[y]] <- x else: right[parent[y]] <- x if y != z: key[z] <- key[y] // copy the node’s data return y