Data Structures CSCI 2720 Spring 2007 Balanced Trees.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal, Amit Kumar
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Search Trees. John Edgar  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement.
Advanced Database Discussion B Trees. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if.
Data Structures and Algorithms
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 Binary Search Trees
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
A balanced life is a prefect life.
Data Structures and Algorithms1 B-Trees with Minimum=1 2-3 Trees.
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)
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.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
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
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.
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
General Trees and Variants CPSC 335. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black.
Indexing (cont.). Insertion in a B+ Tree Another B+ Tree
Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
© 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
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Balanced Trees Ellen Walker CPSC 201 Data Structures Hiram College.
B-trees (Balanced Trees) A B-tree is a special kind of tree, similar to a binary tree. However, It is not a binary search tree. It is not a binary tree.
Balanced Trees AVL Trees Red-Black Trees 2-3 Trees Trees.
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:
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.
Data Structures and Algorithms TREE-TRAVERSAL. Searching - Re-visited Binary tree O(log n) if it stays balanced Simple binary tree good for static collections.
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.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
© 2004 Goodrich, Tamassia Trees
CS 61B Data Structures and Programming Methodology Aug 7, 2008 David Sun.
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
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.
Binary Search Trees (BST)
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
1 Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
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.
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
Binary Search Trees Chapter 7 Objectives
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
Lecture Trees Professor Sin-Min Lee.
COMP261 Lecture 23 B Trees.
Data Structures Balanced Trees CSCI 2720 Spring 2007.
AA Trees.
Chapter 10.1 Binary Search Trees
(edited by Nadia Al-Ghreimil)
Data Structures and Algorithms
Data Structures Balanced Trees CSCI
(2,4) Trees (2,4) Trees 1 (2,4) Trees (2,4) Trees
CS202 - Fundamental Structures of Computer Science II
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
Data Structures and Algorithms
(2,4) Trees (2,4) Trees (2,4) Trees.
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
(2,4) Trees /24/2019 7:30 PM (2,4) Trees (2,4) Trees
(2,4) Trees (2,4) Trees (2,4) Trees.
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
Binary Search Trees < > = Dictionaries
Data Structures Using C++ 2E
CS210- Lecture 20 July 19, 2005 Agenda Multiway Search Trees 2-4 Trees
Presentation transcript:

Data Structures CSCI 2720 Spring 2007 Balanced Trees

CSCI 2720 Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees

CSCI 2720 Slide 3 Why care about advanced implementations? Same entries, different insertion sequence:  Not good! Would like to keep tree balanced.

CSCI 2720 Slide Trees  each internal node has either 2 or 3 children  all leaves are at the same level Features

CSCI 2720 Slide Trees with Ordered Nodes 2-node3-node leaf node can be either a 2-node or a 3-node

CSCI 2720 Slide 6 Example of 2-3 Tree

CSCI 2720 Slide 7 Traversing a 2-3 Tree inorder(in ttTree: TwoThreeTree) if(ttTree’s root node r is a leaf) visit the data item(s) else if(r has two data items) { inorder(left subtree of ttTree’s root) visit the first data item inorder(middle subtree of ttTree’s root) visit the second data item inorder(right subtree of ttTree’s root) } else { inorder(left subtree of ttTree’s root) visit the data item inorder(right subtree of ttTree’s root) }

CSCI 2720 Slide 8 Searching a 2-3 Tree retrieveItem(in ttTree: TwoThreeTree, in searchKey:KeyType, out treeItem:TreeItemType):boolean if(searchKey is in ttTree’s root node r) { treeItem = the data portion of r return true } else if(r is a leaf) return false else { return retrieveItem(appropriate subtree, searchKey, treeItem) }

CSCI 2720 Slide 9 What did we gain? What is the time efficiency of searching for an item?

CSCI 2720 Slide 10 Gain: Ease of Keeping the Tree Balanced Binary Search Tree 2-3 Tree both trees after inserting items 39, 38,... 32

CSCI 2720 Slide 11 Inserting Items Insert 39

CSCI 2720 Slide 12 Inserting Items Insert 38 insert in leaf divide leaf and move middle value up to parent result

CSCI 2720 Slide 13 Inserting Items Insert 37

CSCI 2720 Slide 14 Inserting Items Insert 36 insert in leaf divide leaf and move middle value up to parent overcrowded node

CSCI 2720 Slide 15 Inserting Items... still inserting 36 divide overcrowded node, move middle value up to parent, attach children to smallest and largest result

CSCI 2720 Slide 16 Inserting Items After Insertion of 35, 34, 33

CSCI 2720 Slide 17 Inserting so far

CSCI 2720 Slide 18 Inserting so far

CSCI 2720 Slide 19 Inserting Items How do we insert 32?

CSCI 2720 Slide 20 Inserting Items  creating a new root if necessary  tree grows at the root

CSCI 2720 Slide 21 Inserting Items Final Result

CSCI 2720 Slide Deleting Items Delete 70 80

CSCI 2720 Slide 23 Deleting Items Deleting 70: swap 70 with inorder successor (80)

CSCI 2720 Slide 24 Deleting Items Deleting 70:... get rid of 70

