Download 2-3 Trees: Data Structures and Algorithms and more Slides Data Structures and Algorithms in PDF only on Docsity!
CPSC 223
Algorithms & Data Abstract Structures
Lecture 22:
B Trees: 2-3 & 2-3-4 trees
Today …
- 2-3 Trees [Ch 12: 651-670]
- 2-3-4 Trees (insertion) CPSC 223 -‐-‐ Fall 2010
2-3 Trees
CPSC 223 -‐-‐ Fall 2010
B Trees (… starting with 2-3 trees)
- A “ 2-3 Tree ” is a (search) tree in which:
- Every internal (non-leaf) node has either 2 or 3 children
- All leaves are at the same level
- A node with 2 children is called a “ 2-node ”
- A node with 3 children is called a “ 3-node ” CPSC 223 -‐-‐ Fall 2010
Placing Data in a 2-3 Tree
- A 2-node (2 children) must contain a single data item
The item’s search key S is:
- greater than its left child’s search keys
- smaller than (or equal to) its middle child’s search keys
- A 3-node (3 children) must contain two data items
For S the left and L the right search key:
- S is greater than the left child’s search keys
- S is smaller than (or equal to) the middle child’s search keys
- L is greater than the middle child’s search keys
- L is smaller than (or equal to) the right child’s search keys
- A leaf node may contain one or two data items CPSC 223 -‐-‐ Fall 2010 S L S
Placing Data in a 2-3 Tree
- Is this a well-formed 2-3 tree?
- NO! CPSC 223 -‐-‐ Fall 2010 6 11 4 5 10
2-‐node with 2 items!
Traversing a 2-3 Tree
• Traversal very similar to a BST!
void inorder (const Node* root) const
if(root != NULL) {
inorder (root->leftChild);
visit (root->item1); // visit 1st^ data item
inorder (root->middleChild);
if( has2Keys (root)) {
visit(root->item2); // visit 2nd^ data item
inorder (root->rightChild);
CPSC 223 -‐-‐ Fall 2010
Traversing a 2-3 Tree (inorder)
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90 Start here (root)
Traversing a 2-3 Tree
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90 Start here (root) New root Traverse left Traverse left
Traversing a 2-3 Tree
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90 Start here (root) New root New root Traverse left Traverse left
Traversing a 2-3 Tree
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90 Start here (root) New root New root (^) Visit first item (after traversing to NULL node) Output: 10 Traverse left Traverse left
Traversing a 2-3 Tree
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90 Start here (root) New root New root (^) Visit second item (after traversing to NULL node) Output: 10 20 Traverse left Traverse left
Traversing a 2-3 Tree
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90 Start here (root) New root Traverse middle Output: 10 20 30 Traverse left New root
Traversing a 2-3 Tree
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90 Start here (root) New root Traverse middle Output: 10 20 30 40 Traverse left Visit first item (after traversing to NULL node) New root
Traversing a 2-3 Tree
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90 Start here (root) New root Output: 10 20 30 40 Traverse left Finished visiting nodes
Traversing a 2-3 Tree
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90 Start here (root) Output: 10 20 30 40 50 Visit first item (after traversing to NULL node)
Traversing a 2-3 Tree
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90 Start here (root) Output: 10 20 30 40 50 Traverse middle New root Traverse left
Traversing a 2-3 Tree
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90 Start here (root) Output: 10 20 30 40 50 Traverse middle New root Traverse left New root
Traversing a 2-3 Tree
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90 Start here (root) Output: 10 20 30 40 50 60 Traverse middle New root Traverse left New root Visit first item (after traversing to NULL node)
Traversing a 2-3 Tree
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90 Start here (root) Output: 10 20 30 40 50 60 65 Traverse middle New root Visit first item (after traversing to NULL node)
Searching a 2-3 Tree
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90 Start here (root) Lookup “70”
Searching a 2-3 Tree
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90 Start here (root) Lookup “70”
Since 2-node and 50 < 70
Search middle child
Searching a 2-3 Tree
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90 New root Lookup “70”
Searching a 2-3 Tree
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90 New root Lookup “70”
Since 3-node and 65 < 70 < 80
Search middle child
2-3 Trees
Some Observations
- A 2-3 tree is (obviously) not a binary tree
- But a 2-3 tree is very similar to a full binary tree
- All leaves are on the same level in a 2-3 tree
- So a 2-3 tree is always (perfectly) balanced
- … and has height at most ⎡log 2 ( n +1)⎤
- which is the case for a full BST
- Best case height is ⎡log 3 ( n +1)⎤ (each node has 3 children) CPSC 223 -‐-‐ Fall 2010
2-3 Trees
Advantages/Disadvantages
- 2-3 trees are (fully) balanced … O (log n ) height
- Insertion and deletion not (terribly) difficult to implement
- Shorter height (because of “wider” nodes) does not
necessarily mean more efficient retrieval time
- Requires additional comparisons (up to 2 per node)
- Height helps in some cases (e.g., File Indexes/Databases) CPSC 223 -‐-‐ Fall 2010
Inserting Nodes
So how can we maintain a 2-3 tree on insertion?
- Locate the leaf node where the item “should” be
- If the leaf contains only one item, then we are done!
- Otherwise, we have to “ split ” the node
- Splitting a node may cause the parent to split
- And so on up the tree CPSC 223 -‐-‐ Fall 2010
Insertion into a 2-3 Tree
1. Search for item
CPSC 223 -‐-‐ Fall 2010 50 30 65 80 10 20 40 60 70 90
Insert “75”