Binary Search Trees. Motivation

Similar documents
Binary Search Trees. Lecture 29 Section Robb T. Koether. Hampden-Sydney College. Fri, Apr 8, 2016

Heaps and Priority Queues

ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October

Advanced Implementations of Tables: Balanced Search Trees and Hashing

Fundamental Algorithms

INF2220: algorithms and data structures Series 1

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Data Structures and Algorithms " Search Trees!!

CS 240 Data Structures and Data Management. Module 4: Dictionaries

Outline. Computer Science 331. Cost of Binary Search Tree Operations. Bounds on Height: Worst- and Average-Case

CS Data Structures and Algorithm Analysis

AVL Trees. Manolis Koubarakis. Data Structures and Programming Techniques

Algorithms Theory. 08 Fibonacci Heaps

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :52 AM

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

Search Trees. EECS 2011 Prof. J. Elder Last Updated: 24 March 2015

HW #4. (mostly by) Salim Sarımurat. 1) Insert 6 2) Insert 8 3) Insert 30. 4) Insert S a.

Each internal node v with d(v) children stores d 1 keys. k i 1 < key in i-th sub-tree k i, where we use k 0 = and k d =.

Lecture 14: Nov. 11 & 13

Assignment 5: Solutions

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

CS 151. Red Black Trees & Structural Induction. Thursday, November 1, 12

AVL Trees. Properties Insertion. October 17, 2017 Cinda Heeren / Geoffrey Tien 1

Priority queues implemented via heaps

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

Ordered Dictionary & Binary Search Tree

Binary Search Trees. Dictionary Operations: Additional operations: find(key) insert(key, value) erase(key)

CS361 Homework #3 Solutions

CS60007 Algorithm Design and Analysis 2018 Assignment 1

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

CMSC 132, Object-Oriented Programming II Summer Lecture 12

CMSC 132, Object-Oriented Programming II Summer Lecture 6:

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

Outline Resource Introduction Usage Testing Exercises. Linked Lists COMP SCI / SFWR ENG 2S03. Department of Computing and Software McMaster University

searching algorithms

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

Dictionary: an abstract data type

Dictionary: an abstract data type

Inge Li Gørtz. Thank you to Kevin Wayne for inspiration to slides

Weight-balanced Binary Search Trees

Fibonacci (Min-)Heap. (I draw dashed lines in place of of circular lists.) 1 / 17

Data Structures and Algorithms Winter Semester

Weight-balanced Binary Search Trees

5 Spatial Access Methods

Comp487/587 - Boolean Formulas

Binary Decision Diagrams. Graphs. Boolean Functions

Problem: Data base too big to fit memory Disk reads are slow. Example: 1,000,000 records on disk Binary search might take 20 disk reads

Fundamental Algorithms

B- TREE. Michael Tsai 2017/06/06

Wolf-Tilo Balke Silviu Homoceanu Institut für Informationssysteme Technische Universität Braunschweig

Optimal Color Range Reporting in One Dimension

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

Solution suggestions for examination of Logic, Algorithms and Data Structures,

5 Spatial Access Methods

Slides for CIS 675. Huffman Encoding, 1. Huffman Encoding, 2. Huffman Encoding, 3. Encoding 1. DPV Chapter 5, Part 2. Encoding 2

Multimedia Databases 1/29/ Indexes for Multimedia Data Indexes for Multimedia Data Indexes for Multimedia Data

7.3 AVL-Trees. Definition 15. Lemma 16. AVL-trees are binary search trees that fulfill the following balance condition.

CS213d Data Structures and Algorithms

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Binary Decision Diagrams

Data selection. Lower complexity bound for sorting

The null-pointers in a binary search tree are replaced by pointers to special null-vertices, that do not carry any object-data

Computing Connected Components Given a graph G = (V; E) compute the connected components of G. Connected-Components(G) 1 for each vertex v 2 V [G] 2 d

Elementary Sorts 1 / 18

Data Structures 1 NTIN066

Functional Data Structures

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

More Dynamic Programming

More Dynamic Programming

Height, Size Performance of Complete and Nearly Complete Binary Search Trees in Dictionary Applications

Models of Computation. by Costas Busch, LSU

Lecture 5: Splay Trees

Mathematical Induction. Rosen Chapter 4.1,4.2 (6 th edition) Rosen Ch. 5.1, 5.2 (7 th edition)

