cosc 2007 data structures ii

42
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I

Upload: salim

Post on 05-Jan-2016

50 views

Category:

Documents


0 download

DESCRIPTION

COSC 2007 Data Structures II. Chapter 13 Advanced Implementation of Tables I. Topics. Introduction & terminology 2-3 trees Traversal Search Insertion Deletion. Jane. Tom. Balanced BST. Unbalanced BST. Alan. Bob. Nancy. Jane. Wendy. Bob. Ellen. Ellen. Tom. Bob. Nancy. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COSC 2007  Data Structures II

COSC 2007 Data Structures II

Chapter 13Advanced Implementation of

Tables I

Page 2: COSC 2007  Data Structures II

2

Topics

Introduction & terminology 2-3 trees

Traversal Search Insertion Deletion

Page 3: COSC 2007  Data Structures II

3

Height of Binary Search Tree

TomTom

JaneJane

BobBob

AlanAlan

Wendy

Wendy

Nancy

Nancy

Ellen

Ellen

Unbalanced BST

TomTom

Nancy

Nancy

AlanAlan

BobBob

EllenEllen

JaneJane

Wendy

Wendy

Skewed BST

JaneJane

Nancy

Nancy

BobBob

Ellen

Ellen

Ellen

Ellen

TomTom

Balanced BST

JaneJane

TomTomBobBob

AlanAlan Ellen

Ellen

WendyWendyNancy

Nancy

Full & Complete BST

Page 4: COSC 2007  Data Structures II

4

Introduction & Terminology

The height of a BST is sensitive to the order of insertion & deletion of its nodes BSTs can loose their balance and approach a linear shape

Variations of the basic BST are used to absorb insertion & deletion while preserving their balance Searching in these trees is almost as efficient as with the

minimum-height BST BST Variations

2-3 Trees 2-3-4 Trees (Self study) Red-Black Trees (Self study) AVL Trees

Page 5: COSC 2007  Data Structures II

5

2-3 Trees

A 2-3 tree: is a tree in which each internal node has either 2 or 3 children, and

all leaves are at the same level 2-node:

A node with two children 3-node:

A node with three children

A 2-3 Tree with 2 Subtrees

root

A 2-3 Tree with 3 Subtrees

root

TL TR TL TM TR

Page 6: COSC 2007  Data Structures II

6

2-3 Trees

A 2-3 tree T of height h (Recursive definition) Either T is empty (h = 0) Or T is of the form of a root r containing 1 data item and two

2-3 trees (TL and TR) of height (h-1) Key in r > each key in TL Key in r < each search key in TR

Or T is of the form of a root r containing 2 data items and & three 2-3 trees (TL , TM and TR) of height (h-1).

Smaller key in r > each key in TL

Smaller key in r < each key in TM Larger key in r > each key in TM Larger key in r < each key in TR

Page 7: COSC 2007  Data Structures II

7

2-3 Trees

Example 50 90

30 40 100 110 130 140

120 15070

160806010

20

Page 8: COSC 2007  Data Structures II

8

2-3 Trees

Observations Binary tree? Balanced? A 2-3 tree of height h has at least many nodes

as a full binary tree of height h Height is less than a BST having the same

number of elements (n nodes)

10 20 60 8040

9030

50

100

Page 9: COSC 2007  Data Structures II

9

ADT 2-3 Trees Implementation Node Implementation

class Tree23Node{ private KeyedItem smallItem; private KeyedItem largeItem; private Tree23Node leftChild,

private Tree23Node midChild; private Tree23Node rightChild;// constructor and method} // end Tree23Node

When a node contains only one item Place it in SmallItem Use leftChild & midChild to point to the node's children Assign null to rightChild

10 20 60 8040

9030

50

100

Page 10: COSC 2007  Data Structures II

10

ADT 2-3 Trees Implementation

Main Operations Traverse Search (Retrieve) Insert Delete

10 20 60 8040

9030

50

100

Page 11: COSC 2007  Data Structures II

11

ADT 2-3 Trees Implementation Traverse

In sorted search-key Analog to inorder traversal

inorder(ttTree)// Traverse nonempty 2-3 tree T in sorted search-key orderif (ttTree’s root node r is a leaf)

visit data item(s)else if (r has two data items)

{ inorder (left subtree of ttTree’s root)Visit the first data iteminorder middle subtree of ttTree’s root)Visit the second data iteminorder (right subtree of ttTree’s root)

}else // r has one data item

