1 / 18

Binary search trees

Binary search trees. Definition Binary search trees and dynamic set operations Expected height of BST Balanced binary search trees Tree rotations Red-black trees. Binary search trees. Basic tree property For any node x left subtree has nodes ≤ x right subtree has nodes ≥ x.

liseli
Download Presentation

Binary search trees

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. Binary search trees • Definition • Binary search trees and dynamic set operations • Expected height of BST • Balanced binary search trees • Tree rotations • Red-black trees

  2. Binary search trees • Basic tree property • For any node x • left subtree has nodes ≤x • right subtree has nodes ≥ x

  3. BSTs and Dynamic Sets • Dynamic set operations and binary search trees • Search(S,k) • Insert(S,x) • Delete(S,x) • Minimum or Maximum(S) • Successor or Predecessor (S,x) • List All(S) • Merge(S1,S2)

  4. Dynamic Set Operations • Listall(T) • time to list? • Search(T,k)? • search time? • Minimum(T)? Maximum(T)? • search time? • Successor(T,x)? Predecessor(T,x)? • Search time • Simple Insertion(T,x)

  5. Simple deletion • Delete(T,x): Three possible cases: • a) x is a leaf : • b) x has one child : • c) x has two children : Replace x with successor(x). • Successor(x) has at most one child (why?); • Use step a or b on successor(x)

  6. Simple binary search trees • What is the expected height of a binary search tree? • Difficult to compute if we allow both insertions and deletions • With insertions, analysis of section 12.4 shows that expected height is O(log n) • Implications about BSTs as dynamic sets?

  7. Definitions and Assumptions • We assume that the n items are distinct: 1-n • We assume no deletions • We assume that each of the n! permutations of the n items are equally likely • Let Xi be the height of a tree with i items • Let • Let Zi be an indicator variable that the first item inserted is item i

  8. Recurrence relation for Xn and Yn • Xn = Σi Zi (1 + max(Xi-1,Xn-i)) • Justify this • Yn = Σi Zi (2 max(Yi-1,Yn-i)) • Analysis on handout to get

  9. Tree-Balancing Algorithms • Tree rotations • Red-Black Trees • Splay Trees • Others • AVL Trees • 2-3 Trees and 2-3-4 Trees

  10. Tree Rotations Right Rotate(T,A) A B T1 B A T3 T1 T2 Left Rotate(T,B) T2 T3

  11. Red-Black Trees • All nodes in the tree are either red or black. • Root node is black. • Every leaf (null-child) is included and black. • All red nodes must have two black children. • Every path from any node x (including the root) to a leaf must have the same number of black nodes. • How balanced of a tree will this produce? • How hard will it be to maintain?

  12. Example Red-Black Tree

  13. Insertion(T,z) • Find place to insert using simple insertion • Color node z as red • Fix up tree so that it is still a red-black tree • What needs to be fixed? • Problem 1: z is root (first node inserted) • Minor detail • Problem 2: parent(z) is red

  14. z z RB-Insert-Fixup • Situation: parent(z) is red and z is red • Case 1: uncle(z) is red • Then make both uncle(z) and parent(z) black and p(p(z)) red and recurse up tree

  15. B A T1 A z B z T3 T2 T3 T1 T2 RB-Insert-Fixup(parent(z) is right child of parent(parent(z))) • Situation: parent(z) is red and z is red • Case 2: uncle(z) is black and z is a left child • Right rotate to make into case 3

  16. C B B C A T1 A z T1 T2 T3 T2 T3 RB-Insert-Fixup(parent(z) is right child of parent(parent(z))) • Situation: parent(z) is red and z is red • Case 3: uncle(z) is black and z is a right child • Left rotate to make B root of tree

  17. RB-Insert-Fixup Analysis(parent(z) is right child of parent(parent(z))) • Situation: parent(z) is red and z is red • Case 1: no rotations, always moving up tree • Cases 2 and 3: At most 2 rotations total and tree ends up balanced • No more need to fix up once these cases are met • Total cost: at most 2 rotations and log n operations

  18. Delete(T,z) • Find node y to delete using simple deletion • Let x be a child of y if such a child exists (otherwise x is a null child) • If y is black, fix up tree so that it is still a red-black tree • What needs to be fixed? • Problem 1: y was root, so now we might have red root • Problem 2: x and parent(y) are both red • Problem 3: Removal of y violates black height properties of paths that used to go through y

More Related