Data Structures in Java Lecture 9: Binary Search Trees. 10/7/2015 - - PowerPoint PPT Presentation

data structures in java
SMART_READER_LITE
LIVE PREVIEW

Data Structures in Java Lecture 9: Binary Search Trees. 10/7/2015 - - PowerPoint PPT Presentation

Data Structures in Java Lecture 9: Binary Search Trees. 10/7/2015 Daniel Bauer 1 Contents 1. Binary Search Trees 2. Implementing Maps with BSTs Map ADT A map is collection of (key, value) pairs. Keys are unique, values need not be.


slide-1
SLIDE 1

Data Structures in Java

Lecture 9: Binary Search Trees.

10/7/2015

1

Daniel Bauer

slide-2
SLIDE 2

Contents

  • 1. Binary Search Trees
  • 2. Implementing Maps with BSTs
slide-3
SLIDE 3

Map ADT

  • A map is collection of (key, value) pairs.
  • Keys are unique, values need not be.
  • Two operations:
  • get(key) returns the value associated with this key
  • put(key, value) (overwrites existing keys)

key1 key2 key3 key4 value1 value2 value3 How do we implement map operations efficiently?

slide-4
SLIDE 4

Binary Search Tree Property

  • Goal: Reduce finding an item to O(log N)
  • For every node n with value x

1 4 8 6 2 3

  • the value of all nodes in

the left subtree of n are smaller than x.

  • The value of all nodes

in the right subtree of n are larger than x.

slide-5
SLIDE 5

Binary Search Tree Property

  • Goal: Reduce finding an item to O(log N)
  • For every node n with value x

1 4 8 6 2 3 7

This is not a search tree

  • the value of all nodes in

the left subtree of n are smaller than x.

  • The value of all nodes

in the right subtree of n are larger than x.

slide-6
SLIDE 6

Binary Search Tree (BST) ADT

  • A Binary Search Tree T 


consists of

  • A root node r with value ritem
  • At most two non-empty subtrees Tl and Tr, connected

by a directed edge from r.

  • Tl and Tr satisfy the BST property:
  • For all nodes s in Tl, sitem < ritem.
  • For all nodes t in Tl, titem > ritem. 

  • No value appears more than once in the BST.

r

Tl Tr

slide-7
SLIDE 7

BST operations

  • insert(x) - add value x to T.
  • contains(x) - check if value x is in T.
  • findMin() - find smallest value in T.
  • findMax() -find largest value in T.
  • remove(x) -remove an item from T.
slide-8
SLIDE 8

BST operations: contains

1 4 8 6 2 3

private ¡boolean ¡contains( ¡Integer ¡x, ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.left ¡); ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.right ¡); ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡// ¡Match ¡ }

slide-9
SLIDE 9

BST operations: contains

1 4 8 6 2 3

private ¡boolean ¡contains( ¡Integer ¡x, ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.left ¡); ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.right ¡); ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡// ¡Match ¡ }

contains(3)

6

slide-10
SLIDE 10

BST operations: contains

1 4 8 6 2 3

private ¡boolean ¡contains( ¡Integer ¡x, ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.left ¡); ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.right ¡); ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡// ¡Match ¡ }

contains(3)

2

slide-11
SLIDE 11

BST operations: contains

1 4 8 6 2 3

private ¡boolean ¡contains( ¡Integer ¡x, ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.left ¡); ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.right ¡); ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡// ¡Match ¡ }

contains(3)

4

slide-12
SLIDE 12

BST operations: contains

1 4 8 6 2 3

private ¡boolean ¡contains( ¡Integer ¡x, ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.left ¡); ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.right ¡); ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡// ¡Match ¡ }

contains(3)

3

slide-13
SLIDE 13

BST operations: findMin

1 4 8 6 2 3

private ¡BinaryNode ¡findMin( ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡null; ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.left ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡t; ¡ ¡ ¡ ¡ ¡return ¡findMin( ¡t.left ¡); ¡ }

findMax is equivalent.

slide-14
SLIDE 14

BST operations: findMin

1 4 8 6 2 3

private ¡BinaryNode ¡findMin( ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡null; ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.left ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡t; ¡ ¡ ¡ ¡ ¡return ¡findMin( ¡t.left ¡); ¡ }

findMin()

6

findMax is equivalent.

slide-15
SLIDE 15

BST operations: findMin

1 4 8 6 2 3

private ¡BinaryNode ¡findMin( ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡null; ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.left ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡t; ¡ ¡ ¡ ¡ ¡return ¡findMin( ¡t.left ¡); ¡ }

findMin()

2

findMax is equivalent.

slide-16
SLIDE 16

BST operations: findMin

1 4 8 6 2 3

