890 likes | 1.29k Views
Advanced Data Structures. CHAPTER 2. Outlines. Binary search trees B-trees Heaps and priority queues Skip list. Binary Search Trees. Binary Search Trees: Nodes. left. right. key. parent. internal node. left. key. parent. right. left. key. parent. right. root node. leaf node.
E N D
Advanced Data Structures CHAPTER 2
Outlines • Binary search trees • B-trees • Heaps and priority queues • Skip list Chapter 2: Advanced Data Structures
Binary Search Trees: Nodes left right key parent internal node left key parent right left key parent right root node leaf node Chapter 2: Advanced Data Structures
p q Property of Binary Search Trees For any node n in a binary search tree T, • if p is a node in the left subtree of n, then p.key ≤ n.key; • if q is a node in the right subtree of n, then n.key ≤ q.key. T n Chapter 2: Advanced Data Structures
12 10 8 17 6 19 4 9 4 9 12 20 5 Example of Binary Search Trees Chapter 2: Advanced Data Structures
Inorder Traversal of Binary Search Trees 12 inorder(T: node) ifT is not null, then inorder(T.left) print(T.key) inorder(T.right) return. } 8 17 4 9 5 10 6 19 4 9 12 20 Chapter 2: Advanced Data Structures
Search in Binary Search Trees 12 search(n: node, k: key) ifn is null orn.key = k, then return(n) ifk < n.key, thensearch(n.left, k) elsesearch(n.right, k) return. 8 17 4 9 5 10 6 19 4 9 12 20 Chapter 2: Advanced Data Structures
insert(T: node, z: node) if T is null thenT.root = z return if key[z] < key[T] then if T.left is null then T.left = z z.p = T else insert(x.left, z) elseif T.right is null then x.right = z z.p = T else insert(x.right, z) return insert(T: node, z: node) y = null x = T while x is not null y = x if z.key< x.key then x = x.left else x = x.right z.parent= y if y = NIL then T.root= z else if z.key< y.key then y.left= z else y.right = z Insertion in Binary Search Trees The tree T is empty. Found the place, and insert as the left child. Found the place, and insert as the right child. Chapter 2: Advanced Data Structures
Deletion in Binary Search Trees • See textbook Chapter 2: Advanced Data Structures
B-trees • Index structures for large amount of data. • All data cannot be resided in the main memory. • Data is stored in a disk. Chapter 2: Advanced Data Structures
Disk Operations • In a disk, data is organized in disk pages. • To read data in a disk, • The disk rotates, and the R/W head moves to the page containing the data. • Then, the whole disk page is read. From: Cormen, T., C. Leiserson, R. Rivest, and C. Stein, Introduction to Algorithms, MIT Press, 2001. Chapter 2: Advanced Data Structures
B-trees: Nodes Internal node number of keys false key1 key2 key3 keyn leaf Pointers to other nodes leaf node number of keys true key1 key2 key3 keyn leaf Pointers to data pages Chapter 2: Advanced Data Structures
q p … … … … key1 key1 keyi keyi keyi+1 keyi+1 false false Properties of B-trees For any node n in a B-tree T • n.keyi ≤ c.key1 ≤ c.key2 ≤ … ≤ c.keyq ≤ n.keyi+1 • If n is a leaf node, the depth of nis h, where h is the tree’s height. n c Chapter 2: Advanced Data Structures
Properties of B-trees • The minimum degree, t, of the B-tree is the lower bound on the number of keys a node can contain. (t ≥ 2) • Every node other than the root must have at least t − 1 keys. • Every internal node other than the root thus has at least t children. • If the tree is nonempty, the root must have at least one key. • Every node can contain at most 2t − 1 keys. Therefore, an internal node can have at most 2t children. • We say that a node is full if it contains exactly 2t − 1 keys. Chapter 2: Advanced Data Structures
75 65 26 69 66 11 68 71 17 67 88 60 6 8 12 16 70 Search in B-trees Chapter 2: Advanced Data Structures
Search in B-trees B-TREE-SEARCH(x, k) i = 1 ►find a proper child while i ≤ x.n and k > x.keyi do i =i + 1 if i ≤ x.n and k = x.keyi then return (x, i ) if x.leaf then return NIL ►recursively search in the proper subtree else DISK-READ (x.ci) return B-TREE-SEARCH (x.ci, k) Chapter 2: Advanced Data Structures
Creating B-trees B-TREE-CREATE(T) x=ALLOCATE-NODE() x.leaf =TRUE x.n= 0 DISK-WRITE(x) T.root = x Chapter 2: Advanced Data Structures
8 26 65 Insertion in B-trees 17 11 8 17 26 65 11 Chapter 2: Advanced Data Structures
26 65 8 11 12 16 Insertion in B-trees 6 17 11 12 16 Chapter 2: Advanced Data Structures
26 71 65 6 8 12 16 Insertion in B-trees 60 11 17 65 65 71 Chapter 2: Advanced Data Structures
75 26 67 60 88 71 6 8 12 16 Insertion in B-trees 66 11 17 65 71 75 88 Chapter 2: Advanced Data Structures
75 69 66 26 67 60 88 6 8 12 16 Insertion in B-trees 70 11 17 65 71 68 69 Chapter 2: Advanced Data Structures
11 17 65 71 69 65 11 71 75 26 66 88 17 67 60 6 8 12 16 Insertion in B-trees 70 71 68 Chapter 2: Advanced Data Structures
75 11 66 69 68 26 65 88 60 71 17 67 6 8 12 16 Insertion in B-trees 70 Chapter 2: Advanced Data Structures
Insertion in B-trees B-TREE-INSERT(T, k) r =T.root if r.n = 2t − 1 ► The root node r is full, then create a new rootnode then s =ALLOCATE-NODE() T.root =s; s.leaf=FALSE s.n = 0; s.c1=r ► Split old root into two, and put under the new root B-TREE-SPLIT-CHILD(s, 1, r) B-TREE-INSERT-NONFULL(s, k) else B-TREE-INSERT-NONFULL(r, k) Chapter 2: Advanced Data Structures
B-TREE-SPLIT-CHILD(x, i, y) ►Create a new node z =ALLOCATE-NODE() z.leaf =y.leaf z.n =t − 1 ►Move 1/2 of old node to new for j = 1 to t − 1 do z.keyj=y.keyj+t if not y.leaf then for j = 1 to t do z.cj =y.cj+t y.n =t − 1 ►Move up other 1/2 of old node for j =x.n + 1 downto i + 1 do x.cj+1=x.cj x.ci+1=z for j =x.ndownto i do x.keyj+1=x.keyj x.keyi=y.keyt x.n=x.n + 1 DISK-WRITE(y) DISK-WRITE(z) DISK-WRITE(x) Splitting Nodes in B-trees Chapter 2: Advanced Data Structures
Binary Heaps • Binary heapis an array • can be viewed as a nearly complete binary tree • each node of the tree corresponds to an element of the array that stores the value in the node. • The tree is completely filled on all levels except possibly the lowest, which is filled from the left up to a point. 1 left right 2 3 6 4 5 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 Chapter 2: Advanced Data Structures
Representation of Biinary Heaps • An array A that represents a heap is an object with two attributes: • length[A], which is the number of elements in the array • heap-size[A], the number of elements in the heap stored within array A. heap-size length Chapter 2: Advanced Data Structures
Properties of Binary Heaps • If a heap contains n elements, its height is lg2n. • In a max-heaps For every non-root node i, A[PARENT(i)] ≥ A[i] • In a min-heaps For every non-root node i, A[PARENT(i)] ≤ A[i] Chapter 2: Advanced Data Structures
Examples 15 Max heap 12 8 9 4 6 5 5 3 6 2 1 9 7 11 19 20 18 Min heap 15 12 Chapter 2: Advanced Data Structures
Heapify 7 12 8 9 4 6 5 3 6 2 1 Chapter 2: Advanced Data Structures
Heapify 7 12 12 7 8 9 4 6 5 3 6 2 1 Chapter 2: Advanced Data Structures
Heapify 12 7 9 8 7 9 4 6 5 3 6 2 1 Chapter 2: Advanced Data Structures
Heapify MAX-HEAPIFY(A, i ) l = LEFT(i ) r = RIGHT(i ) ► Find node containing the largest value among i and its valid children. if l ≤ heap-size[A] and A[l] > A[i ] then largest = l else largest = i if r ≤ heap-size[A] and A[r] > A[largest] then largest = r ► Swap the largest node and node i if the node i is not the largest if largest i then exchange A[i ] and A[largest] MAX-HEAPIFY(A, largest) Chapter 2: Advanced Data Structures
Time Complexity for Heapify • worst-case • The left subtree is one-level taller than the right subtree, and both are complete binary trees • The left subtree contains (2n-1)/3 nodes and the right subtree contains (n-2)/3. Let T(n) be time spent to heapify an n-node heap. T(n) = T((2n-1)/3) + k T(n) Ο(lg n) T(n) Ο(h) • since the height h of a heap is in Ο(lg n) Chapter 2: Advanced Data Structures
Building Max-heaps: BUILD-MAX-HEAP(A) 1 2 3 11 7 4 9 5 11 10 2 6 7 3 8 9 10 11 4 2 5 Chapter 2: Advanced Data Structures
Building Max-heaps: BUILD-MAX-HEAP(A) 1 11 11 1 10 7 9 10 1 5 6 3 8 2 4 1 5 Chapter 2: Advanced Data Structures
Building Max-heaps: BUILD-MAX-HEAP(A) heap-size[A] = length[A] ► Start from the rightmost node in the level above leaves for i = length[A]/2downto 1 do MAX-HEAPIFY(A, i ) 1 2 3 4 5 6 7 8 9 10 11 Chapter 2: Advanced Data Structures
Time to Build Max-heaps Let n be the heap size, T(n) be the time spent in BUILD-MAX-HEAP for heap of size n, t(h) be the time spent in MAX-HEAPIFY for the heap of height h, k(h, n) be the number of subtrees of height h in the heap of size n. lg nlg nlg n T(n) t(h) k(h, n) = c hn/2h+1 = c n (h/2h) h=0h=02h=0 lg n T(n) = O(n (h/2h)) h=0 Since (h/2h)) = 2, T(n) = O(n). h=0 Chapter 2: Advanced Data Structures
Heapsort HEAPSORT(A) BUILD-MAX-HEAP(A) for i = length[A] downto 2 ► Swap themax node with last in heap, and ► heapify heap, excluding the last (old max) do exchange A[1] and A[i ] heap-size[A] = heap-size[A] − 1 MAX-HEAPIFY(A, 1) Chapter 2: Advanced Data Structures
Time for Heapsort • The call to BUILD-MAX-HEAP takes O(n) • Each of the n − 1 calls to MAX-HEAPIFY takes O(lg n) • HEAPSORT takes O(n lg n). Chapter 2: Advanced Data Structures
Priority Queues • A priority queue is a max-heap with operations for queues. • Insert • Extract-max • Increase-key HEAP-MAXIMUM(A) return A[1] Chapter 2: Advanced Data Structures
Extract-Max In Priority Queues HEAP-EXTRACT-MAX(A) if heap-size[A] < 1 then error “heap underflow” ► Take the maximum element from the root max = A[1] ► Move the last element in the heap to the root A[1] = A[heap-size[A]] heap-size[A] = heap-size[A] − 1 ► Heapify MAX-HEAPIFY(A, 1) return max Chapter 2: Advanced Data Structures
Increase Key HEAP-INCREASE-KEY(A, i, key) if key < A[i ] then error “new key < current key” A[i ] = key ► Swap the increased element with its parent up toward ► the root until it is not greater than its parent while i > 1 and A[PARENT(i)] < A[i ] do exchange A[i] and A[PARENT(i)] i = PARENT(i) Chapter 2: Advanced Data Structures
Insert MAX-HEAP-INSERT(A, key) ► Insert the minimum element heap-size[A] = heap-size[A] + 1 A[heap-size[A]]=−∞ ► Increase the minimum element HEAP-INCREASE-KEY(A, heap-size[A], key) Chapter 2: Advanced Data Structures