{ inorder(left subtree of ttTree’s root)Visit the data iteminorder (middle subtree of ttTree’s root)

} // end if

Page 12: COSC 2007  Data Structures II

12

ADT 2-3 Trees Implementation

Retrieve Analog to retrieval operations in BSTs Searching is more efficient than in BST, why? More comparisons are needed than in BST

10 20 60 8040

9030

50

100

Page 13: COSC 2007  Data Structures II

13

ADT 2-3 Trees Implementation Retrieve

retrieveItem(ttTree, searchKey){ if (SsarchKey is in ttTree’s root node r)

// Item foundtreeItem = the data portion of r

else if (r is a leaf) // failuretreeItem = null;

else if (r has 2 data items){ // Search appropriate subtree if (searchKey < smaller search key of r)

treeItem=retrieveItem( r’s left subtree, searchKey) else if (SearchKey < larger search key of R)

treeItem= retieveItem( r’s middle subtree, searchKey) else

treeItem=retieveItem( r’s right subtree, searchKey)} else // r has one data item{ if (searckKey < r’s search key)

treeItem= retrieveItem(r’s left subtree, searchKey)else

treeItem= retrieveItem(r’s middle subtree, searchKey)} //end if-else

Page 14: COSC 2007  Data Structures II

14

ADT 2-3 Trees Implementation Insertion

Doesn't degenerate the 2-3 tree as in BST Idea:

When a leaf contains 3 items, split it into 2 nodes When an internal node contains 3 items, split it into 2 nodes and

accommodate its children Splitting & moving items up to parent continues recursively until a

node is reached that had only 1 item before the insertion Insertion doesn’t increase the height of the tree, as long as there is at

least one node containing only one item in the path from the root to the leaf into which the new item is inserted

If root contains 3 items, split it into 2 nodes & create a new root node Height will increase

Page 15: COSC 2007  Data Structures II

15

ADT 2-3 Trees Implementation

Insertion example

Page 16: COSC 2007  Data Structures II

16

ADT 2-3 Trees Implementation

Insertion has three special cases …..

Page 17: COSC 2007  Data Structures II

17

ADT 2-3 Trees Implementation

Splitting a leaf node

P

L S

P

S M LM

Page 18: COSC 2007  Data Structures II

18

ADT 2-3 Trees Implementation

Splitting an internal node

S M L

A B C D

P

A B C D

P

M

P’s parent

S L

Page 19: COSC 2007  Data Structures II

19

ADT 2-3 Trees Implementation Splitting the root node

When the root contains 3 items Split it into 2 nodes Create a new root node

S M L

A B C D

root

A B C D

M

New root

S L

Page 20: COSC 2007  Data Structures II

20

ADT 2-3 Trees Implementation

Insertion

insertItem(ttTree, newItem)// Let sKey be the search key of newItemLocate the leaf leafNode in which sKey belongsAdd newItem to leafNode if (leafNode now has 3 items)

Split (leafNode )

Page 21: COSC 2007  Data Structures II

21

ADT 2-3 Trees Implementation Insertion

split (Tree23Node n)// Splits node n, which contains 3 items. if(n is the root)

Create a new node p (refine later to set success)else Let p be the parent of n

Replace n with 2 nodes, n1 & n2 so that p is their parentGive n1 the item in n with smallest search-key valueGive n2 the item in n with largest search-key valueif(n isn’t a leaf){ n1 becomes parent of n’s two leftmost children

n2 becomes parent of n’s two rightmost children}

Move item in n that has the middle search-key value up to p

if (p now has three items)split (p)

Page 22: COSC 2007  Data Structures II

22

ADT 2-3 Trees Implementation Deletion

Strategy is the inverse of the insertion Merge nodes when they become empty Swap the value deleted with its inorder successor Deletion will always then delete from a leaf

Steps To delete x from a 2-3 tree, first locate n, the node containing x If n is an interior node, find x’s in-order successor and swap it

with x As a result, deletion always begins at a leaf node I

If I contains a value in addition to x, delete x from I and we are done

Page 23: COSC 2007  Data Structures II

23

ADT 2-3 Trees Implementation

Deletion If deleting x from I leaves I empty, move work

must be done Check sibling of now empty leaf

If sibling has two values, redistribute the value

B

A B

C

C A

I

I

Page 24: COSC 2007  Data Structures II

24

ADT 2-3 Trees Implementation

If deleting x from I leaves I empty, move work must be done

