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

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

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

Topic 23 Red Black Trees

CSE 100: RED-BLACK TREES

CSI33 Data Structures

COMP 2804 solutions Assignment 4

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

Chapter 7: Sorting 7.1. Original

CS : Data Structures

MITOCW ocw lec11

Self-Adjusting Binary Search Trees. Andrei Pârvu

Stack permutations and an order relation for binary trees

MITOCW 6. AVL Trees, AVL Sort

Binary Search Tree (Part 2 The AVL-tree)

Twin Binary Sequences: A Nonredundant Representation for General Nonslicing Floorplan

DATA STRUCTURE TREES UNIT II

UMBC 671 Midterm Exam 19 October 2009

Enumerative Combinatoric Algorithms. Gray code

PRIORITY QUEUES AND HEAPS

5 AVL trees: deletion

Random Binary Search Trees. EECS 214, Fall 2017

PRIORITY QUEUES AND HEAPS. Lecture 19 CS2110 Spring 2014

CSc 110, Spring Lecture 40: Sorting Adapted from slides by Marty Stepp and Stuart Reges

The Theory Behind the z/architecture Sort Assist Instructions

From a Ball Game to Incompleteness

CS188 Spring 2010 Section 3: Game Trees

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

Announcements. Homework 1 solutions posted. Test in 2 weeks (27 th ) -Covers up to and including HW2 (informed search)

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

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

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

CS188 Spring 2010 Section 3: Game Trees

CHAPTER 5 PAPR REDUCTION USING HUFFMAN AND ADAPTIVE HUFFMAN CODES

An O(1) Time Algorithm for Generating Multiset Permutations

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

mywbut.com Two agent games : alpha beta pruning

Huffman Coding - A Greedy Algorithm. Slides based on Kevin Wayne / Pearson-Addison Wesley

Lab 6 This lab can be done with one partner or it may be done alone. It is due in two weeks (Tuesday, May 13)

Game Engineering CS F-24 Board / Strategy Games

ARTIFICIAL INTELLIGENCE (CS 370D)

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

MA/CSSE 473 Day 13. Student Questions. Permutation Generation. HW 6 due Monday, HW 7 next Thursday, Tuesday s exam. Permutation generation

Kenken For Teachers. Tom Davis January 8, Abstract

Simple Search Algorithms

Module 3 Greedy Strategy

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

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

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

(Lec19) Geometric Data Structures for Layouts

Lecture5: Lossless Compression Techniques

16.410/413 Principles of Autonomy and Decision Making

Adversary Search. Ref: Chapter 5

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

Module 3 Greedy Strategy

Problem A. Worst Locations

Areas of Various Regions Related to HW4 #13(a)

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

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

Reviewing the Person Information

FamilySearch. When you sign into FamilySearch, your own personalized home page will appear. This page will consistently change.

Solutions to Assignment-2 MOOC-Information Theory

CPS331 Lecture: Search in Games last revised 2/16/10

CMPUT 396 Tic-Tac-Toe Game

On Range of Skill. Thomas Dueholm Hansen and Peter Bro Miltersen and Troels Bjerre Sørensen Department of Computer Science University of Aarhus

Heuristics, and what to do if you don t know what to do. Carl Hultquist

MONTE-CARLO TWIXT. Janik Steinhauer. Master Thesis 10-08

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

Adversarial Search 1

Playing Games. Henry Z. Lo. June 23, We consider writing AI to play games with the following properties:

Lab #6: Op Amps, Part 1

Sorting. Suppose behind each door (indicated below) there are numbers placed in a random order and I ask you to find the number 41.

January 11, 2017 Administrative notes

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

Nested Monte-Carlo Search

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

Chapter 10 Error Detection and Correction 10.1

Problem Set 10 Solutions

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

Problem A Rearranging a Sequence

Computer programs for genealogy- a comparison of useful and frequently used features- presented by Gary Warner, SGGEE database manager.

MITOCW R11. Principles of Algorithm Design

