Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.

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

1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Chapter 4: Trees Part II - AVL Tree
Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees.
CS202 - Fundamental Structures of Computer Science II
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
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.
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
A balanced life is a prefect life.
ITEC200 Week 11 Self-Balancing Search Trees. 2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.
Balanced Search Trees CS Data Structures Mehmet H Gunes Modified from authors’ slides.
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
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.
Multi-Way search Trees Trees: a. Nodes may contain 1 or 2 items. b. A node with k items has k + 1 children c. All leaves are on same level.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Advanced Implementation of Tables.
2-3 Trees Professor Sin-Min Lee. Contents n Introduction n The 2-3 Trees Rules n The Advantage of 2-3 Trees n Searching For an Item in a 2-3 Tree n Inserting.
Balanced Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Balanced Search Trees Chapter 27 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.
Balanced Trees AVL Trees Red-Black Trees 2-3 Trees Trees.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Balanced Search Trees Fundamental Data Structures and Algorithms Margaret Reid-Miller 3 February 2005.
Data Structures CSCI 2720 Spring 2007 Balanced Trees.
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
2-3 Trees, Trees Red-Black Trees
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Binary Search Tree vs. Balanced Search Tree. Why care about advanced implementations? Same entries, different insertion sequence: 10,20,30,40,50,60,70,
Chapter 13 A Advanced Implementations of Tables. © 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the.
File Organization and Processing Week Tree Tree.
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Heaps Chapter 17 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Binary Search Trees Chapter 7 Objectives
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
Lecture Trees Professor Sin-Min Lee.
Trees Chapter 15.
Data Structures Balanced Trees CSCI 2720 Spring 2007.
AA Trees.
Balanced Search Trees Modified from authors’ slides.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Chapter 16 Tree Implementations
Data Structures Balanced Trees CSCI
TCSS 342, Winter 2006 Lecture Notes
CS202 - Fundamental Structures of Computer Science II
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
CS202 - Fundamental Structures of Computer Science II
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
2-3-4 Trees Red-Black Trees
Advanced Implementation of Tables
Advanced Implementation of Tables
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Contents Balanced Search Trees 2-3 Trees Trees Red-Black Trees AVL Trees Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Balanced Search Trees Height of a binary search tree sensitive to order of insertions and removals  Minimum = log 2 (n + 1)  Maximum = n Various search trees can retain balance despite insertions and removals Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Balanced Search Trees FIGURE 19-1 (a) A binary search tree of maximum height; (b) a binary search tree of minimum height Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3 Trees FIGURE 19-2 A 2-3 tree of height 3 A 2-3 tree not a binary tree A 2-3 tree never taller than a minimum- height binary tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3 Trees Placing data items in nodes of a 2-3 tree  A 2-node (has two children) must contain single data item greater than left child’s item(s) and less than right child’s item(s)  A 3-node (has three children) must contain two data items, S and L, such that S is greater than left child’s item(s) and less than middle child’s item(s); L is greater than middle child’s item(s) and less than right child’s item(s).  Leaf may contain either one or two data items. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3 Trees FIGURE 19-3 Nodes in a 2-3 tree: (a) a 2-node; (b) a 3-node Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013`

2-3 Trees FIGURE 19-4 A 2-3 tree View Header file for a class of nodes for a 2-3 tree, Listing 19-1Listing 19-1.htm code listing files must be in the same folder as the.ppt files for these links to work Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Traversing a 2-3 Tree Traverse 2-3 tree in sorted order by performing analogue of inorder traversal on binary tree: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Searching a 2-3 Tree Retrieval operation for 2-3 tree similar to retrieval operation for binary search tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Searching a 2-3 Tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Searching a 2-3 Tree Possible to search 2-3 tree and shortest binary search tree with approximately same efficiency, because:  Binary search tree with n nodes cannot be shorter than log 2 (n + 1)  2-3 tree with n nodes cannot be taller than log 2 (n + 1)  Node in a 2-3 tree has at most two items Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Searching a 2-3 Tree FIGURE 19-5 (a) A balanced binary search tree; Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Searching a 2-3 Tree FIGURE 19-5 (b) a 2-3 tree with the same entries Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Searching a 2-3 Tree FIGURE 19-6 (a) The binary search tree of Figure 19-5a after inserting the sequence of values 32 through 39 (b) the 2-3 tree of Figure 19-5 b after the same insertions Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Inserting Data into a 2-3 Tree FIGURE 19-7 After inserting 39 into the tree in Figure 19-5b Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Inserting Data into a 2-3 Tree FIGURE 19-8 The steps for inserting 38 into the tree in Figure 19-7: (a) The located node has no room; (b) the node splits; (c) the resulting tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Inserting Data into a 2-3 Tree FIGURE 19-9 After inserting 37 into the tree in Figure 19-8c Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Inserting Data into a 2-3 Tree FIGURE (a), (b), (c) The steps for inserting 36 into the tree in Figure 19-9 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Inserting Data into a 2-3 Tree FIGURE (d) the resulting tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Inserting Data into a 2-3 Tree FIGURE The tree after the insertion of 35, 34, and 33 into the tree in Figure 19-10d Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Inserting Data into a 2-3 Tree FIGURE Splitting a leaf in a 2-3 tree when the leaf is a (a) left child; (b) right child Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Inserting Data into a 2-3 Tree FIGURE Splitting an internal node in a 2-3 tree when the node is a (a) left child; (b) right child Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Inserting Data into a 2-3 Tree FIGURE Splitting the root of a 2-3 tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Inserting Data into a 2-3 Tree Summary of insertion strategy Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Inserting Data into a 2-3 Tree Summary of insertion strategy Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Removing Data from a 2-3 Tree FIGURE (a) A 2-3 tree; (b), (c), (d), (e) the steps for removing 70; Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Removing Data from a 2-3 Tree FIGURE (f) the resulting tree; Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Removing Data from a 2-3 Tree FIGURE (a), (b), (c) The steps for removing 100 from the tree in Figure 19-15f; (d) the resulting tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Removing Data from a 2-3 Tree FIGURE The steps for removing 80 from the tree in Figure 19-16d Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Removing Data from a 2-3 Tree FIGURE The steps for removing 80 from the tree in Figure 19-16d Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Removing Data from a 2-3 Tree FIGURE Results of removing 70, 100, and 80 from (a) the 2-3 tree of Figure a and (b) the binary search tree of Figure 19-5 a Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Removing Data from a 2-3 Tree Algorithm for removing data from a 2-3 tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Removing Data from a 2-3 Tree Algorithm for removing data from a 2-3 tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Removing Data from a 2-3 Tree Algorithm for removing data from a 2-3 tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Removing Data from a 2-3 Tree FIGURE (a) Redistributing values; (b) merging a leaf; Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Removing Data from a 2-3 Tree FIGURE (c) redistributing values and children; (d) merging internal nodes Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Removing Data from a 2-3 Tree FIGURE (e) deleting the root Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees FIGURE A tree with the same data items as the 2-3 tree in Figure 19-6 b Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees Rules for placing data items in the nodes of a tree  2-node (two children), must contain a single data item that satisfies relationships pictured in Figure 19-3 a.  3-node (three children), must contain a single data item that satisfies relationships pictured in Figure 19-3 b. ... Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees  4-node (four children) must contain three data items S, M, and L that satisfy: S is greater than left child’s item(s) and less than middle-left child’s item(s) M is greater than middle-left child’s item(s) and less than middle-right child’s item(s); L is greater than middle-right child’s item(s) and less than right child’s item(s).  A leaf may contain either one, two, or three data items Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees FIGURE A 4-node in a tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees Has more efficient insertion and removal operations than a 2-3 tree Has greater storage requirements due to the additional data members in its 4-nodes Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees Searching and Traversing a Tree  Simple extensions of the corresponding algorithms for a 2-3 tree Inserting Data into a Tree  Insertion algorithm splits a node by moving one of its items up to its parent node  Splits 4-nodes as soon as it encounters them on the way down the tree from the root to a leaf Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees FIGURE Inserting 20 into a one-node tree (a) the original tree; (b) after splitting the node; (c) after inserting 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees FIGURE After inserting 50 and 40 into the tree in Figure 19-22c Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees FIGURE The steps for inserting 70 into the tree in Figure 19-23: (a) after splitting the 4-node; (b) after inserting 70 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees FIGURE After inserting 80 and 15 into the tree in Figure 19-24b Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees FIGURE The steps for inserting 90 into the tree in Figure Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees FIGURE The steps for inserting 100 into the tree in Figure 19-26b Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees FIGURE Splitting a 4-node root during insertion into a tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees FIGURE Splitting a 4-node whose parent is a 2-node during insertion into a tree, when the 4-node is a (a) left child; (b) right child Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees FIGURE Splitting a 4-node whose parent is a 3-node during insertion into a tree, when the 4-node is a (a) left child Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees FIGURE Splitting a 4-node whose parent is a 3-node during insertion into a tree, when the 4-node is a (b) middle child Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees FIGURE Splitting a 4-node whose parent is a 3-node during insertion into a tree, when the 4-node is a (c) right child Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

2-3-4 Trees Removing Data from a Tree  Removal algorithm has same beginning as removal algorithm for a 2-3 tree  Locate the node n that contains the item I you want to remove  Find I ’s inorder successor and swap it with I so that the removal will always be at a leaf  If leaf is either a 3-node or a 4-node, remove I. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Red-Black Trees Use a special binary search tree—a red- black tree —to represent a tree Retains advantages of a tree without storage overhead The idea is to represent each 3-node and 4- node in a tree as an equivalent binary search tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Red-Black Trees FIGURE Red-black representation of (a) a 4-node; (b) a 3-node Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Red-Black Trees FIGURE A red-black tree that represents the tree in Figure Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Red-Black Trees Searching and traversing  Red-black tree is a binary search tree, search and traverse it by using algorithms for binary search tree Inserting, removing with a red-black tree  Adjust the insertion algorithms to accommodate the red-black representation Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Red-Black Trees FIGURE Splitting a red-black representation of a 4-node that is the root Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Red-Black Trees FIGURE Splitting a red-black representation of a 4-node whose parent is a 2-node, when the 4-node is a (a) left child Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Red-Black Trees FIGURE Splitting a red-black representation of a 4-node whose parent is a 2-node, when the 4-node is a (b) right child Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Red-Black Trees FIGURE Splitting a red-black representation of a 4-node whose parent is a 2-node Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Red-Black Trees FIGURE Splitting a red-black representation of a 4-node whose parent is a 2-node Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Red-Black Trees FIGURE Splitting a red-black representation of a 4-node whose parent is a 2-node Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

AVL Trees Named for inventors, Adel’son-Vel’skii and Landis A balanced binary search tree  Maintains height close to the minimum  After insertion or deletion, check the tree is still AVL tree – determine whether any node in tree has left and right subtrees whose heights differ by more than 1 Can search AVL tree almost as efficiently as minimum-height binary search tree. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

AVL Trees FIGURE (a) An unbalanced binary search tree; (b) a balanced tree after rotation; (c) a balanced tree after insertion Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

AVL Trees FIGURE (a) Before; (b) and after a single left rotation that decreases the tree’s height; (c) the rotation in general Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

AVL Trees FIGURE (a) Before; (b) and after a single left rotation that does not affect the tree’s height; (c) the rotation in general Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

AVL Trees FIGURE (d) the double rotation in general Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

End Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013