Advanced Data Structures

Chapter 5 Arrays and Strings 5.1 Arrays as abstract data types 5.2 Contiguous representations of arrays 5.3 Sparse arrays 5.4 Representations of

Algorithms. Algorithms 2.2 MERGESORT. mergesort bottom-up mergesort sorting complexity divide-and-conquer ROBERT SEDGEWICK KEVIN WAYNE

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

Advanced Data Structures

Amortized analysis. Amortized analysis

Nearest Neighbor Search with Keywords

Even More on Dynamic Programming

Analysis of Approximate Quickselect and Related Problems

arxiv: v1 [math.co] 22 Jan 2013

Representing Multidimensional Trees. David Brown, Colin Kern, Alex Lemann, Greg Sandstrom Earlham College

Lecture 5: Jun. 10, 2015

Pattern Popularity in 132-Avoiding Permutations

2-INF-237 Vybrané partie z dátových štruktúr 2-INF-237 Selected Topics in Data Structures

CPSC 320 Sample Final Examination December 2013

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

Lecture 5: Sep. 23 &25

Übung Informatik I - Programmierung - Blatt 7

Data Structures and and Algorithm Xiaoqing Zheng

Lecture 4: Stacks and Queues

Insertion Sort. We take the first element: 34 is sorted

25. Minimum Spanning Trees

25. Minimum Spanning Trees

Data Structures and Algorithms

Transcription:

Binary Search Trees Motivation Searching for a particular record in an unordered list takes O(n), too slow for large lists (databases) If the list is ordered, can use an array implementation and use binary search; however, insertion is still O(n). Is there a solution that allows efficient search and insertion? Data Structures 1 Binary Search Trees

Binary Search Trees An efficient data structure that can perform insertions, deletions and searching for arbitrary elements in logarithmic time. Binary Search Tree Property A binary search tree can be empty. Each node of the tree has a unique key. All keys in the left subtree of node with value K have values less than K All keys in the right subtree of node with value K have values greater than or equal to K Data Structures 2 Binary Search Trees

Binary Search Tree Examples 28 17 45 11 20 30 60 3 15 Data Structures 3 Binary Search Trees

Binary Search Tree Examples Binary search trees can be balanced or unbalanced. Depends on the order of the values inserted into the tree. Data Structures 4 Binary Search Trees

Binary Search Tree Operations Find an Element Insert an Element Delete an Element Other Operations: Copy, Equality, Size of Binary Search Trees. Data Structures 5 Binary Search Trees

