Class 28: Binary Search Trees. Binary Search Trees

Similar documents
Class 32: The Java Collections Framework

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Chapter 14 The Binary Search Tree

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

How To Create A Tree From A Tree In Runtime (For A Tree)

Algorithms and Data Structures Written Exam Proposed SOLUTION

Data Structures in the Java API

Ordered Lists and Binary Trees

Binary Search Trees (BST)

Analysis of Algorithms I: Binary Search Trees

Symbol Tables. Introduction

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Binary Heap Algorithms

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Heaps & Priority Queues in the C++ STL 2-3 Trees

Data Structures and Algorithms

Binary Trees and Huffman Encoding Binary Search Trees

Converting a Number from Decimal to Binary

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

D06 PROGRAMMING with JAVA

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Analysis of a Search Algorithm

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Binary Search Trees 3/20/14

The ADT Binary Search Tree

From Last Time: Remove (Delete) Operation

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Persistent Binary Search Trees

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Full and Complete Binary Trees

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Lecture Notes on Binary Search Trees

A Comparison of Dictionary Implementations

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Data Structures CSC212 (1) Dr Muhammad Hussain Lecture - Binary Search Tree ADT

TREE BASIC TERMINOLOGIES

CompSci-61B, Data Structures Final Exam

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Binary Heaps. CSE 373 Data Structures

PES Institute of Technology-BSC QUESTION BANK

Big Data and Scripting. Part 4: Memory Hierarchies

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

CSE 326: Data Structures B-Trees and B+ Trees

Introduction to Data Structures and Algorithms

CSE 2123 Collections: Sets and Iterators (Hash functions and Trees) Jeremy Morris

Algorithms Chapter 12 Binary Search Trees

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

schema binary search tree schema binary search trees data structures and algorithms lecture 7 AVL-trees material

Binary Search Trees CMPSC 122

Collections in Java. Arrays. Iterators. Collections (also called containers) Has special language support. Iterator (i) Collection (i) Set (i),

Data Structures and Algorithms Written Examination

Chapter 8: Binary Trees

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Data storage Tree indexes

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Data Structures Fibonacci Heaps, Amortized Analysis

Efficient representation of integer sets

Object Oriented Software Design

LINKED DATA STRUCTURES

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 221] edge. parent

Laboratory Module 6 Red-Black Trees

Binary Search Tree Intro to Algorithms Recitation 03 February 9, 2011

Fast Sequential Summation Algorithms Using Augmented Data Structures

Exam study sheet for CS2711. List of topics

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Fundamental Algorithms

10CS35: Data Structures Using C

Algorithms and Data Structures

Any two nodes which are connected by an edge in a graph are called adjacent node.

DATABASE DESIGN - 1DL400

Data Structure [Question Bank]

Introduction to Stacks

Introduction Advantages and Disadvantages Algorithm TIME COMPLEXITY. Splay Tree. Cheruku Ravi Teja. November 14, 2011

Lecture Notes on Binary Search Trees

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Why Use Binary Trees?

Structural Design Patterns Used in Data Structures Implementation

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

M-way Trees and B-Trees

Section IV.1: Recursive Algorithms and Recursion Trees

Lecture 2: Data Structures Steven Skiena. skiena

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

DATA STRUCTURES USING C

Java Map and Set collections

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Transcription:

Introduction to Computation and Problem Solving Class 2: Binary Search Trees Prof. Steven R. Lerman and Dr. V. Judson Harward Binary Search Trees In the previous lecture, we defined the concept of binary search tree as a binary tree of nodes containing an ordered key with the following additional property. The left subtree of every node (if it exists) must only contain nodes with keys less than or equal to the parent and the right subtree (if it exists) must only contain nodes with keys greater than or equal to the parent. We saw that performing an inorder traversal of such a tree is would visit each node in order. 2 1

Goals We are going to implement a binary search tree with the usual data structure operations and then critique the implementation. Generalizations of the binary search tree drive many important applications, including commercial databases. The implementation will give you a feel for tree methods that tend to be more "visual" or "topological" than array or list implementations. 3 Keys and Values If binary search trees are ordered, then they must be ordered on some key possessed by every tree node. A node might contain nothing but the key, but it's often useful to allow each node to contain a key and a value. The key is used to look up the node. The value is extra data contained in the node indexed by the key. 4 2

