binary search trees
play

Binary Search Trees CS16: Introduction to Data Structures & - PowerPoint PPT Presentation

Binary Search Trees CS16: Introduction to Data Structures & Algorithms Spring 2020 Outline Binary Search Trees Searching BSTs Adding to BSTs Removing from BSTs by CynthT http://cyntht.deviantart.com/ BST Analysis


  1. Binary Search Trees CS16: Introduction to Data Structures & Algorithms Spring 2020

  2. Outline ‣ Binary Search Trees ‣ Searching BSTs ‣ Adding to BSTs ‣ Removing from BSTs by CynthT http://cyntht.deviantart.com/ ‣ BST Analysis ‣ Balancing BSTs

  3. Binary Search Trees ‣ Binary trees with special property ‣ For each node ‣ left descendants have lower value than node ‣ right descendants have higher value than node ‣ In-order traversal gives nodes in order 3

  4. Searching a BST 13 11 < 13 7 20 11 > 7 5 10 15 24 11 > 10 8 11 ‣ Find 11 ‣ Each comparison tells us whether to go left or right 4

  5. Searching a BST 13 14 > 13 7 20 14 < 20 5 10 15 24 reached leaf w/o finding 8 11 it so not in tree ‣ What if item isn’t in tree? ‣ Find 14 5

  6. Binary Search Tree — Find( ) function find (node, toFind): if node.data == toFind: return node else if toFind < node.data and node.left != null: return find(node.left, toFind) else if toFind > node.data and node.right != null: return find(node.right, toFind) return null 6

  7. Inserting in a BST 13 17 > 13 7 20 17 < 20 5 10 15 24 17 > 15 17 8 11 ‣ To insert, perform a search and add as new leaf ‣ Insert 17 7

  8. Binary Search Tree — Insert( ) function insert (node, toInsert): if node.data == toInsert: # data already in tree return if toInsert < node.data: if node.left == null: # add as left child node.addLeft(toInsert) else: insert(node.left, toInsert) else: if node.right == null: # add as right child node.addRight(toInsert) else: insert(node.right, toInsert) 8

  9. Removing from a BST Can be tricky ‣ 13 Three cases to consider ‣ ‣ Removing a leaf: easy, just do it 7 20 ‣ Removing internal node w/ 1 child (e.g., 15 ) 5 10 15 24 ‣ Removing internal node w/ 2 children (e.g., 7 ) 17 8 11

  10. Removing from a BST - Case #2 Removing internal node w/ 1 child ‣ 13 Strategy ‣ ‣ “Splice out” node by connecting its 7 20 parent to its child Example: remove 15 ‣ 5 10 15 24 set parent’s left pointer to 17 ‣ remove 15’s pointer ‣ 17 8 11 no more references to 15 so erased ‣ (garbage collected) BST order is maintained ‣

  11. Removing from a BST - Case #3 Removing internal node w/ 2 children ‣ 13 Replace node w/ successor ‣ successor: next largest node ‣ 7 20 ‣ Delete successor 5 10 17 24 ‣ Successor a.k.a. the in-order successor 8 11 ‣ Example: remove 7 ‣ What is successor of 7 ? 9

  12. Removing from a BST - Case #3 Since node has 2 children… ‣ 13 …it has a right subtree ‣ Successor is leftmost node ‣ 7 20 in right subtree 5 10 17 24 ‣ 7 ’s successor is 8 successor (node): 8 11 curr = node.right while (curr.left != null): curr = curr.left 9 return curr

  13. Removing from a BST - Case #3 Now, replace node with successor ‣ 13 Observation ‣ Successor can’t have left sub-tree ‣ 7 8 20 …otherwise its left child ‣ would be successor 5 10 17 24 so successor only has right child ‣ Remove successor using ‣ 8 11 Case # 1 or # 2 Here, use case #2 (internal w/ 1 child) ‣ 9 Successor removed and BST order restored ‣

  14. Binary Search Tree — Remove( ) function remove (node): if node has no children: # case 1 node.parent.removeChild(node) else if node only has left child: # case 2a if node.parent.left == node: # node is a left child node.parent.left = node.left else: node.parent.right = node.left else if node only has right child: # case 2b if node.parent.left == node: node.parent.left = node.right else: node.parent.right = node.right else: # case 3 (node has two children) nextNode = successor(node) node.data = nextNode.data #replace w/ nextNode remove(nextNode) # nextNode has at most one child 14

  15. Successor vs. Predecessor ‣ In Remove( ) ‣ OK to remove in-order predecessor instead of in-order successor ‣ Randomly picking between the two keeps tree balanced ‣ In Case # 3 ‣ Predecessor is rightmost node of left subtree 15

  16. Binary Search Tree — Remove( ) 2 min Activity #1 16

  17. Binary Search Tree — Remove( ) 2 min Activity #1 17

  18. Binary Search Tree — Remove( ) 1 min Activity #1 18

  19. Binary Search Tree — Remove( ) 0 min Activity #1 19

  20. Binary Search Tree Analysis 13 ‣ How fast are BST operations? 7 20 ‣ Given a tree, what is the worst- case node to find/remove? 5 10 15 24 ‣ What is the best-case tree? D ‣ a balanced tree ‣ What is the worst-case tree? C ‣ a completely unbalanced tree B A

  21. Binary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations C B A A A B C C B ‣ In-order traversal of all 3 trees is Beyond CS16, But good to B A C know ‣ so BST order is preserved 21

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