Chapter 2 briefly - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Chapter 2 briefly

Description:

Dictionary ... Create to create a new dictionary. Member boolean to determine if the parameter is in the dictionary ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 7
Provided by: foxr
Category:

less

Transcript and Presenter's Notes

Title: Chapter 2 briefly


1
Chapter 2 (briefly)
  • We introduce here a few data structures that we
    will use later in the semester
  • Most of the material covered in chapter 2 should
    have previously been covered in 262 and/or 364
    and is left here for the students to read in
    order to refresh their memory
  • Three specific ADTs that were probably not
    previously covered are
  • In-Tree
  • Union-Find
  • Dictionary
  • We will need to refer back to these three ADTs
    later in the semester when we look to develop
    more efficient algorithms to accomplish various
    tasks, here we take a very brief look at each

2
The In-Tree (2.3.5)
  • An In-tree is a tree with only one kind of access
    from child to parent or from descendant to
    ancestor, but not from ancestor to descendant as
    we have in a normal tree
  • An ancestor is a node in the path from a given
    node to the trees root, a descendant is a node
    in the path from a given node to a leaf in the
    nodes subtree
  • The ADT will require methods to
  • makeNode (this creates a root node)
  • isRoot (boolean)
  • parent (returns a pointer to the parent of the
    current node)
  • setParent (given two nodes, makes one the parent
    of the other)
  • nodeData (returns the data stored in the current
    node)
  • setNodeData (changes the data stored in the
    current node)
  • In an in-tree, new nodes are not added at the
    leaf, but instead the tree is extended upwards
    creating a new parent to a given node

3
Example In-Tree
  • Two in-trees are given to the left
  • We might traverse such a tree by following an
    array that stores pointers to each node
  • so for instance, we have A5 points to F
  • We then traverse the nodes parent pointer until
    we reach the root
  • a root node will have a designator such as parent
    -1
  • To add to a tree, we use setParent
  • As in setParent(G, A) which creates the subtree
    below on the left

G
I
H
J
4
Union-Find ADT (2.5.2)
  • The Union-Find ADT consists of a data structure
    that includes a number of sets and two methods
  • Union take two distinct sets and create a
    single set that combines all elements of the two
  • Find a boolean operation to determine if two
    items are in the same set or not
  • Two other methods that we might want are
  • Create create a new Union-Find structure with n
    initial empty sets
  • makeSet initializes the empty sets to contain a
    single element

5
Union-Find Example
  • Consider the following
  • UnionFind uf new UnionFind()
  • uf.create(10) // creates a structure with 10
    initial empty sets
  • uf.makeSet(a, 1) // makes the first set a
  • uf.makeSet(b, 2) // makes the second set b
  • uf.makeSet(c, 3) // makes the third set c
  • uf.union(1, 2) // unions the two sets to be a,
    b
  • uf.find(1, 2) // returns 1 or 2, whichever is
    the int value that now represents the single set
    of two items
  • uf.find(1, 3) // returns 0 (1 and 3 are not in
    the same set)

6
Dictionary ADT (2.5.3)
  • Like a true dictionary, there are entries that
    are indexed by an identifier and store
    information about that entry, but they need not
    be used to store say pronunciation and definition
    as with a true dictionary
  • The dictionary type can store anything that we
    want to have associated with the identifier
  • In a way, the dictionary ADT is much like a
    database a unique key (the identifier) is used
    to access the information pertaining to that key
  • Methods will include
  • Create to create a new dictionary
  • Member boolean to determine if the parameter is
    in the dictionary
  • Retrieve to retrieve the object that is
    identified by the parameter
  • Store to add a new object in the dictionary
    given the identifier and the object (storage
    information)
Write a Comment
User Comments (0)
About PowerShow.com