SFU CMPT Lecture: Week 9

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

SFU CMPT Lecture: Week 8

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

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

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

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

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

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

Lecture 6: Analysis of Algorithms (CS )

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

Binary Trees, Binary Search Trees

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Dictionaries. Priority Queues

CMSC351 - Fall 2014, Homework #2

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

TREES. Trees - Introduction

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

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

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

Binary Search Trees. Analysis of Algorithms

Lecture: Analysis of Algorithms (CS )

Search Trees. Undirected graph Directed graph Tree Binary search tree

Algorithms. AVL Tree

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

CS24 Week 8 Lecture 1

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

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

Probabilistic analysis of algorithms: What s it good for?

Binary Heaps in Dynamic Arrays

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

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

CS350: Data Structures Binary Search Trees

CS102 Binary Search Trees

Binary Trees

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

COMP Analysis of Algorithms & Data Structures

Data Structures and Algorithms

March 20/2003 Jayakanth Srinivasan,

Friday Four Square! 4:15PM, Outside Gates

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

CS 350 : Data Structures Binary Search Trees

CSCI Trees. Mark Redekopp David Kempe

COMP Analysis of Algorithms & Data Structures

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

Recall: Properties of B-Trees

Algorithms. Deleting from Red-Black Trees B-Trees

Figure 4.1: The evolution of a rooted tree.

Lecture 7. Binary Search Trees / AVL Trees

Priority Queues and Binary Heaps

quiz heapsort intuition overview Is an algorithm with a worst-case time complexity in O(n) data structures and algorithms lecture 3

CS350: Data Structures Red-Black Trees

CE 221 Data Structures and Algorithms

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

We assume uniform hashing (UH):

