Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 Trees III: Binary Search Trees

2 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 binary search treesNow we will look at a specific ADT based on the generic binary tree: binary search trees

3 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 4 Binary Search Trees total order semanticsEntries in a BST must be objects to which total order 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 5 Binary Search Tree 9 511 27 13 17 04 There is no special requirement for the tree to maintain a particular shape -- but balanced a balanced tree (in which there are approximately the same number of nodes in each subtree) facilitates data searching 12

6 6 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.

7 7 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. Example: The items I am storing are records containing data about a state.

8 8 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. Example: The key for each record is the name of the state. Washington

9 9 The 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 10 The Dictionary Data Type When you want to retrieve an item, you specify the key... Item Dictionary::retrieve("Washington");

11 11 Item Dictionary::retrieve("Washington"); The Dictionary Data Type p When you want to retrieve an item, you specify the key...... and the retrieval procedure returns the item.

12 12 The Dictionary Data Type We'll look at how a binary tree can be used as the internal storage mechanism for the dictionary.

13 13 Arizona Arkansas Colorado 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. Washington Oklahoma Florida Mass. New Hampshire West Virginia

14 14 Colorado Arizona Arkansas A Binary Search Tree of States Washington Oklahoma Colorado Florida Mass. New Hampshire West Virginia Storage rules: ¶Every key to the left of a node is alphabetically before the key of the node.

15 15 Arizona Colorado Arkansas A 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 Hampshire West Virginia Example: ' Massachusetts' and ' New Hampshire' are alphabetically before 'Oklahoma'

16 16 Arizona Arkansas 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. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire

17 17 Arizona Arkansas 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. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire

18 18 Arizona Arkansas 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. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire

19 19 Arizona Arkansas Retrieve ' New Hampshire' Washington Oklahoma Colorado Florida West Virginia Mass. 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.

20 20 Arizona Arkansas Retrieve 'New Hampshire' Washington Oklahoma Colorado Florida West Virginia Mass. 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.

21 21 Arizona Arkansas Retrieve 'New Hampshire' Washington Oklahoma Colorado Florida West Virginia Mass. 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.

22 22 Arizona Arkansas Retrieve 'New Hampshire' Washington Oklahoma Colorado Florida West Virginia Mass. 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.

23 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 Oklahoma Colorado Florida West Virginia Mass. New Hampshire Adding a New Item with a Given Key

24 24 Arizona Arkansas 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. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

25 25 Arizona Arkansas 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. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

26 26 Arizona Arkansas 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. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

27 27 Arizona Arkansas 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. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

28 28 Arizona Arkansas 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. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

29 29 Arizona Arkansas 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. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

30 30 Arizona Arkansas Adding Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Where would you add this state? Where would you add this state? Kazakhstan

31 31 Arizona Arkansas Adding Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan is the new right child of Iowa? Kazakhstan is the new right child of Iowa? Kazakhstan

32 32 Arizona Arkansas 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 Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan

33 33 Arizona Arkansas Removing 'Florida' ¶Find the item. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan

34 34 Arizona Arkansas Removing 'Florida' Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan Florida cannot be removed at the moment... Florida cannot be removed at the moment...

35 35 Arizona Arkansas Removing 'Florida' Washington Oklahoma Colorado West Virginia Mass. New Hampshire Iowa Kazakhstan... because removing Florida would break the tree into two pieces.... because removing Florida would break the tree into two pieces.

36 36 Arizona Arkansas Removing 'Florida' Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan The problem of breaking the tree happens because Florida has 2 children. The problem of breaking the tree happens because Florida has 2 children. ·If necessary, do some rearranging.

37 37 Arizona Arkansas Removing 'Florida' ·If necessary, do some rearranging. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan For the rearranging, take the smallest item in the right subtree... For the rearranging, take the smallest item in the right subtree...

38 38 Arizona Arkansas Removing 'Florida' Washington Oklahoma Colorado West Virginia Mass. New Hampshire Iowa Kazakhstan Iowa...copy that smallest item onto the item that we're removing......copy that smallest item onto the item that we're removing... ·If necessary, do some rearranging.

39 39 Arizona Arkansas Removing 'Florida' Washington Oklahoma Colorado West Virginia Mass. New Hampshire Iowa Kazakhstan... and then remove the extra copy of the item we copied...... and then remove the extra copy of the item we copied... ·If necessary, do some rearranging.

40 40 Arizona Arkansas Removing 'Florida' Washington Oklahoma Colorado West Virginia Mass. New Hampshire Iowa Kazakhstan... and reconnect the tree ·If necessary, do some rearranging.

41 41 Arizona Arkansas Removing 'Florida' Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Kazakhstan Why did I choose the smallest item in the right subtree? Why did I choose the smallest item in the right subtree?

42 42 Arizona Arkansas Removing 'Florida' Washington Oklahoma Colorado West Virginia Mass. New Hampshire Iowa Kazakhstan Because every key must be smaller than the keys in its right subtree

43 43 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 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

45 45 Trees III: Binary Search Trees - ends -


Download ppt "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."

Similar presentations


Ads by Google