23FL16-23-tree - 2-3 Trees Professor Sin-Min Lee Contents...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 2-3 Trees Professor Sin-Min Lee Contents Introduction Introduction s The 2-3 Trees Rules s The Advantage of 2-3 Trees s Searching For an Item in a 2-3 Tree s Inserting an Item into a 2-3 Tree s Removing an Item from a 2-3 Tree Removing s INTRODUCTION What Is a 2-3 Tree? s Definition: A 2-3 tree is a tree in which Definition: each internal node(nonleaf) has either 2 or 3 children, and all leaves are at the same level. level. s 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 A 2-3 tree is not a binary tree since a node 2-3 in the 2-3 tree can have three children. in s A 2-3 tree does resemble a full binary tree. s If a 2-3 tree does not contain 3-nodes, it is If like a full binary tree since all its internal nodes have two children and all its leaves are at the same level. are s Cont. If a 2-3 tree does have three children, the If tree will contain more nodes than a full binary tree of the same height. s Therefore, a 2-3 tree of height h has at least Therefore, 2^h - 1 nodes. 2^h s In other words, a 2-3 tree with N nodes In never has height greater then log (N+1), the minimum height of a binary tree with N nodes. nodes. s Example of a 2-3 Tree s The items in the 2-3 are ordered by their The search keys. search 50 70 20 10 30 90 40 60 120 80 100 110 150 160 Node Representation of 2-3 Trees s Using a typedef statement Using typedef typedef treeNode* ptrType; treeNode* struct treeNode struct { treeItemType SmallItem,LargeItem; treeItemType ptrType LChildPtr, MChildPtr, ptrType RChildPtr; RChildPtr; }; }; Node Representation of 2-3 Tree (cont.) When a node contains only one data item, When you can place it in Small-Item and use LChildPtr and MChildPtr to point to the node’s children. node’s s To be safe, you can place NULL in To NULL RChildPtr. RChildPtr. s The Advantages of the 2-3 trees Even though searching a 2-3 tree is not Even 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. search s Maintaining the balance of a 2-3 tree is Maintaining relatively simple than maintaining the balance of a binary search tree . s Consider the two trees contain the same data items. 50 60 30 90 30 10 50 20 40 80 70 70 90 100 10 20 40 A balanced binary search tree A 2-3 tree 60 80 100 Inserting into a 2-3 Tree Perform a sequence of insertions on a 2-3 Perform tree is more easilier to maintain the balance than in binary search tree. than s Example: s 60 90 30 30 50 40 20 80 100 70 39 38 37 36 35 34 1) The binary search tree after a sequence of insertions 37 50 34 30 10 20 35 39 36 38 70 40 2) The 2-3 tree after the same insertions. 60 80 90 100 Inserting into a 2-3 Tree (cont.) s Insert 39. The search for 39 terminates at Insert the leaf <40>. Since this node contains only one item, can siply inser the new item into 50 this node this 70 30 10 20 39 40 60 90 80 100 Inserting into a 2-3 Tree (cont.) s Insert 38: The search terminates at <39 40>. Insert 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. we 30 10 20 39 38 40 Inserting into a 2-3 Tree (cont.) s Insert 37: It’s easy since 37 belongs in a leaf Insert that currently contains only one values, 38. that 30 10 20 37 39 38 40 The Insertion Algorithm To insert an item I into a 2-3 tree, first To locate the leaf at which the search for I would terminate. would s Insert the new item I into the leaf. s If the leaf now contains only two items, you are done. If the leaf contains three items, you must split it. items, s The Insertion Algorithm (cont.) s Spliting a leaf a) P SM L b) M P S P L P SM L M S L Deleting from a 2-3 Tree The deletion strategy for a 2-3 tree is the 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. empty. s Example: s Deleting from a 2-3 Tree (cont.) s Delete 70 80 60 80 90 100 70 Swap with inorder successor 80 60 90 100 60 90 - 100 Delete value from leaf 90 60 80 100 Merge nodes by deleting empty leaf and moving 80 down Deleting from 2-3 Tree (cont.) s Delete 70 50 90 30 10 20 40 60 80 100 Deleting from 2-3 Tree (cont.) s Delete 100 90 60 60 80 80 90 -- Delete value from leaf 60 80 Doesn’t work 60 90 Redistribute ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online