+ All Categories
Home > Documents > 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation...

1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation...

Date post: 04-Jan-2016
Category:
Upload: lee-haynes
View: 214 times
Download: 1 times
Share this document with a friend
40
1 In Chapter 9, binary search trees are used to implement bags and sets. This presentation illustrates how another data type called a dictionary is implemented with binary search trees. Binary Search Trees
Transcript
Page 1: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

1

In Chapter 9, binary search trees are used to implement bags and sets.

This presentation illustrates how another data type called a dictionary is implemented with binary search trees.

Binary Search TreesBinary Search Trees

Page 2: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

2

The Dictionary Data TypeThe Dictionary Data Type

A dictionary is a collection of items, similar to a bag.

But unlike a bag, each item has a string attached to it, called the item's key.

Page 3: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

3

The Dictionary Data TypeThe Dictionary Data Type

A dictionary is a collection of items, similar to a bag.

But unlike a bag, each item has a string attached to it, called the item's key.

Example: The items I am storing are records containing data about a state.

Page 4: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

4

The Dictionary Data TypeThe Dictionary Data Type

A dictionary is a collection of items, similar to a bag.

But unlike a bag, each item has a string attached to it, called the item's key.

Example: The key for each record is the name of the state. Washington

Page 5: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

5

The Dictionary Data TypeThe Dictionary Data Type

The insertion method for a dictionary has two parameters.

public void insert(The key for the new item, The new item)

Washington

Page 6: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

6

The Dictionary Data TypeThe Dictionary Data Type

When you want to retrieve an item, you specify the key...

public Object retrieve("Washington")

Page 7: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

7

public Object retrieve("Washington")

The Dictionary Data TypeThe Dictionary Data Type

When you want to retrieve an item, you specify the key... ... and the retrieval method returns the item.

Page 8: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

8

The Dictionary Data TypeThe Dictionary Data Type

We"ll look at how a binary tree can be used as the internal storage mechanism for the dictionary.

Page 9: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

9

Arizona

Arkansas

Colorado

A Binary Search Tree of StatesA Binary Search Tree of States

The data in the dictionary will be stored in a binary tree, with each node containing an item and a key.

Washington

OklahomaFlorida

Mass.

New

Ham

psh

ire

Wes

tV

irg

inia

Page 10: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

10

Colorado

Arizona

Arkansas

A Binary Search Tree of StatesA Binary Search Tree of States

Washington

OklahomaColorado

Florida

Mass.

New

Ham

psh

ire

Wes

tV

irg

inia

Storage rules: Every key to the left

of a node is alphabetically before the key of the node.

Page 11: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

11

Arizona

Colorado

Arkansas

A Binary Search Tree of StatesA Binary Search Tree of States

Storage rules: Every key to the left of a node

is alphabetically before the key of the node.

Washington

Oklahoma

Florida

Mass.

New

Ham

pshi

re

Wes

tV

irgin

ia

Example: " Massachusetts" and " New Hampshire" are alphabetically before "Oklahoma"

Page 12: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

12

Arizona

Arkansas

A Binary Search Tree of StatesA Binary Search Tree of States

Storage rules: Every key to the left of a

node is alphabetically before the key of the node.

Every key to the right of a node is alphabetically after the key of the node.

Washington

OklahomaColoradoFlorida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Page 13: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

13

Arizona

Arkansas

A Binary Search Tree of StatesA Binary Search Tree of States

Storage rules: Every key to the left of a

node is alphabetically before the key of the node.

Every key to the right of a node is alphabetically after the key of the node..

Washington

OklahomaColoradoFlorida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Page 14: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

14

Arizona

Arkansas

Retrieving DataRetrieving Data

Start at the root. If the current node has the

key, then stop and retrieve the data.

If the current node's key is too large, move left and repeat 1-3.

If the current node's key is too small, move right and repeat 1-3.

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Page 15: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

15

Arizona

Arkansas

Retrieve " New Hampshire"Retrieve " New Hampshire"

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Start at the root. If the current node has the

key, then stop and retrieve the data.

If the current node's key is too large, move left and repeat 1-3.

If the current node's key is too small, move right and repeat 1-3.

Page 16: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

16