FINDING AND MERGING DUPLICATES IN FAMILY TREE

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

EN1740 Computer Aided Visualization and Design Spring /1/2012 Brian C. P. Burke

43.1 Introduction. Foundations of Artificial Intelligence Introduction Monte-Carlo Methods Monte-Carlo Tree Search. 43.

Backtracking. Chapter Introduction

2 person perfect information

CS510 \ Lecture Ariel Stolerman

Universiteit Leiden Computer Science

Foundations of Distributed Systems: Tree Algorithms

and 6.855J. Network Simplex Animations

Click here to give us your feedback. New FamilySearch Reference Manual

Solving Problems by Searching

Graph Application in The Strategy of Solving 2048 Tile Game

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

6.034 Quiz 2 20 October 2010

Games and Adversarial Search II

MITOCW watch?v=c6ewvbncxsc

Information Theory and Communication Optimal Codes

2005 Fryer Contest. Solutions

Transcription:

Balanced Trees Balanced Trees 23 Tree Binary search trees are not guaranteed to be balanced given random inserts and deletes! Tree could degrade to O(n) operations Balanced search trees! Operations maintain a balanced tree 23 Trees 1 23 Trees 2 23 Tree Guaranteed to always be balanced! O(lg n) operations Each interior node has two or three children! Nodes with 2 children are called 2 nodes! Nodes with 3 children are called 3 nodes! NOT A BINARY TREE Data is stored in both internal nodes and leaves 23 Trees 3 2 Node 2 nodes have one data item and 2 children Search keys < S S Search keys >= S 23 Trees 4

3 Node 3 nodes have two data items and 3 children (a left child, a middle child, and a right child) Search keys < S S Search keys >= S and < Search keys >= 23 Trees 5 23 Tree A leaf may contain 1 or 2 data items 23 trees are good because they are easy to maintain as balanced! Operations take care of that for you Node class itemtype smallitem, largeitem Node *left, *middle, *right, *parent // you may find that you need other data members // to make your life easier // do not add a third item or a fourth child 23 Trees 6 23 Tree 90 Traversing a 23 Tree Inorder traversal 20 10 40 60 70 80 100 110 120 1 1 140 160 inorder (node* cur) if current inorder(cur>left) visit small item if it exists inorder(cur>middle) visit large item if it exists inorder(cur>right) 23 Trees 7 23 Trees 8

Searching a 23 Tree Insert // Assumes small and large exist. You will need to modify // to account for nodes with only one value search (Node* cur, itemtype key) if (cur) if (key is in cur) return cur else if (key < cur>small) search down left child else if (key > cur>large) search down right child else search down middle child To insert an item, find a leaf to put the item in then split nodes if necessary 23 Trees 9 23 Trees 10 Splitting a eaf Splitting an Internal Node P M P P M P S M P S M S n1 n2 P M If splitting node causes the parent to have 3 items and 4 children, you will then split an internal node S n1 n2 n S M a b c d a P n e S M b c d e n1 S n2 e a b c d a n1 P M S b c d e n2 23 Trees 11 23 Trees 12

Splitting the Root 23 Tree Root S M a b c d New Root M S a b c d Start with this tree 10 20 40 60 80 100 23 Trees 13 23 Trees 14 Insert ocate leaf to insert Insert 38 ocate leaf to insert 38 10 20 60 80 100 40 eaf to insert only has 1 data item! Add to the leaf 10 20 60 80 100 40 23 Trees 15 23 Trees 16

Insert 38 Conceptualize inserting 38 into this leaf! Do not actually add the item because the node can only hold 2 data items Insert 38 Determine! Smallest = 38! Middle =! argest = 40 10 20 60 80 100 38 40 23 Trees 17 23 Trees 18 Insert 38 Insert 38 Move middle value up to parent p Separate small and large values into two separate nodes that will be children of p 10 20 38 40 60 80 100 10 20 60 80 100 38 40 23 Trees 19 23 Trees 20

