This repo contains implementations of following algorithms:
- RB Tree
- Kruskal's algorithm
- Dijkstra’s algorithm
Consider each separately:
public class RBNode extends Node
1. public RBNode(Integer key, Object data, RBNode leftChild, RBNode rightChild, RBNode parent, boolean color, Boolean nil)
constructor
2. public String toStringWithChildren()
returns a tree converted to a string below this node:
public class RBTree
1. public RBNode getRoot()
returns root node of tree and null if tree is empty
2. public boolean addElement(RBNode node)
returns true if element was added and false if tree contains node with similar key
3. public boolean deleteElement(Integer key)
returns true if element with given key was deleted and false if element with such key doesn't exist
4. public RBNode search(Integer key)
returns node with given key or null if node with given key doesn't exist
5. public RBNode search(Integer key, RBNode startNode)
returns node with given key which is below startNode
6. public Integer getDepth()
returns depth of tree
7. public void print()
prints tree in the same way as RBNode.toStringWithChildren():
8. public boolean testIsOK()
checks the tree for violations of red-black tree rules:
- every node is red or black, the root node is black,
- no two red nodes appear consecutively,
- the path from the root node to all four null leaf nodes passes through two black nodes (either 2 and then 1 , or 2 and then 3) on the way to the null leaves.
9. public Integer getBlackDepth(RBNode node)
returns black depth of the tree
The graph is stored as a list of edges.
public class Edge implements Comparable<Edge>{
private Node startNode;
private Node endNode;
private Integer mass;
public Integer getMass();
public void setMass(Integer mass) ;
public Node getEndNode();
public void setEndNode(Node endNode);
public Node getStartNode();
public void setStartNode(Node startNode);
}
public class Node {
private Integer key;
private Object data;
public int wayWeight = Integer.MAX_VALUE;
public int[] way;
public int wayLength;
public void setData(Object data)
public Object getData()
public void setKey(Integer key)
public Integer getKey()
}
public class Graph
1. public void init()
Creates local arrays. Must be called if you don't set them by hands:
public ArrayList<Node> getNodes();
public void setNodes(ArrayList<Node> nodes);
public ArrayList<Edge> getEdges();
public void setEdges(ArrayList<Edge> edges);
2. public Node getNode(Integer key);
returns node by given key
3. public boolean isInitialized();
returns true if local arrays were initialized or set and false if they are nulls
4.
public boolean addEdge(Edge edge);
public boolean addNode(Node node);
public boolean removeEdge(Edge edge);
public boolean removeNode(Node node);
public boolean removeNode(Integer key);
returns true if action was done and false if not
5.
public ArrayList<Edge> getEdgesFromNode(Node node)
public ArrayList<Edge> getEdgesFromNode(Integer key)
returns edges coming out of the node
6.
public ArrayList<Edge> getEdgesToNode(Node node)
public ArrayList<Edge> getEdgesToNode(Integer key)
returns edges incoming in the node
7. public ArrayList<Edge> getEdges(Integer key);
returns all edges connected to the node
8. public Graph getMinSpanningTree();
returns minimal spanning tree using Kruskal's algorithm
9. public boolean equalsUndirected(Graph graph);
compares an undirected graph with a given one. Returns true if the keys at their nodes and the edges connecting the nodes are equal
10. public void printEdgesUndirected();
prints edges of graph without direction
11. public void findMinWays(Node rootNode);
finds shortest ways using Dijkstra’s algorithm. Results are put in nodes
12. public ArrayList<Node> getNeighbours(Node node);
returns neighbours of given node
13. public void printWays();
allows to print found ways after calling findMinWays(Node rootNode)
1. public Graph readAnswerForKraskalFrom(String str);
builds graf using following format:
numberOfNodes
key11 key12 massOfEdge1
key21 key22 massOfEdge2
... ... ...
where keys of nodes are equal to their index number.
2. public ArrayList<Node> readAnswerForDijkstraFrom(String str);
builds nodes using following format:
key1 weightOfWay1
key2 weightOfWay2
... ...