Arizona

Arkansas

Retrieve "New Hampshire"Retrieve "New Hampshire"

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Start at the root. If the current node has

the key, then stop and retrieve the data.

If the current node's key is too large, move left and repeat 1-3.

If the current node's key is too small, move right and repeat 1-3.

Page 17: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

17

Arizona

Arkansas

Retrieve "New Hampshire"Retrieve "New Hampshire"

Washington

OklahomaColorado

Florida

Wes

tV

irg

inia

Mass.

New

Ham

psh

ire

Start at the root. If the current node

has the key, then stop and retrieve the data.

If the current node's key is too large, move left and repeat 1-3.

If the current node's key is too small, move right and repeat 1-3.

Page 18: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

18

Arizona

Arkansas

Retrieve "New Hampshire"Retrieve "New Hampshire"

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Start at the root. If the current node has

the key, then stop and retrieve the data.

If the current node's key is too large, move left and repeat 1-3.

If the current node's key is too small, move right and repeat 1-3.

Page 19: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

19

Arizona

Arkansas

Adding a New Item with a Given KeyAdding a New Item with a Given Key

Pretend that you are trying to find the key, but stop when there is no node to move to.

Add the new node at the spot where you would have moved to if there had been a node.

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Page 20: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

20

Arizona

Arkansas

AddingAdding

Pretend that you are trying to find the key, but stop when there is no node to move to.

Add the new node at the spot where you would have moved to if there had been a node.

Washington

OklahomaColorado

Florid

a

Wes

tV

irg

ini

a

Mass.

New

Ham

psh

ire

Iowa

Page 21: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

21

Arizona

Arkansas

AddingAdding

Pretend that you are trying to find the key, but stop when there is no node to move to.

Add the new node at the spot where you would have moved to if there had been a node.

Washington

OklahomaColorado

Florida

Wes

tV

irg

inia

Mass.

New

Ham

psh

ire

Iowa

Page 22: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

22

Arizona

Arkansas

AddingAdding

Pretend that you are trying to find the key, but stop when there is no node to move to.

Add the new node at the spot where you would have moved to if there had been a node.

Washington

OklahomaColoradoFlorida

Wes

tV

irg

inia

Mass.

New

Ham

psh

ire

Iowa

Page 23: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

23

Arizona

Arkansas

AddingAdding

Pretend that you are trying to find the key, but stop when there is no node to move to.

Add the new node at the spot where you would have moved to if there had been a node.

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Iowa

Page 24: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

24

Arizona

Arkansas

AddingAdding

Pretend that you are trying to find the key, but stop when there is no node to move to

. Add the new node at

the spot where you would have moved to if there had been a node.

Washington

OklahomaColorado

Florida

Wes

tV

irg

inia

Mass.

New

Ham

psh

ire

Iowa

Page 25: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

25

Arizona

Arkansas

AddingAdding

Pretend that you are trying to find the key, but stop when there is no node to move to.

Add the new node at the spot where you would have moved to if there had been a node.

Washington

OklahomaColoradoFlorida

Wes

tV

irg

inia

Mass.

New

Ham

psh

ire

Iowa

Page 26: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

26

Arizona

Arkansas

Adding Adding

Washington

OklahomaColoradoFlorida

Wes

tV

irg

inia

Mass.

New

Ham

psh

ireIowa

Where would youadd this state?

Where would youadd this state?

Kazakhstan

Page 27: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

27

Arizona

Arkansas

Adding Adding

Washington

OklahomaColoradoFlorida

Wes

tV

irg

inia

Mass.

New

Ham

psh

ireIowa

Kazakhstan is thenew right child

of Iowa?

Kazakhstan is thenew right child

of Iowa?

Kazakhstan

Page 28: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

28

Arizona

Arkansas

Removing an Item with a Given KeyRemoving an Item with a Given Key

Find the item.

If necessary, swap the item with one that is easier to remove.

Remove the item.

Washington

OklahomaColorado

Florida

Wes

tV

irg

inia

Mass.

New

Ham

psh

ireIowa

Kazakhstan

Page 29: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

29

Arizona

Arkansas

Removing "Florida"Removing "Florida"

Find the item.

Washington

