Beruflich Dokumente
Kultur Dokumente
Tutorial 3
Trees and Graphs
import java.util.*;
public BinarySearchTree()
{
root = null;
size = 0;
}
Node<T> replacement;
if (p.left != null)
replacement = p.left;
else
replacement = p.right;
if (replacement != null) {
replacement.parent = p.parent;
if (p.parent == null)
root = replacement;
else if (p == p.parent.left)
p.parent.left = replacement;
else
p.parent.right = replacement;
} else if (p.parent == null) {
root = null;
} else {
if (p == p.parent.left)
p.parent.left = null;
else
p.parent.right = null;
}
return p;
}
private TreeIterator()
{
next = root;
if (next != null)
while (next.left != null)
next = next.left;
}
public T next()
{
if (next == null)
throw new NoSuchElementException();
lastReturned = next;
next = successor(next);
return lastReturned.element;
}
BinarySearchTree.java
1. Write a test program to test add, remove, contains and the iterator
2. Write a method (using recursion) to count the average depth of a tree (you are allowed to modify the class). To compute the average depth, first compute total depth of all the nodes and then divide by the number of nodes.
3. Write a program to compute the average depth versus the number of inputs for randomly generated numbers. Plot this as a graph.
Graph Algorithms
6. Write a Java Graph class which stores a set of vertices and their 2-D positions. It should also store a set of edges together with weights on the edges. Hint
7. Write a method randomGraph(n,p) which generates a graph with n vertices placed at random in the unit square and where the probability of an edge is p. The weight of the edges are the distances between the
nodes.
To visualise the graph use the follow program (this is just for visualisation, to use it create an instance of GraphDisplay)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public GraphDisplay()
{
minX = minY = Double.POSITIVE_INFINITY;
maxX = maxY = Double.NEGATIVE_INFINITY;
}
g.translate(+MARGIN,+MARGIN);
synchronized(this){
for(Edge e: edges)
e.paint(g,xscl,yscl,minX,minY);
for(Node n: nodes.values())
n.paint(g,xscl,yscl,minX,minY);
}
g.translate(-MARGIN,-MARGIN);
}
public void paint(Graphics g, double xscl, double yscl, double tx, double ty)
{
g.setColor(col);
g.fillOval(
(int)((x-tx)*xscl - NODE_RADIUS),
(int)((y-ty)*yscl - NODE_RADIUS),
2*NODE_RADIUS,
2*NODE_RADIUS
);
}
public void paint(Graphics g, double xscl, double yscl, double tx, double ty)
{
Node a = nodes.get(start);
Node b = nodes.get(end);
g.setColor(col);
g.drawLine(
(int)(xscl*(a.x-tx)),
(int)(yscl*(a.y-ty)),
(int)(xscl*(b.x-tx)),
(int)(yscl*(b.y-ty))
);
}
GraphDisplay1.java
8. Write a method that implements Prim's algorithm to compute the minimum spanning tree.
Prim's algorithm is illsutrated in here. Your program should use your Graph class and the Java's collection class priority queue. You should visualise the answer using GraphDisplay.
9. Write a method that implements Kruskal's algorithm to compute the minimum spanning tree.
private int[] s;
}
DisjointSets.java
To use the DisjointSet class you simple write
if (finda!=findb) {
// vertices a and b belong to different trees
dj.union(finda, findb); // NOT dj.union(a,b)
/* add (a,b) to spanning tree */
}
usingds.java
10. Add a method to the graph-colouring program in tutorial 1 so that it performs a branch and bound search (see lesson 17). This should involve modifying the exhaustive search. Compare the time complexity with exhaustive
enumeration.
previous