Lab Exercise 8 Binary Search Trees

Similar documents
Binary Trees, Binary Search Trees

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

Binary Trees

INF2220: algorithms and data structures Series 1

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

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

CE 221 Data Structures and Algorithms

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

CSE 530A. B+ Trees. Washington University Fall 2013

Algorithms. Deleting from Red-Black Trees B-Trees

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

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

Organizing Spatial Data

CS24 Week 8 Lecture 1

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

EE 368. Weeks 5 (Notes)

Binary Trees: Practice Problems

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Figure 4.1: The evolution of a rooted tree.

Binary Trees Fall 2018 Margaret Reid-Miller

Lower Bound on Comparison-based Sorting

Data Structures and Algorithms

Friday Four Square! 4:15PM, Outside Gates

M-ary Search Tree. B-Trees. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. Maximum branching factor of M Complete tree has height =

Why Do We Need Trees?

CS350: Data Structures AVL Trees

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Algorithms. AVL Tree

Balanced Search Trees. CS 3110 Fall 2010

Garbage Collection: recycling unused memory

Trees. Truong Tuan Anh CSE-HCMUT

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

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

M-ary Search Tree. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. B-Trees (4.7 in Weiss)

Compuer Science 62 Assignment 10

Lecture 15 Binary Search Trees

ITI Introduction to Computing II

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

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

You must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

TREES Lecture 12 CS2110 Fall 2016

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

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

Generic BST Interface

Spring 2018 Mentoring 8: March 14, Binary Trees

Chapter 20: Binary Trees

Trees. CSE 373 Data Structures

Topic 14. The BinaryTree ADT

Trees, Binary Trees, and Binary Search Trees

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

Algorithms and Data Structures

CS350: Data Structures Binary Search Trees

ITI Introduction to Computing II

TREES. Trees - Introduction

Data Structures in Java

Second Examination Solution

Programming II (CS300)

8. Binary Search Tree

Analysis of Algorithms

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

CS F-11 B-Trees 1

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

Tree traversals and binary trees

Binary Trees. Examples:

Structure and Interpretation of Computer Programs

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

examines every node of a list until a matching node is found, or until all nodes have been examined and no match is found.

TREES Lecture 12 CS2110 Spring 2019

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

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

Programming Languages and Techniques (CIS120)

Introduction to Trees. D. Thiebaut CSC212 Fall 2014

Binary heaps (chapters ) Leftist heaps

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

CS350: Data Structures Red-Black Trees

Animals Due Part 1: 19 April Part 2: 26 April

CS 151. Binary Trees. Friday, October 5, 12

tree nonlinear Examples

CS 350 : Data Structures Binary Search Trees

Data and File Structures Laboratory

Recursion and Structural Induction

Section 4 SOLUTION: AVL Trees & B-Trees

Trees. A tree is a directed graph with the property

Self-Balancing Search Trees. Chapter 11

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

CSE 214 Computer Science II Introduction to Tree

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Red-Black trees are usually described as obeying the following rules :

Data Structures and Algorithms

Ranking. In an ordered set or a dictionary: Rank of key k is i iff k is the ith smallest key. Example: if s stores these keys 42, 55, 61, then:

Data Structure. IBPS SO (IT- Officer) Exam 2017

Motivation for B-Trees

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

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

Principles of Computer Science

Structure and Interpretation of Computer Programs

Transcription:

Lab Exercise 8 Binary Search Trees A binary search tree is a binary tree of ordered nodes. The nodes are ordered by some key value, for example: alphabetic or numeric. The left subtree of every node (if it exists) contains nodes with keys less than the parent and the right subtree (if it exists) contains nodes with keys greater than the parent. This lab has you program various things on a binary search tree using private helper methods. You will do three tasks (your TA will lead you through the first one), each with these three steps. First figure out how to do the computation on an example tree of beer names, then show the general pattern, and finally implement it as a helper method. Using Eclipse, import lab8 : /afs/cs.uwm.edu/users/classes/cs351/401/pantherid/git/lab8.git But your first task will be on paper. 1 Practice: Find In Range For this task, you will find all the keys in a particular range, your TA will demonstrate the three steps methodology with this task. 1.1 Step 1: Finding Keys in an Example Tree In the following tree, write next to every node, all the keys that are in the subtree rooted at that node that are in the range (dancingman,schlitz). For example the set for is {lakefront}, and the set for michelob is {michelob,}. 1.2 Step 2: Cases for Finding Keys We have separate cases depending on whether the key is before the range, within the range or after the range. We want to avoid looking in a subtree where nothing could be in the range (to save time). Fall 2017 page 1 of 6

