Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Similar documents
Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

CS 380 ALGORITHM DESIGN AND ANALYSIS

Binary search trees (BST) Binary search trees (BST)

ץע A. B C D E F G H E, B ( -.) - F I J K ) ( A :. : ע.)..., H, G E (. י : י.)... C,A,, F B ( 2

Search Trees. Undirected graph Directed graph Tree Binary search tree

Algorithms, Spring 2014, CSE, OSU Lecture 4: Binary search trees

Binary Search Trees. July 13th, Computer Information Systems. c 2009 Vaidė Narváez

Binary Search Tree. Sorting and Searching in a Dynamic Set. S.V. N. (vishy) Vishwanathan. University of California, Santa Cruz

Different binary search trees can represent the same set of values (Fig.2). Vladimir Shelomovskii, Unitech, Papua New Guinea. Binary search tree.

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Binary Search Trees. Antonio Carzaniga. Faculty of Informatics University of Lugano. October 21, Antonio Carzaniga 1

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

12 Binary Search Tree

Balanced search trees

ECE250: Algorithms and Data Structures Binary Search Trees (Part A)

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

Binary Search Trees. What is a Binary Search Tree?

Algorithms, Spring 2014, CSE, OSU Lecture 5: Red-black trees

Binary Trees. Recursive definition. Is this a binary tree?

Announcements. Problem Set 2 is out today! Due Tuesday (Oct 13) More challenging so start early!

Binary search trees :

Week 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.

Week 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

CMSC351 - Fall 2014, Homework #2

SFU CMPT Lecture: Week 9

Lecture 6: Analysis of Algorithms (CS )

Binary Search Trees. Analysis of Algorithms

Lecture 7. Binary Search Trees / AVL Trees

Algorithms. 演算法 Data Structures (focus on Trees)

Binary Search Trees. Motivation. Binary search tree. Tirgul 7

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

Data Structures. Dynamic Sets

Binary Search Trees. 1. Inorder tree walk visit the left subtree, the root, and right subtree.

13.4 Deletion in red-black trees

CSE 502 Class 16. Jeremy Buhler Steve Cole. March A while back, we introduced the idea of collections to put hash tables in context.

We assume uniform hashing (UH):

Lecture: Analysis of Algorithms (CS )

Binary Search Trees. Nearest Neighbor Binary Search Trees Insertion Predecessor and Successor Deletion Algorithms on Trees.

Binary Search Trees. Binary Search Trees. Nearest Neighbor. Nearest Neighbor

Trees. (Trees) Data Structures and Programming Spring / 28

March 20/2003 Jayakanth Srinivasan,

CSE2331/5331. Topic 7: Balanced search trees. Rotate operation Red-black tree Augmenting data struct. CSE 2331/5331

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

13.4 Deletion in red-black trees

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Binary Trees, Binary Search Trees

Chapter 20: Binary Trees

Binary Search Trees Treesort

A set of nodes (or vertices) with a single starting point

Chapter 13. Michelle Bodnar, Andrew Lohr. April 12, 2016

Algorithms. AVL Tree

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

TREES. Trees - Introduction

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

3 Trees: traversal and analysis of standard search trees. Summer Term 2010

COMP Analysis of Algorithms & Data Structures

Binary Trees

Binary Search Trees, etc.

Binary Heaps in Dynamic Arrays

Binary Search Trees. Antonio Carzaniga. Faculty of Informatics University of Lugano. October 22, Antonio Carzaniga 1

ICS 311, Fall 2017, Problem Set 04, Topics 7 & 8


Properties of red-black trees

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Lower Bound on Comparison-based Sorting

Trees. Truong Tuan Anh CSE-HCMUT

COMP Analysis of Algorithms & Data Structures

Multi-Way Search Trees

CH 8. HEAPS AND PRIORITY QUEUES

CH. 8 PRIORITY QUEUES AND HEAPS

INF2220: algorithms and data structures Series 1

Binary Trees. Examples:

Search Trees: BSTs and B-Trees

CMSC 441: Algorithms. Hillol Kargupta, Professor.

Friday Four Square! 4:15PM, Outside Gates

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Trees. CSE 373 Data Structures

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Associate Professor Dr. Raed Ibraheem Hamed

Stores a collection of elements each with an associated key value

Binary Search Trees. See Section 11.1 of the text.

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Augmenting Data Structures

Multi-Way Search Trees

Search Trees - 1 Venkatanatha Sarma Y

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

INF2220: algorithms and data structures Series 1

CMSC 341. Binary Search Trees CMSC 341 BST

CMSC th Lecture: Graph Theory: Trees.

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

Transcription:

COMP3600/6466 Algorithms 2018 Lecture 12 1 Binary search trees Reading: Cormen et al, Sections 12.1 to 12.3 Binary search trees are data structures based on binary trees that support operations on dynamic sets. Each element contains a key. There is a (total) order on the set of keys. Typical operations include: Search Insert Delete Minimum Maximum Predecessor Successor 1

COMP3600/6466 Algorithms 2018 Lecture 12 2 Binary search trees 2 The keys in a binary search tree are always stored in such a way as to satisfy the binary-search-tree property : Let x be a node in a binary search tree. If y is a node in the left subtree of x, then y.key x.key. If y is a node in the right subtree of x, then y.key x.key. In addition to a key and satellite data, each node x in a binary tree has three attributes that are pointers: parent pointer x.p left child pointer x.left right child pointer x.right 2

Binary search trees 3 Cormen et al, p.287 Two binary search trees each containing the keys 2, 5, 5, 6, 7, 8 The worst-case running time for most binary-search-tree operations is proportional to the height of the tree. So the tree on the left is preferable to the tree on the right. We want O(lg n) performance, where n is the number of nodes. COMP3600/6466 Algorithms 2018 Lecture 12 3 3

COMP3600/6466 Algorithms 2018 Lecture 12 4 Traversing binary search trees There are three natural ways to traverse a binary search tree: Inorder tree walk: visit the left subtree, then the root, then the right subtree Preorder tree walk : visit the root, then the left subtree, then the right subtree Postorder tree walk: visit the left subtree, then the right subtree, then the root. 4

COMP3600/6466 Algorithms 2018 Lecture 12 5 Traversing binary search trees 2 Cormen et al, p.288 For Preorder-Tree-Walk, the print statement comes first. For Postorder-Tree-Walk, the print statement comes last. Theorem: If x is the root of an n-node subtree, then the call Inorder-Tree-Walk(x) takes Θ(n) time. The proof is by finding a recurrence and solving it by the substitution method. (See p.288) 5

COMP3600/6466 Algorithms 2018 Lecture 12 6 Traversing binary search trees 3 Cormen et al, p.290 Inorder traversal : 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20 Preorder traversal: 15, 6, 3, 2, 4, 7, 13, 9, 18, 17, 20 Postorder traversal: 2, 4, 3, 9, 13, 7, 6, 17, 20, 18, 15 6

COMP3600/6466 Algorithms 2018 Lecture 12 7 Searching binary search trees Given a pointer to the root of the tree and a key k, Tree-Search returns a pointer to a node with key k if one exists; otherwise, it returns NIL. Cormen et al, p.290 The running time of Tree-Search is O(h), where h is the height of the tree. 7

COMP3600/6466 Algorithms 2018 Lecture 12 8 Searching binary search trees 2 Searching for the node with key 13 traverses the path having nodes with keys 15, 6, 7, and 13. 8

COMP3600/6466 Algorithms 2018 Lecture 12 9 Finding the minimum and maximum Here are the procedures for finding the nodes with the minimum and maximum keys in a binary search tree. Cormen et al, p.291 The running time of Tree-Minimum and Tree-Maximum are both O(h), where h is the height of the tree. Exercise: Use induction to prove that these procedures are correct. 9

COMP3600/6466 Algorithms 2018 Lecture 12 10 Finding the minimum and maximum Finding the minimum traverses the path having nodes with keys 15, 6, 3, and 2. Finding the maximum traverses the path having nodes with keys 15, 18, and 20. 10

COMP3600/6466 Algorithms 2018 Lecture 12 11 Finding the successor and predecessor Let s concentrate on successors. If all keys are distinct, the successor of a node x is the node with the smallest key greater than x.key. Exercise: Consider a binary tree T whose keys are distinct. Show that if the right subtree of a node x in T is empty and x has a successor y, then y is the lowest ancestor of x whose left child is also an ancestor of x. (Recall that every node is its own ancestor.) 11

COMP3600/6466 Algorithms 2018 Lecture 12 12 Finding the successor Cormen et al, p.292 The running time of Tree-Successor O(h), where h is the height of the tree. Exercise: Write the procedure Tree-Predecessor. 12

COMP3600/6466 Algorithms 2018 Lecture 12 13 Insertion and deletion The operations of insertion and deletion cause the dynamic set represented by a binary search tree to change. The data structure must be modified to reflect this change, but in such a way that the binary-search-tree property continues to hold. Inserting a node with a new key into a binary search tree is straightforward. However, deleting a node from a binary search tree is rather more complicated. In the Tree-Insert procedure on the next slide, suppose a node with a key κ is to be inserted into a binary search tree. Then the procedure has a node z as an argument, where z.key = κ, z.left = NIL, and z.right = NIL. 13

Insertion Cormen et al, p.295 Cormen et al, p.294 The running time of Tree-Insert is O(h), where h is the height 14 of the tree. COMP3600/6466 Algorithms 2018 Lecture 12 14

Deletion Cormen et al, Second edition In case (c), any external pointers to the node with key 6 become stale. 15 COMP3600/6466 Algorithms 2018 Lecture 12 15

COMP3600/6466 Algorithms 2018 Lecture 12 16 Deletion 2 The procedure for deleting a given node z from a binary search tree T takes as arguments pointers to T and z. There are four cases to consider. If z has no left child, then we replace z by its right child, which may or may not be NIL. If z has just one child, which is its left child, then we replace z by its left child. Otherwise, z has both a left and a right child. We find z s successor y, which lies in z s right subtree and has no left child. If y is z s right child, then we replace z by y, leaving y s right child alone. Otherwise, y lies within z s right subtree, but is not z s right child. In this case, we first replace y by its own right child, and then we replace z by y. 16

Deletion 3 Cormen et al, p.297 17 COMP3600/6466 Algorithms 2018 Lecture 12 17

COMP3600/6466 Algorithms 2018 Lecture 12 18 Deletion 4 For the deletion operation we use the procedure Transplant. It replaces the subtree rooted at u with the subtree rooted at v. Cormen et al, p.296 18

Deletion 5 Cormen et al, p.298 The running time of Tree-Delete is O(h), where h is the height of the tree. 19 COMP3600/6466 Algorithms 2018 Lecture 12 19

COMP3600/6466 Algorithms 2018 Lecture 12 20 Randomly built binary search trees Define a randomly built binary search tree on n keys as one that arises from inserting the keys in random order into an initially empty tree, where each of the n! permutations of the input keys is equally likely. Theorem: The average height of a randomly built binary search tree on n distinct keys is O(lg n). For the proof, see p.300-303. However, generally we cannot rely on randomness to ensure that the height is logarithmic in the number of nodes. We need to consider a class of balanced trees that have this property by their definition and the way insertion and deletion operations are carried out. 20