1 / 54

Binary Search Trees

Binary Search Trees. One of the tree applications in Chapter 10 is binary search trees . In Chapter 10, binary search trees are used to implement bags. This presentation illustrates how another data type called a dictionary is implemented with binary search trees. Binary Search Trees (BST).

Télécharger la présentation

Binary Search Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Binary Search Trees • One of the tree applications in Chapter 10 is binary search trees. • In Chapter 10, binary search trees are used to implement bags. • This presentation illustrates how another data type called a dictionary is implemented with binary search trees.

  2. Binary Search Trees (BST) • A data structure for efficient searching, insertion and deletion • Binary search tree property • For every node x • All the keys in its left subtree are smaller than the key value in x • All the keys in its right subtree are larger than the key value in x

  3. Binary Search Trees A binary search tree Not a binary search tree

  4. Binary Search Trees • Average depth of a BST is O(log n) • Maximum depth of a BST is O(n) The same set of keys may have different BSTs

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

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

  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 keyfor each record is the name of the state. Washington

  8. void dictionary::insert(The key for the new item, The new item); The Dictionary Data Type • The insertion procedure for a dictionary has two parameters. Washington

  9. Item dictionary::retrieve("Washington"); The Dictionary Data Type • When you want to retrieve an item, you specify the key...

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

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

  12. Arizona Arkansas A Binary Search Tree of States Florida The data in the dictionary will be stored in a binary tree, with each node containing an item and a key. Oklahoma Colorado Mass. Washington New Hampshire West Virginia

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

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

  15. Arizona A Binary Search Tree of States Florida 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. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas

  16. Arizona A Binary Search Tree of States Florida 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. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas

  17. Searching BST • If we are searching for 15, then we are done. • If we are searching for a key < 15, then we should search in the left subtree. • If we are searching for a key > 15, then we should search in the right subtree.

  18. Searching (Find) • Find X: return a pointer to the node that has key X, or NULL if there is no such node • Time complexity: O(depth of the tree)

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

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

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

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

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

  24. Inorder Traversal of BST • Inorder traversal of BSTprints out all the keys in sorted order Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20

  25. findMin/ findMax • Goal:return the node containing the smallest (largest)key in the tree • Algorithm:Start at the root and go left(right)as long as there is a left (right) child. The stopping point is the smallest (largest) element • Time complexity = O(depth of the tree)

  26. Insertion • Proceed downthe tree as you would with a find • If x is found, do nothing (or update something) • Otherwise, insert x at the last spot on the path traversed • Time complexity = O(depth of the tree)

  27. Deletion • When we delete a node, we need to consider how we take care of the children of the deleted node. • This has to be done such that the property of the search tree is maintained.

  28. Deletion under Different Cases • Case 1: the node is a leaf • Delete it immediately • Case 2: the node has one child • Adjust a pointer from the parent to bypass that node

  29. Arizona Adding a New Item with aGiven Key Florida • 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. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas

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

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

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

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

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

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

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

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

  38. Deletion • When we delete a node, we need to consider how we take care of the children of the deleted node. • This has to be done such that the property of the search tree is maintained.

  39. Deletion under Different Cases • Case 1: the node is a leaf • Delete it immediately • Case 2: the node has one child • Adjust a pointer from the parent to bypass that node

  40. Deletion Case 3 • Case 3: the node has 2 children • Replace the key of that node with the minimum element at theright subtree • Delete that minimum element • Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1 or 2. • Time complexity = O(depth of the tree)

  41. Arizona Removing an Item with a Given Key Florida • Find the item. • If necessary, swap the item with one that is easier to remove. • Remove the item. Oklahoma Colorado Mass. Washington Iowa New Hampshire West Virginia Arkansas Kazakhstan

  42. Arizona Removing "Florida" Florida • Find the item. Oklahoma Colorado Mass. Washington Iowa New Hampshire West Virginia Arkansas Kazakhstan

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

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

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

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

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

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

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

More Related