Check sibling of now empty leaf If no sibling of I has two values, merge I with an adjacent

sibling and bring down a value from I’s parent The merging of I may cause the parent to be left without a

value and only one child

A

B

A B

II I

Page 25: COSC 2007  Data Structures II

25

ADT 2-3 Trees Implementation

• If deleting x from I leaves I empty, move work must be done

• Check sibling of now empty leaf The merging of I may cause the parent to be left without a

value and only one child If so, recursively apply deletion procedure to the parent

A

B

A B

II I

Page 26: COSC 2007  Data Structures II

26

ADT 2-3 Trees Implementation

Deletion If the parent has a sibling with two values,

redistribute the value

B

A B

C

C A

W X Y Z

W X

Y z

Page 27: COSC 2007  Data Structures II

27

ADT 2-3 Trees Implementation

Deletion If the parent has no sibling with two values,

merge the parent with a sibling, and let the sibling adopt the parent’s child

A

B

A B

X Y Z

X Y Z

Page 28: COSC 2007  Data Structures II

28

ADT 2-3 Trees Implementation

Deletion If the merging continues so that the root of the

tree is without a value (and has only one child), delete the root. Height will now be h-1

A B

X Y Z

A B

X Y Z

Page 29: COSC 2007  Data Structures II

29

ADT 2-3 Trees Implementation

Deletion example: Delete 70

--10 20 6040

80 9030

50

100

Delete

10 20 60 8040

9030

50

100

Merge

8010 20 6040

70 9030

50

100 7010 20 6040

80 9030

50

100

Replace with

successor

Page 30: COSC 2007  Data Structures II

30

ADT 2-3 Trees Implementation Implementation of deletion

deleteItem( searchKey)Attempt to locate item I with Searck Key equals searchKeyif (theItem is present){

if (theItem is not a leaf)Swap item theItem with its inorder successor in leaf leafNode

Delete Item theItem from leaf leafNodeif (leafNode now has no items)

fix(leafNode)}return true

}Else

return false

Page 31: COSC 2007  Data Structures II

31

ADT 2-3 Trees Implementation

Deletionfix(Tree23Node n)if (n is the root)

Remove the rootelse{ Let p be the parent of n

if (some sibling of n has two items){ Distribute items appropriately among n, sibling, & p

if (n is internal)Move appropriate child from sibling to n

}else // merge the node{ Choose an adjacent sibling s of n

Bring the appropriate item down from p into sif (n is internal)

Move n’s child to sRemove node nif (p is now empty)

fix(p)} // end if

} // end if

Page 32: COSC 2007  Data Structures II

32

Review A(n) ______ is a tree in which each

internal node has either two or three children, and all leaves are at the same level. red-black tree 2-3 tree 2-3-4 tree AVL tree

Page 33: COSC 2007  Data Structures II

33

Review In a 2-3 tree, ______.

all internal nodes have 2 children all internal nodes have 3 children all leaves are at the same level all nodes have 2 data items

Page 34: COSC 2007  Data Structures II

34

Review All the nodes in a binary tree are ______.

single nodes 1-nodes 2-nodes double nodes

Page 35: COSC 2007  Data Structures II

35

Review A node that contains one data item and

has two children is called a ______. 1-node 2-node single node double node

Page 36: COSC 2007  Data Structures II

36

Review A node that contains two data items and

has three children is called a ______. 2-node 3-node double node triple node

Page 37: COSC 2007  Data Structures II

37

Review If a particular 2-3 tree does NOT contain

3-nodes, it is like a ______. general tree binary search tree full binary tree complete binary tree

Page 38: COSC 2007  Data Structures II

38

Review A 2-3 tree of height h always has at least

______ nodes. h2

h2 – 2 2h

2h – 1

Page 39: COSC 2007  Data Structures II

39

Review In a 3-node, ______.

the left child has the largest search key the middle child has smallest search key the middle child has the largest search key the right child has the largest search key

Page 40: COSC 2007  Data Structures II

40

Review In a 2-3 tree, a leaf may contain ______

data item(s). one one or two two two or three three

Page 41: COSC 2007  Data Structures II

41

Review Searching a 2-3 tree is ______.

O(n) O(log2n)

O(log2n * n) O(n2)

Page 42: COSC 2007  Data Structures II

42

Review A 2-3 implementation of a table is ______

for all table operations. O(n) O(log2n)

O(log2n * n) O(n2)