CSC148 Week 7. Larry Zhang

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

Lecture 23: Binary Search Trees

COMP 250. Lecture 22. binary search trees. Oct. 30, 2017

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

Announcements. Problem Set 2 is out today! Due Tuesday (Oct 13) More challenging so start early!

CS102 Binary Search Trees

CSI33 Data Structures

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

Lecture 6: Analysis of Algorithms (CS )

Binary Search Trees. Analysis of Algorithms

Trees. Eric McCreath

TREES Lecture 12 CS2110 Spring 2019

Algorithms. AVL Tree

Balanced Binary Search Trees. Victor Gao

Programming Languages and Techniques (CIS120)

CS 380 ALGORITHM DESIGN AND ANALYSIS

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

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Priority Queues Heaps Heapsort

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

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

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

CS350: Data Structures Binary Search Trees

Lecture 7. Binary Search Trees and Red-Black Trees

CSE 100 Advanced Data Structures

Data Structures in Java

BST Deletion. First, we need to find the value which is easy because we can just use the method we developed for BST_Search.

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

CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2

Binary Trees and Binary Search Trees

Lecture: Analysis of Algorithms (CS )

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

COMP Analysis of Algorithms & Data Structures

Self-Balancing Search Trees. Chapter 11

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

Balanced Binary Search Trees

Search Trees. Undirected graph Directed graph Tree Binary search tree

CS 350 : Data Structures Binary Search Trees

Friday Four Square! 4:15PM, Outside Gates

Data Structures and Algorithms

Search Trees. Data and File Structures Laboratory. DFS Lab (ISI) Search Trees 1 / 17

TREES. Trees - Introduction

COMP Analysis of Algorithms & Data Structures

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

Balanced Search Trees. CS 3110 Fall 2010

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

Search Trees: BSTs and B-Trees

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

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

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

Balanced search trees

Lecture 6. Binary Search Trees and Red-Black Trees

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

INF2220: algorithms and data structures Series 1

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CSCI2100B Data Structures Trees

CS350: Data Structures Red-Black Trees

CISC 235: Topic 4. Balanced Binary Search Trees

Programming Languages and Techniques (CIS120)

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

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

Data Structures Lesson 7

CSC148 Week 6. Larry Zhang

CSE 326: Data Structures B-Trees and B+ Trees

ADVANCED DATA STRUCTURES STUDY NOTES. The left subtree of each node contains values that are smaller than the value in the given node.

Sorting and Selection

8. Binary Search Tree

CSI33 Data Structures

Lecture 3 February 9, 2010

Recall: Properties of B-Trees

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

Programming Abstractions

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

CMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011

Fall, 2015 Prof. Jungkeun Park

We assume uniform hashing (UH):

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

CS350: Data Structures B-Trees

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

Cpt S 122 Data Structures. Data Structures Trees

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE

Binary Search Tree. Revised based on textbook author s notes.

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

CSC236 Week 5. Larry Zhang

Lecture 16: Binary Search Trees

Part 2: Balanced Trees

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

Binary Trees, Binary Search Trees

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

CS : Data Structures

CS Transform-and-Conquer

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 16 Treaps; Augmented BSTs

Spring 2018 Mentoring 8: March 14, Binary Trees

Outline. Binary Tree

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

Module 9: Binary trees

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Section 4 SOLUTION: AVL Trees & B-Trees

Data Structure - Advanced Topics in Tree -

CHAPTER 10 AVL TREES. 3 8 z 4

Transcription:

CSC148 Week 7 Larry Zhang 1

Announcements Test 1 can be picked up in DH-3008 A1 due this Saturday Next week is reading week no lecture, no labs no office hours 2

Recap Last week, learned about binary trees and how to represent binary trees in our code: list of lists nodes and references However, not all trees are binary, the branching factor could be 3, 4, etc. How do we handle arbitrary ( n-ary ) trees in our code? 3

N-ary trees We will use the nodes and references representation Each node keeping left and right references is not going to work anymore Instead, each nodes keeps a list of children. 4

Draw the tree t1 t1 = Tree(4, [Tree(2)]) 4 4 4 2 2 2 All of these are correct, and equivalent. There is no left or right locations defined for n-ary trees, there is only the order of the children. 5

DEMO write some functions for n-ary trees tree.py 6

Takeaway All these methods are naturally implemented using recursion because of the recursive structure of trees. 7

Binary Search Tree (BST) 8

Motivating Binary Search Tree Binary trees, or generally trees, are good for modelling hierarchical structure in real world. But they could be much more useful than just that, if we add some more feature to the binary tree structure. Binary Search Trees are a subset of Binary Trees. They are binary trees with some extra constraint. Useful for efficient searching and sorting. 9

What is a BST? A Binary Search Tree (BST) is a binary tree that has the BST Property For every node x in the tree All nodes in the left subtree have smaller values than x < > All nodes in the right subtree have larger values than x 10

BST or NOT? 65 65 40 80 40 80 33 60 82 33 60 82 41 66 41 64 11

Are these BSTs? 12

Searching Efficiently in a BST Given a BST, we want to determine whether a value v exists in the BST. How do we do this efficiently, i.e., compare with as few nodes as possible? 40 65 80 Observation: Say we are searching for 64, since it is smaller than the root 65, according to the BST property, 64 must be in the left subtree. There is no need to check the right subtree. 33 60 82 41 64 13

