[key, Left subtree, Right subtree]

Similar documents
Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

Linked Structures Songs, Games, Movies Part III. Fall 2013 Carola Wenk

MITOCW watch?v=w_-sx4vr53m

Computer Science 136 Spring 2004 Professor Bruce. Final Examination May 19, 2004

Data Structures and Algorithms

6.001 Notes: Section 8.1

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

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

The following content is provided under a Creative Commons license. Your support

Computational Geometry

Figure 4.1: The evolution of a rooted tree.

In addition to the correct answer, you MUST show all your work in order to receive full credit.

Intro. Scheme Basics. scm> 5 5. scm>

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

6.001 Notes: Section 31.1

Linked Lists. What is a Linked List?

(Refer Slide Time: 01.26)

CSI33 Data Structures

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

(Refer Slide Time: 00:50)

The following content is provided under a Creative Commons license. Your support

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

More Complicated Recursion CMPSC 122

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

Binary search trees. We can define a node in a search tree using a Python class definition as follows: class SearchTree:

B-Trees. Based on materials by D. Frey and T. Anastasio

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

COP Programming Assignment #7

CPSC 320 Notes, Playing with Graphs!

CS F-11 B-Trees 1

6.001 Notes: Section 6.1

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Heap sort. Carlos Moreno uwaterloo.ca EIT

Lecture 22 Tuesday, April 10

Comp Intermediate Programming EXAM #2 March 30, 2005 Rice University - Instructors: Cox & Nguyen

Problem A - Complete the sequence

MITOCW watch?v=rvrkt-jxvko

Intro. Speed V Growth

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Hash Tables (Cont'd) Carlos Moreno uwaterloo.ca EIT

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

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

Principles of Algorithm Design

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

CSE100. Advanced Data Structures. Lecture 8. (Based on Paul Kube course materials)

Slide 1 CS 170 Java Programming 1 Testing Karel

MITOCW watch?v=sdw8_0rdzuw

MITOCW watch?v=flgjisf3l78

Data structures. Priority queues, binary heaps. Dr. Alex Gerdes DIT961 - VT 2018

Binary Heaps in Dynamic Arrays

Chapter 8 Sorting in Linear Time

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Binary Search Trees. Carlos Moreno uwaterloo.ca EIT

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

1 Getting used to Python

Lecture 23: Priority Queues, Part 2 10:00 AM, Mar 19, 2018

CS3110 Spring 2017 Lecture 18: Binary Search Trees

CS 206 Introduction to Computer Science II

Binary Search and Worst-Case Analysis

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

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

CSC 172 Data Structures and Algorithms. Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium

Part 1 Your First Function

Binary Search Trees. See Section 11.1 of the text.

Direct Variations DIRECT AND INVERSE VARIATIONS 19. Name

CMSC 341 Lecture 10 Binary Search Trees

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

Statistics Case Study 2000 M. J. Clancy and M. C. Linn

Binary Search and Worst-Case Analysis

Outline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.

Constraint Satisfaction Problems: A Deeper Look

Black Problem 2: Huffman Compression [75 points] Next, the Millisoft back story! Starter files

MITOCW watch?v=tkwnms5irbu

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

DATA STRUCTURES AND ALGORITHMS

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

Hash Tables (Cont'd) Carlos Moreno uwaterloo.ca EIT

Slide 1 Side Effects Duration: 00:00:53 Advance mode: Auto

CmpSci 187: Programming with Data Structures Spring 2015

arxiv: v3 [cs.ds] 18 Apr 2011

INF2220: algorithms and data structures Series 1

Multiway Search Trees

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

MITOCW watch?v=hverxup4cfg

MITOCW watch?v=zm5mw5nkzjg

6.001 Notes: Section 15.1

Balanced Binary Search Trees. Victor Gao

Week - 04 Lecture - 02 Merge Sort, Analysis

CS02b Project 2 String compression with Huffman trees

ext Total Score /20 /20 /15 /20 /25 /5 Grader

D - Tic Tac Toe. Let's use our 9 sparkles to build a tic tac toe game! 2017 courses.techcamp.org.uk/ Page 1 of 9

CS2 Algorithms and Data Structures Note 6

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

CSE 2123 Recursion. Jeremy Morris

Binary Trees. This recursive defnition prefgures the patern of most algorithms we use on this data structure, as we will see below.

CPSC 320 Sample Solution, Playing with Graphs!

CS2110 Assignment 3 Inheritance and Trees, Summer 2008

(Refer Slide Time: 06:01)

Transcription:

Project: Binary Search Trees A binary search tree is a method to organize data, together with operations on these data (i.e., it is a data structure). In particular, the operation that this organization wants to perform really fast is searching. The data are records that may contain many fields (recall the student record from Chapter 11), but one field is specifically used for ordering the data records; this field is called the record key. In order to simplify things, from now on we will assume that the data record contains only this key, and the keys are integers; but we don't require that keys are unique (so, the same key may show up multiple times). Then a binary search tree consists of nodes that are of the following form: [key, Left subtree, Right subtree] Notice that a node is itself a record. It is a list with three elements: the first element is an integer ( key), and the next two elements are themselves binary search trees, a Left subtree and a Right subtree! So the definition of a binary search tree is a recursive one. Before we define binary search trees though, it is simpler to first define binary trees (without the search component): Definition (binary tree): A binary tree T is either an empty tree (T=[]) (base case) or T=[key, L, R], where L, R are binary trees (called the subtrees of T). We can draw a binary tree using nodes to hold the keys and lines (edges) that show what are the left and right subtrees of the nodes. Here is such a conceptual drawing of two binary trees 1: The actual Python picture of the tree (a), following the definition, is: T = [6, [5, [2, [], []], [5, [], []]], [7, [], [8, [], []]]] This looks confusing, but it isn't hard to see what's going on. In the conceptual picture, you can see that there are three nodes with no subtrees whatsoever (such nodes are called leaves); they are the 1 All figures and pseudo-code in this project come from Chapter 12 of Introduction to Algorithms, 3 rd ed., by T. Cormen, C. Leiserson, R. Rivest, C. Stein. MIT Press 2009.

nodes with key values 2,5, and 8. These nodes are themselves binary trees, but with empty Left and Right subtrees, so they should look like [2, [], []], [5, [], []], [8, [], []]. Let's name them T1 = [2, [], []] T2 = [5, [], []] T3 = [8, [], []] Moving a level up, note that T1, T2 are the Left and Right subtrees of the node with key value 5; so this node is (according to the definition) [5, T1, T2]. Let's name it T4 = [5, T1, T2] Also T3 is the Right subtree of node with key value 7, and this node has no Left subtree; therefore, it is [7, [], T3]. Let's name it T5 = [7, [], T3] Now, notice that T4, T5 are the Left and Right subtrees of the node with key value 6 (since this is the top-most node, i.e., the node where our recursive definition started from, it is called the root of the whole binary tree). So this node should look like [6, T4, T5]. But this is exactly what T is! (tarting from [6, T4, T5], start replacing the names with their actual list values and you'll get T) Practice problem: Repeat the same process for tree (b) in the figure, to find out how it really looks in Python, using the definition. A binary search tree is itself a binary tree, but we need to add to the definition one property that this tree must satisfy; namely, we will require that in a binary search tree T = [k, L, R] the key values in the Left subtree L are all smaller or equal to key value k, and the key values in the Right subtree R are all greater or equal to k. So, the new definition is: Definition (binary search tree): A binary search tree T is either an empty tree (T=[]) (base case) or T=[key, L, R], where L, R are binary search trees (called the subtrees of T) with the following property: the key values in L are all smaller or equal to key, and the key values in R are all greater or equal to key. Note that because we now require L, R to be themselves binary search trees (instead of simply binary trees as before), the key values inside L and R also have this nice key property. Trees (a) and (b) above are actually binary search trees, because their keys satisfy this extra property. So, what kind of operations does this data structure support? Here are some of them: Searching: Now, it should be clear why we would like to organize our data into a binary search tree: Suppose we want to search for a record with key value x in a binary search tree T. If T=[] (empty), then we return -1 or didn't find it or x doesn't exist or something along these lines, that indicates that a record with key value x doesn't exist in our data. If T is not empty, then it must be of the form T=[k,L,R]; in this case, if x=k then this is our record, and we return its data (in our case we don't have any more data to return, so probably we want to return something like 0, or I found it! or something along these lines). But if x is not equal to k, then it may be in L or R; but which one? Here is exactly the point where we use the nice key values property: if x<k, then we should search in L, and if x>k, then we should search in R. The point is that once you determine which subtree to search, then you don't have to even bother with the other subtree. Finding minimum/maximum: It is also extremely easy to find the record with minimum key value:

