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

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CSE 326: Data Structures Lecture #7 Branching Out Steve Wolfman Winter Quarter 2000.
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.
CSE 326: Data Structures Lecture #8 Binary Search Trees Alon Halevy Spring Quarter 2001.
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
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:
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.
CPSC 221: Data Structures Lecture #5 Branching Out Steve Wolfman 2014W1 1.
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.
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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
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.
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.
CSE373: Data Structures & Algorithms Priority Queues
Instructor: Lilian de Greef Quarter: Summer 2017
CSE 326: Data Structures Lecture #8 Pruning (and Pruning for the Lazy)
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
CSE373: Data Structures & Algorithms More Heaps; Dictionaries; Binary Search Trees Riley Porter Winter 2016 Winter 2017.
CPSC 221: Data Structures Lecture #5 Branching Out
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
October 30th – Priority QUeues
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
ITEC 2620M Introduction to Data Structures
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
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+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Find in a linked list? first last 7  4  3  8 NULL
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
David Kaplan Dept of Computer Science & Engineering Autumn 2001
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE 373: Data Structures and Algorithms
CSE 332: Data Abstractions Binary Search Trees
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
CSE 326: Data Structures Lecture #7 Dendrology
Binary Trees, Binary Search Trees
CPSC 221: Data Structures Lecture #5 Branching Out
Analysis of Algorithms - 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
CSE 326: Data Structures Lecture #9 AVL II
Richard Anderson Spring 2016
CSE 326: Data Structures Lecture #10 B-Trees
Trees in Data Structures
Binary Trees, Binary Search Trees
Presentation transcript:

Binary Search Trees

Binary Trees A Binary tree is Properties Representation: B C D E F G H a root left subtree (maybe empty) right subtree (maybe empty) Properties max # of leaves: max # of nodes: average depth for N nodes: Representation: B C D E F G H What’s the maximum # of leaves a binary tree of depth d can have? What’s the max # of nodes a binary tree of depth d can have? Minimum? 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! Data right pointer left I J Binary Search Trees CSE 326 Autumn 2001 2

Binary Tree Representation right child left A B C B right child left C right child left D E F D right child left E right child left F right child left Binary Search Trees CSE 326 Autumn 2001 3

Dictionary ADT: Used Everywhere Arrays Sets Dictionaries Router tables Page tables Symbol tables C++ structures … Anywhere we need to find things fast based on a key Our ADT algorithm says to look at some applications at this point. I think the first app pretty much says it all. We move on from there however, to other incredibly widely used applications. Dictionary is probably the most important and one of the most widely used ADTs. Binary Search Trees CSE 326 Autumn 2001 4

Example and Counter-Example 8 5 5 11 4 8 2 7 6 10 18 1 7 11 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! 4 15 20 3 NOT A BINARY SEARCH TREE 21 BINARY SEARCH TREE Binary Search Trees CSE 326 Autumn 2001 5

Complete Binary Search Tree (aka binary heap): Links are completely filled, except possibly bottom level, which is filled left-to-right. 8 5 15 3 7 9 17 1 4 6 Binary Search Trees CSE 326 Autumn 2001 6

In-Order Traversal 10 visit left subtree visit node visit right subtree What does this guarantee with a BST? 5 15 2 9 20 Anyone notice anything interesting about that in-order listing? Everything in the left subtree is listed first. Then the root. Then everything in the right subtree. OK, let’s work out the code to make the in-order listing. Is there an iterative version that doesn’t use its own stack? Not really, no. So, recursion is probably OK here. Anyway, if the tree’s too deep for recursion, you must have a huge amount of data. If (n != null) inorder(n->left) cout << n inorder(n->right) 7 17 30 In order listing: 25791015172030 Binary Search Trees CSE 326 Autumn 2001 7

Recursive Find 10 5 15 2 9 20 7 17 30 Runtime: Best-worse case? Node * find(Comparable key, Node * t) { if (t == NULL) return t; else if (key < t->key) return find(key, t->left); else if (key > t->key) return find(key, t->right); else return t; } 5 15 2 9 20 7 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: Best-worse case? Worst-worse case? f(depth)? Binary Search Trees CSE 326 Autumn 2001 8

Iterative Find 10 Node * find(Comparable key, Node * t) { while (t != NULL && t->key != key) if (key < t->key) t = t->left; else t = t->right; } return t; 5 15 2 9 20 7 17 30 Binary Search Trees CSE 326 Autumn 2001 9

Insert Concept: Proceed down tree as in Find void insert(Comparable x, Node * t) { if ( t == NULL ) { t = new Node(x); } else if (x < t->key) { insert( x, t->left ); } else if (x > t->key) { insert( x, t->right ); } else { // duplicate // handling is app-dependent } Concept: Proceed down tree as in Find If new key not found, then insert a new node at last spot traversed 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. Binary Search Trees CSE 326 Autumn 2001 10

BuildTree for BSTs Suppose the data 1, 2, 3, 4, 5, 6, 7, 8, 9 is inserted into an initially empty BST: in order in reverse order median first, then left median, right median, etc. OK, 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? Binary Search Trees CSE 326 Autumn 2001 11

BST Bonus: FindMin, FindMax Find minimum Find maximum 10 5 15 2 9 20 7 17 30 Binary Search Trees CSE 326 Autumn 2001 12

Successor Node Next larger node in this node’s subtree 10 5 15 2 9 20 Node * succ(Node * t) { if (t->right == NULL) return NULL; else return min(t->right); } 5 15 2 9 20 Here’s a little digression. Maybe it’ll even have an application at some point. Find the next larger node in 10’s subtree. Can we define it in terms of min and max? It’s the min of the right subtree! 7 17 30 How many children can the successor of a node have? Binary Search Trees CSE 326 Autumn 2001 13

Predecessor Node Next smaller node in this node’s subtree 10 5 15 2 9 Node * pred(Node * t) { if (t->left == NULL) return NULL; else return max(t->left); } 5 15 2 9 20 Predecessor is just the mirror problem. 7 17 30 Binary Search Trees CSE 326 Autumn 2001 14

Deletion 10 5 15 2 9 20 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. 7 17 30 Why might deletion be harder than insertion? Binary Search Trees CSE 326 Autumn 2001 15

Deletion - Leaf Case Delete(17) 10 5 15 2 9 20 7 17 30 Alright, we did it the easy way, but what about real deletions? Leaves are easy; we just prune them. 7 17 30 Binary Search Trees CSE 326 Autumn 2001 16

Deletion - One Child Case Delete(15) 10 5 15 2 9 20 Single child nodes we remove and… Do what? We can just pull up their children. Is the search tree property intact? Yes. 7 30 Binary Search Trees CSE 326 Autumn 2001 17

Deletion - Two Child Case Replace node with descendant whose value is guaranteed to be between left and right subtrees: the successor 10 5 20 2 9 30 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(5) 7 Could we have used predecessor instead? Binary Search Trees CSE 326 Autumn 2001 18

Delete Code void delete(Comparable key, Node *& root) { Node *& handle(find(key, root)); Node * toDelete = handle; if (handle != NULL) { if (handle->left == NULL) { // Leaf or one child handle = handle->right; delete toDelete; } else if (handle->right == NULL) { // One child handle = handle->left; } else { // Two children successor = succ(root); handle->data = successor->data; delete(successor->data, handle->right); } Here’s the code for deletion using lots of confusing reference pointers BUT no leaders, fake nodes. The iterative version of this can get somewhat messy, but it’s not really any big deal. Binary Search Trees CSE 326 Autumn 2001 19