Binary search trees Dictionary ADT Binary search tree properties - - PowerPoint PPT Presentation

binary search trees
SMART_READER_LITE
LIVE PREVIEW

Binary search trees Dictionary ADT Binary search tree properties - - PowerPoint PPT Presentation

Binary search trees Dictionary ADT Binary search tree properties Search Insertion February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1 By the way... Here is a reference to the "three dots" tree traversal shown in


slide-1
SLIDE 1

Binary search trees

Dictionary ADT Binary search tree properties Search Insertion

February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1

slide-2
SLIDE 2

By the way...

  • Here is a reference to the "three dots" tree traversal shown in

class:

– https://en.wikibooks.org/wiki/A- level_Computing/AQA/Paper_1/Fundamentals_of_algorithms/Tree_tra versal – Trace around the tree counter-clockwise – Visit a node when you hit one of its markers for pre-, post-, or in-order

February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 2

slide-3
SLIDE 3

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 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 3

Dictionary ADT

  • Super 9 LC
  • Park it on the sidewalk, it's OK
  • 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-4
SLIDE 4

Data structures for Dictionary ADT

Search Insert Remove

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

February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 4

Naïve implementations, complexity

slide-5
SLIDE 5

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 / Will Evans / Geoffrey Tien 5 February 04, 2019

slide-6
SLIDE 6

BST example

Cinda Heeren / Will Evans / Geoffrey Tien 6

17 13 27 9 16 20 39 11

February 04, 2019

slide-7
SLIDE 7

BST inOrder traversal

Cinda Heeren / Will Evans / Geoffrey Tien 7

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 04, 2019

slide-8
SLIDE 8

BST implementation

  • Binary search trees can be implemented using a reference

structure

  • Tree nodes contain data and two pointers to nodes

Cinda Heeren / Will Evans / Geoffrey Tien 8

data Node* rightchild Node* leftchild Data to be stored in the tree (usually an object) References or pointers to other tree Nodes

February 04, 2019

slide-9
SLIDE 9

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 / Will Evans / Geoffrey Tien 9 February 04, 2019

slide-10
SLIDE 10

BST search example

Cinda Heeren / Will Evans / Geoffrey Tien 10

17 27 search(27);

February 04, 2019

slide-11
SLIDE 11

BST search example

Cinda Heeren / Will Evans / Geoffrey Tien 11

17 13 16 search(16);

February 04, 2019

slide-12
SLIDE 12

BST search example

Cinda Heeren / Will Evans / Geoffrey Tien 12

17 13 9 11 search(12);

February 04, 2019

slide-13
SLIDE 13

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 / Will Evans / Geoffrey Tien 13 February 04, 2019

slide-14
SLIDE 14

BST insertion example

Cinda Heeren / Will Evans / Geoffrey Tien 14

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 04, 2019

slide-15
SLIDE 15

BST insertion example

Cinda Heeren / Will Evans / Geoffrey Tien 15

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 04, 2019

slide-16
SLIDE 16

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 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 16

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

slide-17
SLIDE 17

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 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 17