SlideShare a Scribd company logo
2-3 Trees Lecture 16
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
INTRODUCTION What Is a 2-3 Tree? 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 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.
Example of a 2-3 Tree The items in the 2-3 are ordered by their search keys. 80 160 50  90 20 70 120  150 10 30  40 60 100  110
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. 30 90 10 50 20 40 80 70 100 60 A balanced binary search tree 50 30 40 60 80 100 70  90 10  20 A 2-3 tree
Inserting into a 2-3 Tree Perform a sequence of insertions on a 2-3 tree is more easilier to maintain the balance than in binary search tree. Example:
60 30 90 30 50 80 100 40 20 39 38 37 70 36 35 34 1) The binary search tree after a sequence of  insertions
37 34 50 30 35 60 39 38 40 80 100 36 10  20 70  90 2) The 2-3 tree after the same insertions.
Inserting into a 2-3 Tree (cont.) Insert 39. The search for 39 terminates at the leaf <40>. Since this node contains only one item, can siply inser the new item into this node 50 30 60 80 100 70  90 10  20 39 40
Inserting into a 2-3 Tree (cont.) Insert 38: The search terminates at <39 40>. 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. 38 40 30  10  20 39
Inserting into a 2-3 Tree (cont.) Insert 37: It’s easy since 37 belongs in a leaf that currently contains only one values, 38. 40 30  39 10  20 37 38
The Insertion Algorithm To insert an item I into a 2-3 tree, first locate the leaf at which the search for I would terminate. Insert the new item I into the leaf. 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.) Spliting a leaf P S L P S L S  M  L M  P S  M  L P  M a) b)
 
 
 
 
 
 
 
 
Deleting from a 2-3 Tree 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. Example:
Deleting from a 2-3 Tree (cont.) Delete 70 60 70 100 80  90 Swap with inorder successor 60 - 100 80  90 Delete value from leaf 60 100 90 100 Merge nodes by deleting empty leaf and moving 80 down 80  90 60  80
Deleting from 2-3 Tree (cont.) Delete 70 50 30 40 100 90 10  20 60  80
Deleting from 2-3 Tree (cont.) Delete 100 90 -- 60  80 Delete value from leaf 90 60 80 Doesn’t work 80 60 90 Redistribute 60  80

More Related Content

PPT
PPTX
Tree in data structure
PPTX
PPT
Binary search tree(bst)
PPTX
trees in data structure
PPTX
Tree traversal techniques
PDF
Binary search tree operations
Tree in data structure
Binary search tree(bst)
trees in data structure
Tree traversal techniques
Binary search tree operations

What's hot (20)

PPTX
B and B+ tree
PPTX
Binary tree and operations
PPTX
B+tree Data structures presentation
PPTX
Threaded Binary Tree.pptx
PPT
Stacks & Queues By Ms. Niti Arora
PPTX
Trees (data structure)
PPTX
Binary Search Tree in Data Structure
PPT
Quick sort data structures
PPTX
Unit 2 linked list
PPTX
Tree Traversal
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PDF
sparse matrix in data structure
PPTX
DBMS (Deadlock, deadlock prevention, 2phase locking)
PPT
Bubble sort
PPTX
AVL Tree Data Structure
PPT
b+ tree
PDF
Normalization in DBMS
PPSX
data structure(tree operations)
PPT
1.5 binary search tree
PPTX
Unit I-Data Structures_Intoduction.pptx
B and B+ tree
Binary tree and operations
B+tree Data structures presentation
Threaded Binary Tree.pptx
Stacks & Queues By Ms. Niti Arora
Trees (data structure)
Binary Search Tree in Data Structure
Quick sort data structures
Unit 2 linked list
Tree Traversal
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
sparse matrix in data structure
DBMS (Deadlock, deadlock prevention, 2phase locking)
Bubble sort
AVL Tree Data Structure
b+ tree
Normalization in DBMS
data structure(tree operations)
1.5 binary search tree
Unit I-Data Structures_Intoduction.pptx
Ad

Viewers also liked (20)

PDF
2 - 3 Trees
DOC
23 Tree Best Part
PPTX
Graph theory
PPTX
2-4 tree
PPTX
Advanced data structures slide 1 2
PPT
L7 decision tree & table
PPTX
The Benefits of Trees Presentation
PPTX
Elasticity of supply
PPTX
Price Elasticity of Supply
PPTX
Elasticity of Supply
PPTX
Elasticity of Supply
PPTX
Chapter 20 elasticity of demand and supply
PPT
PRICE ELASTICITY OF SUPPLY WITH EXAMPLES
PPTX
Elasticity of Demand
PDF
Supply and Demand
PPT
Demand and supply
PDF
Collins listening for_ielts_book
PPT
17. Trees and Graphs
PPTX
Elasticity Of Supply And Demand
PPT
The theory of demand and supply
2 - 3 Trees
23 Tree Best Part
Graph theory
2-4 tree
Advanced data structures slide 1 2
L7 decision tree & table
The Benefits of Trees Presentation
Elasticity of supply
Price Elasticity of Supply
Elasticity of Supply
Elasticity of Supply
Chapter 20 elasticity of demand and supply
PRICE ELASTICITY OF SUPPLY WITH EXAMPLES
Elasticity of Demand
Supply and Demand
Demand and supply
Collins listening for_ielts_book
17. Trees and Graphs
Elasticity Of Supply And Demand
The theory of demand and supply
Ad

Similar to 2 3 tree (20)

