Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Slides:



Advertisements
Similar presentations
CS16: Introduction to Data Structures & Algorithms
Advertisements

Chapter 12 Binary Search Trees
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.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
BST Data Structure A BST node contains: A BST contains
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Fundamentals of Python: From First Programs Through Data Structures
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
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.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
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.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Starting at Binary Trees
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.
Binary Search Tree Qamar Abbas.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Binary Search Trees Algorithms and Data Structures.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Search Trees: BSTs and B-Trees David Kauchak cs161 Summer 2009.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
October 9, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Aalborg University
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
David Stotts Computer Science Department UNC Chapel Hill.
Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
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.
BST Trees
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search Trees
Binary Search Trees.
Lecture 7 Algorithm Analysis
Chapter 12: Binary Search Trees
CS6045: Advanced Algorithms
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search Trees
Binary SearchTrees [CLRS] – Chap 12.
Mark Redekopp David Kempe
Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013

Administrative

Proof by contradiction

Number guessing game I’m thinking of a number between 1 and n You are trying to guess the answer For each guess, I’ll tell you “correct”, “higher” or “lower” Describe an algorithm that minimizes the number of guesses

Binary Search Trees BST – A binary tree where a parent’s value is greater than all values in the left subtree and less than or equal to all the values in the right subtree the left and right children are also binary trees Why not? Can be implemented with with pointers or an array

Example

What else can we say? All elements to the left of a node are less than the node All elements to the right of a node are greater than or equal to the node The smallest element is the left-most element The largest element is the right-most element

Another example: the loner 12

Another example: the twig

Operations Search(T,k) – Does value k exist in tree T Insert(T,k) – Insert value k into tree T Delete(T,x) – Delete node x from tree T Minimum(T) – What is the smallest value in the tree? Maximum(T) – What is the largest value in the tree? Successor(T,x) – What is the next element in sorted order after x Predecessor(T,x) – What is the previous element in sorted order of x Median (T) – return the median of the values in tree T

Search How do we find an element?

Finding an element Search(T, 9)

Finding an element Search(T, 9)

Finding an element Search(T, 9) 9 > 12?

Finding an element Search(T, 9)

Finding an element Search(T, 9)

Finding an element Search(T, 13)

Finding an element Search(T, 13)

Finding an element Search(T, 13)

Finding an element ? Search(T, 13)

Iterative search

Is BSTSearch correct?

Running time of BST Worst case? O(height of the tree) Average case? O(height of the tree) Best case? O(1)

Height of the tree Worst case height? n-1 “the twig” Best case height? floor(log 2 n) complete (or near complete) binary tree Average case height? Depends on two things: the data how we build the tree!

Insertion

Similar to search

Insertion Similar to search Find the correct location in the tree

Insertion keeps track of the previous node we visited so when we fall off the tree, we know

Insertion add node onto the bottom of the tree

Correctness? maintain BST property

Correctness What happens if it is a duplicate?

Inserting duplicate Insert(T, 14)

Running time O(height of the tree)

Running time Why not Θ(height of the tree)? O(height of the tree)

Running time Insert(T, 15)

Height of the tree Worst case: “the twig” – When will this happen?

Height of the tree Best case: “complete” – When will this happen?

Height of the tree Average case for random data? Randomly inserted data into a BST generates a tree on average that is O(log n)

Visiting all nodes In sorted order

Visiting all nodes In sorted order

Visiting all nodes In sorted order , 8

Visiting all nodes In sorted order , 8, 9

Visiting all nodes In sorted order , 8, 9, 12

Visiting all nodes What’s happening? , 8, 9, 12

Visiting all nodes In sorted order , 8, 9, 12, 14

Visiting all nodes In sorted order , 8, 9, 12, 14, 20

Visiting all nodes in order

any operation

Is it correct? Does it print out all of the nodes in sorted order?

Running time? Recurrence relation: j nodes in the left subtree n – j – 1 in the right subtree Or How much work is done for each call? How many calls? Θ(n)

What about?

Preorder traversal , 8, 5, 9, 14, 20 How is this useful? Tree copying: insert in to new tree in preorder prefix notation: (2+3)*4 -> *

What about?

Postorder traversal , 9, 8, 20, 14, 12 How is this useful? postfix notation: (2+3)*4 -> * ?

Min/Max

Running time of min/max? O(height of the tree)

Successor and predecessor Predecessor(12)?9

Successor and predecessor Predecessor in general? largest node of all those smaller than this node rightmost element of the left subtree

Successor Successor(12)?13

Successor Successor in general? smallest node of all those larger than this node leftmost element of the right subtree

Successor What if the node doesn’t have a right subtree? smallest node of all those larger than this node leftmost element of the right subtree 9 5

Successor What if the node doesn’t have a right subtree? node is the largest the successor is the node that has x as a predecessor 9

Successor successor is the node that has x as a predecessor 9

Successor successor is the node that has x as a predecessor 9

Successor successor is the node that has x as a predecessor 9

Successor successor is the node that has x as a predecessor 9 keep going up until we’re no longer a right child

Successor

if we have a right subtree, return the smallest of the right subtree

Successor find the node that x is the predecessor of keep going up until we’re no longer a right child

Successor running time O(height of the tree)

Deletion Three cases!

Deletion: case 1 No children Just delete the node

Deletion: case No children Just delete the node

Deletion: case 2 One child Splice out the node

Deletion: case One child Splice out the node

Deletion: case 3 Two children Replace x with it’s successor

Deletion: case Two children Replace x with it’s successor

Deletion: case 3 Two children Will we always have a successor? Why successor? Case 1 or case 2 deletion Larger than the left subtree Less than or equal to right subtree

Height of the tree Most of the operations take time O(height of the tree) We said trees built from random data have height O(log n), which is asymptotically tight Two problems: We can’t always insure random data What happens when we delete nodes and insert others after building a tree?

Balanced trees Make sure that the trees remain balanced! Red-black trees AVL trees trees … B-trees