Case key k comes before (x, y): Look in left subtree? Look in right subtree? Case key k is in range (x, y): Look in left subtree? Look in right subtree? Case key k is after (x, y): Look in left subtree? Look in right subtree? 1.3 Code for Find Implement private helper method findinrange. Do not have any more than three cases, even if x or y is null. If x is null, then k can t come before (x, y). If y is null, then it can t come after. Fall 2017 page 2 of 6

2 Task 1: Count We can define a recursive function to count the nodes in a binary search tree. Consider the number of nodes in a subtree. An empty subtree has zero nodes. A subtree rooted at a leaf has one node. In general, a the number of nodes in a subtree rooted at a given node can be computed from the count of its children. 2.1 Step 1: Count for an Example Tree On the following tree, to the left of each node, please write the number of nodes in the subtree rooted at that node. See the TA for checkoff #1. 2.2 Step 2: Cases Now try to figure out a way to solve the problem in general. Fill in the following table: Count for a null pointer: Count for a node: (equation using c 1 and c 2 ) Assuming c 1 is the count for the left subtree and c 2 is the count for the right subtree. See the TA for checkoff #2. 2.3 Step 3: Code Implement a private helper method docount(node node) in class edu.uwm.cs351.bst that uses a single if to distinguish these two cases. Make sure you pass the test suite TestCount. Fall 2017 page 3 of 6

3 Task 2: Maximum Height A binary search tree will not work very well, if it is very tall; the runtime for many algorithms depends on the height. In this section, you compute the height of a binary search tree node. The height of a node is how many nodes are there on the longest path from this node down to a null pointer. For a leaf node, the height is 1; the null pointer has height 0. 3.1 Step 1: Height for an Example Tree On the following tree, please write the height of each node to the left of the node. Hint: the height of michelob is 2. See the TA for checkoff #3. 3.2 Step 2: Cases Now try to figure out a way to solve the problem in general. Fill in the following table: Height of a null pointer: Height of a node: (equation using h 1 and h 2 ) Assuming h 1 is the height of the left subtree and h 2 is the height of the right subtree. Hint: You may use max(x,y) to compute the maximum of two numbers x and y. See the TA for checkoff #4. 3.3 Step 3: Code Implement a private helper method doheight(node node) in class edu.uwm.cs351.bst that uses a single if to distinguish these two cases. You should use Math.max in your code. Make sure you pass the test suite TestHeight. Fall 2017 page 4 of 6

4 Task 3: Range Check A key invariant for a binary search tree is that, if its left subtree is not empty, then all the keys in that subtree is less than the key of the root node, and similarly if right subtree is not empty, then all keys in that subtree is greater than the key of the root node. Alternatively, the key in a node is greater than all left ancestors (ancestors for which we are in the right subtree) and less than all right ancestors. By picking the greatest left ancestor (if any) and the least right ancestor (if any), the legal range for a key can be written as an open interval (x, y) where x is the lower bound (greatest left ancestor key) and y is the upper bound (least right ancestor key). For example, the range for pabst is (, ) and the range for best is (nothing, ). 4.1 Step 1: Range for an Example Tree Now put the range for each node (you can omit best and pabst to leave more room): See the TA for checkoff #5. Don t go on until the TA has checked that you understand the task. 4.2 Step 2: Cases Now give how to solve the task in general. Since this task is top-down, it has to special case the root. Range for the root node:. Assuming a node has range (x, y) and key k: The range for the left subtree is. The range for the right subtree is. See the TA for checkoff #6. Fall 2017 page 5 of 6

4.3 Step 3: Code Now implement the function checktree(set<string> checks) using the following helper function which should also be implemented: dochecktree(node n, String lo, String hi, Set<String> checks) Each time dochecktree is called on a non-null node, it should create a string k<-(lo,hi) without any spaces. Make sure you pass the TestRange test suite. See TA for the final checkoff #7, to check your code for the last two tasks. Make sure you have no more cases than this sheet says. Fall 2017 page 6 of 6