Section 7: BSTs and Tries - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Section 7: BSTs and Tries

Description:

Searching in a BST requires looking at one node per tree level (in the worst case) ... BST Implementation ... been if it were actually in the BST; insert there ... – PowerPoint PPT presentation

Number of Views:150
Avg rating:3.0/5.0
Slides: 25
Provided by: Qix
Category:
Tags: bst | bsts | section | tries

less

Transcript and Presenter's Notes

Title: Section 7: BSTs and Tries


1
Section 7 BSTs and Tries
CS 225 Data Structures Software Principles
2
Binary Search Trees
  • A Binary Search Tree is a binary tree with the
    following properties
  • Values associated with nodes have a linear order
    (i.e. we can define "less-than" on node values)
  • Every node's value is greater than any value in
    its left sub-tree and less than any value in its
    right sub-tree
  • Abbreviated as BST

3
Binary Search Trees
  • Tree height
  • The height of a complete binary tree with n nodes
    is exactly ?log n?
  • The maximum height of a binary tree with n nodes
    is n-1
  • The minimum height of a binary tree with n nodes
    is ?log n?
  • Why do we care?

4
Binary Search Trees
  • Searching in a BST requires looking at one node
    per tree level (in the worst case)
  • Worst-case search time
  • for all possible search trees with n nodes O(n)
  • for the best search tree with n nodes O(log n)

vs.

5
BST Implementation
  • We can build BSTs from last week's BinaryTree
    code no implementation tricks are needed. (A
    BST is just a normal binary tree, used in a
    special way.)
  • Functions we might like
  • Find (search for an item)
  • Insert (add a new item)
  • Remove (delete an item)

6
Basic BST Operations Find
  • Basic algorithm
  • If we're searching in an empty tree, the value
    we're looking for isn't here.
  • Is the value we're looking for at the root?
  • If so, we're done we found it
  • Otherwise, compare the value at the root to the
    one we're looking for, to figure out which
    subtree it should be in

7
Basic BST Operations Find
  • template lttypename Etypegt
  • bool BinarySearchTreeltEtypegtfind(Etype const
    searchElem,
  • typename BinarySearchTreeltEtypegtTreeNode
    const treePtr) const
  • // what goes here?

8
Basic BST Operations Find
  • template lttypename Etypegt
  • bool BinarySearchTreeltEtypegtfind(Etype const
    searchElem,
  • typename BinarySearchTreeltEtypegtTreeNode
    const treePtr) const
  • if (treePtr NULL)
  • return false
  • else if (searchElem treePtr-gtelement)
  • return true
  • else if (searchElem lt treePtr-gtelement)
  • return find(searchElem, treePtr-gtleft)
  • else // searchElem gt treePtr-gtelement
  • return find(searchElem, treePtr-gtright)

9
Basic BST Operations Find
  • Recursion incurs extra overhead let's make this
    iterative.
  • template lttypename Etypegt
  • bool BinarySearchTreeltEtypegtfind(Etype const
    searchElem,
  • typename BinarySearchTreeltEtypegtTreeNode
    const treePtr) const
  • // what now?

10
Basic BST Operations Find
  • Recursion incurs extra overhead let's make this
    iterative.
  • template lttypename Etypegt
  • bool BinarySearchTreeltEtypegtfind(Etype const
    searchElem,
  • typename BinarySearchTreeltEtypegtTreeNode
    const treePtr) const
  • while (treePtr ! NULL)
  • if (searchElem P-gtelement)
  • return true
  • else if (searchElem lt P-gtelement)
  • P P-gtleft
  • else
  • P P-gtright
  • return false

11
Basic BST Operations Insert
  • Insert
  • Must ensure that tree remains a binary search
    tree after insertion
  • Determine where the element would have been if it
    were actually in the BST insert there
  • What does this mean implementation-wise?
  • Compare Insert() vs Find()

12
Basic BST Operations Remove
  • Remove
  • Not as easy
  • Start by finding the node we want to remove
  • Next, there are three cases to consider
  • The node is a leaf
  • The node has one child
  • The node has two children

13
Terminology for Remove
  • Consider root node (6)
  • In-order successor smallest (left-most) element
    in right subtree
  • In-order predecessor greatest (right-most)
    element in left subtree