PPTX
2-3 tree ujjwal matoliya .pptx
PPT
PPTX
2 3 Trees Algorithm - Data Structure
PPTX
2-3 Tree, Everything you need to know
PDF
Lecture notes data structures tree
PPTX
Trees in Data Structure
PDF
B TREE ( a to z concept ) in data structure or DBMS
PPT
2-3 trees in c++
PPT
1.1 binary tree
PPT
DOCX
Data structures notes
PDF
A41001011
PPTX
R-Trees and Geospatial Data Structures
PDF
481803111-Trees-Unit-Review-pdf - Copy.pdf
PPTX
BST.pptx this isp used for learning binary search trees
PPTX
BST.pptx this is Good for data structure
PPTX
Tree
PPTX
BTrees - Great alternative to Red Black, AVL and other BSTs
PPTX
Unit III - NON LINEAR DATA STRUCTURES
PPT
Lecture 5 trees
2-3 tree ujjwal matoliya .pptx
2 3 Trees Algorithm - Data Structure
2-3 Tree, Everything you need to know
Lecture notes data structures tree
Trees in Data Structure
B TREE ( a to z concept ) in data structure or DBMS
2-3 trees in c++
1.1 binary tree
Data structures notes
A41001011
R-Trees and Geospatial Data Structures
481803111-Trees-Unit-Review-pdf - Copy.pdf
BST.pptx this isp used for learning binary search trees
BST.pptx this is Good for data structure
Tree
BTrees - Great alternative to Red Black, AVL and other BSTs
Unit III - NON LINEAR DATA STRUCTURES
Lecture 5 trees

More from Core Condor (6)

PPT
Weighted graphs
PPT
Red black 2
PPT
Red black 1
PPT
Graph isomorphism
PPT
Disjoint sets
PPT
Red blacktrees
Weighted graphs
Red black 2
Red black 1
Graph isomorphism
Disjoint sets
Red blacktrees

Recently uploaded (20)

PDF
Pre independence Education in Inndia.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Insiders guide to clinical Medicine.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Complications of Minimal Access Surgery at WLH
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
01-Introduction-to-Information-Management.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Business Ethics Teaching Materials for college
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
Pre independence Education in Inndia.pdf
Pharma ospi slides which help in ospi learning
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Insiders guide to clinical Medicine.pdf
TR - Agricultural Crops Production NC III.pdf
VCE English Exam - Section C Student Revision Booklet
Pharmacology of Heart Failure /Pharmacotherapy of CHF
human mycosis Human fungal infections are called human mycosis..pptx
Microbial disease of the cardiovascular and lymphatic systems
102 student loan defaulters named and shamed – Is someone you know on the list?
Renaissance Architecture: A Journey from Faith to Humanism
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Complications of Minimal Access Surgery at WLH
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
RMMM.pdf make it easy to upload and study
01-Introduction-to-Information-Management.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Cell Structure & Organelles in detailed.
Business Ethics Teaching Materials for college
O5-L3 Freight Transport Ops (International) V1.pdf

2 3 tree

  • 2. 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
  • 3. INTRODUCTION What Is a 2-3 Tree? 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.
  • 4. 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
  • 5. A 2-3 Tree of height 3
  • 6.  
  • 7.  
  • 8.  
  • 9.  
  • 10. 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.
  • 11. 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.
  • 12. Example of a 2-3 Tree The items in the 2-3 are ordered by their search keys. 80 160 50 90 20 70 120 150 10 30 40 60 100 110
  • 13. Node Representation of 2-3 Trees Using a typedef statement typedef treeNode* ptrType; struct treeNode { treeItemType SmallItem,LargeItem; ptrType LChildPtr, MChildPtr, RChildPtr; };
  • 14. 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.
  • 15.  
  • 16. 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 .
  • 17. Consider the two trees contain the same data items. 30 90 10 50 20 40 80 70 100 60 A balanced binary search tree 50 30 40 60 80 100 70 90 10 20 A 2-3 tree
  • 18. Inserting into a 2-3 Tree Perform a sequence of insertions on a 2-3 tree is more easilier to maintain the balance than in binary search tree. Example:
  • 19. 60 30 90 30 50 80 100 40 20 39 38 37 70 36 35 34 1) The binary search tree after a sequence of insertions
  • 20. 37 34 50 30 35 60 39 38 40 80 100 36 10 20 70 90 2) The 2-3 tree after the same insertions.
  • 21. Inserting into a 2-3 Tree (cont.) Insert 39. The search for 39 terminates at the leaf <40>. Since this node contains only one item, can siply inser the new item into this node 50 30 60 80 100 70 90 10 20 39 40
  • 22. Inserting into a 2-3 Tree (cont.) Insert 38: The search terminates at <39 40>. 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. 38 40 30 10 20 39
  • 23. Inserting into a 2-3 Tree (cont.) Insert 37: It’s easy since 37 belongs in a leaf that currently contains only one values, 38. 40 30 39 10 20 37 38
  • 24. The Insertion Algorithm To insert an item I into a 2-3 tree, first locate the leaf at which the search for I would terminate. Insert the new item I into the leaf. If the leaf now contains only two items, you are done. If the leaf contains three items, you must split it.
  • 25. The Insertion Algorithm (cont.) Spliting a leaf P S L P S L S M L M P S M L P M a) b)
  • 26.  
  • 27.  
  • 28.  
  • 29.  
  • 30.  
  • 31.  
  • 32.  
  • 33.  
  • 34. Deleting from a 2-3 Tree 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. Example:
  • 35. Deleting from a 2-3 Tree (cont.) Delete 70 60 70 100 80 90 Swap with inorder successor 60 - 100 80 90 Delete value from leaf 60 100 90 100 Merge nodes by deleting empty leaf and moving 80 down 80 90 60 80
  • 36. Deleting from 2-3 Tree (cont.) Delete 70 50 30 40 100 90 10 20 60 80
  • 37. Deleting from 2-3 Tree (cont.) Delete 100 90 -- 60 80 Delete value from leaf 90 60 80 Doesn’t work 80 60 90 Redistribute 60 80