Download
csc 213 large scale programming n.
Skip this Video
Loading SlideShow in 5 Seconds..
Lecture 15: Binary Search Trees (BST) PowerPoint Presentation
Download Presentation
Lecture 15: Binary Search Trees (BST)

Lecture 15: Binary Search Trees (BST)

93 Views Download Presentation
Download Presentation

Lecture 15: Binary Search Trees (BST)

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. CSC 213 – Large Scale Programming Lecture 15:Binary Search Trees (BST)

  2. Today’s Goals • Review Map & Dictionary implementations • Which times are important and why do we care? • What would we like in other options for this ADT? • Review BinaryTree & what they look like • Introduce new implementation concept – BSTs • What type of data used within the nodes of a BST? • How should we connect BinaryTrees to BSTs? • Adding, searching, & removing data from BST how? • How are BST, Map, & Dictionary related?

  3. Today’s Goals • Review Map & Dictionary implementations • Which times are important and why do we care? • What would we like in other options for this ADT? • Review BinaryTree & what they look like • Introduce new implementation concept – BSTs • What type of data used within the nodes of a BST? • How should we connect BinaryTrees to BSTs? • Adding, searching, & removing data from BST how? • How are BST, Map, & Dictionary related?

  4. Map & Dictionary Performance • As much as we can, must get some actions fast • 911 Operators immediatelyneed addresses • Google’s search performance in TB/s • Millions of calls per second must be logged • All of our previous approaches have BIG issues • Function & space needed for hash table to be usable • Unordered lists are fast* to add, horrible to search • Searching good, but adding painful with ordered lists

  5. D A C B Picturing Linked BinaryTree B A C      D

  6. D A C B Nodes in Linked BinaryTree B A C      D

  7. Links in LinkedBinaryTree B B A C      D A C D

  8. Binary Search Trees • Implements a BinaryTree for searching • Map or Dictionarysupport possible from this • Data organized to make usage efficient (maybe) 6 2 9 10 1 4

  9. Binary Search Trees • Implements a BinaryTree for searching • Map or Dictionarysupport possible from this • Data organized to make usage efficient (maybe) • Strict ordering maintained in tree 6 2 9 10 1 4

  10. Binary Search Trees • Implements a BinaryTree for searching • Map or Dictionarysupport possible from this • Data organized to make usage efficient (maybe) • Strict ordering maintained in tree • Nodes to theleft are smaller 6 2 9 10 1 4

  11. Binary Search Trees • Implements a BinaryTree for searching • Map or Dictionarysupport possible from this • Data organized to make usage efficient (maybe) • Strict ordering maintained in tree • Nodes to theleft are smaller • Larger keys in right child of node 6 2 9 10 1 4

  12. Binary Search Trees • Implements a BinaryTree for searching • Map or Dictionarysupport possible from this • Data organized to make usage efficient (maybe) • Strict ordering maintained in tree • Nodes to theleft are smaller • Larger keys in right child of node 6 2 9 10 1 4

  13. Binary Search Trees • Implements a BinaryTree for searching • Map or Dictionarysupport possible from this • Data organized to make usage efficient (maybe) • Strict ordering maintained in tree • Nodes to theleft are smaller • Larger keys in right child of node • Either legal for equal keys • Just be consistent & no problem 6 2 9 10 1 4 6

  14. Binary Search Trees • Implements a BinaryTree for searching • Map or Dictionarysupport possible from this • Data organized to make usage efficient (maybe) • Strict ordering maintained in tree • Nodes to theleft are smaller • Larger keys in right child of node • Either legal for equal keys • True for ALLnodes in tree 6 2 9 10 1 4

  15. BST-basedMap or Dictionary BSTis-a binary tree

  16. BST-basedMap or Dictionary BSTis-a BinaryTree

  17. BST-basedMap or Dictionary BSTis-a BinaryTree not a Map or Dictionary

  18. BST-basedMap or Dictionary BSTis-a BinaryTree not a Map or Dictionary

  19. BST Data • Keys are Comparable or use Comparator • Entry required; implementing Map or Dictionary • For BST to work, keys need to have total ordering • Integer, String, NHLTeam would be acceptable

  20. BST Implementation • Implements a BinaryTree for searching • Map or Dictionarysupport possible from this • Binary tree implementation slightly changed • Entrysonly in internal nodes of tree • This is useful, but not required • Why would this be helpful? • Hint: How do we use Maps? 6 2 9 10 1 4

  21. Searching a BST • Tree searched recursively starting from root AlgorithmTreeSearch(Kkey)returnTreeSearch(key,root())AlgorithmTreeSearch(Kkey,Position<Entry<K,V>>p)ifisExternal(p)return pelse ifc.compare(key, p.element().getKey())== 0returnpelse if c.compare(key, p.element().getKey())< 0returnTreeSearch(key,left(p))else if c.compare(key, p.element().getKey()) > 0 returnTreeSearch(key,right(p))

  22. Example:TreeSearch(8) AlgorithmTreeSearch(Kkey,Position<Entry<K,V>>p)ifisExternal(p)return pelse ifc.compare(key, p.element().getKey())== 0returnpelse if c.compare(key, p.element().getKey())< 0returnTreeSearch(key,left(p))else if c.compare(key, p.element().getKey()) > 0 returnTreeSearch(key,right(p)) 6 2 9 8 1 4

  23. Example:TreeSearch(8) AlgorithmTreeSearch(Kkey,Position<Entry<K,V>>p)ifisExternal(p)return pelse ifc.compare(key, p.element().getKey())== 0returnpelse if c.compare(key, p.element().getKey())< 0returnTreeSearch(key,left(p))else if c.compare(key, p.element().getKey()) > 0 returnTreeSearch(key,right(p)) 6 2 9 8 1 4

  24. Example:TreeSearch(8) AlgorithmTreeSearch(Kkey,Position<Entry<K,V>>p)ifisExternal(p)return pelse ifc.compare(key, p.element().getKey())== 0returnpelse if c.compare(key, p.element().getKey())< 0returnTreeSearch(key,left(p))else if c.compare(key, p.element().getKey()) > 0 returnTreeSearch(key,right(p)) 6 2 9 8 1 4

  25. Example:TreeSearch(8) AlgorithmTreeSearch(Kkey,Position<Entry<K,V>>p)ifisExternal(p)return pelse ifc.compare(key, p.element().getKey())== 0returnpelse if c.compare(key, p.element().getKey())< 0returnTreeSearch(key,left(p))else if c.compare(key, p.element().getKey()) > 0 returnTreeSearch(key,right(p)) 6 2 9 8 1 4

  26. Example:TreeSearch(8) AlgorithmTreeSearch(Kkey,Position<Entry<K,V>>p)ifisExternal(p)return pelse ifc.compare(key, p.element().getKey())== 0returnpelse if c.compare(key, p.element().getKey())< 0returnTreeSearch(key,left(p))else if c.compare(key, p.element().getKey()) > 0 returnTreeSearch(key,right(p)) 6 2 9 8 1 4

  27. Example:TreeSearch(8) AlgorithmTreeSearch(Kkey,Position<Entry<K,V>>p)ifisExternal(p)return pelse ifc.compare(key, p.element().getKey())== 0returnpelse if c.compare(key, p.element().getKey())< 0returnTreeSearch(key,left(p))else if c.compare(key, p.element().getKey()) > 0 returnTreeSearch(key,right(p)) 6 2 9 8 1 4

  28. Example:TreeSearch(8) AlgorithmTreeSearch(Kkey,Position<Entry<K,V>>p)ifisExternal(p)return pelse ifc.compare(key, p.element().getKey())== 0returnpelse if c.compare(key, p.element().getKey())< 0returnTreeSearch(key,left(p))else if c.compare(key, p.element().getKey()) > 0 returnTreeSearch(key,right(p)) 6 2 9 8 1 4

  29. Example:TreeSearch(5) AlgorithmTreeSearch(Kkey,Position<Entry<K,V>>p)ifisExternal(p)return pelse ifc.compare(key, p.element().getKey())== 0returnpelse if c.compare(key, p.element().getKey())< 0returnTreeSearch(key,left(p))else if c.compare(key, p.element().getKey()) > 0 returnTreeSearch(key,right(p)) 6 2 9 8 1 4

  30. Example:TreeSearch(5) AlgorithmTreeSearch(Kkey,Position<Entry<K,V>>p)ifisExternal(p)return pelse ifc.compare(key, p.element().getKey())== 0returnpelse if c.compare(key, p.element().getKey())< 0returnTreeSearch(key,left(p))else if c.compare(key, p.element().getKey()) > 0 returnTreeSearch(key,right(p)) 6 2 9 8 1 4

  31. Example:TreeSearch(5) AlgorithmTreeSearch(Kkey,Position<Entry<K,V>>p)ifisExternal(p)return pelse ifc.compare(key, p.element().getKey())== 0returnpelse if c.compare(key, p.element().getKey())< 0returnTreeSearch(key,left(p))else if c.compare(key, p.element().getKey()) > 0 returnTreeSearch(key,right(p)) 6 2 9 8 1 4

  32. Example:TreeSearch(5) AlgorithmTreeSearch(Kkey,Position<Entry<K,V>>p)ifisExternal(p)return pelse ifc.compare(key, p.element().getKey())== 0returnpelse if c.compare(key, p.element().getKey())< 0returnTreeSearch(key,left(p))else if c.compare(key, p.element().getKey()) > 0 returnTreeSearch(key,right(p)) 6 2 9 8 1 4

  33. Example:TreeSearch(5) AlgorithmTreeSearch(Kkey,Position<Entry<K,V>>p)ifisExternal(p)return pelse ifc.compare(key, p.element().getKey())== 0returnpelse if c.compare(key, p.element().getKey())< 0returnTreeSearch(key,left(p))else if c.compare(key, p.element().getKey()) > 0 returnTreeSearch(key,right(p)) 6 2 9 8 1 4

  34. Example:TreeSearch(5) AlgorithmTreeSearch(Kkey,Position<Entry<K,V>>p)ifisExternal(p)return pelse ifc.compare(key, p.element().getKey())== 0returnpelse if c.compare(key, p.element().getKey())< 0returnTreeSearch(key,left(p))else if c.compare(key, p.element().getKey()) > 0 returnTreeSearch(key,right(p)) 6 2 9 8 1 4

  35. Example:TreeSearch(5) AlgorithmTreeSearch(Kkey,Position<Entry<K,V>>p)ifisExternal(p)return pelse ifc.compare(key, p.element().getKey())== 0returnpelse if c.compare(key, p.element().getKey())< 0returnTreeSearch(key,left(p))else if c.compare(key, p.element().getKey()) > 0 returnTreeSearch(key,right(p)) 6 2 9 8 1 4

  36. Example:TreeSearch(5) AlgorithmTreeSearch(Kkey,Position<Entry<K,V>>p)ifisExternal(p)return pelse ifc.compare(key, p.element().getKey())== 0returnpelse if c.compare(key, p.element().getKey())< 0returnTreeSearch(key,left(p))else if c.compare(key, p.element().getKey()) > 0 returnTreeSearch(key,right(p)) 6 2 9 8 1 4

  37. Example:TreeSearch(5) AlgorithmTreeSearch(Kkey,Position<Entry<K,V>>p)ifisExternal(p)return pelse ifc.compare(key, p.element().getKey())== 0returnpelse if c.compare(key, p.element().getKey())< 0returnTreeSearch(key,left(p))else if c.compare(key, p.element().getKey()) > 0 returnTreeSearch(key,right(p)) 6 2 9 8 1 4

  38. Adding Entry With New Key • Add to leaf whenkey not found • But to find which leaf, begin with BST search • Expand node & set Entry at leaf to add to BST • Process is the same for Map& Dictionary

  39. Adding Entry With New Key • Add to leaf whenkey not found • But to find which leaf, begin with BST search • Expand node & set Entry at leaf to add to BST • Process is the same for Map& Dictionary

  40. Adding Entry With New Key • Example: treeInsert(5,???) • Search BST’s nodesto find where to add Entry • public method that makes initial call starting at root 6 2 9 8 1 4

  41. Adding Entry With New Key • Example: treeInsert(5,???, v) • Search BST from vto find where to add Entry 6 2 9 8 1 4

  42. Adding Entry With New Key • Example: treeInsert(5,???, v) • Search BST from vto find where to add Entry 6 2 9 8 1 4

  43. Adding Entry With New Key • Example: treeInsert(5,???, v) • Search BST from vto find where to add Entry 6 2 9 8 1 4

  44. Adding Entry With New Key • Example: treeInsert(5,???, v) • Search BST from vto find where to add Entry 6 2 9 8 1 4

  45. Adding Entry With New Key • Example: treeInsert(5,???, v) • Search BST from vto find where to add Entry 6 2 9 8 1 4

  46. Adding Entry With New Key • Example: treeInsert(5,???, v) • Search BST from vto find where to add Entry • Should end at leaf, so expand node & set Entry 6 2 9 8 1 4

  47. Adding Entry With New Key • Example: treeInsert(5,???, v) • Search BST from vto find where to add Entry • Should end at leaf, so expand node & set Entry 6 2 9 8 1 4 5

  48. Adding Entry With New Key • Example: treeInsert(5,???, v) • Search BST from vto find where to add Entry • Should end at leaf, so expand node & set Entry 6 2 9 8 1 4 5

  49. Adding Entry With Existing Key • Stop at node with keythat is already in BST • What to do next depends on if Mapor Dictionary • Change Entry's value if this is for a Map • Until at a leaf, continue search for Dictionary • Go to appropriate child for cases when keys are equal • Note that we may end at node far away in tree

  50. Adding Entry With Existing Key • Example: treeInsert(9,???, v) • Search BST from vto find where to add Entry 6 2 9 8 1 4 5