1 CS 310 – Data Structures All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley. All rights reserved. photo ©Oregon Scenics used.

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

Binary Search Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 19 (continued) © 2002 Addison Wesley.
Chapter 4: Trees Part II - AVL Tree
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Topic 23 Red Black Trees "People in every direction No words exchanged No time to exchange And all the little ants are marching Red and black antennas.
CS202 - Fundamental Structures of Computer Science II
Balanced Binary Search Trees
A balanced life is a prefect life.
CSE332: Data Abstractions Lecture 9: B Trees Dan Grossman Spring 2010.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
Trees and Red-Black Trees Gordon College Prof. Brinton.
CS 206 Introduction to Computer Science II 12 / 01 / 2008 Instructor: Michael Eckmann.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
CS2420: Lecture 31 Vladimir Kulyukin Computer Science Department Utah State University.
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
B + -Trees (Part 1) COMP171. Slide 2 Main and secondary memories  Secondary storage device is much, much slower than the main RAM  Pages and blocks.
CSE 326: Data Structures B-Trees Ben Lerner Summer 2007.
CS 206 Introduction to Computer Science II 11 / 24 / 2008 Instructor: Michael Eckmann.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
Tirgul 6 B-Trees – Another kind of balanced trees.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
1 B-Trees Section AVL (Adelson-Velskii and Landis) Trees AVL tree is binary search tree with balance condition –To ensure depth of the tree is.
B-Tree. B-Trees a specialized multi-way tree designed especially for use on disk In a B-tree each node may contain a large number of keys. The number.
Balanced Trees Ellen Walker CPSC 201 Data Structures Hiram College.
ICS 220 – Data Structures and Algorithms Week 7 Dr. Ken Cosh.
More Trees Multiway Trees and 2-4 Trees. Motivation of Multi-way Trees Main memory vs. disk ◦ Assumptions so far: ◦ We have assumed that we can store.
1 B Trees - Motivation Recall our discussion on AVL-trees –The maximum height of an AVL-tree with n-nodes is log 2 (n) since the branching factor (degree,
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Balanced Trees AVL Trees Red-Black Trees 2-3 Trees Trees.
Course: Programming II - Abstract Data Types Red-Black TreesSlide Number 1 Balanced Search Trees Binary Search Tree data structures can allow insertion,
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
CSIT 402 Data Structures II
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
1 Data Structures and Algorithms Searching Red-Black and Other Dynamically BalancedTrees.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Starting at Binary Trees
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, VUW B Trees and B+ Trees COMP 261.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
CS 307 Fundamentals of Computer ScienceRed Black Trees 1 Topic 19 Red Black Trees "People in every direction No words exchanged No time to exchange And.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Keeping Binary Trees Sorted. Search trees Searching a binary tree is easy; it’s just a preorder traversal public BinaryTree findNode(BinaryTree node,
Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A.
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
COMP261 Lecture 23 B Trees.
Red Black Trees Colored Nodes Definition Binary search tree.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Data Structures Balanced Trees CSCI
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
TCSS 342, Winter 2006 Lecture Notes
AVL Trees CENG 213 Data Structures.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
B- Trees D. Frey with apologies to Tom Anastasio
Self-Balancing Search Trees
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

1 CS 310 – Data Structures All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley. All rights reserved. photo ©Oregon Scenics used with permissionOregon Scenics Trees Part II

2 Balanced binary search trees Binary search trees with an extra condition –Left and right subtrees must have the same height. –In practice, we use the condition that left and right subtrees can differ in height by no more than one. –Note: It is convenient to denote the hight of an empty tree as -1.

3 Keeping the tree balanced results in logarithmic search time. AVL trees Adelson-Velskii & Landis First balanced tree – 1962

4 Unbalanced tree insert(tree, 1) – results in an unbalanced tree

5 Which nodes to rebalance? Nodes along the path from the insertion point to the root might need rebalancing.

6 How can we get into trouble? Insertion into left subtree of left child Insertion into right subtree of left child

7 Where else? Symmetric problems when inserting into the right subtree –Insertion in the right subtree of the left child of the node in question. –Insertion in the right subtree of the right child of the node in question.

8 The outside cases When the unbalance comes from inserting on the outside of the tree, we can fix the problem with one rotation.

9 Single rotation

10 Concrete example

11 Symmetric case for single rotation

12

13 The inside cases Rotations on the inside are more difficult.

14 Double rotation rotation 1 swap child and grandchild

15 Double Rotation rotation 2 rotation between grandparent and new parent

16 Practice makes perfect Practice with

17 AVL implementation Implementation difficult Basic idea –For insertion into tree T, insert into appropriate left/right T lr subtree. –If height of T lr remains the same, all done. –Otherwise, we need to see if T has become unbalanced. If so, perform appropriate repairs with root T.

18

19 AVL In practice, requires two passes through tree –down: insertion –up: repair Better schemes have been proposed.

20 Red-Black trees Single top down pass for insertion & deletion Binary search trees with: –Colored nodes: red or black (null nodes treated as black) –Root is always black –If node is red  children are black –Constant black depth. Every path from node to null link has the same number of black nodes.

21 Sentinels Many implementations of red-black trees sometimes create special nodes called sentinels. Sentinel nodes are used in place of the null link to indicate leaves. In red-black trees, sentinels are always colored black.

22 Properties If there are B black nodes along each of the paths, the tree must have at least 2 B -1 black nodes. As there are never two consecutive red nodes: –height is at most 2log(N+1) –which implies logarithmic search

23 Sample red-black tree

24 Insertion New nodes are always inserted as leaves. What color? – black?  Other paths will no longer have the same number of black nodes. –red? If parent is black, then we are okay:

25 Inserting when the parent is red What color is the parent’s sibling? – black and inserted leaf is an outside child relative to grandparent: single rotation & recolor Color change ensures we do not have two consecutive red nodes. Note: Figure does not assume X is a leaf.

26 Red parent & black sibling We saw outside children  single rotation Inside children  double rotation

27 Inserting when the parent is red Parent’s sibling is also red?

28 Insertions with red parent & sibling Before rotationsSingle rotation Problems –consecutive red nodes –recoloring doesn’t help insert 95

29 Insertions with red parent & sibling Double rotation insert 79after first rotationafter second rotation What if the 80’s parent (originally node 70’s parent) had been red? Remember: This is a subtree, you could not construct a tree that looked like this.

30 What if 80’s parent had been red? We could try to propagate this up the tree, applying the rotations to the next higher level. Unfortunately, this puts us in the same situation as the AVL tree which requires two traversals.

31 Red-black tree Top down insertion We only get into trouble when the parent of the inserted node has a red sibling. By recognizing this, we can prevent it from happening.

32 Swapping colors On our way down, if we see two red children: we swap the parent and child colors:

33 Swapping colors Is this all right? –black depth preserved –What if the new red node is the root? It is a problem, but we can just recolor it black. –What if the parent is red? let us think about this…

34 Swapping colors when the parent is also red Parent’s sibling is black? –Swap colors. –Repair with single (slide 28) or double rotation (slide 29) Parent’s sibling is red? –Can’t happen! –Why not?

35 Top-down red-black tree Implementation is complicated by some special cases. Two tricks to ease implementation: –Instead of null links, we have a sentinel node which is always black. –The root pointer points to a pseudoroot node Contains a smaller than any other value (-∞). Right pointer points to real root.

36

37

38

39

40

41

42

43

44 insert 76 while (compare( item, current ) != 0 ) { great = grand; grand = parent; parent = current; current = compare( item, current ) < 0 ? current.left : current.right; // Check if two red children; // fix if so if (current.left.color == RED && current.right.color == RED) handleReorient( item ); }

45 insert 76 private void handleReorient(T item) { // flip color current.color = RED current.left.color = BLACK; current.right.color = BLACK; … }

46 insert 76 private void handleReorient(T item) { … // slightly rewritten from Weiss boolean leftOfGrand = compare(item, grand) < 0; boolean leftOfParent = compare(item, parent) < 0; if (parent.color == RED) { grand.color = RED; if (leftOfGrand != leftOfParent) { // double rotation parent = rotate(item, grand); } current = rotate(item, great); current.color = BLACK; } header.right.color = BLACK; }

47 insert 76 private void handleReorient(T item) { … // slightly rewritten from Weiss boolean leftOfGrand = true; boolean leftOfParent = true; if (parent.color == RED) { grand.color = RED; if (leftOfGrand != leftOfParent) { // double rotation parent = rotate(item, grand); } current = rotate(item, great); current.color = BLACK; } header.right.color = BLACK; }

48 insert 76 rotate(T item, RedBlackNode parent) { if (compare(item, parent) < 0) { … } else { return parent.right = compare(item, parent.right) < 0 ? rotateWithLeftChild(parent.right) : rotateWithRightChild(parent.right); }

49 insert 76 rotate(T item, RedBlackNode parent) { if (compare(item, parent) < 0) { … } else { return parent.right = compare(item, parent.right) < 0 ? rotateWithLeftChild(parent.right) : rotateWithRightChild(parent.right); }

50 insert 76 private void handleReorient(T item) { … // slightly rewritten from Weiss boolean leftOfGrand = true; boolean leftOfParent = true; if (parent.color == RED) { grand.color = RED; if (leftOfGrand != leftOfParent) { // double rotation parent = rotate(item, grand); } current = rotate(item, great); current.color = BLACK; } header.right.color = BLACK; }

51 insert 76 insert continues until current is sentinel parent and now we can insert 76 as a red node.

52 Red/Black tree applets or

53 Red/Black tree deletion More complicated than insertion. We will cover the basic ideas and omit the implementation: –Deleting black nodes causes problems  make sure we delete red nodes. –We replace the values in internal nodes.

54 Red/Black tree deletion X – current node S – sibling P – parent Assume sentinel red Consider what we can do when X’s children are black.

55 Case 1 If S has black children, we can perform a color flip.

56 Case 2 If sibling’s outer child is red, perform a single rotation.

57 Case 3 If sibling’s inner child is red, perform a double rotation.

58 Deletion when X is a leaf Recall sentinels are colored black. Therefore, we can consider X to have two black children and use the 3 cases just described.

59 X has a red child? In all three cases, X was colored red, so the color flip and rotations inappropriate. Without covering the specifics, we can move to the next level and perform an operation to make X one of the following: –red –a leaf node  use one of the three cases –or X has a single child: red child  delete X, make child black black child  use one of the three cases

60 b-trees Log N search seems less convincing when some operations are orders of magnitude slower than others. A fast hard drive in 2006 can find a disk block in about 7.5 ms, or about 133 uncorrelated accesses per s. In contrast, an AMD Sempron (top of the value line late 2006) can execute over 3,000 MFLOPS per s. Successful search in a 10 million record balanced binary search tree requires about 25 comparisons. –Trivial in RAM –About.2 s on disk assuming nobody else is using the system.

61 M-ary b-trees Data stored in leaves Nonleaf nodes –store up to M-1 keys –i th key is the smallest subkey in i+1 th subtree Root either –a leaf or –has between 2 to M children. All interior nodes (except root) ceil(M/2) to M children. All leaves at the same depth with ceil(L/2) to L data items (L maximum number of items, root leaf may have less)

62 Sample b-tree

63 Selecting M & L Optimal choice of M and L depends upon the minimum amount of information that can addressed on a disk. While disks typically operate on a small blocks of bytes (512), many operating systems group the blocks into a larger unit called a cluster.

64 Selecting M & L We typically choose M & L based upon the cluster size. M = floor(cluster size / key size) L = floor(cluster size / record size) In the worst case, we will access about log M/2 (N) clusters.

65 Adding to a b-tree Very easy when there’s room in the leaf node: insert 57

66 Adding to a full node Split into two leaves if possible: insert 55

67 When the interior node is full insert 40 cannot split easily

68 Splitting an interior node interior node covering 40: 8, 18, 26, and 35 leaf node: 35, 36, 37, 38, 39 Split leaf: [35, 36, 37], and [38, 39, 40] Split the interior node to: [8, 18], promote 26 to parent, [35, 38]

69 Splitting an interior node

70 b-tree insertion If parent is full, the process can be repeated.

71 b-tree deletion Just remove it when there are ceil(L/2) items will still remain. When the number of items is too small, merge nodes