CSCI 2720 Slide 25 Deleting Items Result

CSCI 2720 Slide 26 Deleting Items Delete 100

CSCI 2720 Slide 27 Deleting Items Deleting 100

CSCI 2720 Slide 28 Deleting Items Result

CSCI 2720 Slide 29 Deleting Items Delete 80

CSCI 2720 Slide 30 Deleting Items Deleting 80...

CSCI 2720 Slide 31 Deleting Items Deleting 80...

CSCI 2720 Slide 32 Deleting Items Deleting 80...

CSCI 2720 Slide 33 Deleting Items Final Result comparison with binary search tree

CSCI 2720 Slide 34 Deletion Algorithm I 1.Locate node n, which contains item I 2.If node n is not a leaf  swap I with inorder successor  deletion always begins at a leaf 3.If leaf node n contains another item, just delete item I else try to redistribute nodes from siblings (see next slide) if not possible, merge node (see next slide) Deleting item I:

CSCI 2720 Slide 35 Deletion Algorithm II A sibling has 2 items:  redistribute item between siblings and parent No sibling has 2 items:  merge node  move item from parent to sibling Redistribution Merging

CSCI 2720 Slide 36 Deletion Algorithm III Internal node n has no item left  redistribute Redistribution not possible:  merge node  move item from parent to sibling  adopt child of n If n's parent ends up without item, apply process recursively Redistribution Merging

CSCI 2720 Slide 37 Deletion Algorithm IV If merging process reaches the root and root is without item  delete root

CSCI 2720 Slide 38 Operations of 2-3 Trees all operations have time complexity of log n

CSCI 2720 Slide Trees similar to 2-3 trees 4-nodes can have 3 items and 4 children 4-node

CSCI 2720 Slide Tree Example

CSCI 2720 Slide Tree: Insertion Insertion procedure: similar to insertion in 2-3 trees items are inserted at the leafs since a 4-node cannot take another item, 4-nodes are split up during insertion process Strategy on the way from the root down to the leaf: split up all 4-nodes "on the way"  insertion can be done in one pass (remember: in 2-3 trees, a reverse pass might be necessary)

CSCI 2720 Slide Tree: Insertion Inserting 60, 30, 10, 20, 50, 40, 70, 80, 15, 90, 100

CSCI 2720 Slide Tree: Insertion Inserting 60, 30, 10, , 40...

CSCI 2720 Slide Tree: Insertion Inserting 50, ,...

CSCI 2720 Slide Tree: Insertion Inserting , 15...

CSCI 2720 Slide Tree: Insertion Inserting 80,

CSCI 2720 Slide Tree: Insertion Inserting

CSCI 2720 Slide Tree: Insertion Inserting

CSCI 2720 Slide Tree: Insertion Procedure Splitting 4-nodes during Insertion

CSCI 2720 Slide Tree: Insertion Procedure Splitting a 4-node whose parent is a 2-node during insertion

CSCI 2720 Slide Tree: Insertion Procedure Splitting a 4-node whose parent is a 3-node during insertion

CSCI 2720 Slide Tree: Deletion Deletion procedure: similar to deletion in 2-3 trees items are deleted at the leafs  swap item of internal node with inorder successor note: a 2-node leaf creates a problem Strategy (different strategies possible) on the way from the root down to the leaf: turn 2-nodes (except root) into 3-nodes  deletion can be done in one pass (remember: in 2-3 trees, a reverse pass might be necessary)

CSCI 2720 Slide Tree: Deletion Turning a 2-node into a 3-node... Case 1: an adjacent sibling has 2 or 3 items  "steal" item from sibling by rotating items and moving subtree "rotation"

CSCI 2720 Slide Tree: Deletion Turning a 2-node into a 3-node... Case 2: each adjacent sibling has only one item  "steal" item from parent and merge node with sibling (note: parent has at least two items, unless it is the root) merging

CSCI 2720 Slide Tree: Deletion Practice Delete 32, 35, 40, 38, 39, 37, 60

CSCI 2720 Slide 56 Red-Black Tree binary-search-tree representation of tree 3- and 4-nodes are represented by equivalent binary trees red and black child pointers are used to distinguish between original 2-nodes and 2-nodes that represent 3- and 4-nodes

CSCI 2720 Slide 57 Red-Black Representation of 4-node

CSCI 2720 Slide 58 Red-Black Representation of 3-node

CSCI 2720 Slide 59 Red-Black Tree Example

CSCI 2720 Slide 60 Red-Black Tree Example

CSCI 2720 Slide 61 Red-Black Tree Operations Traversals  same as in binary search trees Insertion and Deletion  analog to tree  need to split 4-nodes  need to merge 2-nodes

CSCI 2720 Slide 62 Splitting a 4-node that is a root

CSCI 2720 Slide 63 Splitting a 4-node whose parent is a 2-node

CSCI 2720 Slide 64 Splitting a 4-node whose parent is a 3-node

CSCI 2720 Slide 65 Splitting a 4-node whose parent is a 3-node

CSCI 2720 Slide 66 Splitting a 4-node whose parent is a 3-node