CSS 343 Data Structures, Algorithms, and Discrete Math II. Balanced Search Trees. Yusuf Pisan

Similar documents
RBT Operations. The basic algorithm for inserting a node into an RBT is:

Balanced Trees. Balanced Trees Tree. 2-3 Tree. 2 Node. Binary search trees are not guaranteed to be balanced given random inserts and deletes

CSE 100: RED-BLACK TREES

Topic 23 Red Black Trees

CSI33 Data Structures

Binary Search Tree (Part 2 The AVL-tree)

5 AVL trees: deletion

PRIORITY QUEUES AND HEAPS

CS : Data Structures

Self-Adjusting Binary Search Trees. Andrei Pârvu

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 17: Heaps and Priority Queues

MITOCW 6. AVL Trees, AVL Sort

PRIORITY QUEUES AND HEAPS. Lecture 19 CS2110 Spring 2014

DATA STRUCTURE TREES UNIT II

Simple Search Algorithms

AIMA 3.5. Smarter Search. David Cline

Link State Routing. Brad Karp UCL Computer Science. CS 3035/GZ01 3 rd December 2013

PRIORITY QUEUES AND HEAPS. Slides of Ken Birman, Cornell University

Search then involves moving from state-to-state in the problem space to find a goal (or to terminate without finding a goal).

and 6.855J. Network Simplex Animations

Link State Routing. Stefano Vissicchio UCL Computer Science CS 3035/GZ01

UMBC 671 Midterm Exam 19 October 2009

4.4 Shortest Paths in a Graph Revisited

Chapter 7: Sorting 7.1. Original

mywbut.com Two agent games : alpha beta pruning

Random Binary Search Trees. EECS 214, Fall 2017

MITOCW ocw lec11

Problem A. Worst Locations

ARTIFICIAL INTELLIGENCE (CS 370D)

Adversary Search. Ref: Chapter 5

Artificial Intelligence Lecture 3

A Memory Efficient Anti-Collision Protocol to Identify Memoryless RFID Tags

Foundations of Distributed Systems: Tree Algorithms

Past questions from the last 6 years of exams for programming 101 with answers.

Lecture5: Lossless Compression Techniques

Splay tree concept Splaying operation: double-rotations Splay insertion Splay search Splay deletion Running time of splay tree operations

Collectives Pattern. Parallel Computing CIS 410/510 Department of Computer and Information Science. Lecture 8 Collective Pattern

1 Permutations. 1.1 Example 1. Lisa Yan CS 109 Combinatorics. Lecture Notes #2 June 27, 2018

UNIVERSITY of PENNSYLVANIA CIS 391/521: Fundamentals of AI Midterm 1, Spring 2010

Stack permutations and an order relation for binary trees

Collectives Pattern CS 472 Concurrent & Parallel Programming University of Evansville

Foundations of AI. 3. Solving Problems by Searching. Problem-Solving Agents, Formulating Problems, Search Strategies

CMPUT 396 Tic-Tac-Toe Game

have to get on the phone or family members for the names of more distant relatives.

Twin Binary Sequences: A Nonredundant Representation for General Nonslicing Floorplan

CHAPTER 5 PAPR REDUCTION USING HUFFMAN AND ADAPTIVE HUFFMAN CODES

MITOCW R18. Quiz 2 Review

A Level Computer Science H446/02 Algorithms and programming. Practice paper - Set 1. Time allowed: 2 hours 30 minutes

INF September 25, The deadline is postponed to Tuesday, October 3

Permutations. Example 1. Lecture Notes #2 June 28, Will Monroe CS 109 Combinatorics

Skip Lists S 3 S 2 S 1. 2/6/2016 7:04 AM Skip Lists 1

Lectures: Feb 27 + Mar 1 + Mar 3, 2017

Solving Problems by Searching

ProCo 2017 Advanced Division Round 1

AN ALTERNATIVE METHOD FOR ASSOCIATION RULES