private ¡BinaryNode ¡findMin( ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡null; ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.left ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡t; ¡ ¡ ¡ ¡ ¡return ¡findMin( ¡t.left ¡); ¡ }

findMin()

1

findMax is equivalent.

slide-17
SLIDE 17

BST operations: insert

  • Follow same steps as contains(X)
  • if X is found, do nothing.
  • Otherwise, contains stopped at node n. 


Insert a new node for X as a left or right child of n.

Maintains the BST property.

1 4 8 6 2 3

private ¡BinaryNode ¡insert( ¡Integer ¡x, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryNode ¡t ¡){ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡new ¡BinaryNode( ¡x, ¡null, ¡null ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡ ¡ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.left ¡= ¡insert( ¡x, ¡t.left ¡); ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.right ¡= ¡insert( ¡x, ¡t.right ¡); ¡ ¡ ¡return ¡t; ¡ ¡ ¡ }

slide-18
SLIDE 18

BST operations: insert

  • Follow same steps as contains(X)
  • if X is found, do nothing.
  • Otherwise, contains stopped at node n. 


Insert a new node for X as a left or right child of n.

insert(5) Maintains the BST property.

1 4 8 6 2 3

6

private ¡BinaryNode ¡insert( ¡Integer ¡x, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryNode ¡t ¡){ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡new ¡BinaryNode( ¡x, ¡null, ¡null ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡ ¡ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.left ¡= ¡insert( ¡x, ¡t.left ¡); ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.right ¡= ¡insert( ¡x, ¡t.right ¡); ¡ ¡ ¡return ¡t; ¡ ¡ ¡ }

slide-19
SLIDE 19

BST operations: insert

  • Follow same steps as contains(X)
  • if X is found, do nothing.
  • Otherwise, contains stopped at node n. 


Insert a new node for X as a left or right child of n.

insert(5) Maintains the BST property.

1 4 8 6 2 3

2

private ¡BinaryNode ¡insert( ¡Integer ¡x, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryNode ¡t ¡){ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡new ¡BinaryNode( ¡x, ¡null, ¡null ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡ ¡ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.left ¡= ¡insert( ¡x, ¡t.left ¡); ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.right ¡= ¡insert( ¡x, ¡t.right ¡); ¡ ¡ ¡return ¡t; ¡ ¡ ¡ }

slide-20
SLIDE 20

BST operations: insert

  • Follow same steps as contains(X)
  • if X is found, do nothing.
  • Otherwise, contains stopped at node n. 


Insert a new node for X as a left or right child of n.

insert(5) Maintains the BST property.

1 4 8 6 2 3

4

private ¡BinaryNode ¡insert( ¡Integer ¡x, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryNode ¡t ¡){ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡new ¡BinaryNode( ¡x, ¡null, ¡null ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡ ¡ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.left ¡= ¡insert( ¡x, ¡t.left ¡); ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.right ¡= ¡insert( ¡x, ¡t.right ¡); ¡ ¡ ¡return ¡t; ¡ ¡ ¡ }

slide-21
SLIDE 21

BST operations: insert

  • Follow same steps as contains(X)
  • if X is found, do nothing.
  • Otherwise, contains stopped at node n. 


Insert a new node for X as a left or right child of n.

insert(5) Maintains the BST property.

1 4 8 6 2 3

5

private ¡BinaryNode ¡insert( ¡Integer ¡x, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryNode ¡t ¡){ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡new ¡BinaryNode( ¡x, ¡null, ¡null ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡ ¡ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.left ¡= ¡insert( ¡x, ¡t.left ¡); ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.right ¡= ¡insert( ¡x, ¡t.right ¡); ¡ ¡ ¡return ¡t; ¡ ¡ ¡ }

slide-22
SLIDE 22

BST operations: remove

  • First find x following the same steps as contains(X).
  • If x is found in a node s:

1 4 8 6 2 3

  • if s is a leaf, just remove it.
  • if s has a single child t, attach t to the 


parent of s, in place of s. Maintains the BST property.

slide-23
SLIDE 23

BST operations: remove

  • First find x following the same steps as contains(X).
  • If x is found in a node s:

remove(8)

1 4 8 6 2 3

6

  • if s is a leaf, just remove it.
  • if s has a single child t, attach t to the 


parent of s, in place of s. Maintains the BST property.

slide-24
SLIDE 24

BST operations: remove

  • First find x following the same steps as contains(X).
  • If x is found in a node s:

remove(8)

1 4 8 6 2 3

8

  • if s is a leaf, just remove it.
  • if s has a single child t, attach t to the 


parent of s, in place of s. Maintains the BST property.

slide-25
SLIDE 25

BST operations: remove

  • First find x following the same steps as contains(X).
  • If x is found in a node s:

remove(8)

1 4 6 2 3

  • if s is a leaf, just remove it.
  • if s has a single child t, attach t to the 


parent of s, in place of s. Maintains the BST property.

slide-26
SLIDE 26

BST operations: remove

  • First find x following the same steps as contains(X).
  • If x is found in a node s:

1 4 6 2 3

  • if s is a leaf, just remove it.
  • if s has a single child t, attach t to the 


parent of s, in place of s. Maintains the BST property. remove(4)

6

slide-27
SLIDE 27

BST operations: remove

  • First find x following the same steps as contains(X).
  • If x is found in a node s:

1 4 6 2 3

  • if s is a leaf, just remove it.
  • if s has a single child t, attach t to the 


parent of s, in place of s. Maintains the BST property. remove(4)

2

slide-28
SLIDE 28

BST operations: remove

  • First find x following the same steps as contains(X).
  • If x is found in a node s:

1 4 6 2 3

  • if s is a leaf, just remove it.
  • if s has a single child t, attach t to the 


parent of s, in place of s. Maintains the BST property. remove(4)

4

slide-29
SLIDE 29

BST operations: remove

  • First find x following the same steps as contains(X).
  • If x is found in a node s:

1 6 2 3

  • if s is a leaf, just remove it.
  • if s has a single child t, attach t to the 


parent of s, in place of s. Maintains the BST property. remove(4)

slide-30
SLIDE 30

BST operations: remove

  • First find x following the same steps as contains(X).
  • If x is found in a node s:

1 6 2 3

  • if s is a leaf, just remove it.
  • if s has a single child t, attach t to the 


parent of s, in place of s.

  • what if s has two children?

Maintains the BST property. remove(4)

slide-31
SLIDE 31

BST operations: remove

  • If x is found in a node s that has two children tleft and tright:

1 4 8 6 2 3

  • Find the smallest node u in the subtree rooted in tright.
  • replace value of s with value of u.
  • recursively remove u.

To maintain the BST property, the node to replace s needs to be

  • larger than any node in the

left subtree

  • but smaller than any node

in the right subtree.

slide-32
SLIDE 32

BST operations: remove

  • If x is found in a node s that has two children tleft and tright:

remove(2)

1 4 8 6 2 3

6

  • Find the smallest node u in the subtree rooted in tright.
  • replace value of s with value of u.
  • recursively remove u.

To maintain the BST property, the node to replace s needs to be

  • larger than any node in the

left subtree

  • but smaller than any node

in the right subtree.

slide-33
SLIDE 33

BST operations: remove

  • If x is found in a node s that has two children tleft and tright:

remove(2)

1 4 8 6 2 3

2

  • Find the smallest node u in the subtree rooted in tright.
  • replace value of s with value of u.
  • recursively remove u.

To maintain the BST property, the node to replace s needs to be

  • larger than any node in the

left subtree

  • but smaller than any node

in the right subtree.

slide-34
SLIDE 34

BST operations: remove

  • If x is found in a node s that has two children tleft and tright:

remove(2)

1 4 8 6 2 3

2

  • Find the smallest node u in the subtree rooted in tright.
  • replace value of s with value of u.
  • recursively remove u.

4

To maintain the BST property, the node to replace s needs to be

  • larger than any node in the

left subtree

  • but smaller than any node

in the right subtree.

slide-35
SLIDE 35

BST operations: remove

  • If x is found in a node s that has two children tleft and tright:

remove(2)

1 4 8 6 2 3

2

  • Find the smallest node u in the subtree rooted in tright.
  • replace value of s with value of u.
  • recursively remove u.

3

To maintain the BST property, the node to replace s needs to be

  • larger than any node in the

left subtree

  • but smaller than any node

in the right subtree.

slide-36
SLIDE 36

BST operations: remove

  • If x is found in a node s that has two children tleft and tright:

remove(2)

1 4 8 6 2

2

  • Find the smallest node u in the subtree rooted in tright.
  • replace value of s with value of u.
  • recursively remove u.

3

To maintain the BST property, the node to replace s needs to be

  • larger than any node in the

left subtree

  • but smaller than any node

in the right subtree.

slide-37
SLIDE 37

BST operations: remove

  • Why not just replace s with the root of tleft?

remove(3)

1

4 8 6

3

2

slide-38
SLIDE 38

BST operations: remove

  • Why not just replace s with the root of tleft?

remove(3)

4 8 6 2

1

slide-39
SLIDE 39

Implementing remove

private ¡BinaryNode ¡remove( ¡Integer ¡x, ¡BinaryNode ¡t ¡){ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡return ¡t; ¡// ¡Item ¡not ¡found; ¡do ¡nothing ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(x ¡< ¡t.data ¡) ¡ ¡ ¡ ¡ ¡t.left ¡= ¡remove( ¡x, ¡t.left ¡); ¡ ¡ ¡else ¡if(t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡t.right ¡= ¡remove( ¡x, ¡t.right ¡); ¡ ¡ ¡else ¡//found ¡x ¡ ¡ ¡ ¡ ¡if( ¡t.left ¡!= ¡null ¡&& ¡t.right ¡!= ¡null ¡) ¡{ ¡// ¡2 ¡children ¡ ¡ ¡ ¡ ¡ ¡ ¡t.element ¡= ¡findMin( ¡t.right ¡).element; ¡ ¡ ¡ ¡ ¡ ¡ ¡t.right ¡= ¡remove( ¡t.element, ¡t.right ¡); ¡ ¡ ¡ ¡ ¡} ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(t.left ¡!= ¡null) ¡// ¡1 ¡or ¡0 ¡children. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡t.left; ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡t.right; ¡ }

slide-40
SLIDE 40

Running Time Analysis

  • How long do the BST operations take?
  • Given a BST T, we need a single pass down the

tree to access some node s in depth(s) steps.

  • What is the best/expected/worst-case depth of a

node in any BST?

slide-41
SLIDE 41

Worst and Best Case Height

  • f a BST
  • Assume we have a BST with N nodes.

1 2 3 4

  • Worst case: T does not

branch height(T)=N

  • Best case:

height(T)=log N

1 2 3 5 4

complete binary tree.

slide-42
SLIDE 42

Random BSTs

  • Assume we have N elements. All N! permutations
  • f these elements are equally likely.
  • We insert items in the order of any permutation into

an initially empty BST. What is the average depth of a node? [1 2 3 4 ] [3 1 2 4]

1 2 3 4 2 1 3 4

[1 2 4 3 ]

1 2 4 3

[2 3 4 1]

2 1 3 4

… …

slide-43
SLIDE 43

Randomly generated BST
 N=500, average depth = 9.89

Source: Weiss, Data Structures and Algorithm Analysis in Java, 3rd Edition

Theoretical analysis of random BSTs: Average depth of a node in a random BST of N nodes is about

slide-44
SLIDE 44

What about Different Sequences of Operations?

  • The expected depth of a random BST (insertions of

a random permutation of elements) is .

  • But what happens if there are also random

deletions?

  • Deletion produces shorter right 


subtrees. remove(2)

1 4 8 6

2

3

3

slide-45
SLIDE 45

What about Different Sequences of Operations?

  • The expected depth of a random BST (insertions of

a random permutation of elements) is .

  • But what happens if there are also random

deletions?

  • Deletion produces shorter right 


subtrees. remove(2)

1 4 8 6

2 3

slide-46
SLIDE 46

Random Insertions and Deletions

  • After alternating insertion/deletion pairs,

the expected depth is

Source: Weiss, Data Structures and Algorithm Analysis in Java, 3rd Edition

N=500, randomly generated as before.
 500*500 = 250,000 random insertion/ deletion pairs

slide-47
SLIDE 47

Contents

  • 1. Binary Search Trees
  • 2. Implementing Maps with BSTs
slide-48
SLIDE 48

Map ADT

  • A map is collection of (key, value) pairs.
  • Keys are unique, values need not be.
  • Two operations:
  • get(key) returns the value associated with this key
  • put(key, value) (overwrites existing keys)

key1 key2 key3 key4 value1 value2 value3 How do we implement map operations efficiently?

slide-49
SLIDE 49
  • When keys are integers, arrays provide a

convenient way of implementing maps.

  • Time for get and put is O(1).
  • What if we don’t have integer keys?

Arrays as Maps

A 1 2 3 4 5 6 D B C

slide-50
SLIDE 50

Comparing Complex Items

  • So far, our BSTs contained Integers.
  • One Goal of BSTs: Implement efficient lookup for Map keys and

sorted Sets.

  • We can implement generic BSTs that can contain any kind of

element, including (key,value) pairs.

  • But we must be able to sort the elements, i.e. compare them using

<, >, and =. The (key, value) pair class should implement Comparable.

key1 value1

… …

key2 value2

slide-51
SLIDE 51

Example (key/value) Pair Implementation

private ¡class ¡Pair<K ¡extends ¡Comparable<K>,V> ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡implements ¡Comparable<Pair<K,?>>{ ¡ ¡ ¡ ¡ ¡public ¡K ¡key; ¡ ¡ ¡ ¡ ¡public ¡V ¡value; ¡ ¡ ¡ ¡ ¡public ¡Pair(K ¡theKey, ¡V ¡theValue) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡key ¡= ¡theKey; ¡value ¡= ¡theValue; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡@Override ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡public ¡int ¡compareTo(Pair<K,?> ¡other) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡key.compareTo(other.key); ¡ ¡ ¡ ¡ ¡} ¡ }

slide-52
SLIDE 52

Implementing Maps with BSTs

(see example code)