2 3 4 Trees - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

2 3 4 Trees

Description:

If we could keep a binary search tree complete h would be equal to log n ... To insert a key, value pair we can't just add in new node at 'end' of tree ... – PowerPoint PPT presentation

Number of Views:276
Avg rating:3.0/5.0
Slides: 30
Provided by: bal1
Category:
Tags: tree | trees

less

Transcript and Presenter's Notes

Title: 2 3 4 Trees


1
2 3 4 Trees
  • Multi-Way Search Tree

2
Binary Search Tree
  • BSTs are efficient for storage retrieval but
    have (potentially) poor worst case performance
  • Insert, delete, find all are O(h) hheight of
    tree
  • hn in worst case
  • All three are O(n)

3
Complete Binary Search Tree?
  • If we could keep a binary search tree complete h
    would be equal to log n
  • All operations would be O(log n) like in our heap
  • How can we keep BST complete?

4
Complete Binary Search Tree?
  • Rebuild tree after every insert/delete
  • Takes extra time O(n)
  • Have insert/delete operations ensure completeness
  • Heap used this option
  • BST we talked about did neither

5
Complete BST Performance
  • Dont necessarily want complete BST
  • Want search behavior of BST with performance of
    complete tree
  • Multiple implementations of this
  • Splay trees, AVL trees (most commonly)
    red-black trees

6
Before Red-Black
  • Before we talk about Red-Black trees we will try
    to get an understanding of how they work using
    new kind of tree
  • 2-3-4 Trees (also called 2-4 trees)

7
2-4 Trees
  • Generalization of BST
  • Called multi-way search tree
  • Ordered tree with special nodes
  • Each node holds one or more entries
  • Entry is (key, value) pair

8
Nodes
  • Previously nodes only had one element
  • 2-4 Tree nodes have anywhere from 1-3 elements
  • Need secondary data structure to hold elements
  • Can be as simple as unsorted list
  • 2-4 nodes have children, but number of children
    based on number of elements

9
Example Nodes
  • 2-node
  • 1 element
  • 2 children (2 subtrees)
  • Like binary search tree

8
k
k 8
10
3-Node
  • 2 elements
  • 3 children

8 19
k
k 19
8
11
4-Node
  • 3 elements
  • 4 children

8 19 44
k
k 44
19
8
12
Cannot Have
  • Examples of nodes that cannot be
  • 1 element 3 children
  • 2 elements 1 child
  • 3 elements 2 children

13
Properties
  • Size
  • Every internal node has at least 2, at most 4
    children
  • Depth
  • All external nodes (dummy nodes) have same depth
  • As with BST, will use dummy nodes as external
    nodes
  • Enforces runtime bound of O(log n)

14
Ordering Properties
  • 2-nodes
  • ?c,p LeftDescendant(c,p) ? (? k ? keys(c) ? k key(p))
  • ?c,p RightDescendant(c,p) ? (? k ? keys(c) ? k
    key(p))
  • 3-nodes
  • ?c,p LeftDescendant(c,p) ? (? k ? keys(c) ? k leftKey(p))
  • ? c,p MiddleDescendant(c,p) ? (? k ? keys(c) ? (k
    leftKey(p)) ? (k
  • ?c,p RightDescendant(c,p) ? (? k ? keys(c) ? k
    rightKey(p))
  • 4-nodes
  • ? c,p LeftDescendant(m,n) ? (? k ? keys(m) ? k leftKey(n))
  • ? c,p MiddleLeftDescendant(c,p) ? (? k ? keys(c)
    ? (k leftKey(p)) ? (k
  • ? c,p MiddleRightDescendant(c,p) ? (? k ? keys(c)
    ? (k middleKey(p)) ? (k
  • ? c,p RightDescendant(c,p) ? (? k ? keys(c) ? k
    rightKey(p))

15
Example 2-4 Tree
More entries (15) than nodes (9)
16
2-4 Tree Height
  • Worst case
  • All nodes 2 nodes
  • Complete binary tree
  • Height O(log n)

17
Proposition 10.8
  • Proposition The height of a 2-4 tree sorting n
    entries is O(log n)
  • Justification Assume proposition correct

What is BEST height of 2-4 tree?
18
Find
  • Works very much like in BST
  • Runtime O(h)
  • but we know h is log n so it is O(log n)

19
Insert
  • To insert a key, value pair we cant just add in
    new node at end of tree
  • Would violate depth property
  • Since nodes can have multiple elements most of
    time we can just find correct node and add it

20
Insert Example
21
Insert Time
  • How long did that example take?
  • Find insert location O(h)
  • Insert O(1)
  • Since h log n it took O(log n)
  • But, what if the node is full?

22
Insert
  • If node is full, but need to add there
  • Called overflow
  • Move up one of elements to parent node
  • Promote it
  • And split node
  • Split into two nodes

23
Insert
24
Insert
  • Important Observation
  • Promote and split strategy rows tree from bottom
    up
  • Ensures depth property (log n height) is
    maintained

25
Insert (with promote split) Time
  • How long?
  • Finding insert location O(h)
  • Add entry find overflow O(1)
  • Promote entry O(1)
  • Split node O(1)
  • Since h log n it is O(log n)

26
Insert Example 3
27
What If Parent is Full?
  • If you try to promote entry to parent and parent
    is full promote split again
  • If we get to the root and there is no node to
    promote to
  • make a new node
  • promote to that node
  • make it the root

28
How Long Does THAT Take?
  • Finding insert location O(h)
  • Add entry find overflow O(1)
  • Promote entry O(1)
  • Split node O(1)
  • O(h)O(h) O(h), but hlog n, so O(log n)

From bottom to top (h levels) so O(h)
29
Worksheet
Write a Comment
User Comments (0)
About PowerShow.com