Implementation: Binary Search Tree import java. lang. Comparable ; / / Binary Search Tree implementation class BST<Key extends Comparable<? super Key>, E> implements D i c t i o n a r y <Key, E> { private BSTNode<Key, E> r o o t ; / / Root of the BST i n t nodecount ; / / Number of nodes i n the BST BST ( ) { r o o t = null ; nodecount = 0; public void c l e a r ( ) { r o o t = null ; nodecount = 0; / / I n s e r t a record i n t o the t r e e public void i n s e r t ( Key k, E e ) { r o o t = i n s e r t h e l p ( root, k, e ) ; nodecount ++; / / Remove a record from the t r e e. public E remove ( Key k ) { E temp = f i n d h e l p ( root, k ) ; / / F i r s t f i n d i t i f ( temp!= null ) { r o o t = removehelp ( root, k ) ; / / Now remove i t nodecount ; return temp ; Data Structures 6 Binary Search Trees

Binary Search Tree Property boolean checkbst (BSTNode<Integer, Integer> root, I n t e g e r low, I n t e g e r high ) { i f ( r o o t == null ) return true ; / / Empty subtree I n t e g e r rootkey = r o o t. key ( ) ; i f ( ( rootkey < low ) ( rootkey > high ) ) return false ; / / Out of range i f (! checkbst ( r o o t. l e f t ( ), low, rootkey ) ) return false ; / / L e f t side f a i l e d return checkbst ( r o o t. r i g h t ( ), rootkey, high ) ; Data Structures 7 Binary Search Trees

Binary Search Tree Operations: Insert Inserting 14.. 28 Inserting 14 28 17 45 17 45 11 20 30 60 11 20 30 60 3 15 3 15 14 Data Structures 8 Binary Search Trees

Binary Search Tree Operations: Insert Best and convenient to do this recursively. public void i n s e r t ( Key k, E e ) { r o o t = i n s e r t h e l p ( root, k, e ) ; nodecount ++; private BSTNode<Key, E> i n s e r t h e l p (BSTNode<Key, E> r t, Key k, E e ) { i f ( r t == null ) return new BSTNode<Key, E>(k, e ) ; i f ( r t. key ( ). compareto ( k ) > 0) r t. s e t L e f t ( i n s e r t h e l p ( r t. l e f t ( ), k, e ) ) ; else r t. s e t R i g h t ( i n s e r t h e l p ( r t. r i g h t ( ), k, e ) ) ; return r t ; Data Structures 9 Binary Search Trees

Binary Search Tree Operations: Find Find 15 Find 30 28 17 45 11 20 30 60 3 15 Data Structures 10 Binary Search Trees

Binary Search Tree Operations: Find public E f i n d ( Key k ) { return f i n d h e l p ( root, k ) ; private E f i n d h e l p (BSTNode<Key, E> r t, Key k ) { i f ( r t == null ) return null ; i f ( r t. key ( ). compareto ( k ) > 0) return f i n d h e l p ( r t. l e f t ( ), k ) ; else i f ( r t. key ( ). compareto ( k ) == 0) return r t. element ( ) ; else return f i n d h e l p ( r t. r i g h t ( ), k ) ; Data Structures 11 Binary Search Trees

Binary Search Tree Operations: Removing a Node More complicated than find or insert operations. Need to consider cases of empty tree, nodes with 1 child, or nodes with 2 children. Delete 28 Delete 45 28 30 28 45 17 45 17 45 11 20 30 60 11 20 60 3 15 3 15 Data Structures 12 Binary Search Trees

Removing an Element If element is a leaf node, adjust its parent pointer to become leaf node. If element s left subtree is NULL, make its parent point to its right subtree. If element s right subtree is NULL, make its parent point to its left subtree. Element with 2 non-empty subtrees: more complicated. Replace node with the largest node in left subtree, or, Replace node with the smallest node in right subtree Data Structures 13 Binary Search Trees

Binary Search Tree Operations Removing the Minimum Key private BSTNode<Key, E> deletemin (BSTNode<Key, E> r t ) { i f ( r t. l e f t ( ) == null ) return r t. r i g h t ( ) ; else { r t. s e t L e f t ( deletemin ( r t. l e f t ( ) ) ) ; return r t ; Data Structures 14 Binary Search Trees

Binary Search Tree Operations Getting the Minimum Key private BSTNode<Key, E> getmin (BSTNode<Key, E> r t ) { i f ( r t. l e f t ( ) == null ) return r t ; else return getmin ( r t. l e f t ( ) ) ; Data Structures 15 Binary Search Trees

Binary Search Tree Operations: Removing a Node / / Remove a record from the t r e e. public E remove ( Key k ) { E temp = f i n d h e l p ( root, k ) ; / / F i r s t f i n d i t i f ( temp!= null ) { r o o t = removehelp ( root, k ) ; / / Now remove i t nodecount ; return temp ; Data Structures 16 Binary Search Trees

Remove: removehelp() / / Remove a node with key value k private BSTNode<Key, E> removehelp (BSTNode<Key, E> r t, Key k ) { i f ( r t == null ) return null ; i f ( r t. key ( ). compareto ( k ) > 0) r t. s e t L e f t ( removehelp ( r t. l e f t ( ), k ) ) ; else i f ( r t. key ( ). compareto ( k ) < 0) r t. s e t R ight ( removehelp ( r t. r i g h t ( ), k ) ) ; else { / / Found i t i f ( r t. l e f t ( ) == null ) return r t. r i g h t ( ) ; else i f ( r t. r i g h t ( ) == null ) return r t. l e f t ( ) ; else { / / Two c h i l d r e n BSTNode<Key, E> temp = getmin ( r t. r i g h t ( ) ) ; r t. setelement ( temp. element ( ) ) ; r t. setkey ( temp. key ( ) ) ; r t. s e t R ight ( deletemin ( r t. r i g h t ( ) ) ) ; return r t ; Data Structures 17 Binary Search Trees

Cost of Binary Search Tree Operations Find, Insertion, Deletion costs are a function of the depth of the tree. Traversing a tree: what is the cost? What is the depth of the tree? Average Case, Worst case? Assumption Need to keep the tree balanced. Why? Data Structures 18 Binary Search Trees