CSE 332: Data Abstractions Binary Search Trees

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

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.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
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.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
CSE 326: Data Structures Lecture #7 Branching Out Steve Wolfman Winter Quarter 2000.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
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.
TCSS 342 BST v1.01 BST addElement To addElement(), we essentially do a find( ). When we reach a null pointer, we create a new node there. void addElement(Object.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
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
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
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 Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Tyler Robison Summer
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
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.
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 Priority Queues
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Linda Shapiro Winter 2015.
CSE373: Data Structures & Algorithms
Instructor: Lilian de Greef Quarter: Summer 2017
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 Lecture 5: Dictionary ADTs; Binary Trees Linda Shapiro Spring 2016.
UNIT III TREES.
Binary Search Trees.
October 30th – Priority QUeues
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Hunter Zahn Summer 2016 Summer 2016.
Hashing Exercises.
CSC 172– Data Structures and Algorithms
Cse 373 April 26th – Exam Review.
Binary Search Trees Why this is a useful data structure. Terminology
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Catie Baker Spring 2015.
7/23/2009 Many thanks to David Sun for some of the included slides!
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
CMSC 341: Data Structures Priority Queues – Binary Heaps
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
B-Tree Insertions, Intro to Heaps
CSE 373: Data Structures and Algorithms
Priority Queues (Chapter 6.6):
CMSC 202 Trees.
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
CSE 332: Data Abstractions AVL Trees
CSE 326: Data Structures Lecture #7 Dendrology
Binary Trees, Binary Search Trees
CSE 373 Data Structures and Algorithms
CSE 326: Data Structures Lecture #8 Balanced Dendrology
Binary SearchTrees [CLRS] – Chap 12.
CSE 326: Data Structures Lecture #7 Binary Search Trees
Richard Anderson Spring 2016
Priority Queues (Chapter 6):
CSE 373: Data Structures and Algorithms
Binary Trees, Binary Search Trees
Presentation transcript:

CSE 332: Data Abstractions Binary Search Trees 12/10/2018 CSE 332: Data Abstractions Binary Search Trees Richard Anderson Spring 2016

12/10/2018 Announcements

Fun with sums

ADTs Seen So Far Priority Queue Stack Insert Push DeleteMin Pop Queue 12/10/2018 ADTs Seen So Far Priority Queue Insert DeleteMin Stack Push Pop Queue Enqueue Dequeue None of these support “find” Alright, now we’re armed with the tree expertise we’ll need. So, what can we already do? Well, we can push, pop, enqueue, dequeue, insert, remove, find, and deleteMin. There’s something wrong with our pqueues. Remember decreaseKey? We need to give the location in the heap to perform a decrease. Why? Need pointer! Why? Because find not efficient.

The Dictionary ADT Data: Operations: a set of (key, value) pairs 12/10/2018 The Dictionary ADT seitz Steve Seitz CSE 592 anderson Richard Anderson CSE 582 kainby87 HyeIn Kim CSE 220 … Data: a set of (key, value) pairs Operations: Insert (key, value) Find (key) Remove (key) insert(seitz, ….) find(anderson) anderson Richard, Anderson,… Dictionaries associate some key with a value, just like a real dictionary (where the key is a word and the value is its definition). In this example, I’ve stored user-IDs associated with descriptions of their user. This is probably the most valuable and widely used ADT we’ll hit. The Dictionary ADT is also called the “Map ADT”

Time to move elements, can we mimic BinSearch with BST? 12/10/2018 Implementations insert find delete Unsorted Linked-list Unsorted array Sorted array Θ(1) Θ(n) Θ(n) Θ(1) Θ(n) Θ(n) LL: O(1), O(n), O(n) Uns: O(1), O(n), O(n) Sorted: O(n), O(log n), O(n) Sorted array is oh-so-close. O(log n) find time and almost O(log n) insert time. What’s wrong? Let’s look at how that search goes: Draw recursive calls (and potential recursive calls) in binary search. Note how it starts looking like a binary tree where the left subtrees have smaller elements and the right subtrees have bigger elements. What if we could store the whole thing in the structure this recursive search is building? log n + n Θ(log n) log n + n SO CLOSE! What limits the performance? Time to move elements, can we mimic BinSearch with BST?

Binary Trees Binary tree is Representation: a root 12/10/2018 Binary Trees Binary tree is a root left subtree (maybe empty) right subtree (maybe empty) Representation: A B C D E F Data right pointer left G H I J Alright, we’ll focus today on one type of trees called binary trees.

Binary Tree: Representation 12/10/2018 Binary Tree: Representation A right pointer left A B right pointer left C right pointer left B C D E F D right pointer left E right pointer left F right pointer left

12/10/2018 Tree Traversals A traversal is an order for visiting all the nodes of a tree Three types: Pre-order: Root, left subtree, right subtree In-order: Left subtree, root, right subtree Post-order: Left subtree, right subtree, root + * 2 4 5 (an expression tree) Note the use of expression trees in compiler construction. Also, post-fix, pre-fix, infix conversions

Inorder Traversal void traverse(BNode t){ if (t != NULL) 12/10/2018 Inorder Traversal void traverse(BNode t){ if (t != NULL) traverse (t.left); process t.element; traverse (t.right); }

Binary Tree: Special Cases 12/10/2018 Binary Tree: Special Cases A B D E C F A B C A B D E C G F I H A B D E C G F Complete Tree Perfect Tree “List” Tree Full Tree Note that a perfect tree has the most nodes “packed in” to a tree of height h.

Binary Tree: Some Numbers… 12/10/2018 Binary Tree: Some Numbers… Recall: height of a tree = longest path from root to leaf. For binary tree of height h: max # of leaves: max # of nodes: min # of leaves: min # of nodes: 2h, for perfect tree 2h+1 – 1, for perfect tree 1, for “list” tree h+1, for “list” tree Some tree calculations, for tree of height h: - Max number of leaves (perfect tree): 2^h - Max number of nodes (perfect tree): 2^(h+1) - 1 - Min number of nodes/leaves (degenerate tree): h-1/1 - What fraction of the tree is located in the last level of a perfect tree? 1/2 - Average depth for N nodes: sqrt(N) (We won’t go into this, but if you take N nodes and assume all distinct trees of the nodes are equally likely, you get an average depth of SQRT(N). Is that bigger or smaller than log n? Bigger, so it’s not good enough!)

Binary Search Tree Data Structure 12/10/2018 Binary Search Tree Data Structure Structural property each node has  2 children Order property all keys in left subtree smaller than root’s key all keys in right subtree larger than root’s key 8 5 11 2 6 10 12 4 7 9 14 A binary search tree is a binary tree in which all nodes in the left subtree of a node have lower values than the node. All nodes in the right subtree of a node have higher value than the node. It’s like making that recursion into the data structure! I’m storing integers at each node. Does everybody think that’s what I’m _really_ going to store? What do I need to know about what I store? (comparison, equality testing) 13

Example and Counter-Example 12/10/2018 Example and Counter-Example 5 8 4 8 5 11 1 7 11 2 7 6 10 18 3 4 All children must obey order 15 20 Why is the one on the left a BST? It’s not complete! (B/c BSTs don’t need to be complete) Why isn’t the one on the right a BST? Three children of 5 20 has a left child larger than it. What’s wrong with 11? Even though 15 isn’t a direct child, it _still_ needs to be less than 11! BINARY SEARCH TREES? 21

Find in BST, Recursive 12 5 15 2 9 20 7 10 17 30 Runtime: 12/10/2018 Find in BST, Recursive Node Find(Object key, Node root) { if (root == NULL) return NULL; if (key < root.key) return Find(key, root.left); else if (key > root.key) root.right); else return root; } 12 5 15 2 9 20 7 10 17 30 Now, let’s try finding a node. Find 9. This time I’ll supply the code. 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. Runtime:

Find in BST, Iterative 20 9 2 15 5 12 30 7 17 10 Runtime: 12/10/2018 Find in BST, Iterative Node Find(Object key, Node root) { while (root != NULL && root.key != key) { if (key < root.key) root = root.left; else root = root.right; } return root; 20 9 2 15 5 12 30 7 17 10 OK, find 9 again. Runtime:

Bonus: FindMin/FindMax 12/10/2018 Bonus: FindMin/FindMax Find minimum Find maximum 12 5 15 2 9 20 7 10 17 30 Every now and then everyone succumbs to the temptation to really overuse color.

Insert in BST Insert(13) Insert(8) Insert(31) 12/10/2018 Insert in BST 12 Insert(13) Insert(8) Insert(31) 5 15 2 9 20 7 10 17 30 Insertions happen only at the leaves – easy! Runtime: Let’s do some inserts: insert(8) insert (11) insert(31)

12/10/2018 BuildTree for BST Suppose keys 1, 2, 3, 4, 5, 6, 7, 8, 9 are inserted into an initially empty BST. If inserted in given order, what is the tree? What big-O runtime for this kind of sorted input? If inserted in reverse order, what is the tree? What big-O runtime for this kind of sorted input? Θ(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? Θ(n2)

12/10/2018 BuildTree for BST Suppose keys 1, 2, 3, 4, 5, 6, 7, 8, 9 are inserted into an initially empty BST. If inserted median first, then left median, right median, etc., what is the tree? What is the big-O runtime for this kind of sorted input? 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? 5, 3, 7, 2, 1, 6, 8, 9 better: n log n

Deletion in BST Why might deletion be harder than insertion? 12 5 15 2 12/10/2018 Deletion in BST 12 5 15 2 9 20 7 10 17 30 Why might deletion be harder than insertion? And now for something completely different. Let’s say I want to delete a node. Why might it be harder than insertion? Might happen in the middle of the tree instead of at leaf. Then, I have to fix the BST.

Deletion Removing an item disrupts the tree structure. 12/10/2018 Deletion Removing an item disrupts the tree structure. Basic idea: find the node that is to be removed. Then “fix” the tree so that it is still a binary search tree. Three cases: node has no children (leaf node) node has one child node has two children

Deletion – The Leaf Case 12/10/2018 Deletion – The Leaf Case Delete(17) 12 5 15 2 9 20 7 10 17 30 Alright, we did it the easy way, but what about real deletions? Leaves are easy; we just prune them.

Deletion – The One Child Case 12/10/2018 Deletion – The One Child Case Delete(15) 12 5 15 2 9 20 7 10 30 Single child nodes we remove and… Do what? We can just pull up their children. Is the search tree property intact? Yes.

Deletion: The Two Child Case 12 Delete(5) 5 20 2 9 30 7 10 What can we replace 5 with?

Deletion – The Two Child Case 12/10/2018 Deletion – The Two Child Case Idea: Replace the deleted node with a value between the two child subtrees Options: succ from right subtree: findMin(t.right) pred from left subtree: findMax(t.left) Now delete the original node containing succ or pred Leaf or one child case – easy!

Finally… 7 replaces 5 Original node containing 7 gets deleted 12 7 20 12/10/2018 Finally… 12 7 replaces 5 7 20 2 9 30 10 Original node containing 7 gets deleted This slide is just for closure.

Balanced BST Observations BST: the shallower the better! 12/10/2018 Balanced BST Observations BST: the shallower the better! For a BST with n nodes Average depth (averaged over all possible insertion orderings) is O(log n) Worst case maximum depth is O(n) Simple cases such as insert(1, 2, 3, ..., n) lead to the worst case scenario Solution: Require a Balance Condition that ensures depth is O(log n) – strong enough! is easy to maintain – not too strong!