Sei sulla pagina 1di 4

Week 10 Tut

Dijkstras single source shortest path


priority queue
- can this be the same as
Prims?
each predecessor initially unknown
each distance infinity, since initially unknown
distance of start node = 0, and reorder the priority queue (if it doesnt do so
automatically)
// for each fringe edge
while priority queue not empty
get the next lowest-cost fringe edge
if no edge exists there for that fringe edge
otherwise: check distance is smaller, replace if so
reorder the PQ
Linked list: a data structure
Queue, priority queue: an abstract data structure - specifies how it works but no
specific method
of implementation for it.
reordering a PQ:
we find the minimum vertex (a simple linear search through the array)
we take the vertex out of the array (need to shuffle elements to fill the gap of the
vertex
taken out) and return the vertex (struct)

Tree: a data structure - a graph with no cycles


Binary tree: each node has (UP TO) 2 children max
Binary search tree:
(BS).

- search cost: O(n)

- search: O(log(n)), base 2


- (cut off half the tree on each

comparison)
specification for how the nodes should be arranged
values left of the root node are less than the root node, and vice versa for the
right side
-> if it didnt have the specification you would need to visit every single

node
To determine validity of a binary search tree, easiest to work from the biggest tree (at
root), to the
smallest subtree, and circle each left and right side and specify their required
domains.
Navigation through a BST - simply compare if your required value is <, =, or > the
node youre at.
If you hit NULL, you are at the right place but it doesnt exist.

Implementation: identical to linked list but more links.


Depth first viewing:
Infix: left subtree, root node (current node), right subtree
(in -> current node
inside)
-> results in order only for search trees (it goes left to right, BST is ordered left
to right too)
Prefix: CLR
Postfix: LRC
Level-order: use bfs
Infix prints the same as prefix if:
only one node, OR,
only a right subtree exists
Self-balancing binary trees: modifications to the algorithm to balance the tree and
prevent it
degenerating it into a LL.

Q7. find the longest distance from leaf to root


tree algorithms: a lot of them rely on recursion because of the basic nature of a
tree.
in general:
do something with left tree

do something with right tree


use properties of left / right tree
???
answer

int BSTreeMaxBranchLen (BSTree t) {


if (t == NULL) {
// base case
return 0;
} else if (t-> left == NULL && t->right == NULL) {
return 0;
// root with no children also has no branches
}
int a = BSTreeMaxBranchTreeLen(t->left);
int b = BSTreeMaxBranchTreeLen(t->right);
return ((a > b) ? a : b) + 1;
// max branch length between
the two choices
}

8. Minimum height of BSTree containing n nodes:


level 0 = 1 node
level 1 = 2 nodes
level 2 = 4 nodes
hence, in general: level i = 2^i nodes
sum ( levels(i), from i = 0 to m-1 ) = 2^(m-1) + + 2^1 + 2^0
n = sum () = 2^(m) -1
n + 1 = 2^(m)
m = log(n+1)

// base 2

log is typically going to give a decimal, so we take the ceiling of it.


m = ceil ( log(n+1) )
// base 2
where m = 0 as starting point
also says minimum, not maximum height.
int numNodes(Tree t) {
// base case almost always an empty tree

if (t == NULL) return 0;
return numNodes(t->left) + numNodes(t->right) + 1;
}

Insertion: find the position (if the branch you want to go to is NULL, you insert,
otherwise you keep
traversing down that subtree)
Deletion: a number of approaches
Delete a node, and replace it with its in-order successor (i.e. the next
highest node)
Easy to understand: consider a 1D sequence of numbers, you want an adjacent
number.
Another approach (lazy): just mark the node as deleted.
Search algorithm: if 6 is marked as deleted and the value you want to
search for is 6,
then you can just stop searching there, since the property of BSTs still
holds.
(Subtree values are < 6 and > 6).
Insertion: if 6 is marked as deleted, and you want to insert 6, then remark
that node as
not deleted.

Potrebbero piacerti anche