Algorithms and Data Structures

Similar documents
Balanced Binary Trees

CS Lecture 5. Vidroha debroy. Material adapted courtesy of Prof. Xiangnan Kong and Prof. Carolina Ruiz at Worcester Polytechnic Institute

SIDDHARTH INSTITUTE OF ENGINEERING & TECHNOLOGY :: PUTTUR (AUTONOMOUS) Siddharth Nagar, Narayanavanam Road QUESTION BANK (DESCRIPTIVE)

Heap Sort. Lecture 35. Robb T. Koether. Hampden-Sydney College. Mon, Apr 25, 2016

Uninformed search methods II.

DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2017)

CENG 466 Artificial Intelligence. Lecture 4 Solving Problems by Searching (II)

Uninformed search methods

Artificial Intelligence. Uninformed Search Strategies

Introduction. AI and Searching. Simple Example. Simple Example. Now a Bit Harder. From Hammersmith to King s Cross

Uninformed search strategies

Princess Nora University Faculty of Computer & Information Systems ARTIFICIAL INTELLIGENCE (CS 370D) Computer Science Department

CS 4649/7649 Robot Intelligence: Planning

Uninformed Search (Ch )

Uninformed Search (Ch )

Incremental ARA*: An Anytime Incremental Search Algorithm For Moving Target Search. University of Southern California Singapore Management University

Search I. Tuomas Sandholm Carnegie Mellon University Computer Science Department. [Read Russell & Norvig Chapter 3]

Real World Search Problems. CS 331: Artificial Intelligence Uninformed Search. Simpler Search Problems. Example: Oregon. Search Problem Formulation

Uninformed search methods II.

COMP219: Artificial Intelligence. Lecture 8: Combining Search Strategies and Speeding Up

Better Search Improved Uninformed Search CIS 32

Overview. Depth Limited Search. Depth Limited Search. COMP219: Artificial Intelligence. Lecture 8: Combining Search Strategies and Speeding Up

SEARCH TREE. Generating the children of a node

1. The data below gives the eye colors of 20 students in a Statistics class. Make a frequency table for the data.

Transposition Table, History Heuristic, and other Search Enhancements

Introduction to Algorithms

CS472 Foundations of Artificial Intelligence. Final Exam December 19, :30pm

The system design must obey these constraints. The system is to have the minimum cost (capital plus operating) while meeting the constraints.

Problem Solving as Search - I

Representation. Representation. Representation. Representation. 8 puzzle.

Uninformed search methods

CSE 3401: Intro to AI & LP Uninformed Search II

Decision Trees. Nicholas Ruozzi University of Texas at Dallas. Based on the slides of Vibhav Gogate and David Sontag

/435 Artificial Intelligence Fall 2015

SEARCH SEARCH TREE. Node: State in state tree. Root node: Top of state tree

Problem Solving Agents

Depth-bounded Discrepancy Search

G53CLP Constraint Logic Programming

Uninformed Search Strategies

Aryeh Rappaport Avinoam Meir. Schedule automation

CT PET-2018 Part - B Phd COMPUTER APPLICATION Sample Question Paper

Dispatching Universität Karlsruhe, System Architecture Group

1 Konan University Yuki Hattori Akiyo Nadamoto

Introduction to Algorithms 6.046J/18.401J/SMA5503

Module 3 Developing Timing Plans for Efficient Intersection Operations During Moderate Traffic Volume Conditions

Computational Models: Class 6

LECTURE 5 PRECEDENCE DIAGRAMING METHOD: LINK RELATIONSHIP

Quadratic Probing. Hash Tables (continued) & Disjoint Sets. Quadratic Probing. Quadratic Probing Example insert(40) 40%7 = 5

Advanced Search Hill climbing

Paul Burkhardt. May 19, 2016

CS 221 PROJECT FINAL

Problem-Solving: A* and Beyond. CSCI 3202, Fall 2010

NCSS Statistical Software

CSC242: Intro to AI. Lecture 21

Predicting Horse Racing Results with Machine Learning

CSE 3402: Intro to Artificial Intelligence Uninformed Search II

CITTT CLUB PLAY SYSTEM

Exercise 11: Solution - Decision tree

6/16/2010 DAG Execu>on Model, Work and Depth 1 DAG EXECUTION MODEL, WORK AND DEPTH

Methods for the Anisotropic Wavelet Packet Transform

CSC384: Introduction to Artificial Intelligence. Search

Discovering Special Triangles Learning Task

CS 4649/7649 Robot Intelligence: Planning

CVEN Computer Applications in Engineering and Construction. Programming Assignment #4 Analysis of Wave Data Using Root-Finding Methods

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR NPTEL ONLINE CERTIFICATION COURSE. On Industrial Automation and Control

Exercise 1: Control Functions

Navigate to the golf data folder and make it your working directory. Load the data by typing

CS 351 Design of Large Programs Zombie House

Descriptive Statistics Project Is there a home field advantage in major league baseball?

Estimating the Probability of Winning an NFL Game Using Random Forests

Lecture 04 ( ) Hazard Analysis. Systeme hoher Qualität und Sicherheit Universität Bremen WS 2015/2016

II. NORMAL BUBBLE SORT (WORST CASE)

1. Functional description. Application program usage. 1.1 General. 1.2 Behavior on bus voltage loss and bus voltage. 1.

Provably Secure Camouflaging Strategy for IC Protection

CMPUT680 - Winter 2001

1.1 The size of the search space Modeling the problem Change over time Constraints... 21

Tokyo: Simulating Hyperpath-Based Vehicle Navigations and its Impact on Travel Time Reliability

LING/C SC 581: Advanced Computa9onal Linguis9cs. Lecture Notes Jan 15 th

Splitting cylinder C20