Maps Such data structures with key/value pairs are usually called maps. As an example, consider the entries in a phone book as they might be entered in a binary search tree. The subscriber name, last name first, serves as the key, and the phone number serves as the value. 5 Key and Value Type Sometimes you may not need the value. But if one node has a value, you probably want to supply one for every node. We also haven't specified the type of the key and value. They can be of any class you want, but all nodes should possess keys that are instances of the same class and values that are instances of the same class. Keys and values do not have to be the same type. Keys and values will be handled as Objects by our tree implementation except when we need to compare them. 6 3

Duplicate Keys Do we allow the tree to contain nodes with identical keys? There is nothing in the concept of a binary search tree that prevents duplicate keys. In the interface that we will implement, we use the keys to access the values. If we allowed duplicate keys, we wouldn't be able to distinguish between multiple nodes possessing the same key. Thus, in this implementation, duplicate keys will not be allowed. Duplicate values associated with different keys are legal. 7 SortedMap Interface public interface SortedMap { // keys may not be null but values may be public boolean isempty(); public void clear(); public int size(); public Object get( Object key ); public Object firstkey(); public Object lastkey(); public Object remove( Object key ); public Object put( Object key, Object value ); public MapIterator iterator(); 4

SortedMap Notes get(), firstkey(), lastkey() all return the appropriate values and leave the key value pair in the map. firstkey() and lastkey() return null if the map is empty remove() returns the value if the key is present; otherwise it returns null put(): if the key is not in the map, insert the new key/value pair; if the key is already present, return the old value, and replace the old value with the new one in the map 9 MapIterator Interface public interface MapIterator { public boolean hasnext(); public Object next() throws NoSuchElementException; public Object remove() throws IllegalStateException; public Object key() throws IllegalStateException; public Object getvalue() throws IllegalStateException; public Object setvalue( Object value ) throws IllegalStateException; 10 5

Comparator If keys are arbitrary objects, how can you order them? The user can supply his own Comparator public interface Comparator { // Neither o1 nor o2 may be null public int compare(object o1, Object o2); // e.g., compare( Integer(5), Integer(7)) < 0 // compare( Integer(5), Integer(5)) == 0 11 StringComparator public class StringComparator implements Comparator { public int compare(object o1, Object o2) { return ((String) o1).compareto((string) o2); BinarySearchTree btree = new BinarySearchTree( new StringComparator() ); 12 6

Comparable Interface In recent versions of the JDK, the wrapper classes for builtin types all implement a separate Comparable interface: public interface Comparable { public int compareto(object o); Since String implements the Comparable interface, we don't need to implement the special StringComparator. The compareto() method returns ints with the same sense as the Comparator interface. 13 Implementing BinarySearchTree We will have separate tree and node classes for BinarySearchTree. We will use a private static inner class for the node class, Branch, just as we did for the SLink class in SLinkedList. public class BinarySearchTree implements SortedMap { private Branch root = null; private int length = 0; private Comparator comparator = null; 14 7

Branch Inner Class Branch holds links to its parent node as well as left and right subtrees. This will make some operations like deleting a node much more efficient. static private class Branch { private Branch left = null; private Branch right = null; private Branch parent = null; private Object key; private Object value = null; 15 Simple Methods public BinarySearchTree( Comparator c ) { comparator = c; public BinarySearchTree() { this( null ); public void clear() { root = null; length = 0; public boolean isempty() { return ( root == null ); 16

firstkey() Method public Object firstkey() { if ( root == null ) return null; else return root.first().key; In Branch: Branch first() { Branch f = this; while ( f.left!= null ) { f = f.left; return f; 17 firstkey() in Action 1 st iteration 1 2 nd iteration 11 25 1 9

get() Method public Object get( Object k ) { Branch b = find( k ); return ( b == null )? null : b.value; private Branch find( Object k ) { Branch b = root; int c; while (b!= null && (c=compare( k, b.key ))!= 0) { if ( c < 0 ) b = b.left; else b = b.right; return b; 19 get() in Action get(19) 1 1 st iteration 11 2 nd iteration 25 20 10

compare() Method private int compare(object k1, Object k2) { if ( k1 == null k2 == null ) throw new IllegalArgumentException( "Null key now allowed" ); // if there is a comparator, use it if ( comparator!= null ) return comparator.compare( k1, k2 ); else { return ((Comparable)k1).compareTo(k2); 21 put() Strategy Our strategy is to look for the key supplied as an argument. If we find it, we return the old value after replacing it with the new one. While searching for the node, shadow our search by keeping a reference to the parent of the current node. If we don t find the node, the search ends when we arrive at a null branch. In this case, the parent reference will identify the parent of the new inserted node. 22 11

put() in Action put(20,x) 1 11 25 parent at end of failed search 20 null new node 23 put() Method, 1 public Object put(object k, Object v) { Branch p = null; Branch b = root; int c = 0; // find node with key k retaining ref p to parent while ( b!= null ) { p = b; c = compare( k, b.key ); if ( c == 0 ) { // found it; insert new value, return old Object oldvalue = b.value; b.value = v; return oldvalue; else if ( c < 0 ) b = b.left; else b = b.right; 24 12

put() Method, 2 // Key k doesn't exist in tree; // insert new node below p(arent) Branch newbranch = new Branch( k, v, p ); length++; if ( p == null ) { root = newbranch; else { // c still holds last comparison if ( c < 0 ) p.left = newbranch; else p.right = newbranch; return null; 25 delete() Strategy remove() calls delete() to do most of the work. When we delete a node from a binary search tree, we must ensure that the resulting structure (1) is still a tree, and (2) the tree still obeys the rule of a binary search tree. The rule requires that for every node, the keys in the left subtree if present precede the key of the node which must precede the keys in the right subtree. 26 13

delete() Cases, 1 There are three deletion cases we must consider: 1. The deleted node has no children, e.g., node below. 2. The deleted node has one child, e.g., node 7 below. 3. The deleted node has two children, e.g., node 25 below 1 11 25 delete(), No Children 1 11 25 2 14

delete(), 1 Child 1 11 25 delete(), 2 Children 1 11 25 30 15

delete(), 2 Children Strategy In the last case, that of two children, the problem is more serious since the tree is now in three parts. The solution starts by realizing that we can minimize the ordering problem by stitching the tree back together using the node immediately preceding or following the deleted node in inorder sequence. These are known as the predecessor and successor nodes. 31 Using the Successor Node Let's choose the successor node. The successor of a node with a right subtree will be the first() node of that subtree. If we replace the deleted node by its successor, then all ordering conditions will be met. But what about the successor's children. The successor of a node with two children can have at most one child (a right child). If it had a left subtree, the node couldn't be the successor since members of the left subtree follow the deleted node but precede the successor. Since the successor can have at most one subtree, we can move the successor up to replace the node we want to remove and relink its right subtree, if present, as in the second case above. 16

delete(), find successor 1 11 25 first() of right subtree delete(), replace successor 1 1 11 25 34 17

delete(), replace successor 2 1 11 25 35 delete(), replace successor 3 1 11 25 36 1

The Efficiency of Binary Search It makes intuitive sense that the basic operations on a binary search tree should require O(h) time where h is the height of the tree. It turns out that the height of a balanced binary tree is roughly log 2 (n) where n is the number of elements if the tree remains approximately balanced. It can be proved that if keys are randomly inserted in a binary search tree, this condition will be met, and the tree will remain adequately enough balanced so that search and insertion time will approximate O(log n). 37 Tree Balance There are some extremely simple and common cases, however, where keys will not be inserted in random order. Consider what will happen if you insert keys into a search tree from a sorted list. The tree will assume a degenerate form equivalent to the source list and search and insertion times will degrade to O(n). There are many variants of trees, e.g., red-black trees, AVL trees, B-trees, that try to solve this problem by rebalancing the tree after operations that unbalance it. 3 19

Keys Inserted in Order 1 2 3 4 39 20