Stack. Representations: - Over Array (/ Vector) - Over Linked-List
Stack. Representations: - Over Array (/ Vector) - Over Linked-List
Representations
initEmpty
push O(1)
pop
isEmpty
4/9/2014 1
Queue. Representations
initEmpty
enqueue O(1)
dequeue
isEmpty
4/9/2014 2
Deque. Representations
initEmpty
push_back O(1)
push_front
pop_back
pop_front
isEmpty
4/9/2014 3
STL: Stack, Queue. Issues
std::stack - container adaptor
template < class T, class Container = deque<T> > class queue;
• the underlying container •back()
•push_back()
•pop_back()
4/9/2014 4
STL: deque
Deque:
• Specific libraries may implement deque in different ways
generally as some form of dynamic array
• with efficient insertion and deletion of elements at the beginning and at its end.
4/9/2014 5
Java: Stack, Queue, Deque. Issues
Java™ Platform
Standard Ed. 7
Interface : Queue
Subinterface: Deque
Implementing Classes: ArrayDeque
LinkedList
4/9/2014 6
Java: Stack, Queue, Deque. Issues
Java™ Platform
Standard Ed. 7
4/9/2014 4
Lists. Variations. Application
Sparse Matrices
4/9/2014 5
Sparse Matrix
• sparse … many elements are zero
• dense … few elements are zero
Structured sparse
• diagonal
• tridiagonal
• upper/lower triangular (?)
4/9/2014 6
Unstructured sparse matrix. Example
• Web page matrix.
– web pages are numbered 1 through n
– web(i,j) = number of links from page i to page j
• n ~ 109
• n x n array => 1018 consecutive position
(linear representation)
• each page links to 10 (say) other pages on average
on average there are 10 nonzero entries per row
• space needed for non-empty elements is approximately
1 billion x 10 = 1010 consecutive elements
4/9/2014 7
• sparse matrix is a matrix populated primarily with zeros
• How to store it will depend at least partly on exactly how
sparse it is, and what you want to do.
– For some applications, just treating it as a regular matrix is just
fine (especially if the dimensions aren't very big). A 15x15 sparse
matrix isn't a big memory hog.
– suppose the sparse matrix has 10240x10240 elements, and you're
using 8-byte floating point numbers: how much memory do you
need?
• Representation of sparse matrix (idea)
– list: keep information about non-zero cells
– links: for fast access from one non-zero cell to the next, on the
same raw/column general linked list
4/9/2014
sparse matrix 8
Unstructured Sparse Matrices. Representations
linear list in row-major order.
• nonzero elements of the sparse matrix
• each nonzero element is represented by a triple
(row, column, value)
the list of triples (linked , …)
List:
00304
row 1 1 2 2 4 4
00570
column 3 5 3 4 2 3
00000
value 3 4 5 7 2 6
02600
4/9/2014 9
One Linear List Per Row
4/9/2014 10
One Linear List Per Row (Linked)
null
3 3 5 4
00304 null
3 5 4 7
00570
00000 nul
02600 l
null
2 2 3 6
row[]
Similar:
4/9/2014
one list per column 11
Orthogonal Lists
1 3 3 1 5 4
n n
00304
00570 2 3 5 2 4 7
n
00000
02600 nul
l
4 2 2 4 3 6
n n n
Variation: row[]lists
use circular
4/9/2014 12
#include <iostream>
#include <string>
1
Tree
Free tree (graph theory)
• any two vertices are connected
• no cycles
Rooted tree
• + root : one of the nodes is distinguished from the others
Ordered tree (most used in computer science)
• is a rooted tree in which the children of each node are ordered
if a node has k children, then there is a first child, a second
child, . . . , and a k-th child
Properties:
• Each node has exactly one “predecessor” – its parent
• has exactly zero, one or more “successors” – its children
3
Trees
• root
• parent , children, sibling
• ancestor , descendants
• leaves , internal
• depth (level) , height
• degree
4
Tree
Node degree – the number of descendants
Node depth (level)
• the length of the path to the root
• root – depth 0
Node height:
• the longest path from that node to a leaf (of the tree)
• (equivalent) the height of the subtree having that node as root
If the last edge on the path from the root r of a tree T to a node x is (y, x), then y
is the parent of x, and x is a child of y.
If two nodes have the same parent, they are siblings.
The root is the only node in T with no parent.
A node with no children is a leaf. A non-leaf node is an internal node.
5
k-ary tree
• A k-ary tree – each node have at most k descendants
6
Binary trees
Rooted trees
each node have at most two descendants.
– first descendant is the left descendant
– second descendant is the right descendant
A tree with N nodes has N-1 edges
8
Binary trees
degenerated
perfect
9
Binary trees
10
Binary trees
degenerated
(almost)
complete
11
Binary tree types
Perfect tree:
• all leaves have the same depth
• and all internal nodes have two children
(Almost) complete tree:
• for each level , except possibly the deepest, the nodes have 2
children
• in the deepest level, all nodes are as far left as possible
A degenerate tree
• each parent node has only one child
the tree will essentially behave like a linked list data structure
A balanced binary tree
• no leaf is much farther away from the root than any other leaf
– different balancing schemes allow different definitions of "much farther
12
Binary tree types
(true or false ?)
Perfect tree:
A binary tree with all leaf nodes at the same depth.
All internal nodes have degree 2.
13
Binary tree properties
14
Tree representation
15
Tree representation
16
Tree representation (1)
Based on recursive definition
• Node root information collection?
list of subTrees
remark: a tree is known by knowing its root
(links to subtrees)
18
Tree Representation (2)
=> sibling list
Linked
representation (2) 19
Tree traversal
20
Representation (1) & dynamic allocation
TreeNode: record
info: TElement
left: Position
right:Position
end
Position: ^TreeNode
Tree: record
root: Position
end
21
Representation (1) & dynamic allocation
TreeNode: record class TreeNode {
private:
info: TElement TElement info;
TreeNode* left;
left: ^TreeNode TreeNode* right;
right: ^TreeNode public:
end TreeNode(TElement value) {
this->info = value;
left = NULL;
Tree: record right = NULL;
}
root: ^TreeNode …
};
end class BinaryTree {
private:
TreeNode* root;
public:
…
22
Representation (1b) & dynamic allocation
TreeNode: record This representation fits the
recursive definition of binary tree.
info: TElement
For some recursive algorithms,
left: ^TreeNode we are going to use this
right:^TreeNode representation.
end
Tree: ^TreeNode
23
Representation (1) & over arrays
TreeNode: record
info: TElement
left: Integer
right: Integer
end
Tree: record
root: Integer
nodes: array [1..MAX] of TreeNode
// … information needed for freespace management
end
Variations:
• using 3 arrays: Infos, Lefts, Rights
• over a dynamic vector 24