850 likes | 987 Views
CSC 213 – Large Scale Programming. Lecture 15: Binary Search Trees (BST). 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
E N D
CSC 213 – Large Scale Programming Lecture 15:Binary Search Trees (BST)
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?
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?
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
D A C B Picturing Linked BinaryTree B A C D
D A C B Nodes in Linked BinaryTree B A C D
Links in LinkedBinaryTree B B A C D A C D
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
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
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
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
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
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
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
BST-basedMap or Dictionary BSTis-a binary tree
BST-basedMap or Dictionary BSTis-a BinaryTree
BST-basedMap or Dictionary BSTis-a BinaryTree not a Map or Dictionary
BST-basedMap or Dictionary BSTis-a BinaryTree not a Map or Dictionary
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
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
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))
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Adding Entry With New Key • Example: treeInsert(5,???, v) • Search BST from vto find where to add Entry 6 2 9 8 1 4
Adding Entry With New Key • Example: treeInsert(5,???, v) • Search BST from vto find where to add Entry 6 2 9 8 1 4
Adding Entry With New Key • Example: treeInsert(5,???, v) • Search BST from vto find where to add Entry 6 2 9 8 1 4
Adding Entry With New Key • Example: treeInsert(5,???, v) • Search BST from vto find where to add Entry 6 2 9 8 1 4
Adding Entry With New Key • Example: treeInsert(5,???, v) • Search BST from vto find where to add Entry 6 2 9 8 1 4
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
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
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
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
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