Module 9: Binary trees

Similar documents
Module 8: Binary trees

Trees. Binary arithmetic expressions. Visualizing binary arithmetic expressions. ((2 6) + (5 2))/(5 3) can be defined in terms of two smaller

Trees. Readings: HtDP, sections 14, 15, 16.

Trees. Example: Binary expression trees. Example: Evolution trees. Readings: HtDP, sections 14, 15, 16.

CS115 - Module 8 - Binary trees

Module 9: Trees. If you have not already, make sure you. Read How to Design Programs Sections 14, 15, CS 115 Module 9: Trees

CS115 - Module 10 - General Trees

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

Module 10: General trees

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Data Structures and Algorithms

Programming II (CS300)

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

Working with recursion

Chapter 20: Binary Trees

Graphs. Readings: Section 28. CS 135 Fall : Graphs 1

Graphs. Directed graphs. Readings: Section 28

Types of recursion. Readings: none. In this module: a glimpse of non-structural recursion. CS 135 Winter : Types of recursion 1

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

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

Binary Trees

CS 310 B-trees, Page 1. Motives. Large-scale databases are stored in disks/hard drives.

INF2220: algorithms and data structures Series 1

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

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

Linked Data Structures. Linked lists. Readings: CP:AMA The primary goal of this section is to be able to use linked lists and trees.

CS F-11 B-Trees 1

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Local definitions and lexical scope

Local definitions and lexical scope. Local definitions. Motivating local definitions. s(s a)(s b)(s c), where s = (a + b + c)/2.

Linked Data Structures. Linked lists. Readings: CP:AMA The primary goal of this section is to be able to use linked lists and trees.

Module 5: Lists. Readings: HtDP, Sections 9, 10.

CS 115 Lecture Notes Winter 2019

CS 135 Fall 2018 Final Exam Review. CS 135 Fall 2018 Final Exam Review 1

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

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

Generative and accumulative recursion. What is generative recursion? Example revisited: GCD. Readings: Sections 25, 26, 27, 30, 31

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

Lists. Readings: HtDP, sections 9 and 10. Avoid 10.3 (uses draw.ss). CS 135 Winter : Lists 1

TREES. Trees - Introduction

Trees. Eric McCreath

8. Binary Search Tree

Algorithms. Deleting from Red-Black Trees B-Trees

Problem 1: Binary Trees: Flatten

Binary Trees, Binary Search Trees

Use recursion to write a function that duplicates the following function: (def (f L) (map (lambda (x) (+ (sqr x) x)) L))

Data Structures in Java

Friday Four Square! 4:15PM, Outside Gates

Module 8: Local and functional abstraction

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli

INF2220: algorithms and data structures Series 1

Fall, 2015 Prof. Jungkeun Park

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

Algorithms and Data Structures

Assignment: 7. Due: Language level: Allowed recursion:

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

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

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

Binary Trees. Height 1

CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local. CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 1

Maps; Binary Search Trees

CS115 - Module 9 - filter, map, and friends

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

Data Structures and Algorithms

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

CSCI Trees. Mark Redekopp David Kempe

IX. Binary Trees (Chapter 10)

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

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

CS115 - Module 4 - Compound data: structures

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

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

SFU CMPT Lecture: Week 9

CS 206 Introduction to Computer Science II

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

Programming Languages and Techniques (CIS120)

CSC148 Week 7. Larry Zhang

Lecture 16: Binary Search Trees

Binary Search Trees Treesort

Binary Trees and Binary Search Trees

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

Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

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

Week 2. CS 400 Programming III. Read: Module 2 readings before lecture

Lecture 34. Wednesday, April 6 CS 215 Fundamentals of Programming II - Lecture 34 1

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))

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


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

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

CSE 214 Computer Science II Introduction to Tree