OklahomaColoradoFlorida

Wes

tV

irg

inia

Mass.

New

Ham

psh

ireIowa

Kazakhstan

Page 30: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

30

Arizona

Arkansas

Removing "Florida"Removing "Florida"

Washington

OklahomaColoradoFlorida

Wes

tV

irg

inia

Mass.

New

Ham

psh

ireIowa

Kazakhstan

Florida cannot beremoved at the

moment...

Florida cannot beremoved at the

moment...

Page 31: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

31

Arizona

Arkansas

Removing "Florida"Removing "Florida"

Washington

OklahomaColorado

Wes

tV

irg

inia

Mass.

New

Ham

psh

ireIowa

Kazakhstan

... because removingFlorida would

break the tree intotwo pieces.

... because removingFlorida would

break the tree intotwo pieces.

Page 32: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

32

Arizona

Arkansas

Removing "Florida"Removing "Florida"

Washington

OklahomaColorado

Florida

Wes

tV

irg

inia

Mass.

New

Ham

psh

ireIowa

Kazakhstan

If necessary, do some rearranging.

The problem ofbreaking the treehappens because

Florida has 2 children.

The problem ofbreaking the treehappens because

Florida has 2 children.

Page 33: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

33

Arizona

Arkansas

Removing "Florida"Removing "Florida"

If necessary, do some rearranging.

Washington

OklahomaColoradoFlorida

We

stV

irg

inia

Mass.

New

Ham

psh

ire

Iowa

Kazakhstan

For the rearranging,take the smallest itemin the right subtree...

For the rearranging,take the smallest itemin the right subtree...

Page 34: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

34

Arizona

Arkansas

Removing "Florida"Removing "Florida"

Washington

OklahomaColorado

Wes

tV

irg

inia

Mass.

New

Ham

psh

ire

Iowa

Kazakhstan

Iowa...copy that smallestitem onto the item

that we"re removing...

...copy that smallestitem onto the item

that we"re removing...

If necessary, do some rearranging.

Page 35: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

35

Arizona

Arkansas

Removing "Florida"Removing "Florida"

Washington

OklahomaColorado

Wes

tV

irg

inia

Mass.

New

Ham

psh

ire

Iowa

Kazakhstan

... and then removethe extra copy of the

item we copied...

... and then removethe extra copy of the

item we copied...

If necessary, do some rearranging.

Page 36: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

36

Arizona

Arkansas

Removing "Florida"Removing "Florida"

Washington

OklahomaColorado

Wes

tV

irg

inia

Mass.

New

Ham

psh

ire

Iowa

Kazakhstan

... and reconnectthe tree

If necessary, do some rearranging.

Page 37: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

37

Arizona

Arkansas

Removing "Florida"Removing "Florida"

Washington

OklahomaColorado

Florida

Wes

tV

irg

ini

a

Mass.

New

Ham

psh

ire

Kazakhstan

Why did I choosethe smallest item

in the right subtree?

Why did I choosethe smallest item

in the right subtree?

Page 38: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

38

Arizona

Arkansas

Removing "Florida"Removing "Florida"

Washington

OklahomaColorado

Wes

tV

irg

inia

Mass.

New

Ham

psh

ire

Iowa

Kazakhstan

Because every keymust be smaller than

the keys in itsright subtree

Page 39: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

39

Removing an Item with a Given KeyRemoving an Item with a Given Key

Find the item. If the item has a right child, rearrange the tree:

Find smallest item in the right subtreeFind smallest item in the right subtree

Copy that smallest item onto the one that you Copy that smallest item onto the one that you want to removewant to remove

Remove the extra copy of the smallest item Remove the extra copy of the smallest item (making sure that you keep the tree connected)(making sure that you keep the tree connected) else just remove the item.

Page 40: 1 p p In Chapter 9, binary search trees are used to implement bags and sets. p p This presentation illustrates how another data type called a dictionary.

40

Binary search trees are a good implementation of data types such as sets, bags, and dictionaries.

Searching for an item is generally quick since you move from the root to the item, without looking at many other items.

Adding and deleting items is also quick.

But as you’ll see later, it is possible for the quickness to fail in some cases -- can you see why?

Summary Summary


Recommended