Binary Search Trees Rem Collier Room A1.02

Slides:



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

© 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.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Lecture12: Tree II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
CSC 213 – Large Scale Programming Lecture 17: Binary Search Trees.
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.
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 Trees (BST)
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)
CHAPTER 10.1 BINARY SEARCH TREES    1 ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS.
© 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 < > =
AA 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.
Trees Chapter 11 (continued)
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.
Search Trees.
Binary Search Trees < > =
Trees Chapter 11 (continued)
BST Trees
Binary Search Tree (BST)
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Binary Search Trees < > = Binary Search Trees
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
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
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
Binary Search Trees < > =
(2,4) Trees (2,4) Trees (2,4) Trees.
Binary Search Trees < > =
Binary Search Trees < > = Binary Search 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:
Presentation transcript:

Binary Search Trees Rem Collier Room A1.02 School of Computer Science and Informatics University College Dublin, Ireland

Binary Search Tree A Binary Search Tree is a Binary Tree that satisfies the following properties: Each internal node holds a (unique) value. A total-order relation (~) is defined on those values. Reflexive: k ~ k Antisymmetric: if k1 ~ k2 and k2 ~ k1 then k1 = k2 Transitive: if k1 ~ k2 and k2 ~ k3 then k1 ~ k3 All the values (ki) in the left sub-tree of a node with value k satisfy the relation ki ~ k. All the values (kj) in the right sub-tree of a node with value k satisfy the relation k ~ kj. Useful Feature: An in-order traversal of a binary search trees visits the values in the order specified by the total-order relation.

Binary Search Tree Example Consider a Binary Search Tree for integer values, using the less than (≤) total order relation. The state of a Binary Search Tree depends on the order in which items are added. 6 9 2 4 1 8

Uses of BSTs Binary Search Trees are an implementation strategy for other ADTs. Support three basic operations: insert(e): Add a new node containing e, if one does not already exist find(e): Find the node containing e remove(e): Remove the node containing e Can easily be adapted to store entries and support retrieval by key. Do you know of any other ADTs that have similar operations?

Search To search for a value 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 value of the current node If we reach a leaf, the key is not found and we return null Example: find(4) Algorithm find(k, v) if T.isExternal(v) return null if k < v.element() return find(k, T.leftChild(v)) else if k = v.element() return v else return find(k, T.rightChild(v)) < 6 2 > 9 1 4 = 8

Insertion To perform operation insert(k), we search for value k Assume k is not already in the tree, and let 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) < 6 2 9 > 1 4 8 > w 6 2 9 1 4 8 w 5

Deletion To perform operation remove(k), we search for k. Assume k is in the tree, and let let v be the node storing k If node v has one child, then we can remove it using the existing remove(v) operation. Example: remove(4) 6 < 2 9 > v 1 4 8 w 5 6 2 9 1 5 8

Deletion (cont.) What about the case where the k to be removed is stored at a node v that has 2 children? we find the internal node w that follows v in an inorder traversal we copy w.element() into node v we remove node w and its left child z (which must be a leaf) Example: remove(3) 1 v 3 2 8 6 9 w 5 z 1 v 5 2 8 6 9

Problems Create the following BST: insert(15), insert(25), insert(5), insert(9), remove(15) insert(37), insert(26), insert(35), insert(28), remove(26) insert(40), insert(1), insert(15), insert(26), insert(6), remove(5) insert(H), insert(A), insert(P), insert(Y), insert(O), remove(H) insert(B), insert(C), insert(F), insert(E), remove(A) insert(H), insert(A)

Performance Consider a binary tree containing n items that is of height h the space used is O(n) methods find(), 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! The best case arises when the tree is balanced!