Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Slides:



Advertisements
Similar presentations
Binary Search Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 19 (continued) © 2002 Addison Wesley.
Advertisements

Chapter 14 Multi-Way Search Trees
Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees.
CS202 - Fundamental Structures of Computer Science II
AA Trees another 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 balanced.
EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
ITEC200 Week 11 Self-Balancing Search Trees. 2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 47 Red Black Trees.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Trees and Red-Black Trees Gordon College Prof. Brinton.
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.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
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.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Balanced Search Trees Chapter Chapter Contents AVL Trees Single Rotations Double Rotations Implementation Details 2-3 Trees Searching Adding Entries.
Course: Programming II - Abstract Data Types Red-Black TreesSlide Number 1 Balanced Search Trees Binary Search Tree data structures can allow insertion,
10/20/2015 2:03 PMRed-Black Trees v z. 10/20/2015 2:03 PMRed-Black Trees2 Outline and Reading From (2,4) trees to red-black trees (§9.5) Red-black.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
CS-2851 Dr. Mark L. Hornick 1 Okasaki’s Insertion Method for Red/Black balancing A step-by-step procedure for maintaining balance through application of.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Balanced Search Trees Fundamental Data Structures and Algorithms Margaret Reid-Miller 3 February 2005.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Trees Chapter.
2-3 Trees, Trees Red-Black Trees
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 12: Multi-way Search Trees Java Software Structures: Designing.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
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.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
1 More Trees Trees, Red-Black Trees, B Trees.
Copyright © 2005 Pearson Addison-Wesley. All rights reserved Balancing Binary Trees There are many approaches to balancing binary trees One method.
Balanced Search Trees (partial) Chapter 29 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall - Edited by Nadia Al-Ghreimil.
Keeping Binary Trees Sorted. Search trees Searching a binary tree is easy; it’s just a preorder traversal public BinaryTree findNode(BinaryTree node,
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
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.
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
Chapter 47 Red Black Trees
AA Trees.
Balanced Search Trees Modified from authors’ slides.
Chapter 48 Red Black Trees
Red-Black Trees v z Red-Black Trees Red-Black Trees
Red-Black Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Red-Black Trees 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
TCSS 342, Winter 2006 Lecture Notes
Red-Black Trees v z /20/2018 7:59 AM Red-Black Trees
Red-Black Trees v z Red-Black Trees Red-Black Trees
CS202 - Fundamental Structures of Computer Science II
Chapter 43 Red Black Trees
Balanced Search Trees (partial)
2-3-4 Trees Red-Black Trees
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees v z /6/ :10 PM Red-Black Trees
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Contents AVL Trees  Single Rotations  Double Rotations  Implementation Details 2-3 Trees  Searching a 2-3 Tree  Adding Entries to a 2-3 Tree  Splitting Nodes During Addition Copyright ©2012 by Pearson Education, Inc. All rights reserved

Contents 2-4 Trees  Adding Entries to a 2-4 Tree  Comparing AVL, 2-3, and 2-4 Trees Red-Black Trees  Properties of a Red-Black Tree  Adding Entries to a Red-Black Tree  Java Class Library: The Class TreeMap B-Trees Copyright ©2012 by Pearson Education, Inc. All rights reserved

Objectives Perform rotation to restore balance of an AVL tree after addition Search for or add entry to 2-3 tree Search for or add entry to 2-4 tree Form red-black tree from given 2-4 tree Search for or add entry to red-black tree Describe purpose of a B-tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Balance Recall: operations on binary search tree are O(log n)  Tree must be balanced Add and remove operations do not necessarily maintain that balance We seek ways to maintain balance  And thus maintain efficiency Copyright ©2012 by Pearson Education, Inc. All rights reserved

AVL Trees Binary search tree that rearranges nodes whenever it becomes unbalanced.  Happens during addition or removal of node Uses  Left rotation  Right rotation  Balanced node – the root of a balanced tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-1 After inserting (a) 60; (b) 50; and (c) 20 into an initially empty binary search tree, the tree is not balanced; (d) a corresponding AVL tree rotates its nodes to restore balance Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-2 (a) Adding 80 to the tree in Figure 27-1d does not change the balance of the tree; (b) a subsequent addition of 90 makes the tree unbalanced ; (c) a left rotation restores its balance Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-3 Before and after an addition to an AVL subtree that requires a right rotation to maintain its balance Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-4 Before and after a right rotation restores balance to an AVL tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-5 Before and after an addition to an AVL subtree that requires a left rotation to maintain its balance Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 27-6 (a) Adding 70 to the tree in Figure 27-2c destroys its balance; to restore the balance, perform both (b) a right rotation and (c) a left rotation Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-7 Before and after an addition to an AVL subtree that requires both a right rotation and a left rotation to maintain its balance Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-7 Before and after an addition to an AVL subtree that requires both a right rotation and a left rotation to maintain its balance Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-8 (a) The AVL tree in Figure 27-6c after additions that maintain its balance; (b) after an addition that destroys the balance; Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-8 (c) after a left rotation; (d) after a right rotation Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-9 Before and after an addition to an AVL subtree that requires both a left rotation and a right rotation to maintain its balance Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-9 Before and after an addition to an AVL subtree that requires both a left rotation and a right rotation to maintain its balance Copyright ©2012 by Pearson Education, Inc. All rights reserved

Double Rotations Double rotation is accomplished by performing two single rotations:  A rotation about node N’s grandchild G (its child’s child)  A rotation about node N’s new child Copyright ©2012 by Pearson Education, Inc. All rights reserved

Double Rotations Imbalance at node N of an AVL tree can be corrected by a double rotation if:  Addition occurred in the left subtree of N’s right child (right-left rotation), or  Addition occurred in the right subtree of N’s left child (left-right rotation) Copyright ©2012 by Pearson Education, Inc. All rights reserved

Rotation after Addition Following an addition to an AVL tree, temporary imbalance might occur.  Let N be an unbalanced node closest to new leaf  Either a single or double rotation will restore the tree’s balance Note: similar action restores balance after removal Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE The result of adding 60, 50, 20, 80, 90, 70, 55, 10, 40, and 35 to an initially empty (a) AVL tree; (b) binary search tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Implementation Details Note source code of class AVLTree, Listing 27-1 Listing 27-1  Note private methods  Note public methods  Note method rotateLeftRight is left as an exercise Copyright ©2012 by Pearson Education, Inc. All rights reserved Note: Code listing files must be in same folder as PowerPoint files for links to work Note: Code listing files must be in same folder as PowerPoint files for links to work

2-3 Trees A general search tree  Interior nodes must have either two or three children A 2-Node has one data item, s  Has two children  Data s greater than any data in left subtree  Data s less than any data in right subtree Copyright ©2012 by Pearson Education, Inc. All rights reserved

2-3 Trees A 3-Node has two data item, s and l  Item s is the smaller  Item l is the larger Has three children  Data that is less than s is in left subtree  Data greater than l is in right subtree  Data between s and l is in middle subtree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure (a) A 2-node; (b) a 3-node Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure A 2-3 tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure An initially empty 2-3 tree after adding (a) 60 and (b) 50; (c), (d) adding 20 causes the 3-node to split Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure The 2-3 tree after adding (a) 80; (b) 90; (c) 70 Copyright ©2012 by Pearson Education, Inc. All rights reserved

figure Adding 55 to the 2-3 tree causes a leaf and then the root to split Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure The 2-3 tree after adding (a) 10; (b), (c) 40 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure The 2-3 tree after adding 35 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure Splitting a leaf to accommodate a new entry when the leaf’s parent contains (a) one entry; Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure Splitting a leaf to accommodate a new entry when the leaf’s parent contains (b) two entries Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure Splitting an internal node to accommodate a new entry Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure Splitting the root to accommodate a new entry Copyright ©2012 by Pearson Education, Inc. All rights reserved

2-4 Trees Sometimes called a tree General search tree whose interior nodes must have  Either two, three, or four children  Leaves occur on the same level 4-node contains three data items s, m, and l  Has four children Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure A 4-node Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure An initially empty 2-4 tree after adding (a) 60; (b) 50; (c) 20 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure The 2-4 tree after (a) splitting the root; (b) adding 80; (c) adding 90 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure The 2-4 tree after (a) splitting a 4-node; (b) adding 70 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure The 2-4 tree after adding (a) 55; (b) 10; (c) 40 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure The 2-4 tree after (a) splitting the leftmost 4-node; (b) adding 35 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure Three balanced search trees obtained by adding 60, 50, 20, 80, 90, 70, 55, 10, 40, and 35: (a) AVL tree; (b) 2-3 tree; (c) 2-4 tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Red-Black Trees Red-black tree is a binary tree that is equivalent to a 2-4 tree. Adding entry to a red-black tree like adding entry to a 2-4 tree  Only one pass from root to leaf is necessary. But is a binary tree  Uses simpler operations to maintain i balance Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure Using 2-nodes to represent (a) a 4-node Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure Using 2-nodes to represent (b) a 3-node Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure A red-black tree that is equivalent to the 2-4 tree in Figure 27-27cFigure 27-27c Copyright ©2012 by Pearson Education, Inc. All rights reserved

Properties of a Red-Black Tree The root is black. Every red node has black parent. Any children of red node are black;  Red node cannot have red children. Every path from the root to a leaf contains same number of black nodes. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Adding Entries to a Red-Black Tree Adding a leaf  Use black for a new leaf, we increase number of black nodes on paths to that leaf  But this violates fourth property of a red-black tree  Thus, new node must be red Adding or removing entries can change color of various nodes Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure The result of adding a new entry e to a one-node red-black tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure The possible results of adding a new entry e to a two-node red-black tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE The possible results of adding a new entry e to a two-node red-black tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE The possible results of adding a new entry e to a two-node red-black tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE The possible results of adding a new entry e to a two-node red-black tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE The possible results of adding a new entry e to a two-node red-black tree: mirror images of Figure Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE The possible results of adding a new entry e to a two-node red-black tree: mirror images of Figure Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE The possible results of adding a new entry e to a two-node red-black tree: mirror images of Figure Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE The possible results of adding a new entry e to a two-node red-black tree: mirror images of Figure Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure Splitting a 4-node whose parent is a 2-node in (a) a 2-4 tree; (b) a red-black tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure Splitting a 4-node that has a 3-node parent within (a) a 2-4 tree; (b) a red-black tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure Splitting a 4-node that has a red parent within a red-black tree: Case 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure Splitting a 4-node that has a red parent within a red-black tree: Case 2 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure Splitting a 4-node that has a red parent within a red-black tree: Case 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure Splitting a 4-node that has a red parent within a red-black tree: Case 4 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Java Class Library: The Class TreeMap Uses a red-black tree to implement the methods in the interface SortedMap SortedMap extends the interface Map Methods such as get, put, remove, and containsKey are each an O(log n) operation. Copyright ©2012 by Pearson Education, Inc. All rights reserved

B-Trees M-way search tree  General tree whose nodes have up to m children each Node that has k - 1 data items and k children is called a k-node Copyright ©2012 by Pearson Education, Inc. All rights reserved

B-Trees B-tree of order m is a balanced multiway search tree of order m  The root has either no children or between 2 and m children.  Other interior nodes (nonleaves) have between m/2 and m children each.  All leaves are on the same level Copyright ©2012 by Pearson Education, Inc. All rights reserved

End Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved