Chapter 13 A Advanced Implementations of Tables. © 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the.

Slides:



Advertisements
Similar presentations
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Advertisements

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.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Chapter 15 B External Methods – B-Trees. © 2004 Pearson Addison-Wesley. All rights reserved 15 B-2 B-Trees To organize the index file as an external search.
Balanced Search Trees CS Data Structures Mehmet H Gunes Modified from authors’ slides.
6/14/2015 6:48 AM(2,4) Trees /14/2015 6:48 AM(2,4) Trees2 Outline and Reading Multi-way search tree (§3.3.1) Definition Search (2,4)
Data Structures Topic #9. Today’s Agenda Continue Discussing Trees Examine the algorithm to insert Examine the algorithm to remove Begin discussing efficiency.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
© 2004 Goodrich, Tamassia (2,4) Trees
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Chapter 12 B Priority Queues. © 2004 Pearson Addison-Wesley. All rights reserved 12 B-2 The ADT Priority Queue: A Variation of the ADT Table The ADT priority.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and.
Marc Smith and Jim Ten Eyck
© 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.
Binary Search Trees Chapter 7 Objectives
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
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
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Data Structures CSCI 2720 Spring 2007 Balanced Trees.
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,
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.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Balanced search trees: trees (or 2-4) trees improve the efficiency of insertItem and deleteItem methods of 2-3 trees, because they are performed.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
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.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
Red Black Trees
Binary Search Tree Chapter 10.
Data Structures Balanced Trees CSCI
CS202 - Fundamental Structures of Computer Science II
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
2-3-4 Trees Red-Black Trees
Advanced Implementation of Tables
(2,4) Trees /24/2019 7:30 PM (2,4) Trees (2,4) Trees
Advanced Implementation of Tables
Binary Search Trees Chapter 7 Objectives
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Balanced search trees: trees.
Presentation transcript:

Chapter 13 A Advanced Implementations of Tables

© 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the binary search tree implementation of the ADT table is related to the tree’s height –Height of a binary search tree of n items Maximum: n Minimum:  log 2 (n + 1)  Height of a binary search tree is sensitive to the order of insertions and deletions Variations of the binary search tree –Can retain their balance despite insertions and deletions

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees A 2-3 tree (Example: Figure 13-4) –Has 2-nodes and 3-nodes A 2-node –A node with one data item and two children A 3-node –A node with two data items and three children –Is not a binary tree –Is never taller than a minimum-height binary tree A 2-3 tree with n nodes never has height greater than  log 2 (n + 1) 

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees Rules for placing data items in the nodes of a 2-3 tree –A 2-node must contain a single data item whose search key is Greater than the left child’s search key(s) Less than the right child’s search(s) –A 3-node must contain two data items whose search keys S and L satisfy the following S is –Greater than the left child’s search key(s) –Less than the middle child’s search key(s) L is –Greater than the middle child’s search key(s) –Less than the right child’s search key(s) –A leaf may contain either one or two data items

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees Figure 13.3 Nodes in a 2-3 tree a) a 2-node; b) a 3-node

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees Traversing a 2-3 tree (algorithm on page 662) –To traverse a 2-3 tree Perform the analogue of an inorder traversal Searching a 2-3 tree (algorithm on page 663) –Searching a 2-3 tree is as efficient as searching the shortest binary search tree Searching a 2-3 tree is O(log 2 n) Number of comparisons required to search a 2-3 tree for a given item –Approximately equal to the number of comparisons required to search a binary search tree that is as balanced as possible

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees Advantage of a 2-3 tree over a balanced binary search tree –Maintaining the balance of a binary search tree is difficult –Maintaining the balance of a 2-3 tree is relatively easy

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees: Inserting Into a 2-3 Tree Insertion into a 2-node leaf is simple Insertion into a 3-node causes it to divide Compare BST and 2-3 tree (Figures 13-5 and 13-6) Example (Figures 13-7 ~ 13-11)

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees: The Insertion Algorithm To insert an item I into a 2-3 tree –Locate the leaf at which the search for I would terminate –Insert the new item I into the leaf –If the leaf now contains only two items, you are done –If the leaf now contains three items, split the leaf into two nodes, n 1 and n 2 Figure Splitting a leaf in a 2-3 tree

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees: The Insertion Algorithm When an internal node contains three items –Split the node into two nodes –Accommodate the node’s children Figure Splitting an internal node in a 2-3 tree

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees: The Insertion Algorithm When the root contains three items –Split the root into two nodes –Create a new root node Algorithm on pages 670 – 671 Figure Splitting the root of a 2-3 tree

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees: Deleting from a 2-3 Tree Deletion from a 2-3 tree –Does not affect the balance of the tree Deletion from a balanced binary search tree –May cause the tree to lose its balance Example (Figures 13-15, 13-16, and 13-17) Comparison (Figure 13-18)

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees: The Deletion Algorithm Figure 13.19a and 13.19b a) Redistributing values; b) merging a leaf Algorithm on pages 677 – 678

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees: The Deletion Algorithm Figure 13.19c and 13.19d c) redistributing values and children; d) merging internal nodes

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees: The Deletion Algorithm Figure 13.19e e) deleting the root

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees – Concluding Remarks When analyzing the efficiency of the insertItem and deleteItem algorithms, it is sufficient to consider only the time required to locate the item A 2-3 implementation of a table is O(log 2 n) for all table operations A 2-3 tree is a compromise –Searching a 2-3 tree is not quite as efficient as searching a binary search tree of minimum height –A 2-3 tree is relatively simple to maintain

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees Rules for placing data items in the nodes of a tree –A 2-node must contain a single data item whose search keys satisfy the relationships pictured in Figure 13-3a –A 3-node must contain two data items whose search keys satisfy the relationships pictured in Figure 13-3b –A 4-node must contain three data items whose search keys S, M, and L satisfy the relationship pictured in Figure –A leaf may contain either one, two, or three data items Figure A 4-node in a tree

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees: Searching and Traversing a Tree Search and traversal algorithms for a tree are simple extensions of the corresponding algorithms for a 2-3 tree

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees: Inserting into a Tree Example (Figures ~ 13-27) The insertion algorithm for a tree –Splits a node by moving one of its items up to its parent node –Splits a 4-node as soon as it’s encountered on the way down the tree from the root to a leaf Result: when a 4-node is split and an item is moved up to the node’s parent, the parent cannot possibly be a 4-node and can accommodate another item

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees: Splitting 4-nodes During Insertion A 4-node is split as soon as it is encountered during a search from the root to a leaf The 4-node that is split will –Be the root, or –Have a 2-node parent, or –Have a 3-node parent Figure Splitting a 4-node root during insertion

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees: Splitting 4-nodes During Insertion Figure Splitting a 4-node whose parent is a 2-node during insertion

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees: Splitting 4-nodes During Insertion Figure Splitting a 4-node whose parent is a 3-node during insertion

© 2004 Pearson Addison-Wesley. All rights reserved 13 A-23

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees: Deleting from a Tree The deletion algorithm for a tree –Locate the node n that contains the item theItem –Find theItem ’s inorder successor and swap it with theItem (deletion will always be at a leaf) –If that leaf is a 3-node or a 4-node, remove theItem –To ensure that theItem does not occur in a 2-node Transform each 2-node encountered into a 3-node or a 4-node Reverse of Figures 13-28, 13-29, and 13-30

© 2004 Pearson Addison-Wesley. All rights reserved 13 A Trees: Concluding Remarks Advantage of 2-3 and trees –Easy-to-maintain balance Insertion and deletion algorithms for a tree require fewer steps than those for a 2-3 tree Allowing nodes with more than four children is counterproductive (for internal sorting)