ץע A. B C D E F G H E, B ( -.) - F I J K ) ( A :. : ע.)..., H, G E (. י : י.)... C,A,, F B ( 2

The priority is indicated by a number, the lower the number - the higher the priority.

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

Chapter 2: Basic Data Structures

Binary Search Trees. 1. Inorder tree walk visit the left subtree, the root, and right subtree.

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

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

Search Trees - 1 Venkatanatha Sarma Y

Associate Professor Dr. Raed Ibraheem Hamed

Multi-Way Search Tree

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

Binary Search Trees, etc.

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

Chapter 5. Binary Trees

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

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

Binary Search Trees Treesort

Maintain binary search tree property nodes to the left are less than the current node, nodes to the right are greater

Announcements. Problem Set 2 is out today! Due Tuesday (Oct 13) More challenging so start early!

Trees. Truong Tuan Anh CSE-HCMUT

Lower Bound on Comparison-based Sorting

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

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

CSI33 Data Structures

EE 368. Weeks 5 (Notes)

Successor/Predecessor Rules in Binary Trees

Priority Queues. T. M. Murali. January 29, 2009

Optimal Static Range Reporting in One Dimension

Binary Search Tree (3A) Young Won Lim 6/6/18

Binary search trees :

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

We have the pointers reference the next node in an inorder traversal; called threads

9. Heap : Priority Queue

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

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

Graph Traversal. 1 Breadth First Search. Correctness. find all nodes reachable from some source node s

Data Structures. Dynamic Sets

Different binary search trees can represent the same set of values (Fig.2). Vladimir Shelomovskii, Unitech, Papua New Guinea. Binary search tree.

Recursive Data Structures and Grammars

INF2220: algorithms and data structures Series 1

B-Trees and External Memory

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

Binary Trees. Height 1

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

Binary Search Trees. Motivation. Binary search tree. Tirgul 7

Transcription:

SFU CMPT-307 2008-2 1 Lecture: Week 9 SFU CMPT-307 2008-2 Lecture: Week 9 Ján Maňuch E-mail: jmanuch@sfu.ca Lecture on July 8, 2008, 5.30pm-8.20pm

SFU CMPT-307 2008-2 2 Lecture: Week 9 Binary search trees Support many dynamic-set operations, e.g. Search Minimum Maximum Insert Delete... Can be used as dictionary, priority queue... Running time depends on height of tree: if complete, then ÐÓ Òµ in the worst case if just one chain, then Òµ in the worst case if random, then ÐÓ Òµ expected

nodes Ý in left subtree of Ü have key Ý key Ü SFU CMPT-307 2008-2 3 Lecture: Week 9 First of all, what is a binary search tree? it s a binary tree represented using linked data structure nodes are objects objects store key data pointers to left child, right child, and parent (NULL if one is missing) root is only node with parent=null Binary-search-tree property Ü node in binary search tree nodes Ý in right subtree of Ü have key Ý key Ü Note: heaps are different!

SFU CMPT-307 2008-2 4 Lecture: Week 9 Definition: inorder walk of binary tree: for each node Ü 1. visit left subtree (recursively) 2. print key of Ü 3. visit right subtree (recursively) Inorder-Tree-Walk ܵ 1: Ü if NULL then 2: Inorder-Tree-Walk left ܵµ 3: print key ܵ 4: Inorder-Tree-Walk right ܵµ 5: end if Interesting property of BSTs: In-order walk of BST prints all keys in sorted order

SFU CMPT-307 2008-2 5 Lecture: Week 9 Example for inorder walk: 6 5 10 3 8 12 2 4 Result is 2 3 4 5 6 8 10 12

SFU CMPT-307 2008-2 6 Lecture: Week 9 Assignment Problem 9.1. (deadline: July 15, 5:30pm) Give a nonrecursive algorithm that performs an inorder tree walk using only a constant memory. Your algorithm can test two pointers for equality.

SFU CMPT-307 2008-2 7 Lecture: Week 9 Theorem. If Ü is root of Ò-node (sub)tree, then Inorder-Tree-Walk ܵ takes Òµ time. Proof. Ì Òµ time if procedure called on Ò-node (sub)tree Clearly Ì ¼µ for some constant (test Ü NULL) Otherwise, suppose left subtree has nodes, right subtree Ò ½ has nodes. Then Ò ¼ for Ì Òµ Ì µ Ì Ò ½µ with constant

SFU CMPT-307 2008-2 8 Lecture: Week 9 Ì Òµ Ì µ Ì Ò ½µ We will use substitution method to Ì Òµ µò show Ò ¼ For we µò Ì have ¼µ, OK Induction hypothesis: For Ñ every Ì Ñµ µñ Ò,. We will show that is true also for Ò: Ì Òµ Ì µ Ì Ò ½µ µ µ Ò ½µ µ µò µ µ µò µ µò

SFU CMPT-307 2008-2 9 Lecture: Week 9 Searching Want to search for a node with given key Return pointer to node if exists, otherwise NULL begin search at root follow path downward for Ü node on path, key Ü compare with if equal, done if key Ü, continue in left subtree (left subtree contains keys key Ü ) if key Ü, continue in right subtree (right subtree contains keys key Ü )

SFU CMPT-307 2008-2 10 Lecture: Week 9 µ Tree-Search Ü 1: Ü if NULL key Ü or then 2: return Ü 3: else if key Ü then 4: return Tree-Search left Ü µ 5: else 6: return Tree-Search right Ü µ 7: end if

SFU CMPT-307 2008-2 11 Lecture: Week 9 15 6 18 3 7 17 20 2 4 13 9 Running time is Ç µ, height of tree

SFU CMPT-307 2008-2 12 Lecture: Week 9 Minimum/maximum Minimum of the tree rooted in Ü can be found by following left pointers as long as possible (not necessarily to a leaf!) Tree-Minimum ܵ 1: left Ü while NULL do Ü left Ü 2: 3: end while 4: return Ü Maximum of the tree rooted in Ü can be found by following right pointers as long as possible (not necessarily to a leaf!) Tree-Maximum ܵ 1: right Ü while NULL do Ü right Ü 2: 3: end while 4: return Ü Both have running time Ç µ, height of tree

SFU CMPT-307 2008-2 13 Lecture: Week 9 Successor/predecessor Definition: successor/predecessor in sorted order given by inorder walk For instance, if Ü values Ü ¾ ÜÒ ½ are stored in a tree, then the successor Ü of is Ü ½. 6 5 10 3 8 12 2 4 Idea of an algorithm for finding successor: If right subtree of Ü is nonempty, then successor of Ü is leftmost node in right subtree ( the smallest among the larger ) Found by calling Tree-Minimum on right subtree Otherwise (right subtree is empty and Ü has a successor), then this is the lowest ancestor Ü of whose left child is also ancestor Ü of (A node is ancestor of itself)

SFU CMPT-307 2008-2 14 Lecture: Week 9 Tree-Successor ܵ 1: right Ü if NULL then 2: return Tree-Minimum right Ü µ 3: end if 4: Ý parent Ü 5: while Ý NULL and Ü right Ý do 6: Ü Ý Ý parent Ý 7: 8: end while 9: return Ý Running time clearly Ç µ, height of tree Tree-Predecessor symmetric Theorem. Operations Search, Minimum, Maximum, Successor, Predecessor run in Ç µ time in BST of height

SFU CMPT-307 2008-2 15 Lecture: Week 9 Assignment Problem 9.2. (deadline: July 15, 5:30pm) Consider a binary tree Ì whose keys are distinct. Show that if the right subtree of a node Ü in Ì is empty and Ü has a successor Ý, then Ý is the lowest ancestor of Ü whose left child is also an ancestor of Ü. (Recall that every node is its own ancestor.)

SFU CMPT-307 2008-2 16 Lecture: Week 9 Insertion Now we re talking about dynamic sets Insertion of new element easy. From root, walk down tree according to value of new key and open new leaf 12 5 18 2 9 15 19 13 17 Running time again Ç µ

SFU CMPT-307 2008-2 17 Lecture: Week 9 Want to insert new value Ú Given node Þ with key Þ Ú, left Þ right Þ NULL Tree-Insert Ì Þµ 10: end while 1: Ý NULL 2: Ü root Ì 3: while Ü NULL do 4: Ý Ü 5: if key Þ key Ü then 6: Ü left Ü 7: else 8: Ü right Ü 9: end if 11: parent Þ Ý 12: if Ý NULL then 13: root Ì Þ /* Ì was empty */ 14: else if key Þ key Ý then 15: left Ý Þ 16: else 17: right Ý Þ 18: end if

SFU CMPT-307 2008-2 18 Lecture: Week 9 Deletion Given pointer to some node Þ. Three cases. 1. Þ has no children At Þ s parent parent Þ, just replace link to Þ with NULL 2. Þ has one child splice out Þ, make new link between its parent and its child 3. Þ has two children splice out Þ s successor Ý (which has no left child, as seen from Homework 9.3), and replace Þ s key and data with Ý s key and data

SFU CMPT-307 2008-2 19 Lecture: Week 9 Assignment Problem 9.3. (deadline: July 15, 5:30pm) Show that if a node in a binary search tree has two children, then its successor has no left child and its predecessor has no right child.

SFU CMPT-307 2008-2 20 Lecture: Week 9 no children 15 15 5 16 3 12 20 z 10 13 18 23 6 7 5 16 3 12 20 10 18 23 6 7

SFU CMPT-307 2008-2 21 Lecture: Week 9 one child 15 15 5 16 z 5 20 3 12 20 3 12 18 23 10 13 18 23 10 13 6 6 7 7

SFU CMPT-307 2008-2 22 Lecture: Week 9 two children z 5 15 16 y 6 15 5 z 16 3 12 20 3 12 20 10 13 18 23 10 13 18 23 y 6 7 7 15 6 16 3 12 20 10 13 18 23 7

SFU CMPT-307 2008-2 23 Lecture: Week 9 Tree-Delete Ì Þµ 1: if left Þ NULL or right Þ NULL then 2: Ý Þ 3: else 4: Ý Tree-Successor Þµ 5: end if 6: if left Ý NULL then 7: Ü left Ý 8: else 9: Ü right Ý 10: end if 11: if Ü NULL then 12: parent Ü parent Ý 13: end if 14: if parent Ý NULL then 15: root Ì Ü 16: else if Ý left parent Ý then 17: left parent Ý Ü 18: else 19: right parent Ý Ü 20: end if 21: if Ý Þ then 22: key Þ key Ý 23: copy Ý s data into Þ 24: end if 25: return Ý