Instructor: Lilian de Greef Quarter: Summer 2017

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Dan Grossman Spring 2010.
CSE 326 Binary Search Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
CSE 326: Data Structures Lecture #7 Binary Search Trees Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Lecture #8 Binary Search Trees Alon Halevy Spring Quarter 2001.
CSE373: Data Structures & Algorithms Lecture 5: Dictionaries; Binary Search Trees Aaron Bauer Winter 2014.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Tyler Robison Summer
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees continued Aaron Bauer Winter 2014 CSE373: Data Structures & Algorithms1.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Binary Search Trees (BST)
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Dan Grossman Fall 2013.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Linda Shapiro Winter 2015.
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Lauren Milne Summer 2015.
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Kevin Quinn Fall 2015.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
CSE 373 Data Structures Lecture 7
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Linda Shapiro Winter 2015.
Binary Search Trees What is a binary search tree?
Non Linear Data Structure
Trees Chapter 15.
CSE 373 Binary search trees; tree height and balance
CSE 326: Data Structures Lecture #8 Pruning (and Pruning for the Lazy)
CSE373: Data Structures & Algorithms More Heaps; Dictionaries; Binary Search Trees Riley Porter Winter 2016 Winter 2017.
CSE373: Data Structures & Algorithms
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Trees.
Week 6 - Wednesday CS221.
Trees Lecture 12 CS2110 – Fall 2017.
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Hunter Zahn Summer 2016 Summer 2016.
Trees.
Data Structures & Algorithm Design
Week 11 - Friday CS221.
CSE 373 Data Structures Lecture 7
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
Instructor: Lilian de Greef Quarter: Summer 2017
Find in a linked list? first last 7  4  3  8 NULL
CSE 373: Data Structures and Algorithms
CSE 332: Data Abstractions Binary Search Trees
Binary Trees, Binary Search Trees
CSE 373 Data Structures and Algorithms
Announcements Prelim 1 on Tuesday! A4 will be posted today
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
CSE 326: Data Structures Lecture #8 Balanced Dendrology
Trees.
Binary Trees, Binary Search Trees
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Instructor: Lilian de Greef Quarter: Summer 2017 CSE 373: Data Structures and Algorithms Lecture 9: Binary Search Trees Instructor: Lilian de Greef Quarter: Summer 2017

Today Announcements Binary Trees Binary Search Trees Height Traversals Definition find insert delete buildTree

Announcements Change to office hours for just this week Tuesday’s “office” office hours / private office hours 12:00pm – 12:30pm (not at 1:30pm!) Dorothy and I trading 2:00pm - 3:00pm office hours this week Same time and location Homework 1 Statistics Mean: 39.7/50 (+1 extra credit) Median: 42.5/50 (+0 extra credit) Max: 49/50 (+1) or 47/50 (+4) Standard Deviation: 10.18

Reminder: Tree terminology Node / Vertex A E B D F C G I H L J M K N Root Left subtree Right subtree Edges Leaves

Binary Trees Binary tree: Each node has at most 2 children (branching factor 2) Binary tree is A root (with data) A left subtree (may be empty) A right subtree (may be empty) Special Cases:

