Docsity

Understanding 2-3 Trees: Structure, Advantages, and Operations, Slides of Data Structures and Algorithms

Partial preview of the text

Download Understanding 2-3 Trees: Structure, Advantages, and Operations and more Slides Data Structures and Algorithms in PDF only on Docsity!

2-3 Trees

Contents

• Introduction

• The 2-3 Trees Rules

• The Advantage of 2-3 Trees

• Searching For an Item in a 2-3 Tree

• Inserting an Item into a 2-3 Tree

• Removing an Item from a 2-3 Tree

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

2-3 Tree vs. Binary Tree

• A 2-3 tree is not a binary tree since a node in

the 2-3 tree can have three children.

• A 2-3 tree does resemble a full binary tree.

• 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.

• If a 2-3 tree does have three children, the tree

will contain more nodes than a full binary tree

of the same height.

• Therefore, a 2-3 tree of height h has at least

2^h - 1 nodes.

• 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.

Node Representation of 2-3 Trees

• Using a typedef statement

typedef treeNode* ptrType;

struct treeNode

{ treeItemType SmallItem,LargeItem;

ptrType LChildPtr, MChildPtr,

RChildPtr;

Node Representation of 2-3 Tree

(cont.)

• 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.

• To be safe, you can place NULL in RChildPtr.

The Advantages of the 2-3 trees

• 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.

• 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

  1. The binary search tree after a sequence of insertions
  1. The 2-3 tree after the same insertions.