Date post: | 04-Jan-2016 |
Category: |
Documents |
Upload: | lee-haynes |
View: | 214 times |
Download: | 1 times |
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
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.
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.
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
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
6
The Dictionary Data TypeThe Dictionary Data Type
When you want to retrieve an item, you specify the key...
public Object retrieve("Washington")
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.
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.
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
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.
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"
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
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
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
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.
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.
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.
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.
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
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
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
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
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
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
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
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
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
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
29
Arizona
Arkansas
Removing "Florida"Removing "Florida"
Find the item.
Washington
OklahomaColoradoFlorida
Wes
tV
irg
inia
Mass.
New
Ham
psh
ireIowa
Kazakhstan
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...
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.
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.
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...
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.
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.
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.
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?
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
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.
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