Sorted Tables. Ordered Dictionary. Binary Search. Binary Search

Ordered Dictionary Sorted Tables Dictionary is a set of elements, the keys, supported by the dictionary operations, such as: If a dictionary D is o...
Author: Timothy Gaines
1 downloads 0 Views 1MB Size
Ordered Dictionary

Sorted Tables

Dictionary is a set of elements, the keys, supported by the dictionary operations, such as:

If a dictionary D is ordered, we can store its items in a vector S by nondecreasing order of the keys. This allows for faster searching than would be possible had S been, e.g., a linked list. We refer to this ordered vector implementation of a dictionary D as a lookup table.

findElement (k) -- k position, e element insertItem(k,e) removeElement (k)

Ordered dictionary maintains an order relation for the elements in it. week 3

Complexity of Algorithms

1

Binary Search

Complexity of Algorithms

Complexity of Algorithms

2

Binary Search

Accessing an element of S (in arraybased representation of size n) by its rank takes O(1) time The item at rank i has a key no smaller than keys of the items of ranks 0,…,i-1, and no larger than keys of the items of ranks i+1,…,n-1. Searching is done by decreasing the range of the elements in S week 3

week 3

Looking for k in S, the range in S is defined as a pair of ranks: low and high, s.t., all elements in S with ranks < low (> high) are smaller (larger) then k Initially, low = 0 and high = n-1 key(i) denotes the key at rank i, and elem(i) denotes an element in key(i) 3

week 3

Complexity of Algorithms

4

1

Binary Search

Binary Search - Algorithm

In order to decrease a size of the range we compare k to the key of the median mid of the range, i.e., mid = (low+high)/2 3 cases are possible k = key(mid), the search is completed successfully k < key(mid), search continued with high = mid-1 k > key(mid), search continued with low = mid+1

week 3

Complexity of Algorithms

5

Binary Search - Illustration

week 3

Complexity of Algorithms

6

Binary Search - Complexity Let function f(n) represent the running time of the binary search method We can characterize the running time of the recursive binary search algorithm as follows:

Binary search runs in time O(log n) week 3

Complexity of Algorithms

7

week 3

Complexity of Algorithms

8

2

Binary Search - Complexity

Binary Search Tree (BST)

Binary search vs. linear file

week 3

Complexity of Algorithms

Binary Search Tree (BST) applies the motivation of the binary search procedure to a tree-based data structure. In BST each internal node v stores an element e, s.t., the elements stored in the left subtree of v are less than or equal to e, and the elements stored in the right subtree of v are greater than or equal to e . 9

BST

week 3

Complexity of Algorithms

10

Searching in BST

An inorder traversal of BST visits the elements stored in such a tree in nondecreasing order.

week 3

Complexity of Algorithms

11

week 3

Complexity of Algorithms

12

3

Searching in BST - Analysis

Insertion in BST

Insertion of 78 implemented in time O(h) week 3

Complexity of Algorithms

13

Removal in BST

Complexity of Algorithms

Complexity of Algorithms

14

Removal in BST

Removal of a node (32) with one external child is done in time O(h) week 3

week 3

15

Removal of a node (65) with two internal children is done in time O(h) week 3

Complexity of Algorithms

16

4

Inefficiency of general BSTs

AVL Trees

All operations in BST are performed in time O(h), where h is the height of BST Unfortunately h might be as large as n, e.g., after n consecutive insertions of elements with keys in increasing order The advantages of the binary search (O(log n) time update) might be lost if BST is not balanced week 3

Complexity of Algorithms

17

AVL Trees

week 3

Complexity of Algorithms

18

Insertion in AVL

Theorem: the height of an AVL tree T storing n items is O(log n) Consequence 1: the search in AVL can be performed in time O(log n) Consequence 2: The insertion and the removal in AVL need more careful implementation (rotations) week 3

Height-Balance Property: for every internal node v of T, the heights of the children of v can differ by at most 1

Complexity of Algorithms

19

An insertion in an AVL tree begins as insertion in a general BST, i.e., we attach a new external node (leaf) to BST This action may violate the height-balance property, i.e., for some nodes the action may increase their heights by one The bottom-up mechanism (based on rotations) is applied to fix the “unbalance’’ of AVL subtrees week 3

Complexity of Algorithms

20

5

Fixing AVL after Insertion

Single rotations in AVL

Fixing of an AVL after insertion of 54 week 3

Complexity of Algorithms

21

Double rotations in AVL

week 3

Complexity of Algorithms

22

Removal in AVL An removal in an AVL tree begins as removal in a general BST This action may violate the height-balance property, i.e., for some nodes the action may decrease their heights by one The bottom-up mechanism (based on rotations) is applied to fix the “unbalance’’ of AVL subtrees

week 3

Complexity of Algorithms

23

week 3

Complexity of Algorithms

24

6

Fixing AVL after Removal

AVL Performance All operations (search, insertion and removal) can be implemented in AVL in O(log n) time

Fixing of an AVL after removal of 32 week 3

Complexity of Algorithms

25

(2,4) Trees

Complexity of Algorithms

26

(2,4) Trees

Every node in (2,4) tree has at least 2 and at most 4 children All external nodes (leaves) in (2,4) tree have the same depth Theorem: The height of a (2,4) tree storing n items is Θ (log n)

week 3

week 3

Complexity of Algorithms

27

Each internal node v in (2,4) tree contains 1, 2 or 3 keys that define the ranges of keys stored in 2, 3 or 4 (respectively) consecutive subtrees of v

week 3

Complexity of Algorithms

28

7

(2,4) Trees - Search

(2,4) Tree - Insertion

Search for an key k in (2,4) tree T is done via tracing the path in T starting at the root in a top-down manner Visiting a node v we compare k with keys (ki ) stored at v: If k = k i the search is completed If ki ≤ k ≤ k i+1 , the i+1th subtree of v is searched recursively

week 3

Complexity of Algorithms

29

(2,4) Trees – Split Operation

An insertion of k in an (2,4) tree T begins with a search for an internal node on the lowest level that could accommodate k without violation of the range rule This action may overflow the node-size of a node v acquiring new key k, i.e., the number of keys in v can grow up to 4 The bottom-up mechanism (based on split operation) is applied to fix the “overflown’’ nodes of (2,4) tree T week 3

Complexity of Algorithms

30

Sequence of Insertions - 1

Example of a split operation

week 3

Complexity of Algorithms

31

week 3

Complexity of Algorithms

32

8

Sequence of Insertions - 2

(2,4) Tree - Removal Removal of key k from (2,4) tree T begins with search for a node v possessing key ki = k Key ki is replaced by the largest key in the i th consecutive subtree of v The bottom-up mechanism (based on transfer and fusion operation) is applied to fix the “underflown’’ nodes of (2,4) tree

week 3

Complexity of Algorithms

33

Sequence of Removals - 1

week 3

Complexity of Algorithms

week 3

Complexity of Algorithms

34

Sequence of Removals - 2

35

week 3

Complexity of Algorithms

36

9

Sequence of Removals - 3

(2,4) Tree Performance The height of a (2,4) tree storing n elements is O(log n) A split, transfer and fusion operations take O(1) time A search, insertion, and removal of an element in a tree visits O(log n) nodes

week 3

Complexity of Algorithms

37

week 3

Complexity of Algorithms

38

10