180 likes | 353 Views
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.
E N D
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
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)
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)
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)
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?
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
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
Tree-Balancing Algorithms • Tree rotations • Red-Black Trees • Splay Trees • Others • AVL Trees • 2-3 Trees and 2-3-4 Trees
Tree Rotations Right Rotate(T,A) A B T1 B A T3 T1 T2 Left Rotate(T,B) T2 T3
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?
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
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
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
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
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
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