CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees

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

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
CSE 326: Data Structures Lecture #7 Binary Search Trees Alon Halevy Spring Quarter 2001.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
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:
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
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.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
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.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees continued Aaron Bauer Winter 2014 CSE373: Data Structures & Algorithms1.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
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.
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.
CSE332: Data Abstractions Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Linda Shapiro Winter 2015.
Non Linear Data Structure
Instructor: Lilian de Greef Quarter: Summer 2017
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 Lecture 5: Dictionary ADTs; Binary Trees Linda Shapiro Spring 2016.
CSE373: Data Structures & Algorithms
Binary Search Trees.
Binary Search Tree (BST)
CSE373: Data Structures & Algorithms
October 30th – Priority QUeues
Lecture 22 Binary Search Trees Chapter 10 of textbook
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Hunter Zahn Summer 2016 Summer 2016.
Trees.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Binary Search Trees Why this is a useful data structure. Terminology
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
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
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Find in a linked list? first last 7  4  3  8 NULL
CSE 332: Data Abstractions Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Binary Trees, Binary Search Trees
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
CSE 326: Data Structures Lecture #7 Binary Search Trees
Richard Anderson Spring 2016
Mark Redekopp David Kempe
B-Trees.
Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Linda Shapiro Spring 2016

CSE373: Data Structures & Algorithms Announcements HW2 due start of class Wednesday April 13 on paper. Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Previously Dictionary ADT stores (key, value) pairs find, insert, delete Trees Terminology Binary Trees Spring 2016 CSE373: Data Structures & Algorithms

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 Spring 2016 CSE373: Data Structures & Algorithms

Example Tree Calculations Recall: Height of a tree is the maximum number of edges from the root to a leaf. Height = 4 A E B D F C G I H L J M K N What is the height of this tree? A A Height = 0 Height = 1 B What is the depth of node G? Depth = 2 What is the depth of node L? Depth = 4 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms 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 What is full? Every node has 0 or 2 children. Spring 2016 CSE373: Data Structures & Algorithms

11/13/2018 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 (an expression tree) + * 2 4 5 + * 2 4 5 2 * 4 + 5 2 4 * 5 + Spring 2016 CSE373: Data Structures & Algorithms

