trees
play

trees 1 are lists enough? for correctness sure want to - PowerPoint PPT Presentation

trees 1 are lists enough? for correctness sure want to effjciently access items better than linear time to fjnd something 2 want to represent relationships more naturally inter-item relationships in lists 1 2 3 4 5 List: nodes


  1. 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

  2. not a binary search tree 8 5 2 4 6 11 10 15 18 20 21 26

  3. 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

  4. 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)

  5. 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);

  6. BST: fjndMin (pseudocode) return node; else } 30 findMin(Node *node, key) { if (node − >left == NULL) insert(node − >left, key);

  7. BST: remove (1) 5 4 1 3 9 7 11 5 4 1 3 9 11 case 1: no children 31

  8. BST: remove (2) 5 4 1 3 9 7 11 5 4 3 9 7 11 case 2: one child 32

  9. 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

  10. binary tree: worst-case height 1 2 3 4 5 6 7 34 n -node BST: worst-case height/depth n − 1

  11. binary tree: best-case height 4 2 1 3 6 5 7 35 height h : at most 2 h +1 − 1 nodes

  12. 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

  13. 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

  14. 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

  15. 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

  16. AVL animation tool http://webdiis.unizar.es/asignaturas/EDA/ AVLTree/avltree.html 40

  17. 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

  18. AVL gaurentee the height of the left and right subtrees of every node difgers by at most one 42

  19. 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

  20. example AVL tree 5 4 1 9 7 8 11 44

  21. 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

  22. 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

  23. 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

  24. 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 )

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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

  42. 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

  43. 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

  44. 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

  45. 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

  46. 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

  47. 61

  48. 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

  49. 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

  50. 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

  51. 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

  52. 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

  53. 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

  54. 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

  55. 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

  56. 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

  57. 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

  58. 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

  59. 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

  60. 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 )

  61. 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

  62. 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

  63. 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 �

  64. 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

  65. 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

  66. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend