1 / 34

Analysis of Algorithms CS 477/677

Analysis of Algorithms CS 477/677. Instructor: Monica Nicolescu Lecture 11. Binary Search Trees. Support many dynamic set operations SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR, INSERT, DELETE Running time of basic operations on binary search trees On average: (lgn)

rhian
Download Presentation

Analysis of Algorithms CS 477/677

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Analysis of AlgorithmsCS 477/677 Instructor: Monica Nicolescu Lecture 11

  2. Binary Search Trees • Support many dynamic set operations • SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR, INSERT, DELETE • Running time of basic operations on binary search trees • On average: (lgn) • The expected height of the tree is lgn • In the worst case: (n) • The tree is a linear chain of n nodes CS 477/677 - Lecture 11

  3. parent L R key data Right child Left child Binary Search Trees • Tree representation: • A linked data structure in which each node is an object • Node representation: • Key field • Satellite data • Left: pointer to left child • Right: pointer to right child • p: pointer to parent (p [root [T]] = NIL) • Satisfies the binary search tree property CS 477/677 - Lecture 11

  4. 5 3 7 2 5 9 Binary Search Tree Example • Binary search tree property: • If y is in left subtree of x, then key [y] ≤ key [x] • If y is in right subtree of x, then key [y] ≥ key [x] CS 477/677 - Lecture 11

  5. 15 6 18 3 7 17 20 2 4 13 9 Successor Def: successor (x ) =y, such that key [y] is the smallest key > key [x] • E.g.:successor (15) = successor (13) = successor (9) = • Case 1: right (x) is non empty • successor (x ) = the minimum in right (x) • Case 2: right (x) is empty • go up the tree until the current node is a left child: successor (x ) is the parent of the current node • if you cannot go further (and you reached the root): x is the largest element 17 15 13 CS 477/677 - Lecture 11

  6. 15 6 18 3 7 17 20 2 4 13 9 Finding the Successor Alg: TREE-SUCCESSOR(x) • if right [x]  NIL • then return TREE-MINIMUM(right [x]) • y ← p[x] • while y  NIL and x = right [y] • do x ← y • y ← p[y] • return y Running time: O (h), h – height of the tree y x CS 477/677 - Lecture 11

  7. 15 6 18 3 7 17 20 2 4 13 9 Predecessor Def: predecessor (x ) =y, such that key [y] is the largest key < key [x] • E.g.:predecessor (15) = predecessor (9) = predecessor (13) = • Case 1: left (x) is non empty • predecessor (x ) = the maximum in left (x) • Case 2: left (x) is empty • go up the tree until the current node is a right child: predecessor (x ) is the parent of the current node • if you cannot go further (and you reached the root): x is the smallest element 13 7 9 CS 477/677 - Lecture 11

  8. Insert value 13 12 5 18 2 9 15 19 1 3 13 17 Insertion • Goal: • Insert value vinto a binary search tree • Idea: • Begining at the root, go down the tree and maintain: • Pointer x: traces the downward path (current node) • Pointer y: parent of x (“trailing pointer” ) • If key [x] < v move to the right child of x, else move to the left child of x • When x is NIL, we found the correct position • If v < key [y] insert the new node as y’s left child else insert it as y’s right child CS 477/677 - Lecture 11

  9. y y 12 12 12 12 x 5 5 5 5 18 18 18 18 2 2 2 2 9 9 9 9 15 15 15 15 19 19 19 19 1 1 1 1 3 3 3 3 17 17 17 17 x 13 Example: TREE-INSERT x, y=NIL Insert 13: x = NIL y = 15 CS 477/677 - Lecture 11

  10. 12 5 18 2 9 15 19 1 3 13 17 Alg:TREE-INSERT(T, z) • y ← NIL • x ← root [T] • while x≠ NIL • do y ← x • if key [z] < key [x] • then x ← left [x] • else x ← right [x] • p[z] ← y • if y = NIL • then root [T] ← z Tree T was empty • else if key [z] < key [y] • then left [y] ← z • else right [y] ← z Running time: O(h) CS 477/677 - Lecture 11

  11. 15 15 5 16 16 5 20 20 3 3 12 12 10 13 18 18 23 23 10 delete 6 6 7 7 Deletion • Goal: • Delete a given node z from a binary search tree • Idea: • Case 0: z has no children • Delete z by making the parent of z point to NIL, instead of to z z CS 477/677 - Lecture 11

  12. 15 15 delete 5 5 20 16 10 13 10 18 23 20 6 6 3 3 12 12 18 23 7 7 Deletion • Case 1: zhas one child • Delete z by making the parent of z point to z’s child, instead of to z • Update the parent of z’s child to be z’s parent z CS 477/677 - Lecture 11

  13. 6 15 15 z delete 16 16 5 6 20 20 3 3 12 12 18 18 23 23 10 13 10 13 6 7 7 Deletion • Case 2: z has two children • z’s successor (y) is the minimum node in z’s right subtree • y has either no children or one right child (but no left child) • Delete y from the tree (via Case 0 or 1) • Replace z’s key and satellite data with y’s. y CS 477/677 - Lecture 11

  14. Idea for TREE-DELETE(T, z) • Determine a node y that has to be deleted • If z has 0 or 1 child  y = z (case 0 or 1) • If z has 2 children  y = TREE-SUCCESSOR(z) (case 2) • In any case y has at most 1 child!!! • Set a node x to the non-nil child of y • Delete node y: set the parent of x to be the parent of y • If y is the root, x becomes the new root otherwise, update parent pointers accordingly • If the deleted node y was the successor of z (case 2), move y’s key and satellite data onto z • The deleted node y is returned for recycling CS 477/677 - Lecture 11

  15. 15 5 x 16 20 10 13 3 12 18 23 6 7 TREE-DELETE(T, z) • if left[z] = NIL or right[z] = NIL • then y ← z • else y ← TREE-SUCCESSOR(z) • if left[y] NIL • then x ← left[y] • else x ← right[y] • if x  NIL • then p[x] ← p[y] z has 0 or 1 child z has 2 children y CS 477/677 - Lecture 11

  16. 15 5 16 20 10 13 3 12 18 23 6 7 TREE-DELETE(T, z) – cont. y • if p[y] = NIL • then root[T] ← x • else if y = left[p[y]] • then left[p[y]] ← x • else right[p[y]] ← x • if y  z • then key[z] ← key[y] • copy y’s satellite data into z • return y x CS 477/677 - Lecture 11

  17. Binary Search Trees - Summary • Operations on binary search trees: • SEARCHO(h) • PREDECESSORO(h) • SUCCESORO(h) • MINIMUMO(h) • MAXIMUMO(h) • INSERTO(h) • DELETEO(h) • These operations are fast if the height of the tree is small – otherwise their performance is similar to that of a linked list CS 477/677 - Lecture 11

  18. Red-Black Trees • “Balanced” binary trees guarantee an O(lgn) running time on the basic dynamic-set operations • Red-black tree • Binary tree with an additional attribute for its nodes: color which can be red or black • Constrains the way nodes can be colored on any path from the root to a leaf • Ensures that no path is more than twice as long as another  the tree is balanced • The nodes inherit all the other attributes from the binary-search trees: key, left, right, p CS 477/677 - Lecture 11

  19. Red-Black Trees Properties • Every node is either red or black • The root is black • Every leaf (NIL) is black • If a node is red, then both its children are black • No two red nodes in a row on a simple path from the root to a leaf • For each node, all paths from the node to descendant leaves contain the same number of black nodes CS 477/677 - Lecture 11

  20. Example: RED-BLACK TREE • For convenience we use a sentinel NIL[T] to represent all the NIL nodes at the leafs • NIL[T] has the same fields as an ordinary node • Color[NIL[T]] = BLACK • The other fields may be set to arbitrary values 26 17 41 NIL NIL 30 47 38 50 NIL NIL NIL NIL NIL NIL CS 477/677 - Lecture 11

  21. 26 17 41 NIL NIL 30 47 38 50 NIL NIL NIL NIL NIL NIL Black-Height of a Node • Height of a node:the number of edges in a longest path to a leaf • Black-height of a node x: bh(x) is the number of black nodes (including NIL) on a path from x to leaf, not counting x h = 4 bh = 2 h = 3 bh = 2 h = 1 bh = 1 h = 2 bh = 1 h = 2 bh = 1 h = 1 bh = 1 h = 1 bh = 1 CS 477/677 - Lecture 11

  22. 26 17 41 30 47 38 50 Properties of Red-Black Trees • Claim • Any node with height h has black-height ≥ h/2 • Proof • By property 4, there are at most h/2red nodes on the path from the node to a leaf • Hence at least h/2 are black • Property 4: if a node is red then both its children are black CS 477/677 - Lecture 11

  23. Properties of Red-Black Trees Claim • The subtree rooted at any node x contains at least 2bh(x) - 1 internal nodes Proof: By induction on height of x Basis: height[x] = 0  x is a leaf (NIL[T])  bh(x) = 0 # of internal nodes: 20 - 1 = 0 x NIL CS 477/677 - Lecture 11

  24. 26 17 41 30 47 38 50 Properties of Red-Black Trees Inductive step: • Let height(x) = h and bh(x) = b • Any child y of x has: • bh (y) = • bh (y) = b (if the child is red), or b - 1 (if the child is black) CS 477/677 - Lecture 11

  25. Properties of Red-Black Trees • Want to prove: • The subtree rooted at any node x contains at least 2bh(x) - 1 internal nodes • Assume true for children of x: • Their subtrees contain at least 2bh(x) - 1 – 1 internal nodes • The subtree rooted at x contains at least: (2bh(x) - 1 – 1) + (2bh(x) - 1 – 1) + 1 = 2 · (2bh(x) - 1 - 1) + 1 = 2bh(x) - 1 internal nodes x r l CS 477/677 - Lecture 11

  26. Properties of Red-Black Trees Lemma: A red-black tree with n internal nodes has height at most 2lg(n + 1). Proof: n • Add 1 to all sides and then take logs: n + 1 ≥ 2b ≥ 2h/2 lg(n + 1) ≥ h/2 h ≤ 2 lg(n + 1) root height(root) = h bh(root) = b r l ≥ 2h/2 - 1 ≥ 2b - 1 number n of internal nodes since b  h/2 CS 477/677 - Lecture 11

  27. Operations on Red-Black Trees • The non-modifying binary-search tree operations MINIMUM, MAXIMUM, SUCCESSOR, PREDECESSOR, and SEARCH run in O(h) time • They take O(lgn) time on red-black trees • What about TREE-INSERT and TREE-DELETE? • They will still run in O(lgn) • We have to guarantee that the modified tree will still be a red-black tree CS 477/677 - Lecture 11

  28. 26 17 41 30 47 38 50 INSERT INSERT: what color to make the new node? • Red? Let’s insert 35! • Property 4: if a node is red, then both its children are black • Black? Let’s insert 14! • Property 5: all paths from a node to its leaves contain the same number of black nodes CS 477/677 - Lecture 11

  29. 26 OK! Does not change any black heights OK! Does not create two red nodes in a row 17 41 30 47 38 50 DELETE DELETE: what color was the node that was removed? Red? • Every node is either red or black • The root is black • Every leaf (NIL) is black • If a node is red, then both its children are black • For each node, all paths from the node to descendant leaves contain the same number of black nodes OK! OK! OK! CS 477/677 - Lecture 11

  30. Not OK! If removing the root and the child that replaces it is red 26 Not OK! Could change the black heights of some nodes Not OK! Could create two red nodes in a row 17 41 30 47 38 50 DELETE DELETE: what color was the node that was removed? Black? • Every node is either red or black • The root is black • Every leaf (NIL) is black • If a node is red, then both its children are black • For each node, all paths from the node to descendant leaves contain the same number of black nodes OK! OK! CS 477/677 - Lecture 11

  31. Rotations • Operations for restructuring the tree after insert and delete operations on red-black trees • Rotations take a red-black tree and a node within the tree and: • Together with some node re-coloring they help restore the red-black tree property • Change some of the pointer structure • Do not change the binary-search tree property • Two types of rotations: • Left & right rotations CS 477/677 - Lecture 11

  32. Left Rotations • Assumption for a left rotation on a node x: • The right child of x (y) is not NIL • Idea: • Pivots around the link from x to y • Makes y the new root of the subtree • x becomes y’s left child • y’s left child becomes x’s right child CS 477/677 - Lecture 11

  33. Example: LEFT-ROTATE CS 477/677 - Lecture 11

  34. Readings • Chapters 12, 13 CS 477/677 - Lecture 11

More Related