Scheme Basics > (butfirst '(help!)) ()

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

Recall: Properties of B-Trees

2.2 Syntax Definition

V Advanced Data Structures

Tree Travsersals and BST Iterators

Computer Science II Fall 2009

Transcription:

Module 9: Binary trees Readings: HtDP, Section 14 We will cover the ideas in the text using different examples and different terminology. The readings are still important as an additional source of examples. CS 115 Fall 2018 9: Binary trees 1

Binary arithmetic expressions A binary arithmetic expression is made up of numbers joined by binary operations, +, /, and. ((2 6) + (5 2))/(5 3) can be defined in terms of two smaller binary arithmetic expressions, (2 6) + (5 2) and 5 3. Each smaller expression can be defined in terms of even smaller expressions. The smallest expressions are numbers. CS 115 Fall 2018 9: Binary trees 2

Visualizing binary arithmetic expressions ((2 6) + (5 2))/(5 3) can be represented as a tree: / + - * * 5 3 2 6 5 2 CS 115 Fall 2018 9: Binary trees 3

Tree terminology root 5 internal node parent 1 6 child leaves 14 8 20 siblings subtree CS 115 Fall 2018 9: Binary trees 4

Variations on trees Number of children of internal nodes: exactly two at most two any number Labels: on all nodes just on leaves Order of children (sometimes important) Tree structure (from data or for convenience) CS 115 Fall 2018 9: Binary trees 5

Representing binary arithmetic expressions Internal nodes each have exactly two children. Leaves have number labels. Internal nodes have symbol labels. For subtraction and division, we care about the order of children. The structure of the tree is dictated by the expression. How can we group together information for an internal node? How can we allow different definitions for leaves and internal nodes? CS 115 Fall 2018 9: Binary trees 6

(define-struct binode (op arg1 arg2)) ;; A Binary arithmetic expression Internal Node (BINode) ;; is a (make-binode (anyof * + / -) BinExp BinExp) ;; A Binary arithmetic expression (BinExp) is one of: ;; * a Num ;; * a BINode ;; Examples 5 (make-binode 2 6) (make-binode + 2 (make-binode 5 3)) CS 115 Fall 2018 9: Binary trees 7

A more complex example: (make-binode / (make-binode + (make-binode 2 6) (make-binode 5 2)) (make-binode 5 3)) CS 115 Fall 2018 9: Binary trees 8

Template for binary arithmetic expressions The only new idea in forming the template is the application of the recursive function to each piece that satisfies the data definition. ;; binexp-template: BinExp Any (define (binexp-template ex) (cond [(number? ex)... ] [else (... (binode-op ex)... (binexp-template (binode-arg1 ex))... (binexp-template (binode-arg2 ex))... )])) CS 115 Fall 2018 9: Binary trees 9

Evaluating a binary arithmetic expression Exercise: Develop a function eval that consumes a BinExp and produces its simplified numeric value. Use the template. ;; (eval ex) produces the simplified value of ex ;; eval: BinExp Num ;; Requires: no division by 0 ;; Examples: (check-expect (eval 5) 5) (check-expect (eval (make-binode + 2 (make-binode 5 3))) 4) (define (eval ex)... ) CS 115 Fall 2018 9: Binary trees 10

Our next example is also a binary tree (at most two children for each node), but differs from expression trees in several important ways: Internal nodes can have one child or two. The order of children always matters. The tree structure does not come from the data - we order it. We use trees to try to provide a more efficient implementation of dictionaries. CS 115 Fall 2018 9: Binary trees 11

Dictionaries revisited Recall from Module 6 that a dictionary stores a set of (key, value) pairs, with at most one occurrence of any key. It supports lookup, add, and remove operations. We implemented a dictionary as an association list of two-element lists. This implementation had the problem that a search could require looking through the entire list, which will be inefficient for large dictionaries. CS 115 Fall 2018 9: Binary trees 12

Binary search trees The new implementation uses a tree structure known as a binary search tree. The (key,value) pairs are stored at nodes of a tree. There is more than one possible tree. The placement of pairs in nodes may make it possible to improve the running time compared to association lists. We will start with a basic binary tree definition first. CS 115 Fall 2018 9: Binary trees 13

(define-struct node (key val left right)) ;; A Node is a (make-node Nat Str BT BT) ;; A binary tree (BT) is one of: ;; * empty ;; * (make-node Nat Str BT BT) What is the template? Note: empty still refers to the empty list empty. We will use the constant empty in our definitions. However, remember that we are working with tress, not lists. In calculated values, empty may appear. CS 115 Fall 2018 9: Binary trees 14

A BT template ;; bt-template: BT Any (define (bt-template t) (cond [(empty? t)... ] [else (... (node-key t)... (node-val t)... (bt-template (node-left t))... (bt-template (node-right t))... )])) CS 115 Fall 2018 9: Binary trees 15

Counting leaves in a BT How many leaves are in a BT t? An empty tree has 0 leaves. A tree containing only a leaf has 1. Otherwise, add together the number of leaves in the left subtree and the number of leaves in the right subtree. CS 115 Fall 2018 9: Binary trees 16

Solution ;; (count-leaves t) produces number of leaves in t ;; count-leaves: BT Nat (define (count-leaves t) (cond [(empty? t) 0] [(and (empty? (node-left t)) (empty? (node-right t))) 1] [else (+ (count-leaves (node-left t)) (count-leaves (node-right t)))])) CS 115 Fall 2018 9: Binary trees 17

So far, we just have a binary tree. Add conditions to define a binary search tree. (define-struct node (key val left right)) ;; A binary search tree (BST) is either ;; * empty, or ;; * (make-node Nat Str BST BST), ;; which satisfies the ordering property recursively: ;; * every key in left is less than key ;; * every key in right is greater than key CS 115 Fall 2018 9: Binary trees 18

A BST example (make-node 5 "Tony" (make-node 1 "Qiang" empty empty) (make-node 6 "Judy" empty (make-node 14 "Wole" empty empty))) CS 115 Fall 2018 9: Binary trees 19

Drawing BSTs 5 5 1 6 1 6 14 14 (Note: the value field is not usually represented in diagrams since the keys determine the shape of the tree.) CS 115 Fall 2018 9: Binary trees 20

We have made several minor changes from HtDP: We use empty instead of false for the base case. We will usually use the constant empty rather than false in our examples. We use key instead of ssn, and val instead of name. The value can be any Racket value. We can generalize to other key types with a comparison operation (e.g. strings, using string<?). CS 115 Fall 2018 9: Binary trees 21

There can be several different BST holding a particular set of (key, value) pairs. 5 5 1 6 1 14 14 6 CS 115 Fall 2018 9: Binary trees 22

A BST template ;; bst-template: BST Any (define (bst-template t) (cond [(empty? t)... ] [else (... (node-key t)... (node-val t)... (bst-template (node-left t))... (bst-template (node-right t))... )])) Note that this template is identical to the BT template. CS 115 Fall 2018 9: Binary trees 23

Counting values in a BST Some uses of the BST template do not make use of the ordering property (e.g. counting values equal to v). ;; count-values: BST Str Nat (define (count-values abst v) (cond [(empty? abst) 0] [else (+ (cond [(equal? v (node-val abst)) 1] [else 0]) (count-values (node-left abst) v) (count-values (node-right abst) v))])) CS 115 Fall 2018 9: Binary trees 24

If we produce a new binary tree by adding 1 to every key of an existing BST, the result still has the ordering property and so is a BST. ;; increment: BST BST (define (increment t) (cond [(empty? t) empty] [else (make-node (add1 (node-key t)) (node-val t) (increment (node-left t)) (increment (node-right t)))])) CS 115 Fall 2018 9: Binary trees 25

Retrieving all keys in a BST The following function creates a list of all keys in a BST. (define (all-keys t) (cond [(empty? t) empty] [else (append (all-keys (node-left t)) (list (node-key t)) (all-keys (node-right t)))])) What is special about the order of the produced list? CS 115 Fall 2018 9: Binary trees 26

Making use of the ordering property Main advantage: for certain computations, one of the recursive calls in the template can always be avoided. This is more efficient (sometimes considerably so). In the following slides, we will demonstrate this advantage for searching and adding. We will write the code for searching, and briefly sketch adding, leaving you to write the Racket code. CS 115 Fall 2018 9: Binary trees 27

Searching in a BST How do we search for a key n in a BST? We reason using the data definition of bst. If the BST is empty, then n is not in the BST. If the BST is of the form (make-node k v l r), and k equals n, then we have found it. Otherwise it might be in either of the trees l, r, and we can determine which one by comparing the values of k and n. CS 115 Fall 2018 9: Binary trees 28

If k > n, then n cannot be in r, and we only need to recursively search in l. If k < n, then n cannot be in l, and we only need to recursively search in r. Either way, we save one recursive call. CS 115 Fall 2018 9: Binary trees 29

;; (search-bst n t) produces the value associated with n in t, ;; or false if n is not in t. ;; search-bst: Nat BST (anyof Str false) (define (search-bst n t) (cond [(empty? t) false] [(= n (node-key t)) (node-val t)] [(< n (node-key t)) (search-bst n (node-left t))] [(> n (node-key t)) (search-bst n (node-right t))])) CS 115 Fall 2018 9: Binary trees 30

Creating a BST How do we create a BST from a list of keys? We reason using the data definition of a list. If the list is empty, the BST is empty. If the list is of the form (cons (list k v) lst), we add the pair (k, v) to the BST created from the list lst. CS 115 Fall 2018 9: Binary trees 31

Adding to a BST How do we add a pair (k, v) to a BST bstree? If bstree is empty, then the result is a BST with only one node. Otherwise bstree is of the form (make-node n w l r). If k = n, we form the tree with k, v, l, and r (we replace the old value, w, by v). If k < n, then the pair must be added to l, and if k > n, then the pair must be added to r. Again, we need only make one recursive call. CS 115 Fall 2018 9: Binary trees 32

Binary search trees in practice If the BST has all left subtrees empty, it looks and behaves like a sorted association list, and the advantage is lost. In later courses, you will see ways to keep a BST balanced so that most nodes have nonempty left and right children. By that time you will better understand how to analyze the efficiency of algorithms and operations on data structures. CS 115 Fall 2018 9: Binary trees 33

Goals of this module You should be familiar with tree terminology. You should understand the data definitions for binary arithmetic expressions, binary trees, and binary search trees, understand how the templates are derived from those definitions, and how to use the templates to write functions that consume those types of data. You should understand the definition of a binary search tree, and how it can be generalized to hold additional data. CS 115 Fall 2018 9: Binary trees 34

You should be able to write functions which consume binary search trees, including those sketched (but not developed fully) in lecture. You should be able to develop and use templates for other binary trees, not necessarily presented in lecture. CS 115 Fall 2018 9: Binary trees 35