Lecture 23: Binary Search Trees

Similar documents
CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Algorithms. AVL Tree

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

CSC148 Week 7. Larry Zhang

CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees. Linda Shapiro Spring 2016

COMP 250. Lecture 22. binary search trees. Oct. 30, 2017

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

CS102 Binary Search Trees

Search Trees: BSTs and B-Trees

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

CS350: Data Structures Red-Black Trees

Data Structures and Algorithms for Engineers

Self-Balancing Search Trees. Chapter 11

Exam Monday. Lecture 19: Binary Search & Splay Trees. Assignment BST

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CS 350 : Data Structures Binary Search Trees

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

CSC 421: Algorithm Design & Analysis. Spring 2015

Fall, 2015 Prof. Jungkeun Park

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

CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. Kevin Quinn Fall 2015

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

4/18/ Binary Search Trees (BSTs) 17.1 Adding an Element to a BST Removing an Element from a BST. 17.

Binary Search Trees. Analysis of Algorithms

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

CS350: Data Structures Binary Search Trees

Lecture 6: Analysis of Algorithms (CS )

A Binary Search Tree Implementation

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

Binary Trees Fall 2018 Margaret Reid-Miller

CS 261 Data Structures. AVL Trees

CISC 235: Topic 4. Balanced Binary Search Trees

CS 315 Data Structures mid-term 2

A grossly unbalanced tree, with some long paths. Next to add: When does it occur? Why is it undesirable?

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

CMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011

Binary Tree Applications

Outline. Computer Science 331. Insertion: An Example. A Recursive Insertion Algorithm. Binary Search Trees Insertion and Deletion.

CSE373 Fall 2013, Midterm Examination October 18, 2013

Dynamic Access Binary Search Trees

8. Binary Search Tree

Search Trees. The term refers to a family of implementations, that may have different properties. We will discuss:

CSC 321: Data Structures. Fall 2016

Advanced Set Representation Methods

Binary Search Trees 1

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

CS 361, Lecture 21. Outline. Things you can do. Things I will do. Evaluation Results

CS350: Data Structures Tree Traversal

CmpSci 187: Programming with Data Structures Spring 2015

Binary Trees and Huffman Encoding Binary Search Trees

Friday Four Square! 4:15PM, Outside Gates

Lecture: Analysis of Algorithms (CS )

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

TREES Lecture 12 CS2110 Spring 2018

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

CSCI Trees. Mark Redekopp David Kempe

Part 2: Balanced Trees

CSCI-1200 Data Structures Fall 2018 Lecture 19 Trees, Part III

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

COMP171. AVL-Trees (Part 1)

Search Trees. Undirected graph Directed graph Tree Binary search tree

Trees. Prof. Dr. Debora Weber-Wulff

TREES Lecture 12 CS2110 Spring 2019

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

Binary Trees, Binary Search Trees

Balanced Search Trees

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Binary Search Trees. BinaryTree<E> Class (cont.) Section /27/2017

CS24 Week 8 Lecture 1

Balanced search trees

Section 1: True / False (1 point each, 15 pts total)

TREES. Trees - Introduction

TREES Lecture 12 CS2110 Fall 2016

DATA STRUCTURES AND ALGORITHMS

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

Balanced Search Trees. CS 3110 Fall 2010

Where we are. CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. The Dictionary (a.k.a. Map) ADT

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Disk Accesses. CS 361, Lecture 25. B-Tree Properties. Outline

Dynamic Access Binary Search Trees

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE

void insert( Type const & ) void push_front( Type const & )

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

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

If you took your exam home last time, I will still regrade it if you want.

Module 4: Dictionaries and Balanced Search Trees

BST Deletion. First, we need to find the value which is easy because we can just use the method we developed for BST_Search.

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

CS 380 ALGORITHM DESIGN AND ANALYSIS

Programming Abstractions

The Problem with Linked Lists. Topic 18. Attendance Question 1. Binary Search Trees. -Monty Python and The Holy Grail

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Spring 2018 Profs Bill & Jon

Transcription:

Lecture 23: Binary Search Trees CS 62 Fall 2017 Kim Bruce & Alexandra Papoutsaki 1

BST A binary tree is a binary search tree iff it is empty or if the value of every node is both greater than or equal to every value in its left subtree and less than or equal to every value in its right subtree. 2

Interface public class BinarySearchTree> { protected BinaryTree root; public void add(e value); public void contains(e value); public void remove(e value); protected BinaryTree locate(binarytree root, E val); protected BinaryTree predecessor(binarytree node); protected BinaryTree removetop(binarytree topnode); 3

Locating a Value Useful for add, contains, and remove Returns a pointer to the node with a given value or to a node where that exact value could be added Recursive implementation (could be iterative) 4

Locating a Value Check current value vs. the search value If equal, return this node If smaller, locate within left subtree Else within right subtree If the appropriate subtree is empty, return this node 5

// @pre root and value are non-null // @post returned: 1 - existing tree node with the desired value, or // 2 - the node to which value should be added protected BinaryTree<E> locate(binarytree<e> root, E value) { E rootvalue = root.value(); BinaryTree<E> child; if (rootvalue.equals(value)) return root; // found at root // look left if less-than, right if greater-than if (ordering.compare(rootvalue,value) < 0) { child = root.right(); else { child = root.left(); // no child there: not in tree, return this node, // else keep searching if (child.isempty()) { return root; else { return locate(child, value); 6

Using locate to add a node Case One: Locate returns pointer to where node should be added If value less than returned node, create new left child If value greater than returned node, create new right child 7

Using locate to add a node Case Two: Locate returns pointer to node with same value Duplicates go in left subtree (could have chosen right) Where in the left subtree? 8

Using locate to add a node Case Two: Locate returns pointer to node with same value Duplicates go in left subtree (could have chosen right) Should be the rightmost descendant 9

Predecessor Finds the rightmost descendent in left subtree The next-smallest value in the tree What s the big-o runtime? 10

protected BinaryTree<E> predecessor(binarytree<e> root) { BinaryTree<E> result = root.left(); while (!result.right().isempty()) { result = result.right(); return result; protected BinaryTree<E> successor(binarytree<e> root) { BinaryTree<E> result = root.right(); while (!result.left().isempty()) { result = result.left(); return result; 11

public void add(e value) { BinaryTree<E> newnode = new BinaryTree<E>(value,EMPTY,EMPTY); // add value to binary search tree // if there's no root, create value at root if (root.isempty()) { root = newnode; else { BinaryTree<E> insertlocation = locate(root,value); E nodevalue = insertlocation.value(); // The location returned is the successor or predecessor // of the to-be-inserted value if (ordering.compare(nodevalue,value) < 0) { insertlocation.setright(newnode); else { if (!insertlocation.left().isempty()) { // if value is in tree, we insert just before predecessor(insertlocation).setright(newnode); else { insertlocation.setleft(newnode); count++; 12

Removing nodes Easy cases Node is a leaf Node has only one child x x A A B B 13

Removing nodes General Case Left Child has a right subtree: x 3 1 1 2 2 3 predecessor( x) 14

Removing nodes Calling remove(e val) removes node with value val Predecessor of root becomes new root Predecessor is in left subtree Predecessor has no right subtree Complexity is O(h) where h is height of tree Worst-case O(h) to locate Worst-case O(h) to find predecessor 15

Complexity locate, add, contains, remove are all O(h) Can we guarantee that h is O(log n)? Only if tree stays balanced!! Binary search trees that stay balanced AVL trees Red-black trees We ll do splay tree, which doesn t guarantee balance but guarantees good average behavior easier to understand than alternatives better than others if likely to go back to recent nodes 16