Lecture 6: Binary Search Trees CSCI 700 - Algorithms I. Andrew Rosenberg



Similar documents
Binary Heaps. CSE 373 Data Structures

Binary Heap Algorithms

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

6 March Array Implementation of Binary Trees

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Binary Search Trees 3/20/14

From Last Time: Remove (Delete) Operation

Algorithms and Data Structures

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

How To Create A Tree From A Tree In Runtime (For A Tree)

Chapter 14 The Binary Search Tree

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Binary Search Trees (BST)

Analysis of Algorithms I: Binary Search Trees

Data Structures and Algorithms

Algorithms Chapter 12 Binary Search Trees

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Analysis of Binary Search algorithm and Selection Sort algorithm

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Converting a Number from Decimal to Binary

Binary search algorithm

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Ordered Lists and Binary Trees

Rotation Operation for Binary Search Trees Idea:

Why Use Binary Trees?

Exam study sheet for CS2711. List of topics

Lecture Notes on Binary Search Trees

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

Binary Search Trees. Ric Glassey

TREE BASIC TERMINOLOGIES

Analysis of Algorithms I: Optimal Binary Search Trees

Binary Trees and Huffman Encoding Binary Search Trees

Sample Questions Csci 1112 A. Bellaachia

Lecture Notes on Binary Search Trees

Symbol Tables. Introduction

CSE 326: Data Structures B-Trees and B+ Trees

A Comparison of Dictionary Implementations

CS711008Z Algorithm Design and Analysis

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

DATA STRUCTURES USING C

Physical Data Organization

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Binary Search Trees CMPSC 122

Heaps & Priority Queues in the C++ STL 2-3 Trees

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Cpt S 223. School of EECS, WSU

schema binary search tree schema binary search trees data structures and algorithms lecture 7 AVL-trees material

The ADT Binary Search Tree

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Full and Complete Binary Trees

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Binary Search Tree Intro to Algorithms Recitation 03 February 9, 2011

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Big Data and Scripting. Part 4: Memory Hierarchies

Introduction to Data Structures and Algorithms

DATABASE DESIGN - 1DL400

Analysis of a Search Algorithm

OPTIMAL BINARY SEARCH TREES

Data Structure [Question Bank]

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

Data storage Tree indexes

Chapter 13: Query Processing. Basic Steps in Query Processing

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Merkle Hash Trees for Distributed Audit Logs

Introduction Advantages and Disadvantages Algorithm TIME COMPLEXITY. Splay Tree. Cheruku Ravi Teja. November 14, 2011

Data Structures Fibonacci Heaps, Amortized Analysis

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Zabin Visram Room CS115 CS126 Searching. Binary Search

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Unit Storage Structures 1. Storage Structures. Unit 4.3

Persistent Binary Search Trees

CIS 631 Database Management Systems Sample Final Exam

Persistent Data Structures and Planar Point Location

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Lecture 4: Balanced Binary Search Trees

Data Structures and Algorithms(5)

CompSci-61B, Data Structures Final Exam

1/1 7/4 2/2 12/7 10/30 12/25

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Algorithms and Data Structures Written Exam Proposed SOLUTION

Introduction to Data Structures and Algorithms

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

An Immediate Approach to Balancing Nodes of Binary Search Trees

Database Design Patterns. Winter Lecture 24

International Journal of Software and Web Sciences (IJSWS)

Transcription:

Lecture 6: Binary Search Trees CSCI 700 - Algorithms I Andrew Rosenberg

Last Time Linear Time Sorting Counting Sort Radix Sort Bucket Sort

Today Binary Search Trees

Data Structures Data structure is a set of elements and the relationship between them. The appropriate data structure for a task is determined by the functions it needs to support.

Data Structures Data structure is a set of elements and the relationship between them. The appropriate data structure for a task is determined by the functions it needs to support. A dictionary supports (minimally) Insert, Search and Delete. Other data structures might need Minimum, Maximum, etc.

Data Structures Data structure is a set of elements and the relationship between them. The appropriate data structure for a task is determined by the functions it needs to support. A dictionary supports (minimally) Insert, Search and Delete. Other data structures might need Minimum, Maximum, etc. For a restricted domain, D = {1,...,k}, we can use an array, A[1..k]. This allows Insert, Search and Delete to be O(1).

