|
1
|
- One of the tree applications in Chapter 9 is binary search trees.
- 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.
|
|
2
|
- 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
|
- 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.
|
|
4
|
- 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.
|
|
5
|
- The insertion method for a dictionary has two parameters.
|
|
6
|
- When you want to retrieve an item, you specify the key...
|
|
7
|
|
|
8
|
- We"ll look at how a binary tree can be used as the internal storage
mechanism for the dictionary.
|
|
9
|
- The data in the dictionary will be stored in a binary tree, with each
node containing an item and a key.
|
|
10
|
- Storage rules:
- Every key to the left of a node is alphabetically before the key of the
node.
|
|
11
|
- Storage rules:
- Every key to the left of a node is alphabetically before the key of the
node.
|
|
12
|
- 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.
|
|
13
|
- 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.
|
|
14
|
- 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.
|
|
15
|
- 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
|
- 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
|
- 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
|
- 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
|
- 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.
|
|
20
|
- 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.
|
|
21
|
- 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.
|
|
22
|
- 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.
|
|
23
|
- 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.
|
|
24
|
- 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.
|
|
25
|
- 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.
|
|
26
|
|
|
27
|
|
|
28
|
- Find the item.
- If necessary, swap the item with one that is easier to remove.
- Remove the item.
|
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
- If necessary, do some rearranging.
|
|
33
|
- If necessary, do some rearranging.
|
|
34
|
- If necessary, do some rearranging.
|
|
35
|
- If necessary, do some rearranging.
|
|
36
|
- If necessary, do some rearranging.
|
|
37
|
|
|
38
|
|
|
39
|
- 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.
|
|
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?
|
|
41
|
|