Search Trees: BSTs and B-Trees

Similar documents
Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Binary Trees, Binary Search Trees

Trees. (Trees) Data Structures and Programming Spring / 28

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Associate Professor Dr. Raed Ibraheem Hamed

Lecture 6: Analysis of Algorithms (CS )

TREES. Trees - Introduction

Programming II (CS300)

Lecture 23: Binary Search Trees

Binary search trees (BST) Binary search trees (BST)

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Lecture: Analysis of Algorithms (CS )

Binary Search Trees. Antonio Carzaniga. Faculty of Informatics University of Lugano. October 21, Antonio Carzaniga 1

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Binary Search Trees. Antonio Carzaniga. Faculty of Informatics University of Lugano. October 22, Antonio Carzaniga 1

Data Structures and Algorithms for Engineers

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Module 4: Dictionaries and Balanced Search Trees

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

A set of nodes (or vertices) with a single starting point

CS 361, Lecture 21. Outline. Things you can do. Things I will do. Evaluation Results

COMP Analysis of Algorithms & Data Structures

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

COMP Analysis of Algorithms & Data Structures

Trees, Binary Trees, and Binary Search Trees

Binary Trees and Binary Search Trees

COMP Analysis of Algorithms & Data Structures

CMSC 341. Binary Search Trees CMSC 341 BST

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Dictionaries. Priority Queues

Analysis of Algorithms

CS102 Binary Search Trees

Data Structures and Algorithms

Binary Search Trees. Analysis of Algorithms

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

8. Binary Search Tree

Binary Search Trees Treesort

Binary Trees

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CMPS 2200 Fall 2017 Red-black trees Carola Wenk

CS350: Data Structures Red-Black Trees

CS 171: Introduction to Computer Science II. Binary Search Trees

Binary Search Tree (2A) Young Won Lim 5/17/18

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Module 4: Priority Queues

Binary search trees (chapters )

Search Trees. Undirected graph Directed graph Tree Binary search tree

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Binary Trees. Recursive definition. Is this a binary tree?

Sample Exam 1 Questions

BST Deletion. First, we need to find the value which is easy because we can just use the method we developed for BST_Search.

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

CS 350 : Data Structures Binary Search Trees

Partha Sarathi Mandal

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

ECE250: Algorithms and Data Structures Binary Search Trees (Part A)

Data Structures and Algorithms

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Binary Search Trees. July 13th, Computer Information Systems. c 2009 Vaidė Narváez

CS 350 : Data Structures Skip Lists

Binary Search Tree. Revised based on textbook author s notes.

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

CSC148 Week 7. Larry Zhang

Data Structures and Algorithms

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

Search Trees. COMPSCI 355 Fall 2016

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

Binary Search Trees. What is a Binary Search Tree?

Balanced Search Trees

Multi-Way Search Tree

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Binary Search Trees. See Section 11.1 of the text.

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Advanced Tree Data Structures

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

Programming II (CS300)

TREES Lecture 12 CS2110 Spring 2018

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Balanced Search Trees. CS 3110 Fall 2010

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Quiz 1 Solutions. Asymptotic growth [10 points] For each pair of functions f(n) and g(n) given below:

Course Review. Cpt S 223 Fall 2009

Midterm solutions. n f 3 (n) = 3

March 20/2003 Jayakanth Srinivasan,

TREES Lecture 12 CS2110 Spring 2019

CSCI Trees. Mark Redekopp David Kempe

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Course Review for Finals. Cpt S 223 Fall 2008

Transcription:

3//13 Search Trees: BSTs and B-Trees Administrative David Kauchak cs302 Spring 13 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 1

3//13 Binary Search Trees Example 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 lefttree( < i righttree( the left and right children are also binary trees Why not? lefttree( i righttree( Can be implemented with with pointers or an array What else can we say? All elements to the left of a node are less than the node Another example: the loner 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 2

3//13 Another example: the twig 5 1 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? (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) 3

3//13 Finding an element Search(T, 9) Finding an element Search(T, 9) 9 >? Finding an element Search(T, 9) Finding an element Search(T, 9) 4

3//13 Finding an element Search(T, 13) Finding an element Search(T, 13) Finding an element Search(T, 13) Finding an element Search(T, 13)? 5

3//13 Iterative search Is BSTSearch correct? Running time of BST Worst case? l O(height of the tree) Average case? l O(height of the tree) Best case? l O(1) Height of the tree Worst case height? l n-1 l the twig Best case height? l floor(log 2 n) l complete (or near complete) binary tree Average case height? l Depends on two things: l the data l how we build the tree! 6

3//13 Insertion Insertion Similar to search Insertion Insertion Similar to search Find the correct location in the tree keeps track of the previous node we visited so when we fall off the tree, we know 7

3//13 Insertion Correctness? add node onto the bottom of the tree maintain BST property Correctness What happens if it is a duplicate? Inserting duplicate Insert(T, )

3//13 Running time Running time O(height of the tree) O(height of the tree) Why not Θ(height of the tree)? Running time Insert(T, 15) Height of the tree Worst case: the twig When will this happen? 5 1 9

3//13 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 Visiting all nodes In sorted order In sorted order 5 10

3//13 Visiting all nodes Visiting all nodes In sorted order 5, In sorted order 5,, 9 Visiting all nodes Visiting all nodes In sorted order 5,, 9, What s happening? 5,, 9, 11

3//13 Visiting all nodes Visiting all nodes In sorted order 5,, 9,, In sorted order 5,, 9,,, Visiting all nodes in order Visiting all nodes in order any operation

3//13 Is it correct? Running time? Does it print out all of the nodes in sorted order? Recurrence relation: Or l j nodes in the left subtree l n j 1 in the right subtree T( n) = T( j) + T( n j 1) + Θ(1) l How much work is done for each call? l How many calls? l Θ(n) What about? Preorder traversal,, 5, 9,, How is this useful? Tree copying: insert in to new tree in preorder prefix notation: (2+3)*4 -> * + 2 3 4 13

3//13 What about? Postorder traversal 5, 9,,,, How is this useful? postfix notation: (2+3)*4 -> 4 3 2 + *? Min/Max Running time of min/max? O(height of the tree)

3//13 and predecessor and predecessor Predecessor()? 9 Predecessor in general? largest node of all those smaller than this node rightmost element of the left subtree ()? 13 in general? smallest node of all those larger than this node leftmost element of the right subtree 15

3//13 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 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 5 9 13 successor is the node that has x as a predecessor successor is the node that has x as a predecessor 16

3//13 successor is the node that has x as a predecessor keep going up until we re no longer a right child successor is the node that has x as a predecessor if we have a right subtree, return the smallest of the right subtree 17

3//13 running time find the node that x is the predecessor of O(height of the tree) keep going up until we re no longer a right child Deletion Deletion: case 1 No children Just delete the node Three cases! 17 1

3//13 Deletion: case 1 Deletion: case 2 No children Just delete the node One child Splice out the node 5 13 5 13 17 17 Deletion: case 2 One child Deletion: case 3 Two children Splice out the node Replace x with it s successor 5 5 13 13 17 17 19

3//13 Deletion: case 3 Two children Replace x with it s successor Deletion: case 3 Two children Will we always have a successor? 5 13 17 Why successor? l Case 1 or case 2 deletion l Larger than the left subtree l 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 Balanced trees Make sure that the trees remain balanced! l Red-black trees l AVL trees l 2-3-4 trees l Two problems: l We can t always insure random data l What happens when we delete nodes and insert others after building a tree? B-trees