More on traversals void inOrderTraversal(Node t){ if(t != null) { 11/13/2018 More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B C D E F G = current node = processing (on the call stack) = completed node = element has been processed A ✓ Spring 2016 CSE373: Data Structures & Algorithms

More on traversals void inOrderTraversal(Node t){ if(t != null) { 11/13/2018 More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B C D E F G = current node = processing (on the call stack) = completed node = element has been processed A ✓ Spring 2016 CSE373: Data Structures & Algorithms

More on traversals void inOrderTraversal(Node t){ if(t != null) { 11/13/2018 More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B C D E F G = current node = processing (on the call stack) = completed node = element has been processed A ✓ Spring 2016 CSE373: Data Structures & Algorithms

More on traversals D void inOrderTraversal(Node t){ if(t != null) { 11/13/2018 More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B C ✓ D E F G = current node = processing (on the call stack) = completed node = element has been processed A ✓ D Spring 2016 CSE373: Data Structures & Algorithms

More on traversals D B void inOrderTraversal(Node t){ if(t != null) { 11/13/2018 More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A ✓ B C ✓ D E F G = current node = processing (on the call stack) = completed node = element has been processed A ✓ D B Spring 2016 CSE373: Data Structures & Algorithms

More on traversals D B E void inOrderTraversal(Node t){ 11/13/2018 More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A ✓ B C ✓ ✓ D E F G = current node = processing (on the call stack) = completed node = element has been processed A ✓ D B E Spring 2016 CSE373: Data Structures & Algorithms

More on traversals D B E A ✓ void inOrderTraversal(Node t){ 11/13/2018 More on traversals ✓ void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A ✓ B C ✓ ✓ D E F G = current node = processing (on the call stack) = completed node = element has been processed A ✓ D B E A Spring 2016 CSE373: Data Structures & Algorithms

More on traversals D B E A ✓ void inOrderTraversal(Node t){ 11/13/2018 More on traversals ✓ void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A ✓ B C ✓ ✓ D E F G = current node = processing (on the call stack) = completed node = element has been processed A ✓ D B E A Spring 2016 CSE373: Data Structures & Algorithms

More on traversals D B E A F C ✓ void inOrderTraversal(Node t){ 11/13/2018 More on traversals ✓ void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A ✓ ✓ B C ✓ ✓ ✓ D E F G = current node = processing (on the call stack) = completed node = element has been processed A ✓ D B E A F C Spring 2016 CSE373: Data Structures & Algorithms

More on traversals D B E A F C G ✓ void inOrderTraversal(Node t){ 11/13/2018 More on traversals ✓ void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A ✓ ✓ B C ✓ ✓ ✓ ✓ D E F G = current node = processing (on the call stack) = completed node = element has been processed A ✓ D B E A F C G Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms 11/13/2018 More on traversals ✓ void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A A ✓ ✓ B B C C ✓ ✓ ✓ ✓ D D E E F F G G Sometimes order doesn’t matter Example: sum all elements Sometimes order matters Example: evaluate an expression tree Spring 2016 CSE373: Data Structures & Algorithms

Binary Search Tree (BST) Data Structure 11/13/2018 Binary Search Tree (BST) Data Structure Structure property (binary tree) Each node has  2 children Result: keeps operations simple Order property All keys in left subtree smaller than node’s key All keys in right subtree larger than node’s key Result: easy to find any given key 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!) Spring 2016 CSE373: Data Structures & Algorithms

All children must obey order 11/13/2018 Are these BSTs? Activity! What nodes violate the BST properties? 3 11 7 1 8 4 5 4 18 10 6 2 11 5 8 20 21 7 15 All children must obey order Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms 11/13/2018 Find in BST, Recursive 12 Data find(Key key, Node root){ if(root == null) return null; if(key < root.key) return find(key,root.left); if(key > root.key) return find(key,root.right); return root.data; } 5 15 2 9 20 7 10 17 30 This should look a _lot_ like binary search! How long does it take? Log n is an easy answer, but what if the tree is very lopsided? So really, this is worst case O(n)! A better answer is theta of the depth of the node sought. If we can bound the depth of that node, we can bound the length of time a search takes. What is the time complexity? Worst case. Worst case running time is O(n). - Happens if the tree is very lopsided (e.g. list) 1 2 3 4 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms 11/13/2018 Find in BST, Iterative Let’s look for 16. 12 Data find(Key key, Node root){ while(root != null && root.key != key) { if(key < root.key) root = root.left; else(key > root.key) root = root.right; } if(root == null) return null; return root.data; 5 15 2 9 20 7 10 17 30 This should look a _lot_ like binary search! How long does it take? Log n is an easy answer, but what if the tree is very lopsided? So really, this is worst case O(n)! A better answer is theta of the depth of the node sought. If we can bound the depth of that node, we can bound the length of time a search takes. Worst case running time is O(n). - Happens if the tree is very lopsided (e.g. list) Spring 2016 CSE373: Data Structures & Algorithms

Bonus: Other BST “Finding” Operations 11/13/2018 Bonus: Other BST “Finding” Operations FindMin: Find minimum node Left-most node FindMax: Find maximum node Right-most node 12 5 15 2 9 20 7 10 17 30 How would we implement? Spring 2016 CSE373: Data Structures & Algorithms

Bonus: Other BST “Finding” Operations 11/13/2018 Bonus: Other BST “Finding” Operations FindMin: Find minimum node Left-most node 12 5 15 Node FindMin(Node root){ if(root == null) return null; if(root.left==null) return root; return FindMin(root.left); } 2 9 20 7 10 17 30 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms 11/13/2018 Insert in BST Find the right spot and hook on a new node. 12 insert(13) insert(8) insert(31) 5 15 2 9 13 20 (New) insertions happen only at leaves – easy! 7 10 17 30 8 31 Again… worst case running time is O(n), which may happen if the tree is not balanced. Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms 11/13/2018 Deletion in BST 12 5 15 2 9 20 7 10 17 30 Why might deletion be harder than insertion? Removing an item may disrupt the tree structure! Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms 11/13/2018 Deletion in BST Basic idea: find the node to be removed, then “fix” the tree so that it is still a binary search tree Three potential cases to fix: Node has no children (leaf) Node has one child Node has two children Spring 2016 CSE373: Data Structures & Algorithms

Deletion – The Leaf Case 11/13/2018 Deletion – The Leaf Case delete(17) 12 5 15 2 9 20 7 10 17 30 Spring 2016 CSE373: Data Structures & Algorithms

Deletion – The One Child Case 11/13/2018 Deletion – The One Child Case delete(15) 12 15 5 2 9 20 7 10 30 Spring 2016 CSE373: Data Structures & Algorithms

Deletion – The One Child Case 11/13/2018 Deletion – The One Child Case delete(15) 12 5 2 9 20 7 10 30 Spring 2016 CSE373: Data Structures & Algorithms

Deletion – The Two Child Case 11/13/2018 Deletion – The Two Child Case 12 delete(5) 5 20 2 9 30 4 7 10 A value guaranteed to be between the two subtrees! succ from right subtree - pred from left subtree 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. largest value on its left smallest value on its right What can we replace 5 with? Spring 2016 CSE373: Data Structures & Algorithms

Deletion – The Two Child Case 11/13/2018 Deletion – The Two Child Case Idea: Replace the deleted node with a value guaranteed to be between the two child subtrees Options: successor minimum node from right subtree findMin(node.right)* the text does this predecessor maximum node from left subtree findMax(node.left) Now delete the original node containing successor or predecessor Spring 2016 CSE373: Data Structures & Algorithms

Deletion: The Two Child Case (example) 12 delete(23) 5 23 2 9 18 30 7 10 15 19 25 32 Spring 2016 CSE373: Data Structures & Algorithms

Deletion: The Two Child Case (example) 12 delete(23) 5 23 2 9 18 30 7 10 15 19 25 32 Spring 2016 CSE373: Data Structures & Algorithms

Deletion: The Two Child Case (example) 12 delete(23) 5 25 2 9 18 30 7 10 15 19 25 32 Spring 2016 CSE373: Data Structures & Algorithms

Deletion: The Two Child Case (example) 12 delete(23) 5 25 2 9 18 30 7 10 15 19 32 Success!  Spring 2016 CSE373: Data Structures & Algorithms

Deletion: The Two Child Case (exercise) 12 delete(12) 5 25 2 9 18 30 7 10 15 19 32 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms 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? Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms 11/13/2018 BuildTree for BST Let’s consider buildTree Insert all, starting from an empty tree Insert keys 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 this kind of sorted input? 1 + 2 + 3 + . . . + n = n(n+1)/2 Is inserting in the reverse order any better? 1 2 3 Θ(n2) OK, we had a buildHeap, let’s buildTree. How long does this take? Well, IT DEPENDS! Let’s say we want to build a tree from 123456789 What happens if we insert in order? Reverse order? What about 5, then 3, then 7, then 2, then 1, then 6, then 8, then 9? O(n2) Not a happy place Spring 2016 CSE373: Data Structures & Algorithms Θ(n2)

O(n log n), definitely better 11/13/2018 BuildTree for BST Insert keys 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST What if we 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? So the order the values come in is important! 8 4 2 7 3 5 9 6 1 OK, we had a buildHeap, let’s buildTree. How long does this take? Well, IT DEPENDS! Let’s say we want to build a tree from 123456789 What happens if we insert in order? Reverse order? What about 5, then 3, then 7, then 2, then 1, then 6, then 8, then 9? O(n log n), definitely better 5, 3, 7, 2, 1, 6, 8, 9 better: n log n Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Exercise Build a binary search tree from the following ordered input. If you get a duplicate, just ignore it as already there. The month of your birthday. The day of your birthday. The number of siblings you have. The number of courses you are taking. Your age. Your weight divided by 10 rounded down. The rightmost digit of your social security number or student number. 8. The hour that your last class on Mondays ends. What is the height of your tree? Spring 2016 CSE373: Data Structures & Algorithms

Complexity of Building a Binary Search Tree Worst case: O(n2) Best case: O(n log n) We do better by keeping the tree balanced. How are we going to do that? Spring 2016 CSE373: Data Structures & Algorithms