Download Trees

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

B-tree wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Quadtree wikipedia , lookup

Red–black tree wikipedia , lookup

Linked list wikipedia , lookup

Java ConcurrentMap wikipedia , lookup

Interval tree wikipedia , lookup

Bloom filter wikipedia , lookup

Control table wikipedia , lookup

Comparison of programming languages (associative array) wikipedia , lookup

Binary search tree wikipedia , lookup

Hash table wikipedia , lookup

Rainbow table wikipedia , lookup

Transcript
CO4301 – Advanced Games
Development
Week 12
Using Trees
Gareth Bellaby
1
Set
•
•
A set is an Abstract Data Type (ADT). It's an
implementation of a finite set.
A collection of unique objects. Each object only
occurs once. Sets are unordered. (In practice
we'll order them for the sake of efficiency)
2
Set
•
•
There are a number of operations associated
with sets. The most important are:
• union. The join of two sets.
• intersection. The overlap of two sets.
But it'll be useful to have:
• difference. The set of objects in A, which are
not in B.
• subset.
• size
• filter
3
Implementation
•
•
•
Consider how we could possibly implement a
set.
Need to store the set.
Need efficient implementation of operations
such as union: combine set A and set B,
setting aside duplicates. Note that an efficient
implementation might well reuse the existing
structures rather than create a brand new third
one.
4
Implementation
•
•
•
•
•
•
Underlying
this
implementations of
would
be
efficient
Add (Insert): Traverse the objects and if not
found then insert into the set (using whatever
method).
Delete (Remove)
Search (Lookup)
Modify (Replace)
But also the general maintenance of the
structure
5
List
•
•
•
List (start with unordered)
Straightforward but inefficient.
Why?
6
Ordered list
•
•
•
•
•
Ordered list
Better.
Use something like insertion sort. Could build
custom structure which similarly sorts as it's
built.
Search using binary search, for example.
Search is good, but obvious problems with
insertion and deletion, e.g. insert at beginning
of array. Requires moving all of the other items
along if using an array.
7
Linked List
•
•
Linked List
Insertion at the beginning of the list (or end
with doubly linked list), but again obvious
limitations are traversal.
8
Tree
•
•
•
Tree
Binary search tree
No restrictions on it becoming unbalanced.
Hence can degenerate into the equivalent of
an ordered list.
9
Map
•
•
•
•
The ADP of an associative array.
Collection of (key,value) pairs.
Each key is unique (so obvious link with sets)
Each node contains a key and a value.
Obviously the value could be a link to larger
data set.
10
Dictionary Problem
•
•
•
•
A map is a of a general problem in computer
science called the Dictionary Problem
How to store data which, like a dictionary, has
a key and a value.
In a dictionary the key is the word, and the
value is the definition of the word.
Databases
11
Approaches
•
•
•
Efficient data structure to implement search,
delete and insert operations.
Two main possibilities:
• Hash tables
• Trees
These are both efficient solutions to the
dictionary problem.
12
Hash table
•
•
•
Hash table is good.
Chained or open access
reasonable approaches.
both
provide
It does have the problem of a worst case
scenario, e.g. in chained hash table, all entries
hashed to a single bucket and therefore
degenerates to a linked list.
13
Hash table
•
However, generally a hash table does not
approach a worst case condition. The average
time for a tree is slower than a comparable
hash table because the operations need to
maintain the self-balancing properties of the
tree are slower than the probing operations or
linked list costs associated with hash tables.
14
Choice
•
•
•
•
Often a pragmatic decision, but can give some
advice.
Hash table probably results in fewer reads.
Need an efficient hash function. May be difficult
or time consuming.
Patterns of data.
15
Choice
•
•
Hash misses may cause a problem if the
implementation requires a consistent time.
Degradation of performance is better in a BST
than a hash table, e.g. doesn’t the potential
catastrophic or rapid-fall in performance that a
hash table has.
16
Choice
•
•
•
Hash tables are unordered data.
BST are ordered data.
Imagine wanting to retrieve the data in an
ordered fashion.
17
Choice
•
•
•
•
BST may pack the data together better.
Better if interested in a range of objects. Trees
allow very easily for subtrees and hence
ranges of values.
No equivalence in hash tables.
Allows access to other operations such as
“closest match”, cf. closest points next week.
18
Choice
•
Possible to combine the two, e.g. index the
hash table using a BST.
19
Considerations
•
•
•
The STL set, map, multiset, and multimap are
generally built using a red-black tree.
Search trees
Open list for directed search
20
Tree unification
•
•
Identifying whether one tree is the same as a
another tree
Can be used for solving equations or logic
statements. Consider a tree representing an
equation. If one tree can be considered to be
the same as another, then the equations are
equivalent.
21