1 Permutations. Example 1. Lecture #2 Sept 26, Chris Piech CS 109 Combinatorics

Universal Cycles for Permutations Theory and Applications

Foundations of AI. 3. Solving Problems by Searching. Problem-Solving Agents, Formulating Problems, Search Strategies

Checkpoint Questions Due Monday, October 7 at 2:15 PM Remaining Questions Due Friday, October 11 at 2:15 PM

Divide & conquer. Which works better for multi-cores: insertion sort or merge sort? Why?

Homework Assignment #1

Game Engineering CS F-24 Board / Strategy Games

CS188 Spring 2010 Section 3: Game Trees

An O(1) Time Algorithm for Generating Multiset Permutations

Programming Abstractions

Binary trees. Application: AVL trees and the golden ratio. The golden ratio. φ 1=1/φ 1. φ =1+

Informed search algorithms. Chapter 3 (Based on Slides by Stuart Russell, Richard Korf, Subbarao Kambhampati, and UW-AI faculty)

Coding for Efficiency

COCI 2008/2009 Contest #3, 13 th December 2008 TASK PET KEMIJA CROSS MATRICA BST NAJKRACI

CSE 100: BST AVERAGE CASE AND HUFFMAN CODES

The Theory Behind the z/architecture Sort Assist Instructions

Adversarial Search 1

A Note on Downup Permutations and Increasing Trees DAVID CALLAN. Department of Statistics. Medical Science Center University Ave

MITOCW 7. Counting Sort, Radix Sort, Lower Bounds for Sorting

Problem Solving and Search

MITOCW watch?v=c6ewvbncxsc

Informatica Universiteit van Amsterdam. Performance optimization of Rush Hour board generation. Jelle van Dijk. June 8, Bachelor Informatica

Will Monroe June 28, with materials by Mehran Sahami and Chris Piech. Combinatorics

Printing: You may print to the printer at any time during the test.

COMP 2804 solutions Assignment 4

CS188 Spring 2010 Section 3: Game Trees

Communication Theory II

: Principles of Automated Reasoning and Decision Making Midterm

Adverserial Search Chapter 5 minmax algorithm alpha-beta pruning TDDC17. Problems. Why Board Games?

16.410/413 Principles of Autonomy and Decision Making

Game Tree Search. CSC384: Introduction to Artificial Intelligence. Generalizing Search Problem. General Games. What makes something a game?

6.034 Quiz 2 20 October 2010

Games (adversarial search problems)

Intelligent Agents & Search Problem Formulation. AIMA, Chapters 2,

Lecture 20: Combinatorial Search (1997) Steven Skiena. skiena

Algorithms for Data Structures: Search for Games. Phillip Smith 27/11/13

CSE 332: Data Structures and Parallelism Games, Minimax, and Alpha-Beta Pruning. Playing Games. X s Turn. O s Turn. X s Turn.

Game Theory and Algorithms Lecture 19: Nim & Impartial Combinatorial Games

Data Structures and Algorithms

Backtracking. Chapter Introduction

Algorithms and Data Structures CS 372. The Sorting Problem. Insertion Sort - Summary. Merge Sort. Input: Output:

Computer Science and Software Engineering University of Wisconsin - Platteville. 4. Game Play. CS 3030 Lecture Notes Yan Shi UW-Platteville

Module 3. Problem Solving using Search- (Two agent) Version 2 CSE IIT, Kharagpur

Game Playing Beyond Minimax. Game Playing Summary So Far. Game Playing Improving Efficiency. Game Playing Minimax using DFS.

Graph Application in The Strategy of Solving 2048 Tile Game

Transcription:

CSS 343 Data Structures, Algorithms, and Discrete Math II Balanced Search Trees Yusuf Pisan

