Binary Search Trees (10.1) CSE 2011 Winter 2011 2 August 2018.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Binary Search Trees
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Data Structures Lecture 11 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
© 2004 Goodrich, Tamassia Binary Search Trees   
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
Binary Search Trees1 Part-F1 Binary Search Trees   
Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Lecture12: Tree II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
CSC401 – Analysis of Algorithms Lecture Notes 6 Dictionaries and Search Trees Objectives: Introduce dictionaries and its diverse implementations Introduce.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
Search Trees Chapter   . Outline  Binary Search Trees  AVL Trees  Splay Trees.
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
1 Searching the dictionary ADT binary search binary search trees.
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
1 Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
Algorithms Design Fall 2016 Week 6 Hash Collusion Algorithms and Binary Search Trees.
Binary Search Trees Chapter 7 Objectives
Part-D1 Binary Search Trees
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
COMP9024: Data Structures and Algorithms
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Search Trees.
Binary Search Trees < > =
Binary Search Trees Rem Collier Room A1.02
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Binary Search Trees < > = Binary Search Trees
Binary Trees, Binary Search Trees
Chapter 2: Basic Data Structures
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Priority Queues and Heaps
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Copyright © Aiman Hanna All rights reserved
Part-D1 Priority Queues
Binary Search Trees < > =
Binary Search Trees < > = Binary Search Trees
Dictionaries < > = /3/2018 8:58 AM Dictionaries
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Dictionaries < > = /9/2018 3:06 AM Dictionaries
(2,4) Trees (2,4) Trees (2,4) Trees.
Binary Search Trees < > =
Binary Search Trees.
Dictionaries < > = /17/2019 4:20 PM Dictionaries
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
Binary Search Trees < > =
Binary Trees, Binary Search Trees
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
(2,4) Trees (2,4) Trees (2,4) Trees.
Binary Search Trees < > =
Binary Search Trees < > = Binary Search Trees
1 Lecture 13 CS2013.
Binary Trees, Binary Search Trees
Binary Search Trees < > = Dictionaries
Dictionaries 二○一九年九月二十四日 ADT for Map:
Presentation transcript:

Binary Search Trees (10.1) CSE 2011 Winter 2011 2 August 2018

Dictionary ADT (9.5.1) Dictionary ADT methods: get(k): if the dictionary has an item with key k, returns its element, else, returns NULL getAll(k): returns an iterator of entries with key k put(k, o): inserts item (k, o) into the dictionary remove(k): if the dictionary has an item with key k, removes it from the dictionary and returns its element, else returns NULL removeAll(k): remove all entries with key k; return an iterator of these entries. size(), isEmpty() The dictionary ADT models a searchable collection of key-element items The main operations of a dictionary are searching, inserting, and deleting items Multiple items with the same key are allowed Applications: address book credit card authorization SIN database student database

Binary Search Trees An inorder traversal of a binary search trees visits the keys in increasing order The left-most child has the smallest key The right-most child has the largest key A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying the following property: Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u)  key(v)  key(w) External nodes (dummies) do not store items (non-empty proper binary trees, for coding simplicity) 6 9 2 4 1 8

Example of BST A binary search tree Not a binary search tree

More Examples of BST The same set of keys may have different BSTs. Average depth of a node is O(logN). Maximum depth of a node is O(N). Where is the smallest key? largest key?

Inorder Traversal of BST Inorder traversal of BST prints out all the keys in sorted order. Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20

Searching BST If we are searching for 15, then we are done. If we are searching for a key < 15, then we should search in the left subtree. If we are searching for a key > 15, then we should search in the right subtree.

Search Algorithm To search for a key k, we trace a downward path starting at the root The next node visited depends on the outcome of the comparison of k with the key of the current node If we reach a leaf, the key is not found and we return v (where the key should be if it will be inserted) Example: TreeSearch(4, T.root()) Running time: ? Algorithm TreeSearch( k, v ) if T.isExternal (v) return (v); // or return NO_SUCH_KEY if k < key(v) return TreeSearch( k, T.left(v) ) else if k = key(v) return v else { k > key(v) } return TreeSearch( k, T.right(v) ) < 6 2 > 9 1 4 = 8

Insertion (distinct keys) To perform operation insertItem(k, o), we search for key k Assume k is not already in the tree, and let w be the leaf reached by the search We insert k at node w and expand w into an internal node using insertAtExternal(w, (k,e)) Example: insertAtExternal(w, (5,e)) with e having key 5 Running time: ? < 6 2 9 > 1 4 8 > w 6 2 9 1 4 8 w 5

Insertion Algorithm (distinct keys) Algorithm TreeInsert( k, e, v ) { w = TreeSearch( k, v ); T.insertAtExternal( w, k, e ); return w; } Algorithm insertAtExternal( w, k, e ) { if ( T.isExternal( w ) { make w an internal node, store k and e into w; add two dummy nodes (leaves) as w’s children; } else { error condition }; First call: TreeInsert( 5, e, T.root( ) )

Insertion (duplicate keys) Insertion with duplicate keys Example: insert(2) Call TreeSearch(k, leftChild(w)) to find the leaf node for insertion Can insert to either the left subtree or the right subtree (call TreeSearch(k, rightChild(w)) Running time: ? Homework: implement method getAll(k)

Insertion Algorithm (duplicate keys) Algorithm TreeInsert( k, e, v ) { w = TreeSearch( k, v ); if k == key(w) // key exists return TreeInsert( k, e, T.left( w ) ); // *** T.insertAtExternal( w, k, e ); return w; } First call: TreeInsert( 2, e, T.root() ) ***Note: if inserting the duplicate key into the left subtree, keep searching the left subtree after a key has been found.

Deletion To perform operation removeElement(k), we search for key k Assume key k is in the tree, and let v be the node storing k Two cases: Case 1: v has no children Case 2: v has at least one child

Deletion: Case 1 Case 1: v has no children 6 Case 1: v has no children We simply remove v and its 2 dummy leaves. Replace v by a dummy node. Example: remove 5 2 9 1 4 8 5 6 2 9 1 8 15

Deletion: Case 2 Case 2: v has at least one child (and possibly grandchildren, great-grandchildren, etc.) Identify v’s “heir”: either one of the following two nodes: the node x that immediately precedes v in an inorder traversal (where is this node?) the node x that immediately follows v in an inorder traversal (where is this node?) Two steps: copy content of x into node v (heir “inherits” node v); remove x from the tree (as in case 1).

Deletion: Case 2 Example 6 Example: remove 4 Heir = ? < 2 9 > v 1 4 8 x 5 6 2 9 1 5 8 17

Deletion: Case 2 Example (2) Example: remove 3 Heir = ? Running time of deletion algorithm:? Homework: implement removeAll(k) 1 v 3 2 8 6 9 x 5 1 v 5 2 8 6 9

Performance Consider a dictionary with n items implemented by means of a binary search tree of height h the space used is O(n) methods get(k) , put() and remove(k) take O(h) time The height h is O(n) in the worst case and O(log n) in the best case

Next time … AVL trees (10.2) BST Java code: section 10.1.3