Docsity

2-3 Trees: Balanced Search Tree Data Structure - Prof. Tianming Liu, Study notes of Data Structures and Algorithms

Partial preview of the text

Download 2-3 Trees: Balanced Search Tree Data Structure - Prof. Tianming Liu and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Tianming Liu

Department of Computer Science & Bioimaging Research Center

The University of Georgia

Lecture 20: 2-3 Tree CSCI 2720: Data Structures

Slides Courtesy: Eileen Kraemer

Why care about advanced implementations?

Same entries, different insertion sequence:

 Not good! Would like to keep tree balanced.

2-3 Trees

 each internal node has either 2 or 3 children

 all leaves are at the same level

Features

Example of 2-3 Tree

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) }

Gain: Ease of Keeping the Tree Balanced

Binary Search

Tree

2-3 Tree

both trees after

inserting items

Inserting Items

Insert 39

Inserting Items

Insert 37

Inserting Items

Insert 36

insert in leaf divide leaf and move middle value up to parent overcrowded node

Inserting Items

After Insertion of 35, 34, 33

Inserting so far

Inserting Items

 creating a new root if necessary

 tree grows at the root

Inserting Items

How do we insert 3 2?