Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees

Slides:



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

Data Structures Lecture 11 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
© 2004 Goodrich, Tamassia Binary Search Trees   
Goodrich, Tamassia Search Trees1 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.
© 2004 Goodrich, Tamassia (2,4) Trees
Lecture12: Tree II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
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.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
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.
Part-D1 Binary Search Trees
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
COMP9024: Data Structures and Algorithms
Red-Black Trees v z Red-Black Trees Red-Black Trees
Red-Black Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Red-Black Trees 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Binary Search Trees < > =
Search Trees.
Binary Search Trees < > =
Binary Search Trees Rem Collier Room A1.02
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Binary Search Trees.
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Binary Search Trees < > = Binary Search Trees
Binary Trees, Binary Search Trees
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
(2,4) Trees (2,4) Trees 1 (2,4) Trees (2,4) Trees
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Copyright © Aiman Hanna All rights reserved
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Binary Search Trees < > =
Red-Black Trees v z /20/2018 7:59 AM Red-Black Trees
Red-Black Trees v z Red-Black Trees Red-Black Trees
Binary Search Trees < > = Binary Search Trees
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
Dictionaries < > = /3/2018 8:58 AM Dictionaries
Dictionaries < > = /9/2018 3:06 AM Dictionaries
(2,4) Trees (2,4) Trees (2,4) Trees.
Binary Search Trees < > =
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
Dictionaries < > = /17/2019 4:20 PM Dictionaries
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
Binary Search Trees < > =
(2,4) Trees /24/2019 7:30 PM (2,4) Trees (2,4) Trees
(2,4) Trees (2,4) Trees (2,4) Trees.
Binary Search Trees < > =
Binary Search Trees < > = Binary Search Trees
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
1 Lecture 13 CS2013.
Binary Search Trees < > = Dictionaries
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Red-Black Trees v z /6/ :10 PM Red-Black Trees
Dictionaries 二○一九年九月二十四日 ADT for Map:
CS210- Lecture 20 July 19, 2005 Agenda Multiway Search Trees 2-4 Trees
Presentation transcript:

Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees Dr Zeinab Eid

10.1 Binary Search Trees < > = 6 2 9 1 4 8 Dr Zeinab Eid

10.1 Binary Search Trees A binary search tree is a proper binary tree storing keys (or key-value entries) 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 do not store items An inorder traversal of a binary search trees visits the keys in increasing order 6 9 2 4 1 8 Dr Zeinab Eid Search Trees

10.1.1 Search 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 null Example: find(4): Call TreeSearch(4,root) Algorithm TreeSearch(k, v) if T.isExternal (v) then return v if k < key(v) return TreeSearch(k, T.left(v)) else if k = key(v) else { k > key(v) } return TreeSearch(k, T.right(v)) < 6 2 > 9 1 4 = 8 Dr Zeinab Eid Search Trees

10.1.2 Update Operations Insertion Removal Dr Zeinab Eid Dr Zeinab Eid Search Trees

Insertion Let us assume a proper binary tree T supports the following update operation: insertAtExternal(v,e):Insert the element e at the external node v, and expand v to be internal, having new (empty) external node children; an error occurs if v is an internal node. Dr Zeinab Eid Dr Zeinab Eid Search Trees

Insertion < 6 To perform operation insert(k, e), we search for key k (using TreeSearch) 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 Example: insert 5 2 9 > 1 4 8 > w 6 2 9 1 4 8 w 5 Dr Zeinab Eid Dr Zeinab Eid Search Trees

Removal We assume that a proper binary tree T supports the following additional update operation: removeExternal(v): Remove an external node v and its parent, replacing v’s parent with v’s sibling; an error occurs if v is not external. Dr Zeinab Eid Dr Zeinab Eid Search Trees

Removal < To perform operation remove(k), we search for key k: > 6 < To perform operation remove(k), we search for key k: Assume key k is in the tree, and let let v be the node storing k If node v has a leaf child w, we remove v and w from the tree with operation removeExternal(w), which removes w and its parent Example: remove 4 2 9 > v 1 4 8 w 5 6 2 9 1 5 8 Dr Zeinab Eid Dr Zeinab Eid Search Trees

Removal (cont.) We consider the case where the key k to be removed is stored at a node v whose children are both internal we find the internal node w that follows v in an inorder traversal we copy key(w) into node v we remove node w and its left child z (which must be a leaf) by means of operation removeExternal(z) Example: remove 3 1 v 3 2 8 6 9 w 5 z 1 v 5 2 8 6 9 Dr Zeinab Eid Dr Zeinab Eid Search Trees

Example: Delete 65: - Find In-order Successor of 65, which is 76. - Copy the key of this successor node into node to be deleted. - Remove this successor node, which must have external node as its left child. Dr Zeinab Eid Dr Zeinab Eid Search Trees

Performance Analysis of Binary-Search Tree All Search, insert and remove algorithms traverse a path from the root to a node in the BST. In all three algorithms, we spend O(1) time at each node visited. In the worst case, the number of nodes visited is proportional to the height h of T. Thus, in a BST of n-nodes, all search, insert and remove methods run in O(h) time. Dr Zeinab Eid Dr Zeinab Eid Search Trees

Performance Analysis of Binary-Search Tree A BST, T, is an efficient implementation of a dictionary of n-entries only if the height of T is small. Best Case: T has height h=log(n+1), leading to a logarithmic-time performance of all dictionary operations. Worst Case: T has height n, leading to a linear-time performance, as in case of a set of ordered keys. Dr Zeinab Eid Dr Zeinab Eid Search Trees

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 search, insert and remove take O(h) time The height h is O(n) in the worst case and O(log n) in the best case Dr Zeinab Eid Dr Zeinab Eid Search Trees

Notice That: The running time of search and update operations in a BST varies dramatically, depending on the tree’s height. On the average, a BST with n-keys, generated by a random series of insertions and removals, of n keys has an expected height of O(log n) . Nevertheless, we must not use standard BST in applications, where updates are not random. Dr Zeinab Eid Dr Zeinab Eid Search Trees