45
1 Trees III: Binary Search Trees

1 Trees III: Binary Search Trees. 2 A forest full of trees The generic toolkit of functions we have seen thus far can be applied to many types of data

  • View
    219

  • Download
    1

Embed Size (px)

Citation preview

1

Trees III:

Binary Search Trees

2

A forest full of trees

• The generic toolkit of functions we have seen thus far can be applied to many types of data structures derived from binary trees

• Now we will look at a specific ADT based on the generic binary tree: binary search binary search treestrees

3

BST characteristics

• Based on binary trees

• Defining quality has to do with the order in which data are stored

• Commonly used in database applications where rapid retrieval of data is desired

4

Binary Search Trees

• Entries in a BST must be objects to which total total order semanticsorder semantics apply -- in other words, objects for which all the binary comparison operators are defined

• Storage rules -- for every node n:– every entry in n’s left subtree is less than or equal

to the entry in n– every entry in n’s right subtree is greater than n’s

entry

5

Binary Search Tree

9

5 11

2 7 13

170 4

There is no specialrequirement for the treeto maintain to maintaina particular shape -- buta balancedbalanced tree (in which there are approximately the same number of nodes in each subtree) facilitates data searching

12

6

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.

7

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.

8

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

9

The Dictionary Data TypeThe Dictionary Data Type

• The insertion procedure for a dictionary has two parameters.

void Dictionary::insert(The key for the new item, The new item);

Washington

10

The Dictionary Data TypeThe Dictionary Data Type

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

Item Dictionary::retrieve("Washington");

11

Item Dictionary::retrieve("Washington");

The Dictionary Data TypeThe Dictionary Data Type

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

12

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.

13

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

Oklahoma

Florida

Mass.

New

Ham

pshi

re

Wes

tV

irgin

ia

14

Colorado

Arizona

Arkansas

A Binary Search Tree of StatesA Binary Search Tree of States

Washington

OklahomaColorado

Florida

Mass.

New

Ham

pshi

re

Wes

tV

irgin

ia

Storage rules: Every key to the left

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

15

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'

16

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

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

17

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

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

18

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

19

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.

20

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.

21

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.

22

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.

23

Arizona

Arkansas

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

Adding a New Item with aGiven Key

Adding a New Item with aGiven Key

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

irgin

ia

Mass.

New

Ham

pshi

re

Iowa

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

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Iowa

26

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

27

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

28

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

29

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

reIowa

30

Arizona

Arkansas

Adding Adding

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

reIowa

Where would youadd this state?

Where would youadd this state?

Kazakhstan

31

Arizona

Arkansas

Adding Adding

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

reIowa

Kazakhstan is thenew right child

of Iowa?

Kazakhstan is thenew right child

of Iowa?

Kazakhstan

32

Arizona

Arkansas

Removing an Item with a Given Key

Removing 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

irgin

ia

Mass.

New

Ham

pshi

reIowa

Kazakhstan

33

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Find the item.

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

reIowa

Kazakhstan

34

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

reIowa

Kazakhstan

Florida cannot beremoved at the

moment...

Florida cannot beremoved at the

moment...

35

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Washington

OklahomaColorado

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

reIowa

Kazakhstan

... because removingFlorida would

break the tree intotwo pieces.

... because removingFlorida would

break the tree intotwo pieces.

36

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

reIowa

Kazakhstan

The problem ofbreaking the treehappens because

Florida has 2 children.

The problem ofbreaking the treehappens because

Florida has 2 children.

If necessary, do some rearranging.

37

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

If necessary, do some rearranging.

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

reIowa

Kazakhstan

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

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

38

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Washington

OklahomaColorado

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Iowa

Kazakhstan

Iowa...copy that smallest

item onto the itemthat we're removing...

...copy that smallestitem onto the item

that we're removing...

If necessary, do some rearranging.

39

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Washington

OklahomaColorado

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

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.

40

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Washington

OklahomaColorado

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Iowa

Kazakhstan... and reconnect

the tree

If necessary, do some rearranging.

41

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Kazakhstan

Why did I choosethe smallest item

in the right subtree?

Why did I choosethe smallest item

in the right subtree?

42

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Washington

OklahomaColorado

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Iowa

Kazakhstan

Because every keymust be smaller than

the keys in itsright subtree

43

Removing an Item with a Given Key

Removing 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 subtree

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

– Remove the extra copy of the smallest item (making sure that you keep the tree connected)

else just remove the item.

44

• 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

45

Trees III:

Binary Search Trees

- ends -