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

β–Ά
binary search trees
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Binary search trees

Traversals (continued) Dictionary ADT Binary search tree

February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1

slide-2
SLIDE 2

Announcements

  • Final exam: Monday, Apr.20, 15:30 – 18:00

– Location(s) TBA

February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2

slide-3
SLIDE 3

Traversal

  • 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

Cost

void preOrder(Node* nd) { if (nd != NULL) { cout << nd->data << " "; preOrder(nd->left); preOrder(nd->right); } } void inOrder(Node* nd) { if (nd != NULL) { inOrder(nd->left); cout << nd->data << " "; inOrder(nd->right); } } And post-order...

slide-4
SLIDE 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

February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 4

π‘œπ‘€ + π‘œπ‘† + 1 = π‘œ

  • Proof (by strong induction):

𝐹 0 = 1 empty tree has single empty node 𝐹 π‘œ = 𝐹 π‘œπ‘€ + 𝐹 π‘œπ‘† = π‘œπ‘€ + 1 + π‘œπ‘† + 1 by I.H. = π‘œπ‘€ + π‘œπ‘† + 1 + 1 = π‘œ + 1

slide-5
SLIDE 5

Traversal

  • Proof (by strong induction):

π‘ˆ 0 ≀ 𝑐 π‘ˆ π‘œ ≀ 𝑑 + π‘ˆ π‘œπ‘€ + π‘ˆ π‘œπ‘† π‘ˆ π‘œ ≀ 𝑑 + 𝑑 βˆ™ π‘œπ‘€ + 𝑐 π‘œπ‘€ + 1 + 𝑑 βˆ™ π‘œπ‘† + 𝑐 π‘œπ‘† + 1 by I.H. π‘ˆ π‘œ ≀ 𝑑 1 + π‘œπ‘€ + π‘œπ‘† + 𝑐 π‘œπ‘€ + π‘œπ‘† + 1 + 1 π‘ˆ π‘œ ≀ 𝑑 βˆ™ π‘œ + 𝑐 π‘œ + 1 ∈ 𝑃 π‘œ

February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 5

Cost

void preOrder(Node* nd) { if (nd != NULL) { cout << nd->data << " "; preOrder(nd->left); preOrder(nd->right); } }

– A tree with π‘œ nodes has π‘œ + 1 empty nodes – Pay 𝑑 at each of the π‘œ real nodes – Pay 𝑐 at each of the π‘œ + 1 empty nodes – Cost: π‘ˆ π‘œ ≀ 𝑑 βˆ™ π‘œ + 𝑐 π‘œ + 1

slide-6
SLIDE 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

slide-7
SLIDE 7

Binary search tree

Dictionary ADT Binary search tree

February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 7

slide-8
SLIDE 8

Motivation for an efficient lookup

  • Stores values associated with user-specified keys

– Values may be any (homogenous) type – Keys may be any (homogenous) comparable type

  • Dictionary operations

– Create – Destroy – Insert – Find – Remove

February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 8

Dictionary ADT

  • Super 9 LC
  • Smell like a lawnmower
  • Z125 Pro
  • Fun in the sun!
  • CB300F
  • For the mild-mannered

commuter

Insert

  • Feet
  • Useful for something,

presumably

Find(Z125 Pro)

  • Z125 Pro
  • Fun in the sun!
slide-9
SLIDE 9

Data structures for Dictionary ADT

Search Insert Remove

  • Linked list
  • Unsorted array
  • Sorted array
  • Ordered linked list

February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 9

NaΓ―ve implementations, complexity

slide-10
SLIDE 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

Cinda Heeren / Andy Roth / Geoffrey Tien 10 February 12, 2020

slide-11
SLIDE 11

BST example

Cinda Heeren / Andy Roth / Geoffrey Tien 11

17 13 27 9 16 20 39 11

February 12, 2020

slide-12
SLIDE 12

BST inOrder traversal

Cinda Heeren / Andy Roth / Geoffrey Tien 12

visit(nd);

5 3 1 2 4 7 6 8

inOrder(nd->leftchild); inOrder(nd->rightchild);

visit inOrder(left) inOrder(right)

17 13 9 11 16 27 20 39

visit inOrder(left) inOrder(right) visit inOrder(left) inOrder(right) visit inOrder(left) inOrder(right) visit inOrder(left) inOrder(right) visit inOrder(left) inOrder(right) visit inOrder(left) inOrder(right)

inOrder traversal on a BST retrieves data in sorted order

February 12, 2020

slide-13
SLIDE 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

Cinda Heeren / Andy Roth / Geoffrey Tien 13 February 12, 2020

slide-14
SLIDE 14

BST search example

Cinda Heeren / Andy Roth / Geoffrey Tien 14

17 27 search(27);

February 12, 2020

slide-15
SLIDE 15

BST search example

Cinda Heeren / Andy Roth / Geoffrey Tien 15

17 13 16 search(16);

February 12, 2020

slide-16
SLIDE 16

BST search example

Cinda Heeren / Andy Roth / Geoffrey Tien 16

17 13 9 11 search(12);

February 12, 2020

slide-17
SLIDE 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)

Cinda Heeren / Andy Roth / Geoffrey Tien 17 February 12, 2020

slide-18
SLIDE 18

BST insertion example

Cinda Heeren / Andy Roth / Geoffrey Tien 18

47 32 63 19 10 23 41 37 44 54 53 59 79 96 7 12 30 43 57 91 97 Insert 43 Create new node 43 Find position Link node

February 12, 2020

slide-19
SLIDE 19

BST insertion example

Cinda Heeren / Andy Roth / Geoffrey Tien 19

Create new BST Insert 3 Insert 15 Insert 21 Insert 23 Insert 37 3 15 21 23 37 Search 45 How many operations for Search? Complexity?

February 12, 2020

slide-20
SLIDE 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

February 12, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 20

43 18 68 12 7 9 33 52 56 67 27 39 50 21

slide-21
SLIDE 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