binary search trees
play

Binary search trees Traversals (continued) Dictionary ADT Binary - PowerPoint PPT Presentation

Binary search trees Traversals (continued) Dictionary ADT Binary search tree February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1 Announcements Final exam: Monday, Apr.20, 15:30 18:00 Location(s) TBA February 12, 2020


  1. Binary search trees Traversals (continued) Dictionary ADT Binary search tree February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1

  2. Announcements • Final exam: Monday, Apr.20, 15:30 – 18:00 – Location(s) TBA February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2

  3. Traversal Cost void preOrder(Node* nd) { void inOrder(Node* nd) { if (nd != NULL) { if (nd != NULL) { cout << nd->data << " "; inOrder(nd->left); preOrder(nd->left); cout << nd->data << " "; preOrder(nd->right); inOrder(nd->right); } } } } And post-order... • Cost? Let's try a recurrence relation: 𝑈 0 ≤ 𝑐 𝑈 𝑜 ≤ 𝑑 + 𝑈 𝑜 𝑀 + 𝑈 𝑜 𝑆 – Where 𝑜 𝑀 and 𝑜 𝑆 are the number of nodes in the left and right subtrees – But the shape/structure of the tree is not guaranteed! – Let's look at another property first February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 3

  4. How many empty trees? • Consider an arbitrary binary tree with 𝑜 nodes – How many empty (null) subtrees are there? – Each of the 𝑜 nodes has 2 children (real or empty) • 2𝑜 "children" in total • 𝑜 − 1 of these are real nodes (root is not a child) • 𝑜 + 1 empty subtrees 𝑜 𝑆 • Proof (by strong induction): 𝑜 𝑀 𝐹 0 = 1 empty tree has single empty node 𝑜 𝑀 + 𝑜 𝑆 + 1 = 𝑜 𝐹 𝑜 = 𝐹 𝑜 𝑀 + 𝐹 𝑜 𝑆 = 𝑜 𝑀 + 1 + 𝑜 𝑆 + 1 by I.H. = 𝑜 𝑀 + 𝑜 𝑆 + 1 + 1 = 𝑜 + 1 February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 4

  5. Traversal Cost – A tree with 𝑜 nodes has 𝑜 + 1 empty nodes void preOrder(Node* nd) { if (nd != NULL) { – Pay 𝑑 at each of the 𝑜 real nodes cout << nd->data << " "; – Pay 𝑐 at each of the 𝑜 + 1 empty nodes preOrder(nd->left); – Cost: 𝑈 𝑜 ≤ 𝑑 ∙ 𝑜 + 𝑐 𝑜 + 1 preOrder(nd->right); } } • Proof (by strong induction): 𝑈 0 ≤ 𝑐 𝑈 𝑜 ≤ 𝑑 + 𝑈 𝑜 𝑀 + 𝑈 𝑜 𝑆 𝑈 𝑜 ≤ 𝑑 + 𝑑 ∙ 𝑜 𝑀 + 𝑐 𝑜 𝑀 + 1 + 𝑑 ∙ 𝑜 𝑆 + 𝑐 𝑜 𝑆 + 1 by I.H. 𝑈 𝑜 ≤ 𝑑 1 + 𝑜 𝑀 + 𝑜 𝑆 + 𝑐 𝑜 𝑀 + 𝑜 𝑆 + 1 + 1 𝑈 𝑜 ≤ 𝑑 ∙ 𝑜 + 𝑐 𝑜 + 1 ∈ 𝑃 𝑜 February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 5

  6. Traversal uses • Consider the following scenarios: – Copy constructor – Destructor – Which traversal (in-order, pre-order, post-order) would be most suitable? February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 6

  7. Binary search tree Dictionary ADT Binary search tree February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 7

  8. Motivation for an efficient lookup Dictionary ADT • Stores values associated with user-specified keys – Values may be any (homogenous) type – Keys may be any (homogenous) comparable type • Dictionary operations – Create • Super 9 LC Insert – Destroy • Smell like a lawnmower • – Insert Feet • • Useful for something, Z125 Pro – Find • presumably Fun in the sun! – Remove • CB300F Find(Z125 Pro) • For the mild-mannered commuter • Z125 Pro • Fun in the sun! February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 8

  9. Data structures for Dictionary ADT Naïve implementations, complexity Search Insert Remove • Linked list • Unsorted array • Sorted array • Ordered linked list February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 9

  10. Binary search tree property • A binary search tree is a binary tree with a special property – For all nodes in the tree: • All nodes in a left subtree have labels less than the label of the subtree's root • All nodes in a right subtree have labels greater than or equal to the label of the subtree's root • Binary search trees are fully ordered February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 10

  11. BST example 17 13 27 9 16 20 39 11 February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 11

  12. BST inOrder traversal inOrder(nd->leftchild); inOrder traversal on a BST retrieves data in sorted order 5 visit(nd); inOrder(nd->rightchild); 17 3 7 inOrder(left) inOrder(left) 13 27 visit visit inOrder(right) inOrder(right) 8 4 6 1 inOrder(left) inOrder(left) inOrder(left) 16 20 39 visit 9 visit visit inOrder(right) inOrder(right) inOrder(right) inOrder(left) visit 2 inOrder(right) inOrder(left) 11 visit inOrder(right) February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 12

  13. BST search • To find a value in a BST search from the root node: – If the target is less than the value in the node search its left subtree – If the target is greater than the value in the node search its right subtree – Otherwise return true, (or a pointer to the data, or …) • How many comparisons? – One for each node on the path – Worst case: height of the tree + 1 February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 13

  14. BST search example search(27); 17 27 February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 14

  15. BST search example search(16); 17 13 16 February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 15

  16. BST search example search(12); 17 13 9 11 February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 16

  17. BST insertion • The BST property must hold after insertion • Therefore the new node must be inserted in the correct position – This position is found by performing a search – If the search ends at the (null) left child of a node make its left child refer to the new node – If the search ends at the right child of a node make its right child refer to the new node • The cost is about the same as the cost for the search algorithm, O( height ) February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 17

  18. BST insertion example Insert 43 47 Create new node Find position 32 63 Link node 19 41 54 79 43 10 23 37 44 53 59 96 7 12 30 43 57 91 97 February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 18

  19. BST insertion example Create new BST 3 Insert 3 Insert 15 15 Insert 21 Insert 23 21 Insert 37 Search 45 23 How many operations for Search? Complexity? 37 February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 19

  20. Find min, Find max • Find minimum: – From the root, keep following left child links until no more left child exists • Find maximum: – From the root, follow right child links until no more right child exists 43 18 68 12 33 52 7 27 39 50 56 9 21 67 February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 20

  21. Readings for this lesson • Carrano & Henry – Chapter 15.2 – 15.3 (Binary/search tree) – Chapter 18.1 (Dictionary) • Next class: – Chapter 15.3, 16.1 – 16.2 (Binary search tree) February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 21

Recommend


More recommend