Balanced search trees: 2-3 trees.

Slides:



Advertisements
Similar presentations
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Advertisements

Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Binary Search Trees1 Part-F1 Binary Search Trees   
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Homework #3 Due Thursday, April 17 Problems: –Chapter 11: 11.6, –Chapter 12: 12.1, 12.2, 12.3, 12.4, 12.5, 12.7.
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.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
Balanced Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Balanced search trees: 2-3 trees. 2-3 trees allow us to process ordered lists in more efficient way than binary trees with an ordering property. Recall.
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Balanced search trees: trees (or 2-4) trees improve the efficiency of insertItem and deleteItem methods of 2-3 trees, because they are performed.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Binary Search Trees (BST)
Red-Black trees Red-black trees are trees represented as binary trees at the expense of one extra bit per node. The idea is to represent 3- and 4-nodes.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right subtrees.
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
AA Trees.
B-Trees B-Trees.
Data Structures and Algorithms for Information Processing
Multiway Search Trees Data may not fit into main memory
Search Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Extra: B+ Trees CS1: Java Programming Colorado State University
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Chapter 11: Multiway Search Trees
SNS COLLEGE OF TECHNOLOGY (Autonomous ) COIMBATORE-35
Chapter 10.1 Binary Search Trees
Heaps 9/13/2018 3:17 PM Heaps Heaps.
ITEC 2620M Introduction to Data Structures
Trees 4 The B-Tree Section 4.7
(edited by Nadia Al-Ghreimil)
Monday, April 16, 2018 Announcements… For Today…
Data Structures Balanced Trees CSCI
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
(2,4) Trees (2,4) Trees 1 (2,4) Trees (2,4) Trees
Wednesday, April 18, 2018 Announcements… For Today…
Lecture 26 Multiway Search Trees Chapter 11 of textbook
CS202 - Fundamental Structures of Computer Science II
Height Balanced Trees 2-3 Trees.
Multi-Way Search Trees
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,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
B- Trees D. Frey with apologies to Tom Anastasio
B- Trees D. Frey with apologies to Tom Anastasio
B-Tree.
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
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,4) Trees (2,4) Trees (2,4) Trees.
Binary Search Trees.
Binary Search Trees A special case of a Binary Tree
CSIT 402 Data Structures II With thanks to TK Prasad
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
B- Trees D. Frey with apologies to Tom Anastasio
(2,4) Trees /24/2019 7:30 PM (2,4) Trees (2,4) Trees
(edited by Nadia Al-Ghreimil)
Non-Linear Structures
Trees.
Self-Balancing Search Trees
(2,4) Trees (2,4) Trees (2,4) Trees.
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Heaps & Multi-way Search Trees
Binary Search Trees < > = Dictionaries
Data Structures Using C++ 2E
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right.
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 20 July 19, 2005 Agenda Multiway Search Trees 2-4 Trees
Balanced search trees: trees.
Presentation transcript:

Balanced search trees: 2-3 trees. 2-3 trees allow us to process ordered lists in more efficient way than binary trees with an ordering property. Recall that the worst case search efficiency in the later is O(N). All balanced trees guaranty at least O(logN) efficiency for the search operation. Definition: A 2-3 tree is a general tree which satisfies the following properties: Each node may store two data items. Each node may have three children. The second data item in any node may be empty, in which case sentinel value emptyFlag is stored there (assume emptyFlag := 0). If it is not empty, the first data item precedes the second one according to the specified ordering relationship. For each node, data in the first child precedes the first data item in the node; data in the second child follows the first data item, but precedes the second; data in the third child follows the data in the second data item. All leaf nodes are on the same level.

Example 2-3 tree 400 500 300 450 600 700 200 330 370 420 470 530 570 650 750 800 Class Node23tree { Node23tree firstChild; Node23tree secondChild; Node23tree thirdChild; Node23tree parent; int firstItem; int secondItem; .... class methods follow }

