Module 8: Binary trees

Similar documents
Module 9: Binary trees

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

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

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

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

CS115 - Module 10 - General Trees

Module 10: General trees

Data Structures and Algorithms

Chapter 20: Binary Trees

Working with recursion

Programming II (CS300)

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

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

Graphs. Directed graphs. Readings: Section 28

Binary Trees

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

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

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

CS F-11 B-Trees 1

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

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.

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

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

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.

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

CS 115 Lecture Notes Winter 2019

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

TREES. Trees - Introduction

Trees. Eric McCreath

8. Binary Search Tree

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

Algorithms. Deleting from Red-Black Trees B-Trees

Problem 1: Binary Trees: Flatten

Binary Trees, Binary Search Trees

Module 8: Local and functional abstraction

INF2220: algorithms and data structures Series 1

Data Structures in Java

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

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

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

Binary Trees. Height 1

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

Maps; Binary Search Trees

Data Structures and Algorithms

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

IX. Binary Trees (Chapter 10)

Friday Four Square! 4:15PM, Outside Gates

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

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

Fall, 2015 Prof. Jungkeun Park

CSC148-Section:L0301

SFU CMPT Lecture: Week 9

CS 206 Introduction to Computer Science II

Programming Languages and Techniques (CIS120)

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

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

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

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

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

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

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

CSCI Trees. Mark Redekopp David Kempe

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

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

Module 4: Dictionaries and Balanced Search Trees

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

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

Tree Travsersals and BST Iterators

Lecture 16: Binary Search Trees

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

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

Binary Trees and Binary Search Trees

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

Binary Search Trees. Analysis of Algorithms

CSC148 Week 7. Larry Zhang

Chapter 5. Binary Trees

Associate Professor Dr. Raed Ibraheem Hamed

CS 206 Introduction to Computer Science II

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

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

INF2220: algorithms and data structures Series 1

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

March 20/2003 Jayakanth Srinivasan,

CSE 100 Advanced Data Structures

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

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

Extra: B+ Trees. Motivations. Differences between BST and B+ 10/27/2017. CS1: Java Programming Colorado State University

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

Transcription:

Module 8: 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 Winter 2018 8: 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 Winter 2018 8: 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 Winter 2018 8: Binary trees 3

Tree terminology root 5 internal node parent 1 6 child leaves 14 8 20 siblings subtree CS 115 Winter 2018 8: 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 Winter 2018 8: 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 Winter 2018 8: 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 Winter 2018 8: 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 Winter 2018 8: 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 Winter 2018 8: Binary trees 9

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 Winter 2018 8: Binary trees 10

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 Winter 2018 8: Binary trees 11

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 Winter 2018 8: Binary trees 12

(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, just to reinforce that we are working with trees, rather than lists. In calculated values, empty may appear. CS 115 Winter 2018 8: Binary trees 13

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 Winter 2018 8: Binary trees 14

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 Winter 2018 8: Binary trees 15

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 Winter 2018 8: Binary trees 16

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 Winter 2018 8: Binary trees 17

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 Winter 2018 8: Binary trees 18

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 Winter 2018 8: Binary trees 19

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 Winter 2018 8: Binary trees 20

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

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 Winter 2018 8: Binary trees 22

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 Winter 2018 8: Binary trees 23

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 Winter 2018 8: Binary trees 24

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 Winter 2018 8: Binary trees 25

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 Winter 2018 8: Binary trees 26

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 Winter 2018 8: Binary trees 27

;; (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 Winter 2018 8: Binary trees 28

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 Winter 2018 8: Binary trees 29

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 Winter 2018 8: Binary trees 30

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 Winter 2018 8: Binary trees 31

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 Winter 2018 8: Binary trees 32

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 Winter 2018 8: Binary trees 33