Insert 37 ocate leaf to insert 37 eaf contains 1 data value, just insert value Insert 36 ocate leaf to insert 36 Conceptualize inserting 36 into this leaf! Determine small (36), middle (37), and large (38) 10 20 37 38 40 60 80 100 10 20 36 37 38 40 60 80 100 23 Trees 21 23 Trees 22 Insert 36 Conceptualize moving middle value up to parent p! Do not actually move, node can t have 3 data values Insert 36 Conceptualize attaching as children to p the smallest and largest values! Do not actually attach because a node can t have 4 children 37 10 20 36 37 38 40 60 80 100 10 20 36 38 40 60 80 100 23 Trees 23 23 Trees 24

Insert 36 Parent p now has 3 data values and 4 children Split similar to leaf situation where leaf has 3 data values! You can generalize both situations into one Insert 36 Split parent p! Divide to small (), middle (37), and large ()! Move middle value to nodes parent! Small and large become new children, s and l 37 s 37 l 10 20 36 38 40 60 80 100 10 20 36 38 40 60 80 100 23 Trees 25 23 Trees 26 Insert 36 Divide 4 children! Two leftmost become children of s! Two rightmost become children of l 37 Insert 35 Insert 35! Inserts into leaf 37 10 20 36 38 40 60 80 100 10 20 35 36 38 40 60 80 100 23 Trees 27 23 Trees 28

Insert 34 Insert 34! Causes a split Insert 34 Insert 34! Causes a split 37 37 10 20 34 35 36 38 40 60 80 100 10 20 34 35 36 38 40 60 80 100 23 Trees 29 23 Trees Insert 34 Insert 34! Causes a split 37 Insert 33 Insert 33! Inserts into leaf 37 35 35 10 20 34 36 38 40 60 80 100 10 20 33 34 36 38 40 60 80 100 23 Trees 31 23 Trees 32

