Programming Languages and Techniques (CIS120)

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Balanced Search Trees. CS 3110 Fall 2010

Programming Languages and Techniques (CIS120)

Chapter 20: Binary Trees

Programming Languages and Techniques (CIS120e)

TREES. Trees - Introduction

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

Friday Four Square! 4:15PM, Outside Gates

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

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

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

Binary Search Tree (3A) Young Won Lim 6/2/18

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

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

Programming II (CS300)

Binary Trees, Binary Search Trees

Binary Search Tree (2A) Young Won Lim 5/17/18

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

Name: CIS 120e Midterm I Review

TREES Lecture 12 CS2110 Spring 2019

CSC/MAT-220: Lab 6. Due: 11/26/2018

Binary Search Tree (3A) Young Won Lim 6/6/18

Binary Search Trees Part Two

Binary Search Tree (3A) Young Won Lim 6/4/18

Data Structures in Java

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

Trees. Eric McCreath

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

Binary Trees: Practice Problems

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

Trees. Truong Tuan Anh CSE-HCMUT

CSC148 Week 7. Larry Zhang

CIS 120 Midterm I February 16, 2015 SOLUTIONS

Balanced Trees. Nate Foster Spring 2018

Multi-Way Search Tree

CSI33 Data Structures

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

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

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

Data Structures and Algorithms for Engineers

Balanced Trees. Prof. Clarkson Fall Today s music: Get the Balance Right by Depeche Mode

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

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

INF2220: algorithms and data structures Series 1

We have the pointers reference the next node in an inorder traversal; called threads

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

CS302 - Data Structures using C++

CSC148-Section:L0301

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

Programming Languages and Techniques (CIS120)

CS24 Week 8 Lecture 1

Self-Balancing Search Trees. Chapter 11

Binary Search Trees. Ravi Chugh March 28, 2014

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

Binary Trees

CS350: Data Structures Binary Search Trees

COMP Analysis of Algorithms & Data Structures

Section 4 SOLUTION: AVL Trees & B-Trees

I2206 Data Structures TS 6: Binary Trees - Binary Search Trees

Binary Search Trees. See Section 11.1 of the text.

CMSC 341. Binary Search Trees CMSC 341 BST

SFU CMPT Lecture: Week 9

CMSC 341 Lecture 10 Binary Search Trees

Fall, 2015 Prof. Jungkeun Park

COMP Analysis of Algorithms & Data Structures

CSI33 Data Structures

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

ECE 242. Data Structures

TREES Lecture 12 CS2110 Spring 2018

CS350: Data Structures AVL Trees

Last week. Last week. Last week. Today. Binary Trees. CSC148 Intro. to Computer Science. Lecture 8: Binary Trees, BST

Binary Search Trees Treesort

CS 350 : Data Structures Binary Search Trees

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

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

Binary Trees. Examples:

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

Unit III - Tree TREES

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

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

CS 171: Introduction to Computer Science II. Binary Search Trees

Algorithms. Deleting from Red-Black Trees B-Trees

March 20/2003 Jayakanth Srinivasan,

Spring 2018 Mentoring 8: March 14, Binary Trees

BBM 201 Data structures

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

3137 Data Structures and Algorithms in C++

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

CSC148 Week 6. Larry Zhang

TREES Lecture 10 CS2110 Spring2014

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

Algorithms. AVL Tree

Transcription:

Programming Languages and Techniques () Lecture 6 January 24, 2018 Binary Search Trees (Lecture notes Chapter 7)

Announcements Homework 2: Computing Human Evolution due Tuesday, September 19 th Reading: Chapter 7

Recap: Binary Trees trees with (at most) two branches

Binary Trees 3 root node root s left child 2 2 root s right child left subtree 0 1 3 1 leaf node empty A binary tree is either empty, or a node with at most two children, both of which are also binary trees. A leaf is a node whose children are both empty.

Binary Trees in OCaml type tree = Empty Node of tree * int * tree let t : tree = Node (Node (Empty, 1, Empty), 3, Node (Empty, 2, Node (Empty, 4, Empty))) = 3 1 2 4

Some Tree Coding examples: size, height

Some functions on trees (* counts the number of nodes in the tree *) let rec size (t:tree) : int = begin match t with Empty -> 0 Node(l,x,r) -> 1 + (size l) + (size r) end (* counts the longest path from the root to a leaf *) let rec height (t:tree) : int = begin match t with Empty -> 0 Node(l,x,r) -> 1 + max (height l) (height r) end

Some functions on trees (* counts the number of nodes in the tree *) let rec size (t:tree) : int = begin match t with Empty -> 0 Node(l,_,r) -> 1 + (size l) + (size r) end (* counts the longest path from the root to a leaf *) let rec height (t:tree) : int = begin match t with Empty -> 0 Node(l,_,r) -> 1 + max (height l) (height r) end