Search in 2-3 trees Algorithm search23tree (tree, target, precedes) Input: tree, a 2-3 tree; target, item sought; precedes, a comparator object Output: where, subtree containing the data; boolean found := false if (tree != null) { boolean atLeastOneItem := (firstItem != emptyFlag) boolean twoItems := (secondItem != emptyFlag) if (atLeastOneItem) if (firstItem = target) { item := firstItem found := true } else if (twoItems) if (secondItem = target) { item := secondItem found := true }

Search in 2-3 trees (contd.) if (found || leafNode(tree) where := tree else if (atLeastOneItem) { if (precedes.lessThan(target, firstItem) search23tree (tree.firstChild, target, precedes) if (! twoItems) search23tree (tree.secondChild, target, precedes) if (precedes.lessThan(target, second Item) search23tree (tree.thirdChild, target, precedes) } else // the tree is emptry

Search in 2-3 trees (contd.) Efficiency of the search operation: Because leaf nodes are at the same level (by definition), we have a full tree. The maximal path length in a full tree is log n + 1. Therefore, the efficiency of the search operation in 2-3 tree is guaranteed to be logarithmic. In the best and average cases, it is better than logarithmic, because nodes contain more than one data item.

Insertion in 2-3 trees Insert 250 and 850 in the following tree: 400 500 300 450 600 700 200 330 370 420 470 530 570 650 750 800 search for 250 stops here search for 850 stops here To find where to insert, we must first search for 250. Search terminates in a node currently containing one item, 200. Therefore, we can insert 250 as a second item in that node. Search for 850 terminates in a node which already contains two items. We cannot create a new leaf node because this would violate the fullness of the tree. But we can split the node currently containing 750 and 800, into two “two” nodes (one containing 750, and the other containing 850). The middle value, 800, is recursively passed to the parent node, which may eventually have room to accommodate it.

Insertion example continued 700 600 800 800 400 500 750 850 300 450 600 700 200 330 370 420 470 530 570 650 750 800 The resulting tree: 500 400 700 300 450 600 800 200 250 330 370 420 470 530 570 650 750 850

Insertion in 2-3 tree (contd.) In general, the following cases are possible when inserting an item in a 2-3 tree: Adding an item in a leaf node with room to accommodate the new item. Forcing a split with data being passed recursively up to their parent nodes until a node with one item is found to accommodate the passed data, or the root is split and the tree grows in height. The first item is inserted in the empty tree. Before we develop the insertItem method, we must say how the split of a node is performed.

The splitNode method Algorithm splitNode(tree, where, data, branch, addBranch, precedes) Input: tree, a 2-3 tree; where, a subtree to be split; data, item forcing the split; branch, if addBranch is true, branch is a subtree to be added as a child to a tree node that is splitting; if addBranch is false, has no well defined value upon entry to the method; addBranch, a Boolean variable which is true if branch is added to a splitting node, and false if a leaf node is being split; precedes, a comparator object defining the ordering relationship. Output: tree, a 2-3 tree eventually with a new root; where, a subtree which now contains only one item and has a sibling to its right.

The splitNode method (contd.) /* case 1: splitting an external node / splitting the root node insert 700 parent, tree tree, where 600 800 where 600 800 */ if (where = tree) { Node23tree parent = new Node23tree() where.parent := parent parent.firstChild := where tree := parent } else parent := where.parent Node23tree sibling = new Node23tree () sibling.parent := parent

The splitNode method (contd.) if (precedes.lessThan (data, where.firstItem)) { int middle := where.firstItem where.firstItem := data sibling.firstItem := where.secondItem where.secondItem := emptyFlag } else if (precedes.lessThan (data, where.secondItem)) { middle := data where.secondItem := emptyFlag } else { sibling.firstItem := data middle := where.secondItem where.secondItem := emptyFlag } /* the result in our example parent 700 where 600 800 sibling */

The splitNode method (contd.) /* case 2: the splitting node is an internal node middle := 700 splitting node where sibling 600 800 branch 530 570 650 750 850 splitted node */ if (addBranch) { int key := branch.firstItem if (precedes.lessThan(key, where.firstChild.firstItem)) { sibling.secondChild := where.thirdChild sibling.firstChild := where.secondChild where.thirdChild := NULL where.secondChild := where.firstChild where.firstChild := branch sibling.secondChild.parent := sibling sibling.firstChild.parent := sibling where.firstChild.parent := where }

else if (precedes.lessThan(key, where.secondChild.firstItem)) { sibling.secondChild := where.thirdChild sibling.firstChild := where.secondChild where.thirdChild := NULL where.secondChild := branch sibling.secondChild.parent := sibling sibling.firstChild.parent := sibling where.secondChild.parent := where } if (precedes.lessThan(key,where.thirdChild.firstItem)) { sibling.firstChild := branch sibling.firstChild.parent := sibling } else { sibling.secondChild := branch sibling.firstChild := where.thirdChild sibling.firstChild.parent := sibling }

/* the resulting tree parent 700 where sibling 600 800 530 570 650 750 850 Now 700 must be accomodated in the parent node; if not possible, the parent node must split further. */ if (parent.firstItem = emptyFlag) { parent.firstItem := middle parent.firstChild := where parent.secondChild := sibling } else if (parent.secondItem = emptyFlag) { if (precedes.lessThan(parent.firstItem, middle)) { parent.secondItem := middle parent.thirdChild := sibling } else {

parent.secondItem := parent.firstItem parent.firstItem := middle parent.thirdChild := parent.secondChild parent.secondChild := sibling } // end else else // split the parent node, which is an internal node splitNode(tree, parent, middle, sibling, true, precedes) // splitNode method end here

The insertItem method Algorithm insertItem(tree, newData, precedes) Input: tree, a 2-3 tree; newData, item to be inserted; precedes, a comparator object defining the ordering relationship. Output: tree, a 2-3 tree with newData inserted Node23tree leaf = search23tree (tree, newData, precedes) if (leaf.FirstItem = emptyFlag) //insertion in an empty tree tree.firstItem := newData else { //add newData in the leaf; if not possible, call splitNode to split the leaf boolean twoItems := (leaf.secondItem != emptyFlag) if (twoItems) splitNode(tree, leaf, newData, leaf, false, precedes) else if (precedes.lessThan(leaf.firstItem, newData) leaf.secondItem := newData leaf.secondItem := leaf.firstItem leaf.firstItem := newData }