Notes
Slide Show
Outline
1
Binary Search Trees
  • 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
The 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 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.
4
The 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.
5
The Dictionary Data Type
  • The insertion method for a dictionary has two parameters.
6
The Dictionary Data Type
  • When you want to retrieve an item, you specify the key...
7
The Dictionary Data Type
8
The Dictionary Data Type
  • We"ll look at how a binary tree can be used as the internal storage mechanism for the dictionary.
9
A 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.
10
A Binary Search Tree of States
  • Storage rules:
  • Every key to the left of a node is alphabetically before the key of the node.
11
A Binary Search Tree of States
  • Storage rules:
  • Every key to the left of a node is alphabetically before the key of the node.
12
A 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.
13
A 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.
14
Retrieving 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.
15
Retrieve " New Hampshire"
  • 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
Retrieve "New Hampshire"
  • 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
Retrieve "New Hampshire"
  • 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
Retrieve "New Hampshire"
  • 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
Adding 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.
20
Adding
  • 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
Adding
  • 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
Adding
  • 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
Adding
  • 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
Adding
  • 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
Adding
  • 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
Adding
27
Adding
28
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.
29
Removing "Florida"
  • Find the item.
30
Removing "Florida"
31
Removing "Florida"
32
Removing "Florida"
  • If necessary, do some rearranging.
33
Removing "Florida"
  • If necessary, do some rearranging.
34
Removing "Florida"
  • If necessary, do some rearranging.
35
Removing "Florida"
  • If necessary, do some rearranging.
36
Removing "Florida"
  • If necessary, do some rearranging.
37
Removing "Florida"
38
Removing "Florida"
39
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.
40
   Summary
  • 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
THE  END