BST Search The algorithm: We compare v to the value r at the root If v = r, then the value is found and we are done If v < r, we proceed down the left subtree and repeat the process If v > r, we proceed down the right subtree and repeat the process if we go off the tree, then the value is NOT in the BST. def find(root, k): if root is None or k == root.value: return root if k < root.value: return find(root.left, k) else: return find(root.right, k) 14

Efficiency of BST Search Searching in a BST can be much more efficient than searching in a linear structure. When searching in a linear list, in the worst case, how many comparisons do we need to make to find a value? ~ total number of nodes When searching in a BST, in the worst case, how many comparisons do we need to make to find a value? ~ the height of the tree What s the worst possible height of the tree? 15

Bad BST, Good BST BST search in this BST is as slow as searching in a linear list. This balanced BST is good because its height is very small compared to its size. How small exactly? 16

Minimum-Height Binary Tree Given that a binary tree has 7 nodes, what is minimum possible height it can have? What does the tree look like? How about a binary tree with n nodes? 17

Complete Binary Trees To minimize height, we fill each position on each successive level before we create a new level. A complete binary tree with n nodes is a binary tree such that every level is full, except possibly the bottom level which is filled in left to right. A complete binary tree has the minimum height of any binary tree with n nodes. This minimum height satisfies 18

Proof sketch: We ll prove the equivalent statement: n >= 2^h The smallest possible complete binary tree of height h is one with levels 0 to h-1 filled, plus a single node on level h. The number of nodes in this tree is QED 19

You can also prove that the height of a complete binary tree satisfies that h log₂(n+1) - 1 20

BST Search Efficiency If the BST is balanced like the complete binary tree, the BST search algorithm can finish within log(n) steps. This is a big improvement compared to linear search (which takes roughly n steps). log2(1,000,000) = 19.93 log2(1,000,000,000,000) = 39.86 There are many kinds of balanced binary search trees that have log(n) height. You will learn more in later courses: AVL tree, Red-Black tree, 2-3 tree, AA tree, Scapegoat tree, Splay tree, Treap, For now, in CSC148, we ll stick to the regular BST that can unfortunately become very unbalanced! 21

Representation of a BST 22

Representing BST Previous, we have learned two ways of representing a binary tree list of lists nodes and references (object-oriented) For BST, we use a form of nodes and references. We ll use a BTNode class to represent a node in the BST We ll use a BST class to represent the tree itself The BST class has a _root attribute that is None when the BST is empty, or A reference to the root BTNode of the tree, otherwise (This is not the only way to represent BST) 23

24

Inserting into a BST 25

General assumption: Our BST does not keep duplicate values, therefore if the value/data you re inserting already exists in the tree, nothing needs to happen. 26

insert(root, data) insert a new node with data into the tree rooted at root if data already exists in the tree, do nothing returns the root of the resulting tree 27

Insert(root, data) 42 Go down, left and right like what we do in BST_Search 40 65 80 When next position is None, insert there 33 60 82 If find node with equal data, do nothing. 43 64 42 62 28

Exercise 81 65 64 40 80 33 60 82 43 61 81 62 64 29

Ex 2: Insert sequence into an empty tree Insert sequence 1: 11, 5, 13, 14, 12, 3, 6 Insert sequence 2: 3, 5, 6, 11, 14, 13, 12 11 3 5 5 13 6 11 3 6 12 14 14 Different insert sequences result in different shapes (heights) of the BST. 12 13 30

Insert: code General assumption: Our BST does not keep duplicate values, therefore if the value/data you re inserting already exists in the tree, nothing needs to happen. 31

TreeMaximum(x) Return the node with the maximum value of the subtree rooted at x 32

TreeMaximum(root) start from root keep going to the right, until cannot go anymore return that final node 40 65 80 33 60 82 41 64 33

TreeMaximum code Do NOT assume that the solution always has to be recursive! Recursive VS Iterative implementation 34

Deleting from a BST 35

Delete(root, data) if exists, delete the node with data; otherwise, do nothing returns the root of the resulting tree 36

Deletion is a bit tricky The positions of some nodes in the tree need to be rearranged after the deletion, while the BST property must be maintained. 40 65 80 33 60 82 43 64 62 37

delete node x: organized into 3 cases Assume that we have found the node x with data. Case 1: x has no child Case 2: x has one child Case 3: x has two children Easy Easy Less easy 38

Case 1: x has no child Just delete it, nothing else need to be changed. 40 65 80 node60.left = None 33 60 82 43 64 62 39

Case 2: x has one child 65 Some other node need to replace the deleted node. 40 80 33 55 82 then x s only child will replace x. 58 node40.right = node58 57 62 40

Case 2: x has one child 65 Some other node need to replace the deleted node. 40 80 33 58 82 then x s only child will replace x. node40.right = node58 57 62 41

Case 3: x has two children Delete x x A node y should replace x as the root of the subtree, such that L < y < R (BST property maintained) Who should be y? L y R y the maximum of L, i.e., the predecessor of x L < y because y is it s maximum, y < R because of R s on the right 42

Case 3: x has two children So y should replace x as the root of this tree, and x should be gone. x How to make that happen? L y R 43

Case 3: x has two children Step 1: overwrite x with y s data. xy Step 2: recursively, delete y s data from L. L y R 44

DEMO: Code for Delete in bst.py 45