David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.

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,
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.
CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
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.
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.
Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm.
BST Data Structure A BST node contains: A BST contains
Lecture 11: Binary Search Trees Shang-Hua Teng. Data Format KeysEntryKeysSatellite data.
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,
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
CS 2133: Data Structures Binary Search Trees.
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.
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 4: Data Structures.
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.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Binary Search Trees Algorithms and Data Structures.
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.
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.
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,
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
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 3013 Binary Search Trees.
Lecture 7 Algorithm Analysis
Ch. 12: Binary Search Trees Ming-Te Chi
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
Binary SearchTrees [CLRS] – Chap 12.
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Presentation transcript:

David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees

David Luebke 2 7/2/2015 Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular, structures 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 7/2/2015 Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite data, eleements 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 7/2/2015 Binary Search Trees ● BST property: key[leftSubtree(x)]  key[x]  key[rightSubtree(x)] ● Example: F BH KDA

David Luebke 5 7/2/2015 Inorder Tree Walk ● What does the following code do? TreeWalk(x) 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 7/2/2015 Inorder Tree Walk ● Example: ● How long will a tree walk take? ● Prove that inorder walk prints in monotonically increasing order F BH KDA

David Luebke 7 7/2/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 7/2/2015 BST Search: Example ● Search for D and C: F BH KDA

David Luebke 9 7/2/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 10 7/2/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 11 7/2/2015 BST Insert: Example ● Example: Insert C F BH KDA C

David Luebke 12 7/2/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 ■ We’ll keep all analysis in terms of h for now ■ Later we’ll see how to maintain h = O(lg n)

David Luebke 13 7/2/2015 Sorting With Binary Search Trees ● Informal code for sorting array A of length n: BSTSort(A) for i=1 to n TreeInsert(A[i]); InorderTreeWalk(root); ● Argue that this is  (n lg n) ● What will be the running time in the ■ Worst case? ■ Average case? (hint: remind you of anything?)

David Luebke 14 7/2/2015 Sorting With BSTs ● Average case analysis ■ It’s a form of quicksort! for i=1 to n TreeInsert(A[i]); InorderTreeWalk(root);

David Luebke 15 7/2/2015 Sorting with BSTs ● Same partitions are done as with quicksort, but in a different order ■ In previous example ○ Everything was compared to 3 once ○ Then those items < 3 were compared to 1 once ○ Etc. ■ Same comparisons as quicksort, different order! ○ Example: consider inserting 5

David Luebke 16 7/2/2015 Sorting with BSTs ● Since run time is proportional to the number of comparisons, same time as quicksort: O(n lg n) ● Which do you think is better, quicksort or BSTsort? Why?

David Luebke 17 7/2/2015 Sorting with BSTs ● Since run time is proportional to the number of comparisons, same time as quicksort: O(n lg n) ● Which do you think is better, quicksort or BSTSort? Why? ● A: quicksort ■ Better constants ■ Sorts in place ■ Doesn’t need to build data structure

David Luebke 18 7/2/2015 More BST Operations ● BSTs are good for more than sorting. For example, can implement a priority queue ● What operations must a priority queue have? ■ Insert ■ Minimum ■ Extract-Min

David Luebke 19 7/2/2015 BST Operations: Minimum ● How can we implement a Minimum() query? ● What is the running time?

David Luebke 20 7/2/2015 BST Operations: Successor ● For deletion, we will need a Successor() operation ● Draw Fig 13.2 ● What is the successor of node 3? Node 15? Node 13? ● What are the general rules for finding the successor of node x? (hint: two cases)

David Luebke 21 7/2/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 22 7/2/2015 BST Operations: Delete ● Deletion is a bit tricky ● 3 cases: ■ x has no children: ○ Remove x ■ x has one child: ○ Splice out x ■ x has two children: ○ Swap x with successor ○ Perform case 1 or 2 to delete it F BH KDA C Example: delete K or H or B

David Luebke 23 7/2/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 24 7/2/2015 The End ● Up next: guaranteeing a O(lg n) height tree