Insert Insert into a tree without duplicates insert (itemtype item) leaf = leaf node to insert item (may be null or have 1 or 2 data items) if (leaf is null only happens when root is null) add new root to tree with item else if (# data items in leaf = 1) add item to node else // leaf has 2 data items split ( leaf, item ) Insert (continued) // Item is to be inserted into n. The insertion // of item will cause n to have 3 items so n // must be split split ( Node* n, itemtype item, // you may need more ) if ( n is the root ) create a new node p else let p be the parent of n Replace node n with 2 nodes, n1 and n2, so that p is their parent Give n1 the item in n with the smallest value Give n2 the item in n with the largest value // continued on next slide 23 Trees 33 23 Trees 34 Insert (continued) if (n is not a leaf) n1 becomes the parent of n s two leftmost children n2 becomes the parent of n s two rightmost children x = the item in n that has the middle key value if ( adding x to p would cause p to have 3 items ) split (p, x) else add x to p Insert 32 In class exercise! Insert 32 into the tree below 37 35 10 20 33 34 36 38 40 60 80 100 23 Trees 35 23 Trees 36

Insert 32 Insert 32 37 37 35 33 35 10 20 32 33 34 36 38 40 60 80 100 10 20 32 34 36 38 40 60 80 100 23 Trees 37 23 Trees 38 Insert 32 Insert 32 37 33 37 33 35 10 20 32 34 36 38 40 60 80 100 35 10 20 32 34 36 38 40 60 80 100 23 Trees 23 Trees 40

Remove Remove Redistribute With insertion, we split nodes. With removing, we merge nodes P Deletion process needs to begin with a leaf but you might be deleting a value that is not a leaf! Swap item to delete with inorder successor S PS P S S P P Delete 70 80 90 S X Y PS X Y 60 80 100 60 70 100 23 Trees 41 23 Trees 42 Remove Redistribute Remove Merge PS P X S P X S PS P S S P PS P Take from right PS S S X Y S X Y P A B S A B 23 Trees 43 23 Trees 44

Remove Merge Remove Redistribute S Merge to the left A B A S B P S a b c d S P a b c d 23 Trees 45 23 Trees 46 Remove Redistribute Remove Merge PS P S P P S X Y PS X Y S S P a b c d e f g a b c d e f g a b c a b c 23 Trees 47 23 Trees 48

Remove Merge Deleting the Root S Empty root New root A B S A B S P S P a b c d e a b c d e a b c a b c 23 Trees 49 23 Trees 23 Tree Start with this tree Remove 65 65 is an internal node swap with inorder successor! Inorder successor will always be in a leaf 65 90 10 20 40 60 70 80 100 65 90 10 20 40 60 70 80 100 23 Trees 51 23 Trees 52

Remove 65 65 is now in an invalid location but that is okay because we will remove it Remove 65 Since there are 2 data values in the leaf, just remove data value 10 20 40 60 65 80 100 10 20 40 60 80 100 23 Trees 53 23 Trees 54 Delete 70 70 is an internal node swap with inorder successor! Inorder successor will always be in a leaf Delete 70 70 is now in an invalid location but that is okay we will be removing that node 80 90 10 20 40 60 80 100 10 20 40 60 70 100 23 Trees 55 23 Trees 56

Delete 70 Removing leaf leaves us with an invalid 23 tree Delete 70 Merge nodes to fix tree 80 90 90 10 20 40 60 100 10 20 40 60 80 100 23 Trees 57 23 Trees 58 Delete 100 100 is already leaf, just remove leaf Delete 100 Sibling has data item to spare, redistribute 90 90 10 20 40 60 80 10 20 40 60 80 23 Trees 59 23 Trees 60

Delete 100 Sibling has data item to spare, redistribute Delete 80 Swap 80 with inorder successor 80 80 10 20 40 60 90 10 20 40 60 90 23 Trees 61 23 Trees 62 Delete 80 Can t redistribute so merge nodes Delete 80 Can t redistribute so merge nodes Invalid 23 tree, continue recursively up the tree 90 10 20 40 60 10 20 40 60 90 23 Trees 63 23 Trees 64

Delete 80 Can t redistribute so merge nodes Delete 80 Can t redistribute so merge nodes 10 20 40 60 90 10 20 40 60 90 23 Trees 65 23 Trees 66 Delete 80 Root is now empty, set new root pointer Root 10 20 40 60 90 23 Trees 67 Delete deleteitem (itemtype item) node = node where item exists (may be null if no item) if (node) if (item is not in a leaf) swap item with inorder successor (always leaf) leafnode = new location of item to delete else leafnode = node delete item from leafnode if (leafnode now contains no items) fix (leafnode) 23 Trees 68

Delete // completes the deletion when node n is empty by either // removing the root, redistributing values, or merging nodes. // Note: if n is internal, it has only one child fix (Node* n, ) //may need more parameters { if (n is the root) { } else { remove the root set new root pointer et p be the parent of n Delete if ( some sibling of n has two items ) { Distribute items appropriately among n, the sibling and the parent (remember take from right first) if ( n is internal ) { Move the appropriate child from sibling n (May have to move many children if distributing across multiple siblings) } 23 Trees 69 23 Trees 70 Delete else { //merge nodes Choose an adjacent sibling s of n (remember, merge left first) Bring the appropriate item down from p into s if ( n is internal ) move n s child to s Delete In class exercise remove the following values from the tree in this order! 37, 70 37 Remove node n 35 } //end if if ( p is now empty ) fix (p) 10 20 33 34 36 38 40 60 80 100 }//end if 23 Trees 71 23 Trees 72

Delete 37 38 Delete 37 35 38 70 10 20 33 34 36 40 60 80 100 38 35 90 10 20 33 34 36 40 60 80 100 35 10 20 33 34 36 40 60 80 100 23 Trees 73 23 Trees 74 Delete 70 38 80 Delete 70 38 35 90 35 80 10 20 33 34 36 40 60 100 38 80 10 20 33 34 36 40 60 35 90 100 35 38 80 10 20 33 34 36 40 60 90 100 23 Trees 75 10 20 33 34 36 40 60 90 100 23 Trees 76