COMP282: Advanced Data Structures
Lecture 02
COMP182 Review
Basic Linear and Logarithmic Structures
Linear Structures [O(n)]
- Arrays
- Advantages:
- Easy to program / built-in to the language
- Space efficient*
- Support random element access in O(1) time.
- Disadvantages
- Fixed size once allocated
- Deletion and insertion are generally O(n).
- Limited by contiguous memory constraints.**
- Space wasteful*
Linked Lists(advantages)
- Linked List:
- Advantages
- Deletion and Insertion at a know location = O(1)
- Space efficient*
- Memory doesn't have to be contigous
- Flexible and extensible (stacks and queue classes)
- Dynamic size
- Nicely suited to recursive solutions.
Linked List (Disadvantages)
- Disadvantages
- More complicated to program and implement.
- Difficult to make efficient use of sorted data.
- Still exhibit linear efficiency [ O(n) ]
- Mistakes in implementation or usage can render large pools of information lost. (Memory leaks in languages that do not perform garbage collecting.)
Programming Linked Lists
- Node class
class ListNode
{
Object item;
ListNode next;
ListNode prev; // a doubly linked list
// constructors and other “node” methods
}
- List class:
class LinkedList
{
ListNode first;
ListNode last;
int count;
// constructors and other “list” methods [ add, delete, search ]
}
Lazy Solution (JAVA)
- But you don't need to go through the hassle of programming your own doubly linked list:
- JAVA has many such pre-implemented packages.
- For linked lists you may also want to use:
- This allows you an opaque method for accessing and manipulating the items stored on the abstract Linked List provided by java.util.LinkedList.
Stacks
- Stack: a data structure that supports:
- Push() [ pushes an item onto the top of the “stack” ]
- Pop() [ returns the item on the top of the “stack” ]
- The effect that the next item “removed” is always the last item that was added.
- Arrays and Linked Lists both support stack behavior as long as you add and remove items from the same end all the time.
Queues
- Queue: a data structure that supports:
- enqueue() [ adds an item at the end of the “queue” ]
- dequeue() [ returns the item at the “queue”'s front ]
- The effect that the next item “removed” is always the first item that was added.
- Arrays and Linked Lists both support queue behavior as long as you add items to the opposite end of the list from which you are removing items.
Logarithmic Structures (BST)
- Quest for better performance:
- O(n) is ok and easily acheived.
- O(1) is great but difficult to acheive
- O(lg(N)) is very good.
- By taking advantage of certain properties (mathematical order for instance) this can be acheived.
BST
- Binary Search Tree:
- Nodes have two children and are mathematically ordered such that:
- All items present in the left subtree of a node N are “less than” the item stored at N.
- All items present in the right subtree of a node N are “greater than” the item stored at N.
BST Topology
- Example of a full BST with a height of 4
- Detail of structure near root:
Performance of BST
- Advantages:
- Supports insertion, deletion and search in O(lg(n))*
- Disadvantages:
- Even more complicated to program
- Requires some way to “order” items in a precise, repeatable fashion.
- Poorly chosen/presented data can render the efficiency only linear time again [ O(n) ]
Poor BST performance
- Poor data can result in “vines” which exhibit linear behavior:
- Balanced Binary Search
Trees will be covered
later and prevent this
from becoming a problem.
(so does java.util.TreeMap)