Data Structures Data structure is a set of elements and the relationship between them. The appropriate data structure for a task is determined by the functions it needs to support. A dictionary supports (minimally) Insert, Search and Delete. Other data structures might need Minimum, Maximum, etc. For a restricted domain, D = {1,...,k}, we can use an array, A[1..k]. This allows Insert, Search and Delete to be O(1). Aside: Hashing attempts to map an unrestricted domain to a restricted one.

Binary Search Tree Binary Search Trees (BSTs) are a simple and efficient implementation of a dictionary. A BST is a rooted binary tree. The keys are at the nodes. For every node, v, the keys of the left subtree key(v) For every node, v, the keys of the right subtree > key(v)

Binary Search Tree Binary Search Trees (BSTs) are a simple and efficient implementation of a dictionary. A BST is a rooted binary tree. The keys are at the nodes. For every node, v, the keys of the left subtree key(v) For every node, v, the keys of the right subtree > key(v) Binary Search Trees are not, by definition, balanced.

Binary Search Tree Data Structure parent key left right

Binary Search Tree Example null root 10 5 20 1 8

Binary Search Tree Example null root 20 10 8 5 1

Not a Binary Search Tree Example null root 10 5 20 1 8

Not a Binary Search Tree Example null root 10 1 5 8 20

Height of a BST The height of a BST is at most n 1. The height of a BST is at least logn.

Sorting with a BST Binary Search Trees are sorted. Constructing a sorted array is Θ(n) Traverse(T) if T.root then Traverse(T.left) Print T.key Traverse(T.right) end if

Sorting with a BST Binary Search Trees are sorted. Constructing a sorted array is Θ(n) Traverse(T) if T.root then Traverse(T.left) Print T.key Traverse(T.right) end if Can we prove that this is correct and Θ(n)?

Searching a BST Search(T, x) if T then if T.key = x then return T else if T.key < x then return Search(T.left, x) else return Search(T.right, x) end if end if else return null end if

Searching a BST Search(T,x) is O(height) If balanced, height = logn, so O(logn). Worst case scenario, a sequential search, O(n).

Finding specific elements in a BST Minimum(T) = O(height). Traverse to the left. Maximum(T) = O(height). Traverse to the right. Successor(T) - Find the node with smallest key greater than T.key.

Successor(T) Successor(T) If T has a right child, then return Minimum(T.right) If T has no right child, and is a left child, then return T.parent If T has no right child and is a right child, then traverse up until a left child is found - then this node s parent. Else T has no successor. Successor(T) = O(height)

Inserting an Element into a BST Insert(T,x) if T then if T.key x then Insert(T.left, x) else Insert(T.right, x) end if else T NewNode(x). end if Insert is a lot like Search. Insert(T,x) = O(height)

Building a BST To build a BST, Insert n random elements in order to an empty BST. It takes O(nlogn) to build a BST from such a set of random elements. Run Insert n times. n i=1 logi = nlogn Expected height = O(log n)

Building a BST To build a BST, Insert n random elements in order to an empty BST. It takes O(nlogn) to build a BST from such a set of random elements. Run Insert n times. n i=1logi = nlogn Expected height = O(log n) Can a BST be constructed in less than O(nlogn)?

Building a BST To build a BST, Insert n random elements in order to an empty BST. It takes O(nlogn) to build a BST from such a set of random elements. Run Insert n times. n i=1logi = nlogn Expected height = O(log n) Can a BST be constructed in less than O(nlogn)? No. It s equivalent to a comparison sort. Since Comparison sorting is Ω(nlogn), constructing a BST is Ω(nlogn).

Delete(T,v) Delete(T,v) If v is a leaf, delete v. If v has 1 child, delete v, replace v with its child. If v has 2 children, swap v with Successor(v), then Delete(v). How long does each case take?

Delete(T,v) Delete(T,v) If v is a leaf, delete v. If v has 1 child, delete v, replace v with its child. If v has 2 children, swap v with Successor(v), then Delete(v). How long does each case take? How can we be sure Delete(v) terminates?

Delete(T,v) Delete(T,v) If v is a leaf, delete v. If v has 1 child, delete v, replace v with its child. If v has 2 children, swap v with Successor(v), then Delete(v). How long does each case take? How can we be sure Delete(v) terminates? Show that this holds the BST properties.

Recap Binary Search Trees are an efficient, simple dictionary data structure. Construction O(n log n) Insertion O(log n) Search O(log n) Deletion O(log n) Binary Search Trees are sorted representations of data.

Bye Next time Heaps.