all you have to do is go left all the way, until you cannot go anymore left (why does this work?). Finding the maximum key value is symmetric (I'll let you think about it yourselves). Sorting: By now it should be obvious how to output all key values (remember! we allow multiple instances of the same key value!) sorted from smallest to largest. Insertion: Recall that we initially start with an empty data structure (T=[]); then we build it up by inserting new records (each with its own key) one-by-one. In order to insert a record with key x, you need to first find exactly where x should be inserted in order that the new tree is still a binary search tree (hint: you already know how to do this!). An example of insertion is the following figure: In this figure, key 13 was inserted in the tree; the light-grey nodes show the path of nodes that the insertion function looked before finding the one (with key 15) that got 13 as a new left subtree. Deletion: Deletion is the most complicated operation to perform in binary search trees. It is probably best described by the following figure, that describes what happens when a node z=[z.key, l, r] (z is possibly the left of right subtree of a node q, or is the root of the whole tree itself) is deleted. There are four distinct cases, with case (d) being the complicated one: first you split the node z into two parts (trees), then you find the minimum 2 key of r (let's say it's y), you promote it to be the new root of r as shown, and then you combine again the two trees into one, as shown. 3 Those of you that are observant have already noticed that case (c) is, in fact, the special case of (d) when y is the same as r, i.e., y/r is already the minimum key node for the right subtree of z. Practice problem: Delete 18, 12, 15 successively from the tree above (in this order). 2 We already know how to do this! 3 In the figure, NIL stands for an empty tree [].

Project Implement a class called binary_search_tree that contains at least the following internal variables and methods (in the process of designing your class, you may end up implementing/using more variables/methods; it's totally up to you): T: the variable that contains the tree init : the class constructor; must set the initial tree to 'empty' (T=[]) gettree: method that returns the tree T searchtree: takes a key value x, and returns -1 if x is not found in T, and 0 otherwise. minimumtree, maximumtree: return the minimum, maximum keys of T, respectively

increasing_ordertree: outputs a sorted list of all the keys in T (duplicates are allowed) inserttree: takes a key value x, and inserts it in T deletetree: takes a key value x, and deletes it from T (or does nothing if x isn't in T) Then, use an object from your class to write a sorting program. The user is asked to give integer values one after the other, separated by a space, and then the program outputs the same numbers but sorted from smallest to largest, and then the same numbers sorted from largest to smallest (hint: you may want to implement a method decreasing_ordertree, doing the symmetric job of increasing_ordertree).