6
4
10
7
12
1
5
14
Basic BST Operations Remove
  • If the node we're removing has
  • No children
  • Just delete it!
  • One child
  • Attach the node's parent to its child
  • Then delete it!
  • Two children
  • Find the in-order successor
  • Swap the values between the node and its IOS
  • Remove the "old" value from the right
    subtree(how do we know this removal will be
    "easy"?)

15
Intermission
  • Midterm 1 is graded!
  • Average 65 (out of 90) s 17
  • To request a regrade, write up a list of the
    problems you think we should look at, and why we
    should look at them. Give it to a TA.
  • If you want (or might want) a regrade, don't take
    your exam home today (leave it with me).

16
Tries
  • Data structure optimized for lookups on a key
    that can be decomposed into characters
  • Represented using a tree of arrays
  • For a character set of size k, the corresponding
    Trie structure is a (k1)-ary tree

17
Tries
  • i-th character (starting at 0) in the data
    corresponds to a node at depth i
  • Need a mapping of character to an array index
  • The extra cell in the array represents the null
    character ( ? )
  • Represents the end of a word
  • Points to a leaf
  • Ideally, no need to store key in a leaf, since it
    is completely determined by path followed
  • Info stored at the leaf
  • Spend only constant time at each level

18
Trie Example
Words in Trie
raft star start stir
19
Tries
  • Running time of Find operation O(L) where L is
    the length of the string we are looking for
  • Advantage NOT dependent on the number of strings
    we have in the Trie structure
  • Disadvantage memory waste
  • 27 cell array, one per character needed for
    Strings
  • Space (k1) nodes sizeof(pointer)
  • Although, could be better for a large number of
    short strings

20
Jasons CodeTrieNode Data
  • TrieNode
  • int nodeLevel // level of the nodebool
    isLeaf // is this a leaf?ArrayltTrieNodegt
    subtries //array nodes String key // string
    key in leaf nodes Etype storedInfo //
    associated info in leaf nodes

21
Code Review Trie Search
  • template ltclass Etypegt
  • pairltbool, Etypegt TrieltEtypegtfind(String const
    searchKey, typename TrieltEtypegtTrieNode
    const nodePtr)
  • if (nodePtrNULL)
  • return pairltbool, Etypegt(false, Etype())
  • else if (nodePtr-gtisLeaf true) //
    found a leaf
  • if (searchKey nodePtr-gtkey)
  • return pairltbool, Etypegt(true,
    nodePtr-gtstoredInfo)
  • else
  • return pairltbool, Etypegt(false,
    Etype())
  • else // not a leaf
  • int index ascIndex(searchStringnodePtr-
    gtnodeLevel)
  • return find(searchKey, (nodePtr-gtsubtries)
    index)

22
Code Review Trie Insert
  • template ltclass Etypegt
  • void TrieltEtypegtinsert(String insKey, Etype
    insInfo, typename TrieltEtypegtTrieNode
    nodePtr, int prevLevel)
  • if (nodePtr NULL) // NULL case
  • if (prevLevel insKey.length()) //
    make leaf node
  • nodePtr new TrieNode(insKey,
    insInfo)
  • nodePtr-gtnodeLevel prevLevel1
  • else
    // make internal node
  • nodePtr new TrieNode()
  • nodePtr-gtnodeLevel prevLevel1
  • insert(insKey, insInfo,
  • (nodePtr-gtsubtries)
    ascIndex(
  • insKeynodePtr-gtnodeLevel
    ) , nodePtr-gtnodeLevel)
  • // more

23
Trie Insert
  • else if (nodePtr-gtisLeaf true) // leaf
    case
  • cout ltlt "This key already exists in the
    trie!" ltlt endl
  • return
  • else // nodePtr-gtnodeType false, array
    node case
  • insert(insString, insInfo,
  • (nodePtr-gtsubtries)ascIndex(
  • insStringnodePtr-gtnodeLevel)
    , nodePtr-gtnodeLevel)

24
Trie Remove
  • What's the basic idea?
  • Search for the key
  • If it's not there, give up
  • Otherwise, remember the leaf that corresponds to
    it
  • Delete the leaf
  • Figure out if this makes any of the internal
    nodes "empty" if so, delete them too
  • When we actually implement this, we'll execute
    multiple steps at the same time
Write a Comment
User Comments (0)
About PowerShow.com