(Last week’s practice) What does the following method do? int height(Node root){ if (root == null), return -1; return 1 + max(height(root.left), height(root.right); } It calculates the number of nodes in the tree. It calculates the depth of the nodes. It calculates the height of the tree. It calculates the number of leaves in the tree.

Binary Trees: Some Numbers Recall: height of a tree = longest path from root to leaf (count edges) For binary tree of height h: max # of leaves: max # of nodes: min # of leaves: min # of nodes: For n nodes, the min height (best-case) is the max height (worst-case) is

Tree Traversals A traversal is an order for visiting all the nodes of a tree Pre-order: root, left subtree, right subtree In-order: left subtree, root, right subtree Post-order: left subtree, right subtree, root A B C D E F G

Tree Traversals: Practice Which one makes sense for evaluating this expression tree? Pre-order: root, left subtree, right subtree In-order: left subtree, root, right subtree Post-order: left subtree, right subtree, root + * 2 4 5

Binary Search Tree (BST) Data Structure 5/13/2018 Binary Search Tree (BST) Data Structure Structure property (binary tree) Each node has  2 children Result: keeps operations simple Order property Result: straight-forward to find any given value 4 12 10 6 2 11 5 8 14 13 7 9 A binary search tree is a type of binary tree (but not all binary trees are binary search trees!)

Practice: are these BSTs? 5/13/2018 Practice: are these BSTs? 3 11 7 1 8 4 5 4 18 10 6 2 11 5 8 20 21 7 15 All children must obey order

How do we find(value) in BST’s? 20 9 2 15 5 12 30 7 17 10

find in BST: Recursive Version Data find(Data value, Node root){ if(root == null) return null; if(key < root.value) return find(value, root.left); if(key > root.value) return find(value, root.right); return root.value; } 20 9 2 15 5 12 30 7 17 10 What is the running time?

find in BST: Iterative Version 20 9 2 15 5 12 30 7 17 10 Data find(Object value, Node root){ while(root != null && root.value != value) { if (value < root.value) root = root.left; else (value > root.value) root = root.right; } if(root == null) return null; return root.value;

Other BST “Finding” Operations findMin: Find minimum node findMax: Find maximum node 20 9 2 15 5 12 30 7 17 10

insert in BST insert(13) insert(8) insert(31) Worst-case running time: 20 9 2 15 5 12 30 7 17 10

Practice with insert, primer for delete Start with an empty tree. Insert the following values, in the given order: 14, 2, 5, 20, 42, 1, 4, 16 Then, changing as few nodes as possible, delete the following in order: 42, 14 What would the root of the resulting tree be? 2 4 5 16

(Extra space for scratch work / notes)

delete in BST Why might delete be harder than insert? Basic idea: Three potential cases to fix:

delete case: Leaf 20 9 2 15 5 12 30 7 17 10 delete(17)

delete case: One Child 20 9 2 15 5 12 30 7 10 delete(15)

delete case: Two Children 20 9 2 5 12 30 7 10 delete(5) What can we replace 5 with? Ah, now the hard case. How do we delete a two child node? We remove it and replace it with what? It has all these left and right children that need to be greater and less than the new value (respectively). Is there any value that is guaranteed to be between the two subtrees? Two of them: the successor and predecessor! So, let’s just replace the node’s value with it’s successor and then delete the succ.

delete case: Two Children What can we replace the node with? Options: Options: successor minimum node from right subtree findMin(node.right) predecessor maximum node from left subtree findMax(node.left)

delete case: Two Children (example #2) 30 9 2 23 5 12 7 10 18 19 15 32 25 Ah, now the hard case. How do we delete a two child node? We remove it and replace it with what? It has all these left and right children that need to be greater and less than the new value (respectively). Is there any value that is guaranteed to be between the two subtrees? Two of them: the successor and predecessor! So, let’s just replace the node’s value with it’s successor and then delete the succ.

Practice with insert, primer for delete Revisited Changing as few nodes as possible, delete the following in order: 42, 14 42 5 1 20 2 14 4 16

delete through Lazy Deletion Lazy deletion can work well for a BST Simpler Can do “real deletions” later as a batch Some inserts can just “undelete” a tree node But Can waste space and slow down find operations Make some operations more complicated: e.g., findMin and findMax?

buildTree for BST Let’s consider buildTree (insert values starting from an empty tree) Insert values 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST If inserted in given order, what is the tree? What big-O runtime for buildTree on this sorted input? Is inserting in the reverse order any better?

buildTree for BST Insert values 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST What we if could somehow re-arrange them median first, then left median, right median, etc. 5, 3, 7, 2, 1, 4, 8, 6, 9 What tree does that give us? What big-O runtime?