Performance Visualization of Bubble Sort in Worst Case Using R Programming in Personal Computer

Due on: November 1, 2016: AM Use additional sheets, if necessary. You should staple your exam. Submit on-time at AM

Supplementary Material for Bayes Merging of Multiple Vocabularies for Scalable Image Retrieval

Hierarchical ORAM Revisited, and Applications to Asymptotically Efficient ORAM and OPRAM. Hubert Chan, Yue Guo, Wei-Kai Lin, Elaine Shi 2017/12/5

Solving Problems by Searching chap3 1. Problem-Solving Agents

DATA MINING ON CRICKET DATA SET FOR PREDICTING THE RESULTS. Sushant Murdeshwar

Spider Search: An Efficient and Non-Frontier-Based Real-Time Search Algorithm

Percymon: A Pokemon Showdown Artifical Intelligence

Chapter 4 (sort of): How Universal Are Turing Machines? CS105: Great Insights in Computer Science

PSY201: Chapter 5: The Normal Curve and Standard Scores

Number Bases LESSON ONE By John Owen. Computer Science

MJA Rev 10/17/2011 1:53:00 PM

Workshop 1: Bubbly Flow in a Rectangular Bubble Column. Multiphase Flow Modeling In ANSYS CFX Release ANSYS, Inc. WS1-1 Release 14.

46 Chapter 8 Statistics: An Introduction

Urban OR: Quiz 2 Solutions (2003) ( 1 ρ 1 )( 1 ρ 1 ρ 2 ) ( 1 12 )( ) σ S ] 24 [ 2 = 60, 2 2 ] ( 2 ) 3

HOW TO SETUP ROUND ROBIN IN DARTS FOR WINDOWS

Non-Motorized Transport (NMT) Development. Gerald Ollivier Transport Cluster Leader Transport Global Practice The World Bank

Efficient I/O for Computational Grid Applications

Three New Methods to Find Initial Basic Feasible. Solution of Transportation Problems

CRICKET ONTOLOGY. Project Description :- Instructor :- Prof: Navjyothi Singh

Neural Networks II. Chen Gao. Virginia Tech Spring 2019 ECE-5424G / CS-5824

Transcription:

Algorithms and Data Structures CMPSC 465 LECTURES 22-23 Binary Search Trees Adam Smith S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1

Heaps: Review Heap-leap-jeep-creep(A): 1. A.heap-size = 0 2. For i=1 to len(a) tmp = A[i] Heap-Insert(A,tmp) 3. For i=len(a) down to 1 tmp = Heap-Extract-Max(A) A[i]=tmp What does this do? What is the running time of this operation? S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 2

Trees Rooted Tree: collection of nodes and edges Edges go down from root (from parents to children) No two paths to the same node Sometimes, children have names (e.g. left, right, middle, etc) S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 3

Binary Search Trees Binary tree: every node has 0, 1 or 2 children BST property: If y is in left subtree of x, then key[y] <= key[x] same for right 3 1 2 6 5 7 8 S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 4

Binary Search Trees Binary tree: every node has 0, 1 or 2 children BST property: If y is in left subtree of x, then key[y] <= key[x] same for right 3 1 2 6 5 7 8 S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 5

Implementation Keep pointers to parent and both children Each NODE has left: pointer to NODE right: pointer to NODE p: pointer to NODE key: real number (or other type that supports comparisons) S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 6

Drawing BST s 1 To help you visualize relationships: Drop vertical lines to keep nodes in right order For example: picture shows the only legal location to insert a node with key 4 3 2 6 5 7 S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 7 8 1 2 3 5 6 7 8

Height of a BST can be...... as little as log(n) full balanced tree n... as much as n-1 unbalanced tree 1... n-1 S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 8

Searching a BST Running time: Θ(height) S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 9

Insertion Find location of insertion, keep track of parent during search Running time: Θ(height) S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 10

Tree Min and Max Running time: Θ(height) S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 11

Tree Successor Running time: Θ(height) S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 12

Traversals (not just for BSTs) Traversal: an algorithm that visits every node in the tree to perform some operation, e.g., Printing the keys Updating some part of the structure 3 basic traversals for trees Inorder Preorder Postorder S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 13

Inorder traversal Running time: Θ(n) S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 14

Postorder traversal Write pseudocode that computes height of a BST number of nodes in a BST average depth... Example: height Find-height(T) if T==NIL return -1 else h1 := Find-height(T.left) h2 := Find-height(T.left) return max(h1, h2) + 1 S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 15

Preorder Traversal Recall that insertion order affects the shape of a BST insertion in sorted order: height n-1 random order: height O(log n) with high probability (we will prove that later in the course) Write pseudocode that prints the elements of a binary search tree in a plausible insertion order that is, an insertion order that would produce this particular shape (print nodes in a preorder traversal) S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 16

Exercise on insertion order Exercise: Write pseudocode that takes a sorted list and produces a good insertion order (that would produce a balanced tree) (Hint: divide and conquer: always insert the median of a subarray before inserting the other elements) S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 17

Deletion Cases analyzed in book Running time: Θ(height) S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 18

Binary-search-tree sort T Create an empty BST for i = 1 to n do TREE-INSERT(T, A[i]) Perform an inorder tree walk of T. Example: A = [3 1 8 2 6 7 5] Tree-walk time = O(n), but how long does it take to build the BST? 1 3 2 6 5 7 8 S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 19

Analysis of BST sort BST sort performs the same comparisons as quicksort, but in a different order! 3 1 8 2 6 7 5 1 2 8 6 7 5 2 6 7 5 5 7 The expected time to build the tree is asymptotically the same as the running time of quicksort. S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 20

Node depth The depth of a node = the number of comparisons made during TREE-INSERT. Assuming all input permutations are equally likely, we have Average node depth. (quicksort analysis) S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 21