binary search trees 5 right subtree of 5 right subtree of 4 left subtree of 4 8 6 7 3 binary tree and … 1 2 4 keys in node’s right subtree are greater than node’s keys in node’s left subtree are less than node’s for each node: each node has a key 25
not a binary search tree 8 5 2 4 6 11 10 15 18 20 21 26
binary search tree versus binary tree binary search trees are a kind of binary tree …but — often people say “binary tree” to mean “binary search tree” 27
BST: fjnd (pseudocode) find(node, key) { if (node == NULL) return NULL; else // if (key == node->key) return node; } 28 else if (key < node − >key) return find(node − >left, key) else if (key > node − >key) return find(node − >right, key)
BST: insert (pseudocode) if (node == NULL) else // if (key > root->key) ; // duplicate -- no new node needed } 29 insert(Node *&node, key) { node = new BinaryNode(key); else if (key < node − >key) insert(node − >left, key); else if (key < root − >key) insert(node − >right, key);
BST: fjndMin (pseudocode) return node; else } 30 findMin(Node *node, key) { if (node − >left == NULL) insert(node − >left, key);
BST: remove (1) 5 4 1 3 9 7 11 5 4 1 3 9 11 case 1: no children 31
BST: remove (2) 5 4 1 3 9 7 11 5 4 3 9 7 11 case 2: one child 32
BST: remove (3) 4 (alternately: maximum of left subtree, …) replace with minimum of right subtree case 3: two children 11 9 3 7 5 11 7 9 3 1 4 33
binary tree: worst-case height 1 2 3 4 5 6 7 34 n -node BST: worst-case height/depth n − 1
binary tree: best-case height 4 2 1 3 6 5 7 35 height h : at most 2 h +1 − 1 nodes
binary tree: proof best-case height is possible create a new tree as follows: create a new root node add edges from the root node to the roots of the copies the number of nodes is 36 proof by induction : can have 2 h +1 − 1 nodes in h -height tree h = 0 : h = 0 : exactly one node; 2 h +1 − 1 = 1 nodes h = k → h = k + 1 : start with two copies of a maximum tree of height k the height of this new tree is k + 1 path of length k in old tree + either new edge 2(2 k +1 − 1) + 1 = 2 k +1+1 − 2 + 1 = 2 k +1+1 − 1
binary tree: best-case height is best (informally) property of trees in root: except for the leaves, every node in tree has 2 children no way to add nodes without increasing height add below leaf — longer path to root — longer height add above root — every old node has longer path to root 37
38 binary tree height formula n : number of nodes h : height n + 1 ≤ 2 h +1 2 h +1 � � log 2 ( n + 1) ≤ log 2 log( n + 1) ≤ h + 1 h ≥ log 2 ( n + 1) − 1 shortest tree of n nodes: ∼ log 2 ( n ) height
perfect binary trees 4 2 1 3 6 5 7 a binary tree is perfect if all leaves have same depth all nodes have zero children (leaf) or two children 39 exactly the trees that achieve 2 h +1 − 1 nodes
AVL animation tool http://webdiis.unizar.es/asignaturas/EDA/ AVLTree/avltree.html 40
AVL tree idea AVL trees: one of many balanced trees — avoid “tree is just a long linked list” scenarios AVL = Adelson-Velskii and Landis 41 search tree balanced to keep height Θ(log n ) gaurentees Θ(log n ) for fjnd, insert, remove
AVL gaurentee the height of the left and right subtrees of every node difgers by at most one 42
AVL state normal binary search tree stufg: data; and left, right, parent pointers additional AVL stufg: height of right subtree minus height of left subtree called “balance factor” -1, 0, +1 (kept up to date on insert/delete — computing on demand is too slow) 43
example AVL tree 5 4 1 9 7 8 11 44
example AVL tree 5 b: +1 4 b: -1 1 b: 0 9 b: -1 7 b: +1 8 b: 0 11 b: 0 44
example non-AVL tree b: 0 b: 0 11 b: 0 7 b: 0 8 2 5 b: -1 3 b: +2 1 b: -3 4 b: -2 45
AVL tree algorithms fjnd — exactly the same as binary search tree just ignore balance factors insert — two extra steps: update balance factors “fjx” tree if it became unbalanced runtime for both where is depth of node found/inserted max balance factor at root max depth of node is 46
AVL tree algorithms fjnd — exactly the same as binary search tree just ignore balance factors insert — two extra steps: update balance factors “fjx” tree if it became unbalanced 46 runtime for both Θ( d ) where d is depth of node found/inserted max balance factor ± 1 at root max depth of node is Θ(log 2 n + 1) = Θ(log n )
AVL insertion cases simple case: tree remains balanced otherwise: 47 let x be deepest imbalanced node (+2/-2 balance factor) insert in left subtree of left child of x : single rotation right insert in right subtree of right child of x : single rotation left insert in right subtree of left child of x : double left-right rotation insert in left subtree of right child of x : double right-left rotation
AVL: simple right rotation just inserted 0 unbalanced root becomes new left child 3 b: -2 2 b: -1 1 b: 0 2 b: 0 1 b: 0 3 b: 0 48
AVL: less simple right rotation (1) 3 b: 0 10 b: 0 4 b: 0 5 b: 0 1 b: -1 2 b: 0 b: 0 just inserted 0 10 b: 0 4 b: 0 1 b: -1 2 b: -1 3 b: -2 5 unbalanced root becomes new left child 49
AVL: simple left rotation just inserted 1 deepest unbalanced node is 3 1 b: +2 2 b: +1 3 b: 0 2 b: 0 1 b: 0 3 b: 0 50
AVL rotation: up and down at least one node moves up (this case: 1 and 2) at least one node moves down (this case: 3) 3 b: -2 2 b: -1 1 b: 0 2 b: 0 1 b: 0 3 b: 0 51
AVL: less simple right rotation (2) b: 0 b: 0 2 b: -1 1 b: 0 5 b: 0 4 10 b: -1 b: 0 20 b: 0 17 b: 0 21 b: 0 deepest unbalanced subtree 3 15 just inserted 1 1 15 b: -2 5 b: -2 3 b: -1 2 b: -1 b: 0 b: 0 4 b: 0 10 b: 0 20 b: 0 17 b: 0 21 52
AVL: less simple right rotation (2) b: 0 b: 0 2 b: -1 1 b: 0 5 b: 0 4 10 b: -1 b: 0 20 b: 0 17 b: 0 21 b: 0 deepest unbalanced subtree 3 15 just inserted 1 1 15 b: -2 5 b: -2 3 b: -1 2 b: -1 b: 0 b: 0 4 b: 0 10 b: 0 20 b: 0 17 b: 0 21 52
AVL: less simple right rotation (2) b: 0 b: 0 2 b: -1 1 b: 0 5 b: 0 4 10 b: -1 b: 0 20 b: 0 17 b: 0 21 b: 0 deepest unbalanced subtree 3 15 just inserted 1 1 15 b: -2 5 b: -2 3 b: -1 2 b: -1 b: 0 b: 0 4 b: 0 10 b: 0 20 b: 0 17 b: 0 21 52
AVL: less simple right rotation (2) b: 0 b: 0 2 b: -1 1 b: 0 5 b: 0 4 10 b: -1 b: 0 20 b: 0 17 b: 0 21 b: 0 deepest unbalanced subtree 3 15 just inserted 1 1 15 b: -2 5 b: -2 3 b: -1 2 b: -1 b: 0 b: 0 4 b: 0 10 b: 0 20 b: 0 17 b: 0 21 52
general single rotation a left rotate right rotate (h) Z (h) Y (h+1) a X b (h) Z (h) Y (h+1) X b 53 X < b < Y < a < Z
step 2: rotate imbalanced tree right double rotation 8 8 b: 1 10 b: 0 17 b: -1 16 b: 0 step 1: rotate subtree left b: 8 5 6 b: -1 4 b: -1 3 b: 0 5 b: 0 10 b: 0 b: 0 b: 0 15 b: 0 b: -2 8 b: -2 4 b: 1 3 b: 0 6 b: -1 5 10 3 b: 0 17 b: -1 16 b: 0 15 b: -1 6 b: 0 4 b: 0 54
double rotation 8 b: 0 8 b: 1 10 b: 0 17 b: -1 16 b: 0 step 1: rotate subtree left b: 8 15 6 b: -1 4 b: -1 3 b: 0 5 b: 0 10 b: 0 5 b: 0 3 b: 0 b: -2 8 b: -2 4 b: 1 3 b: 0 6 b: -1 5 10 b: 0 b: 0 17 b: -1 16 b: 0 15 b: -1 6 b: 0 4 54 step 2: rotate imbalanced tree right
double rotation 8 b: 0 8 b: 1 10 b: 0 17 b: -1 16 b: 0 step 1: rotate subtree left b: 8 15 6 b: -1 4 b: -1 3 b: 0 5 b: 0 10 b: 0 5 b: 0 3 b: 0 b: -2 8 b: -2 4 b: 1 3 b: 0 6 b: -1 5 10 b: 0 b: 0 17 b: -1 16 b: 0 15 b: -1 6 b: 0 4 54 step 2: rotate imbalanced tree right
double rotation 8 8 b: 1 10 b: 0 17 b: -1 16 b: 0 step 1: rotate subtree left step 2: rotate imbalanced tree right b: 8 5 6 b: -1 4 b: -1 3 b: 0 5 b: 0 10 b: 0 b: 0 b: 0 15 b: 0 b: -2 8 b: -2 4 b: 1 3 b: 0 6 b: -1 5 10 3 b: 0 17 b: -1 16 b: 0 15 b: -1 6 b: 0 4 b: 0 54
general double rotation (h) both switch parents and becomes root, so its children rotate (h) Z (h-1) Y a (h) X W a b c (h) Z (h-1) Y (h) X c (h) W b 55 W < b < X < c < Y < Z
general double rotation b rotate (h) Z (h-1) Y a (h) X (h) a W c (h) Z (h-1) Y (h) X c (h) W b 55 W < b < X < c < Y < Z c becomes root, so its children X and Y both switch parents
double rotation names sometimes “double left” fjrst rotation left, or second? us: “double left-right” rotate child tree left rotate parent tree right “double right-left” rotate child tree right rotate parent tree left 56
AVL insertion cases simple case: tree remains balanced otherwise: 57 let x be deepest imbalanced node (+2/-2 balance factor) insert in left subtree of left child of x : single rotation right insert in right subtree of right child of x : single rotation left insert in right subtree of left child of x : double left-right rotation insert in left subtree of right child of x : double right-left rotation
choose rotation based on lowest imbalanced node AVL insert cases (revisited) 2 (inserted node is green+dashed) and on direction of insertion right-left left-right single right single left b: 0 2 b: -1 3 b: +2 1 b: 0 b: +1 3 1 b: -2 3 b: 0 3 b: +1 2 b: +2 1 b: 0 1 b: -1 2 b: -2 58
AVL insert cases (revisited) 2 (inserted node is green+dashed) and on direction of insertion right-left left-right single right single left b: 0 2 b: -1 3 b: +2 1 b: 0 b: +1 3 1 b: -2 2 b: -1 1 b: 0 b: +2 1 2 b: +1 3 b: 0 3 b: -2 58 choose rotation based on lowest imbalanced node
AVL insert case: detail (1) b: -1 (inserted node is green+dashed) and on direction of insertion lowest imbalanced node choose rotation based on b: 0 9 b: 0 1 2 3 b: -2 3 b: -2 7 b: 0 1 b: -1 2 b: -2 59
AVL insert case: detail (2) b: 0 green+dashed) (inserted node is and on direction of insertion lowest imbalanced node choose using b: 0 8 b: 0 6 b: +1 5 b: 0 3 0 3 b: -1 1 b: -1 2 b: -1 4 b: -2 7 b: 0 0 b: -1 2 b: -2 60
61
AVL tree: runtime worst case: traverse from root to worst depth leaf worst case: traverse from root to worst depth leaf then back up (update balance factors) then perform constant time rotation left as exercise (similar to insert) 62 worst depth of node: Θ(log 2 n + 2) = Θ(log n ) fjnd: Θ(log n ) insert: Θ(log n ) remove: Θ(log n ) print: Θ( n ) visit each of n nodes
other types of trees many kinds of balanced trees not all binary trees difgerent ways of tracking balance factors, etc. difgerent ways of doing tree rotations or equivalent 63
red-black trees n n NULL n NULL 25 22 NULL 17 n NULL 27 n NULL n NULL 15 NULL each node is red or black NULL null leafs considered nodes to aid analysis (still null pointers…) rules about when nodes can be red/black gaurentee maximum depth 13 8 1 n 6 n n NULL n NULL 11 n NULL 64
red-black tree rules root is black counting null pointers as nodes, leaves are black every simple path from node to leaf under it contains same number of black nodes (property holds regardless of whether null pointers are considered nodes) 65 a red node’s children are black → a red node’s parents are black
worst red-black tree imbalance J O N G M L I K H same number of black nodes on paths to leaves F C E D B A 66 → factor of 2 imbalance max
red-black insert default: insert as red (no change to black node count), but… (1) if new node is root: color black (3) if parent and uncle is red : adjust several colors perform a rotation, then go to case 5 (5) if parent is red , uncle is black , new node is left child perform a rotation property: “root is black” no children no worries about # black nodes on difgerent paths property: “children of red node are black ” no change in # of black nodes on paths 67 (2) if parent is black: keep child red (4) if parent is red , uncle is black , new node is right child
red-black insert default: insert as red (no change to black node count), but… (1) if new node is root: color black (2) if parent is black: keep child red (3) if parent and uncle is red : adjust several colors perform a rotation, then go to case 5 (5) if parent is red , uncle is black , new node is left child perform a rotation property: “root is black” no children no worries about # black nodes on difgerent paths property: “children of red node are black ” no change in # of black nodes on paths 68 (4) if parent is red , uncle is black , new node is right child
red-black insert default: insert as red (no change to black node count), but… (1) if new node is root: color black (3) if parent and uncle is red : adjust several colors perform a rotation, then go to case 5 (5) if parent is red , uncle is black , new node is left child perform a rotation property: “root is black” no children no worries about # black nodes on difgerent paths property: “children of red node are black ” no change in # of black nodes on paths 69 (2) if parent is black: keep child red (4) if parent is red , uncle is black , new node is right child
case 3: parent, uncle are red make grandparent red , parent and uncle black image: Wikipedia/Abloomfj solution: recurse to the grandparent, as if it was just inserted (property: children of red node are black) but…what if grandparent’s parent is red? just swapped grandparent and parent/uncle in those paths (property: every path to leaf has same number of black nodes) 70 G G P U P U N N 3 4 5 3 4 5 1 2 1 2
case 3: parent, uncle are red make grandparent red , parent and uncle black image: Wikipedia/Abloomfj solution: recurse to the grandparent, as if it was just inserted (property: children of red node are black) but…what if grandparent’s parent is red? just swapped grandparent and parent/uncle in those paths (property: every path to leaf has same number of black nodes) 70 G G P U P U N N 3 4 5 3 4 5 1 2 1 2
case 3: parent, uncle are red make grandparent red , parent and uncle black image: Wikipedia/Abloomfj (property: children of red node are black) but…what if grandparent’s parent is red? just swapped grandparent and parent/uncle in those paths (property: every path to leaf has same number of black nodes) 70 G G P U P U N N 3 4 5 3 4 5 1 2 1 2 solution: recurse to the grandparent, as if it was just inserted
red-black insert default: insert as red (no change to black node count), but… (1) if new node is root: color black (3) if parent and uncle is red : adjust several colors (4) if parent is red , uncle is black , new node is right child perform a rotation, then go to case 5 (5) if parent is red , uncle is black , new node is left child perform a rotation property: “root is black” no children no worries about # black nodes on difgerent paths property: “children of red node are black ” no change in # of black nodes on paths 71 (2) if parent is black: keep child red
case 4: parent red, uncle black, right child perform left rotation on parent subtree and new node image: Wikipedia/Abloomfj 72 G G P U N U N P 4 5 4 5 1 3 2 3 1 2 now case 5 (but new node is P , not N )
red-black insert default: insert as red (no change to black node count), but… (1) if new node is root: color black (3) if parent and uncle is red : adjust several colors perform a rotation, then go to case 5 (5) if parent is red , uncle is black , new node is left child perform a rotation property: “root is black” no children no worries about # black nodes on difgerent paths property: “children of red node are black ” no change in # of black nodes on paths 73 (2) if parent is black: keep child red (4) if parent is red , uncle is black , new node is right child
case 5: parent red, uncle black, left child perform right rotation of grandparent and parent image: Wikipedia/Abloomfj every path to leaf has same number of black nodes red parent’s children are black preserves properties: swap colors of parent and grandparent 74 G P P U N G N U 4 5 3 1 2 3 4 5 1 2
recusively examine grandparent 3 grandparent becomes red example recursive case 100 8 before: case 3: parent, uncle are red: parent/uncle black case 3 (parent, uncle are red) continued: case 5: parent (of 3) is red uncle is black, left child 10 1 3 … … 15 200 case 5: parent is red uncle is black, left child: perform right rotation of parent + grandparent (of 3) (and swap parent/grandparent colors) 5 3 100 5 10 3 1 5 8 15 200 10 3 1 8 case 3: parent, uncle are red 100 15 200 initially: same number of black nodes insert 8 initially make red 75 leaves are black � red node’s children are black � in every path from node to leaves �
recusively examine grandparent 3 grandparent becomes red example recursive case 100 8 before: case 3: parent, uncle are red: parent/uncle black case 3 (parent, uncle are red) continued: case 5: parent (of 3) is red uncle is black, left child 10 1 3 … … 15 200 case 5: parent is red uncle is black, left child: perform right rotation of parent + grandparent (of 3) (and swap parent/grandparent colors) 5 3 100 5 10 3 1 5 8 15 200 10 3 1 8 case 3: parent, uncle are red 100 15 200 initially: leaves are black red node’s children are black same number of black nodes in every path from node to leaves insert 8 initially make red 75
recusively examine grandparent 3 example recursive case 100 5 8 before: case 3: parent, uncle are red: parent/uncle black case 3 (parent, uncle are red) continued: case 5: parent (of 3) is red uncle is black, left child 10 100 3 … … 15 200 case 5: parent is red uncle is black, left child: perform right rotation of parent + grandparent (of 3) (and swap parent/grandparent colors) 1 3 case 3: parent, uncle are red 5 10 3 1 5 8 15 200 10 3 1 8 initially make red 100 15 200 initially: leaves are black red node’s children are black same number of black nodes in every path from node to leaves insert 8 75 grandparent becomes red
grandparent becomes red example recursive case 100 5 8 before: case 3: parent, uncle are red: parent/uncle black case 3 (parent, uncle are red) continued: case 5: parent (of 3) is red uncle is black, left child 10 100 3 … … 15 200 case 5: parent is red uncle is black, left child: perform right rotation of parent + grandparent (of 3) (and swap parent/grandparent colors) 1 3 case 3: parent, uncle are red 5 10 3 1 5 8 15 200 10 3 1 8 initially make red 100 15 200 initially: leaves are black red node’s children are black same number of black nodes in every path from node to leaves insert 8 75 recusively examine grandparent 3
Recommend
More recommend