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.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal, Amit Kumar
Advertisements

Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
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.
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.
B-Trees. Motivation for B-Trees Index structures for large datasets cannot be stored in main memory Storing it on disk requires different approach to.
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)
CPSC 231 B-Trees (D.H.)1 LEARNING OBJECTIVES Problems with simple indexing. Multilevel indexing: B-Tree. –B-Tree creation: insertion and deletion of nodes.
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
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.
© 2004 Goodrich, Tamassia (2,4) Trees
CSE 326: Data Structures B-Trees Ben Lerner Summer 2007.
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.
B-Trees Chapter 9. Limitations of binary search Though faster than sequential search, binary search still requires an unacceptable number of accesses.
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.
Indexing (cont.). Insertion in a B+ Tree Another B+ Tree
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Advanced Implementation of Tables.
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.
IntroductionIntroduction  Definition of B-trees  Properties  Specialization  Examples  2-3 trees  Insertion of B-tree  Remove items from B-tree.
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.
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.
B+ Trees COMP
INTRODUCTION TO MULTIWAY TREES P INTRO - Binary Trees are useful for quick retrieval of items stored in the tree (using linked list) - often,
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,
Comp 335 File Structures B - Trees. Introduction Simple indexes provided a way to directly access a record in an entry sequenced file thereby decreasing.
Chapter 13 A Advanced Implementations of Tables. © 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the.
2-3 Trees Extended tree.  Tree in which all empty subtrees are replaced by new nodes that are called external nodes.  Original nodes are called internal.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
Balanced search trees: trees (or 2-4) trees improve the efficiency of insertItem and deleteItem methods of 2-3 trees, because they are performed.
© 2004 Goodrich, Tamassia Trees
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.
(2,4) Trees1 What are they? –They are search Trees (but not binary search trees) –They are also known as 2-4, trees.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
Binary Search Trees Chapter 7 Objectives
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
Lecture Trees Professor Sin-Min Lee.
TCSS 342, Winter 2006 Lecture Notes
Data Structures Balanced Trees CSCI 2720 Spring 2007.
AA Trees.
B-Trees B-Trees.
Multiway Search Trees Data may not fit into main memory
(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
Lecture 26 Multiway Search Trees Chapter 11 of textbook
CS202 - Fundamental Structures of Computer Science II
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
(2,4) Trees (2,4) Trees (2,4) Trees.
CSIT 402 Data Structures II With thanks to TK Prasad
(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
Binary Trees, Binary Search Trees
(edited by Nadia Al-Ghreimil)
Solution for Section Worksheet 4, #7b & #7c
(2,4) Trees (2,4) Trees (2,4) Trees.
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
B-Trees.
Binary Trees, Binary Search Trees
CS210- Lecture 20 July 19, 2005 Agenda Multiway Search Trees 2-4 Trees
Presentation transcript:

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 an Item into a 2-3 Tree n Removing an Item from a 2-3 Tree

INTRODUCTION n What Is a 2-3 Tree? n Definition: A 2-3 tree is a tree in which each internal node(nonleaf) has either 2 or 3 children, and all leaves are at the same level.

2-3 Trees a node may contain 1 or 2 keys all leaf nodes are at the same depth all non-leaf nodes (except the root) have either 1 key and two subtrees, or 2 keys and three subtrees insertion is at the leaf: if the leaf overflows, split it into two leaves, insert them into the parent, which may also overflow deletion is at the leaf: if the leaf underflows (has no items), merge it with a sibling, removing a value and subtree from the parent, which may also underflow the only changes in depth are when the root splits or underflows

A 2-3 Tree of height 3

2-3 Tree vs. Binary Tree n A 2-3 tree is not a binary tree since a node in the 2-3 tree can have three children. n A 2-3 tree does resemble a full binary tree. n If a 2-3 tree does not contain 3-nodes, it is like a full binary tree since all its internal nodes have two children and all its leaves are at the same level.

Cont. n If a 2-3 tree does have three children, the tree will contain more nodes than a full binary tree of the same height. n Therefore, a 2-3 tree of height h has at least 2^h - 1 nodes. n In other words, a 2-3 tree with N nodes never has height greater then log (N+1), the minimum height of a binary tree with N nodes.

Example of a 2-3 Tree n The items in the 2-3 are ordered by their search keys

Node Representation of 2-3 Trees n Using a typedef statement typedef treeNode* ptrType; typedef treeNode* ptrType; struct treeNode struct treeNode { treeItemType SmallItem,LargeItem; { treeItemType SmallItem,LargeItem; ptrType LChildPtr, MChildPtr, ptrType LChildPtr, MChildPtr, RChildPtr; RChildPtr; }; };

Node Representation of 2-3 Tree (cont.) n When a node contains only one data item, you can place it in Small-Item and use LChildPtr and MChildPtr to point to the node’s children. n To be safe, you can place NULL in RChildPtr.

The Advantages of the 2-3 trees n Even though searching a 2-3 tree is not more efficient than searching a binary search tree, by allowing the node of a 2-3 tree to have three children, a 2-3 tree might be shorter than the shortest possible binary search tree. n Maintaining the balance of a 2-3 tree is relatively simple than maintaining the balance of a binary search tree.

Consider the two trees contain the same data items A balanced binary search tree A 2-3 tree

Inserting into a 2-3 Tree n Perform a sequence of insertions on a 2-3 tree is more easilier to maintain the balance than in binary search tree. n Example:

) The binary search tree after a sequence of insertions

) The 2-3 tree after the same insertions.

Inserting into a 2-3 Tree (cont.) n Insert 39. The search for 39 terminates at the leaf. Since this node contains only one item, can siply inser the new item into this node

Inserting into a 2-3 Tree (cont.) n Insert 38: The search terminates at. Since a node cannot have three values, we divide these three values into smallest(38), middle(39), and largest(40) values. Now, we move the (39) up to the node’s parent

Inserting into a 2-3 Tree (cont.) n Insert 37: It’s easy since 37 belongs in a leaf that currently contains only one values,

The Insertion Algorithm n To insert an item I into a 2-3 tree, first locate the leaf at which the search for I would terminate. n Insert the new item I into the leaf. n If the leaf now contains only two items, you are done. If the leaf contains three items, you must split it.

The Insertion Algorithm (cont.) n Spliting a leaf P SL P SL S M L M P S M L P M a) b)

Deleting from a 2-3 Tree n The deletion strategy for a 2-3 tree is the inverse of its insertion strategy. Just as a 2-3 tree spreads insertions throughout the tree by splitting nodes when they become too full, it spreads deletions throughout the tree by merging nodes when they become empty. n Example:

Deleting from a 2-3 Tree (cont.) n Delete Swap with inorder successor Delete value from leaf Merge nodes by deleting empty leaf and moving 80 down

Deleting from 2-3 Tree (cont.) n Delete

Deleting from 2-3 Tree (cont.) n Delete Delete value from leaf Doesn’t work Redistribute 60 80