csc 213 lecture 8: (2,4) trees. review of last lecture binary search tree – plain and tall no...

Post on 17-Jan-2016

213 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CSC 213

Lecture 8: (2,4) Trees

Review of Last Lecture

Binary Search Tree – plain and tall No balancing, no splaying, no speed

AVL Tree – liberté, égalité, fraternité Traverse up the tree & check for balance When a node’s children’s heights differ

by 2 or more Each restructure uses node, taller child,

tallest grandchild

Review of Last Lecture

Splay Tree – Greed is Good Splay a node until it becomes the root Each splay operation involves the

node, its parent, and its grandparent Only exception is if a node’s parent is the

root

Multi-Way Search Tree (§ 9.4.1)

A multi-way search tree is an ordered treeEach internal node in a multi-way search tree: Has at least 2 children (can have more!) Has 1 fewer entries than it has children Keeps its entries and children in sorted order

11 24

2 6 8 15

30

27 32

Multi-Way Search Tree (§ 9.4.1)

For a node with children v1 v2 … vd storing entries with keys k1 k2 … kd1

Keys in the subtree of v1 are smaller than k1

Keys in the subtree of vi are between ki1 and ki

Keys in the subtree of vd are greater than kd1

11 24

2 6 8 15

30

27 32

11

2 6 8 15

11 2424

Multi-Way Inorder Traversal

Can traverse multi-way search trees in-orderAlternate visiting child i then entry ei

Like with a BST, inorder traversal visits keys in increasing order

11 24

2 6 8 15

30

27 321 2 3 7 9

4 6

5

8

Multi-Way SearchingSimilar to search in a BSTAt each with children v1 v2 … vd and entries e1 e2 … ed1

for i = 1 to d – 1 if k ki: return result of search in child vi

if k = ki: return ei

Return result of search in child vd Throw exception if we reach an external node

Example: find(30)11 24

2 6 8 15

30

27 3227 32

30

27 32

Multi-Way SearchingSimilar to search in a BSTAt each with children v1 v2 … vd and entries e1 e2 … ed1

for i = 1 to d – 1 if k ki: return result of search in child vi

if k = ki: return ei

Return result of search in child vd Throw exception if we reach an external node

Example: find(1)11 24

2 6 8 15

30

27 322 6 8

(2,4) Trees (§ 9.4.2)(2,4) tree is multi-way search tree with 2 properties:

Node-Size Property: internal nodes have at most 4 children Depth Property: all the external nodes have the same depth

Nodes can be a 2-node, 3-node or 4-node

10 15 24

2 8 12 27 3218

Height of a (2,4) Tree(2,4) tree of size n has height O(log n) In the largest tree, each node has 2 children But external nodes must be at same depth

This property automatically balances the tree!

This worst case scenario: a balanced binary tree!

1

2

2h1

0

entries

0

1

h1

h

depth

InsertionBegin with a search for the key kIf k does not exist within the tree, add entry to last internal node we searched in……thereby preserving the depth propertyExample: insert(30)

27 32 35

10 15 24

2 8 12 18

10 15 24

2 8 12 27 30 32 3518

27 32 35v

v

InsertionThis insertion can cause an overflow! Node v became a 5-node This violates Node-Size property

27 32 35

15 24

12 18

15 24

12 27 30 32 3518

v

v

1 2 3 4 5

Overflow and SplitHandle overflow using split:

Split node v into 2 nodes (v' and v") A 3-node (v’) with keys e1 e2 and children v1 v2 v3

A 2-node (v’’) with key e4 and children v4 v5

Promote e3 to parent node u (this may create new root)

This may cause parent node (u) to overflow

15 24

12 27 30 32 3518v

u

v1 v2 v3 v4 v5

15 24 32

12 27 3018v'

u

v1 v2 v3 v4 v5

35v"

Overflow and Split

Splitting a node when the parent also overflows is similar, but takes a little more work Example: insert(29)

15 24 32

12 27 28 29 3018 3527 28 30

Overflow and Split

Example: insert(29)

15 24 29 32

12 27 2818 3530

Overflow and Split

Example: insert(29)

15 24 29 32

12 27 2818 3530

Overflow and Split

Example: insert(29)

15 24

12 27 2818 3530

29

32

Overflow and Split

Example: insert(29)

15 24

12 27 2818 3530

29

32

Overflow and Split

Example: insert(29)But this promotion could also cause an overflow…

15 24

12 27 2818 3530

29

32

Analysis of InsertionAlgorithm insert(k, o)

1. Search for key k to find insert node v

2. Add the new entry (k, o) at node v

3. while overflow(v)if isRoot(v)

create a new empty root above vsplit(v)v parent(v)

What is big-Oh time for: Step 1?

Step 2?

Checking overflow(v)?

Calling isRoot(v)?

Calling split(v)?

Step 3?

insert(k,o) big-Oh time?

Deletion

Deletion from a leaf node is simple: Just remove the entry and the null child

Example: delete(27)

27 32 35

10 15 24

2 8 12 18

32 35

10 15 24

2 8 12 18

27 32 35

DeletionIf entry is not at a leaf node, replace it with inorder successor Go to right child and then go as far left as possible

Example: delete(24)

27 32 35

10 15 24

2 8 12 18

32 35

10 15 27

2 8 12 18

27 32 35

Underflow and FusionDeleting an entry may cause underflow Cause a node to be a 1-node (has one child,

but no entries) This has two solutions, the solution depends

on the situation

Example: remove(15)

9 14

2 5 7 10 15

9 149 14

FusionCase 1: v has adjacent siblings that is a 2-node Make new node (v’) by merging node (v) with

adjacent sibling (w) Steal entry from parent (u) that was between v & w

This may propagate underflow to the parent!

Example: remove(15)

9 14

2 5 7 10 15

u

v

9 14

10 14

u

v'w2 5 7 10

99 14

6 8

TransferCase 2: v has adjacent sibling w that is 3- or 4-node

Move entry in u that is between w & v into v Promote entry in w that is closest to v into u Make child of w that is next to v be a child of v No more underflows can exist

Example: remove(10)

4 9

6 82 10

u

vw2

u

vw

4 9

9

4 94

6 8

4 8

6

Analysis of DeletionWhat is big-Oh time needed for deletion?

Height of tree = O(log n)

Time to find entry to delete

Time needed for each fusion

Maximum possible number of fusions

Time needed for transfer

Maximum possible number of transfers

Total time needed for deletion

Your Turn

Insert 1, 2, 3, 4, 5, 6, 7, 8 into a(2,4) treeDelete 3, 6, 5, 2, 8, 4, 1, 7 from your tree

Daily Quiz

Write the Node class for a (2,4)-tree. What public methods should you define (does it make sense to include all getters and setters)? You will want to consider using other data structures in your Node.

top related