Height Height of a tree impacts how long it takes to find an item Balanced tree O(log n) vs Degenerate tree O(n) What can we do? Reduce height - have 3, 4 or more children for each node Keep tree balanced 2-3 Tree Up to 3 children: left, middle, right Up to 2 data items: small (S) and large (L) S is greater than each item in left subtree S is smaller than each item in middle subtree L is larger than each item in middle subtree All leaves at same level 2-node: single data, 2 children 3-node: 2 data, 3 children 2

2-3 Insertion 1. Insert into the appropriate node 2. If node has 3 data value (overfull), the node splits and the middle data gets sent to the parent. 3. If the parent node is overfull, the parent splits and the middle data gets set to the parent's parent Inserting 10, 20, 30, 40, 50, 60 below. Exercise: Insert prime numbers 2 to 19 3

2-3 Inorder Traversal if at a leaf node visit data item(s) else if node has 1 item inorder left subtree visit data item inorder right subtree else // must have 2 data items inorder left subtree visit small data item inorder middle subtree visit large data item inorder right subtree 4

Delete 70 1. Swap 70 with inorder successor, 80 2. Delete the 70 from the node 3. If the node still has another data item, done 4. If the node does not have a data item, delete node 5. 3-node with 2 values must have 3 children not 2, so move smaller value from parent to left child 2-3 Deletion https://people.ok.ubc.ca/ylucet/ds/algorithms. html uses inorder predecessor 5

Delete 100 5. 6. All leaves not at same level, so a. Check the siblings of the now-empty leaf and distribute values is sibling has 2 b. If no sibling has two items, merge the leaf with an adjacent sibling by moving an item down from the parent into the sibling and deleting the empty leaf. c. If parent became empty, apply merging (6) step recursively 2-3 Deletion 6

Delete 80 5. 6. All leaves not at same level, so a. Check the siblings of the now-empty leaf and distribute values is sibling has 2 b. If no sibling has two items, move an item down from the parent into the sibling and deleting the empty leaf. c. If parent became empty, apply merging (6) step recursively 2-3 Deletion 7

Deleting 70 8

Best-First Search Generalizing breadth-first search to find a specific vertex Instead of queue us priorityqueue Cost function f(n) is the priority UPDATED Add initial vertex to PQ OPEN while PQ is not empty w = top of queue if w is targetvertex, return add w to CLOSED list for each vertex u adjacent to w if u is not in CLOSED calculate f(u), push u to PQ OPEN with cost f(u) A node is OPEN, if we will still look at paths from that node A node is CLOSED, when it has been fully explored f(n) == depth(n) or all edges are of length 1, then we have breadth-first search EXplore nodes at distance 1, and then distance 2, and then distance 3,. if f(n) is sum of edge costs from start to n, we have Dijkstra CLOSED is equivalent to vertexset OPEN keeps track of shortest paths so far 9

2-3-4 Tree 10

2-3-4 Tree 4-nodes, nodes with 3 data items and 4 children: left, middleleft, middleright, right Up to 3 data items: S, M and L Insertion moves extra item to parent BUT to avoid the return path it preemptively splits 4-nodes (with 3 values) on the way down middle value gets pushed up to parent Preemptive split 11

2-3 vs 2-3-4 tree Both are balanced Reduction in height leads to larger number of comparisons, so not much gained 2-3-4 tree has single pass insertion and removal, more efficient than 2-3 tree How about 2-3-4-5 -20 tree? Too many comparisons BUT worth it if implemented for external storage where moving from node-to-node is more expensive than comparisons 2-3-4 tree good for dictionaries, databases, file systems 12

AVL (Adelson-Velskii and Landis) AVL Trees Binary search tree Balanced - height close to the minimum Rotations restore the balance Common to keep track of the height of each node 13

Left Rotate: (Right-Right, right child s right child is heavy) Right Rotate: (left-left, left child s left child is heavy) 14

Insert: 3, 2, 1, 4, 5, 6, 7 into a fresh AVL tree. Show each step of the process, e.g., for each insertion: heights, rotation node, and tree before and after each rotation https://people.ok.ubc.ca/ylucet/ds/avltree.html 15