Recap: Trees as Containers

Trees as Containers Like lists, binary trees aggregate data As we did for lists, we can write a function to determine whether the data structure contains a particular element type tree = Empty Node of tree * int * tree

Searching for Data in a Tree let rec contains (t:tree) (n:int) : bool = begin match t with Empty -> false Node(lt,x,rt) -> x = n (contains lt n) (contains rt n) end This function searches through the tree, looking for n In the worst case, it might have to traverse the entire tree 5 1 7 0 3

Search during (contains t 8) 5 1 7 0 3 9 8

New: Recursive Tree Traversals 1 4 7 2 5 2 5 3 6 3 4 6 1 3 7 1 2 5 7 6 4 Pre-Order Root Left Right In Order Left Root Right Post-Order Left Right Root (* Pre-Order Traversal: *) let rec f (t:tree) : = begin match t with Empty -> Node(l, x, r) -> let root = x in (* process root *) let left = f l in (* recursively process left *) let right = f r in (* recursively process right *) combine root left right end Different traversals vary the order in which these are computed

In what sequence will the nodes of this tree be visited by a post-order traversal? 1. [0;1;6;2;7;8] 2. [0;1;2;6;7;8] 3. [2;1;0;7;6;8] 4. [7;8;6;2;1;0] 5. [2;1;7;8;6;0] 1 0 6 2 7 8 Post-Order Left Right Root Answer: 5

let rec inorder (t:tree) : int list = begin match t with Empty -> [] Node (left, x, right) -> inorder left @ (x :: inorder right) end 4 What is the result of applying this function on this tree? 1. [] 2 5 2. [1;2;3;4;5;6;7] 3. [1;2;3;4;5;7;6] 4. [4;2;1;3;5;6;7] 1 3 6 5. [4] 6. [1;1;1;1;1;1;1] 7 7. none of the above Answer: 3

Searching for Data in a Tree 1 0 3 5 7 let rec contains (t:tree) (n:int) : bool = begin match t with Empty -> false Node(lt,x,rt) -> x = n (contains lt n) (contains rt n) end contains (Node(Node(Node (Empty, 0, Empty), 1, Node(Empty, 3, Empty)), 5, Node (Empty, 7, Empty))) 7 5 = 7 contains (Node(Node (Empty, 0, Empty), 1, Node(Empty, 3, Empty))) 7 contains (Node (Empty, 7, Empty)) 7 (1 = 7 contains (Node (Empty, 0, Empty)) 7 contains (Node(Empty, 3, Empty)) 7) contains (Node (Empty, 7, Empty)) 7 ((0 = 7 contains Empty 7 contains Empty 7) contains (Node(Empty, 3, Empty)) 7) contains (Node (Empty, 7, Empty)) 7 contains (Node(Empty, 3, Empty)) 7 contains (Node (Empty, 7, Empty)) 7 contains (Node (Empty, 7, Empty)) 7

Ordered Trees Big idea: find things faster by searching less

Key Insight: Ordered data can be searched more quickly This is why telephone books are arranged alphabetically But requires the ability to focus on (roughly) half of the current data

Binary Search Trees A binary search tree (BST) is a binary tree with some additional invariants*: Node(lt,x,rt) is a BST if - lt and rt are both BSTs - all nodes of lt are < x - all nodes of rt are > x Empty is a BST The BST invariant means that container functions can take time proportional to the height instead of the size of the tree. *An data structure invariant is a set of constraints about the way that the data is organized. types (e.g. list or tree) are one kind of invariant, but we often impose additional constraints.

An Example Binary Search Tree < 5 > Note that the BST invariants hold for this tree. < 1 > > 7 0 3 9 < Node(lt,x,rt) is a BST if - lt and rt are both BSTs - all nodes of lt are < x - all nodes of rt are > x Empty is a BST 8

Search in a BST: (lookup t 8) < 5 8 > 5 > 8 > 7 < 1 > > 7 0 3 8 < 9 < 9 8

Searching a BST (* Assumes that t is a BST *) let rec lookup (t:tree) (n:int) : bool = begin match t with Empty -> false Node(lt,x,rt) -> if x = n then true else if n < x then lookup lt n else lookup rt n end The BST invariants guide the search. Note that lookup may return an incorrect answer if the input is not a BST! This function assumes that the BST invariants hold of t.

Is this a BST?? 1. yes 2. no 4 2 5 1 3 6 Node(lt,x,rt) is a BST if - lt and rt are both BSTs - all nodes of lt are < x - all nodes of rt are > x Empty is a BST Answer: no, 7 to the left of 6 7