Insert: 3, 2, 1, 4, 5, 6, 7 into a fresh AVL tree. Insert 5, unbalances 3, left-rotate on 4. Insert 6, unbalances 2, left-rotate on 4 Insert 7, unbalances 5, left-rotate on 6 16

Red-Black Trees 2-3-4 trees require more storage than a BST Convert 3-nodes and 4-nodes to binary nodes All the child pointers in the original 2-3-4 tree be black, and use red child pointers to link the 2-nodes that result when you split 3-nodes and 4-nodes. 17

Red-Black Trees Traversal: Like BST, ignore link color Root is black Every red node has a black parent Any children of red node are black (red node cannot have red children) Every path from root to leaf contains same number of black nodes Carrano show 2-3-4 insertion equivalence to red-black tree, but might be easier to think about it separetely 18

Red-Black Trees - Insertion Case 0: X is the root. Make X black Case 1: Both parent and uncle are red - push blackness down from grandparent 1. Color the grandparent red 2. Color the parent black uncle sibling of parent node. 3. Color the uncle black Every node is red on insertion Empty child is considered black 4. Point X to grandparent Case 2: Parent is red, uncle is black. X and its parent are both left or both right children 1. Color the grandparent red 2. Color parent black 3. Rotate 3a. If both left children, rotate right on parent 3b. If both right children, rotate left on parent 4. Point X to parent Case 3: Parent is red, uncle is black. X and its parent are opposite type of children 1. Need double rotation. 2. Rotate 2a. If parent is left, child is right, rotate left on X 3. 2b. If parent is right, child is left, rotate right on X 4. Set X to new child (which used to be parent) and proceed to Case 2 19

Case 0: X is the root. Make X black Case 1: Both parent and uncle are red - push blackness down from grandparent Case 2: Parent is red, uncle is black. X and its parent are both left or both right children Case 3: Parent is red, uncle is black. uncle sibling of parent node. Every node is red on insertion Empty child is considered black Add 10, Case-0, root is black Add 20, parent is black Add 30, Case-2, parent (20) is red, uncle (empty) is black, Color the grandparent (10) red. Color parent (20) black. Both right children, rotate-left on parent (20). Point X to parent (20) Case-0, X (20) is root, make it black 20

Case 0: X is the root. Make X black Case 1: Both parent and uncle are red - push blackness down from grandparent Case 2: Parent is red, uncle is black. X and its parent are both left or both right children Case 3: Parent is red, uncle is black. uncle sibling of parent node. Every node is red on insertion Empty child is considered black Add 40, Case-1 parent (30) and uncle (10) are red, push blackness down. Color the parent (30) black, Color the uncle (10) black, point X to grandparent (20) Case-0, X (20) is root, make it black 21

Case 0: X is the root. Make X black Case 1: Both parent and uncle are red - push blackness down from grandparent Case 2: Parent is red, uncle is black. X and its parent are both left or both right children Case 3: Parent is red, uncle is black. uncle sibling of parent node. Every node is red on insertion Empty child is considered black Add 3, Case-2 parent (5) is red, uncle (empty) is black. Color the grandparent (10) red. Color parent (5) black. Both left children, rotate-right on parent (5). Point X to parent (5), nothing to be done 22

Case 0: X is the root. Make X black Case 1: Both parent and uncle are red - push blackness down from grandparent Case 2: Parent is red, uncle is black. X and its parent are both left or both right children Case 3: Parent is red, uncle is black. Add 12, Case-3 parent (15) is red, uncle (empty) is black. X and its parent are opposite type of children. Need double rotation. Parent (15) is right, X (12) is left child, so rotate right on X (11). Set X to new child (15). Go to Case-2 Parent (12) is red, uncle (empty) is black. Color the grandparent (10) red. Color parent (12) black. Both right children, rotate-left on parent (12). Point X to parent (10) 23

Review 2-3 Trees 2-3-4 Trees AVL Trees Red-Black Trees (equivalent to 2-3-4) 24