Exploring Tree Algorithms: Traversal, Search, and Height Calculation

public ArrayList<>> simplePaths(Vertex v1,Vertex v2) {
ArrayList<>>paths = new ArrayList<>>();
ArrayListpathCurr = new ArrayList();
TreeMap visits = new TreeMap();
Iterator itr = adjacencyMap.KeySet().Iterator();
while (itr.HasNext())
visits.Put (itr.Next(), false);
simplePaths(paths,(ArrayList) pathCurr.Clone(),(TreeMap) visits.Clone(),v1,v2);
return paths; }
private void simplePaths(ArrayList<>> paths, ArrayList pathCurr, TreeMap visits, Vertex current, Vertex end) {
if(current.Equals(end)){
pathCurr.Add((String)current);
paths.Add(pathCurr);
return; }
if(visits.Get(current)){
return; }
if(!AdjacencyMap.Get(current).IsEmpty()){
pathCurr.Add((String) current);
visits.Put(current, true);
Iterator neighbors = getNeighbors(current).Iterator();
while(neighbors.HasNext())
simplePaths(paths, (ArrayList) pathCurr.Clone(), (TreeMap) visits.Clone(), neighbors.Next(), end); } }
,>,>,>,>,>


public ArrayListtoArrayDFS(Vertex start) {
result = new ArrayList();
visited = new TreeMap();
Iterator itr = adjacencyMap.KeySet().Iterator();
while (itr.HasNext())
visited.Put(itr.Next(), false);
toArrayDFSAux(start);
return result;
}
public void toArrayDFSAux(Vertex current) {
result.Add(current);
visited.Put(current, true);
TreeMap neighborMap = adjacencyMap.Get(current);
for (Map.Entry entry : neighborMap.EntrySet()) {
Vertex to = entry.GetKey();
if (!Visited.Get(to))
toArrayDFSAux(to);
} }
public ArrayListtoArrayDFSIterative(Vertex start) {
result = new ArrayList();
ALStack st = new ALStack();
visited = new TreeMap();
Vertex current;
Iterator itr = adjacencyMap.KeySet().Iterator();
while (itr.HasNext())
visited.Put(itr.Next(), false);
st.Push(start);
visited.Put(start, true);
while (!St.IsEmpty()) {
Vertex to;
current = st.Peek();
st.Pop();
result.Add(current);
TreeMap neighborMap = adjacencyMap.Get(current);
for (Map.Entry entry : neighborMap.EntrySet()) {
to = entry.GetKey();
if (!Visited.Get(to)) {
visited.Put(to, true);
st.Push(to);
} } }
return result; }
public ArrayListtoArrayBFS(Vertex start) {
result = new ArrayList();
LinkedQueue q = new LinkedQueue();
visited = new TreeMap();
Vertex current;
Iterator itr = adjacencyMap.KeySet().Iterator();
while (itr.HasNext())
visited.Put(itr.Next(), false);
q.Push(start);
visited.Put(start, true);
while (!Q.IsEmpty()) {
Vertex to;
current = q.Peek();
q.Pop();
result.Add(current);
TreeMap neighborMap = adjacencyMap.Get(current);
for (Map.Entry entry : neighborMap.EntrySet()) {
to = entry.GetKey();
if (!Visited.Get(to)) {
visited.Put(to, true);
q.Push(to);

} } }
return result; }


public String toStringInorder() {
String s = toStringInorder(root);
return s; }
public static String toStringInorder(BSTNode t) {
String cadena=””;
if(t!=null)
{
cadena=cadena+toStringInorder(t.

Left

;
cadena=cadena+t.NodeValue+” “;
cadena=cadena+toStringInorder(t.Right);
} else{
cadena=””;
} return cadena; }
public String toStringIterativeInorder() {
String s = toStringIterativeInorder(root);
return s; }
public static String toStringIterativeInorder(BSTNode t) {
String resultado=””;
Stack<>> nodes = new ALStack<>>();
while (!Nodes.IsEmpty() || null != t) {
if (null != t) {
nodes.Push(t);
t = t.Left;
} else {
t = nodes.Pop();
resultado += t.NodeValue+” “;
t = t.Right; } }
return resultado; }
public String toStringPreorder() {
String s = toStringPreorder(root);
return s; }
public String toStringPreorder(BSTNode t) {
String cadena=””;
if(t!=null){
cadena=cadena+t.NodeValue+” “;
cadena=cadena+toStringPreorder(t.Left);
cadena=cadena+toStringPreorder(t.Right); }
return cadena; }
public String toStringIterativePreorder() {
String s = toStringIterativePreorder(root);
return s; }
public String toStringIterativePreorder(BSTNode root) {
String resultado=””;
Stack<>> nodes = new ALStack<>>();
nodes.Push(root);
BSTNode currentNode;
while (!Nodes.IsEmpty()) {
currentNode = nodes.Pop();
BSTNode right = currentNode.Right;
if (right != null) {
nodes.Push(right); }
BSTNode left = currentNode.Left;
if (left != null) {
nodes.Push(left); }
resultado += currentNode.NodeValue+” “; }
return resultado; }


public String toStringPostorder(BSTNode t) {
String cadena=””;
if(t!=null){
cadena=cadena+toStringPostorder(t.Left);
cadena=cadena+toStringPostorder(t.Right);
cadena=cadena+t.NodeValue+” “;
} else{
cadena=””; }
return cadena;
}
public String toStringIterativePostorder(BSTNode t) {
String resultado = “”;
Stack<>> nodes = new ALStack<>>();
nodes.Push(t);
System.Out.Println(t.NodeValue);
while (nodes.Size() > 0) {
t = nodes.Pop();
if (t.Left == null && t.Right == null) {
resultado += t.NodeValue + ” “;
} else {
nodes.Push(t);
System.Out.Println(t.NodeValue);
if (t.Right != null){
nodes.Push(t.Right);
System.Out.Println(t.Right.NodeValue); }
if (t.Left != null){
nodes.Push(t.Left);
System.Out.Println(t.Left.NodeValue);
} } }
return resultado;
}
public String toStringIterativePostorder() {
String s = toStringPostorder(root);
return s; }
public int pathHeight(T x) {
BSTNode t = root;
int cont = 0, orderValue;
boolean esta=false;
while (t != null && esta == false) {
orderValue = ((Comparable) x).CompareTo(t.NodeValue);
if (orderValue < 0)=””>
t = t.Left;
else if (orderValue > 0)
t = t.Right;
else
esta = true;
cont++; }
return (cont-1); }


 private static class AVLNode{
public T nodeValue;
public AVLNode left, right;
public int height;

public AVLNode(T item)
{ nodeValue = item; left = null; right = null; height = 0; }}
private static AVLNodesingleRotateRight(AVLNode p)
{ AVLNode lc = p.Left;
p.Left = lc.Right;
lc.Right = p;
p.Height = max( height( p.Left ), height( p.Right ) ) + 1;
lc.Height = max( height( lc.Left ), lc.Height ) + 1;
return lc; }

private static AVLNode singleRotateLeft(AVLNode p){
AVLNode rc = p.Right; // rc = right child
p.Right = rc.Left; // lc = left child
rc.Left = p;
p.Height = max(height(p.Left), height(p.Right)) + 1;
rc.Height = max(height(rc.Right), rc.Height) + 1;
return rc;}
private staticAVLNodedoubleRotateRight(AVLNode p){
p.Left = singleRotateLeft(p.Left);
return singleRotateRight(p);
}//Hacer lo mismo pero para doubleRotateLeft


public T findMin() {
return findMin(root).NodeValue; }
private BSTNodefindMin(BSTNode t) {
if (t == null) {
return null; }
else if (t.Left == null)
  return t;
return findMin(t.Left); }
public T findMinIterative() {
return findMinIterative(root).NodeValue; }
private BSTNodefindMinIterative(BSTNode t) {
if (t==null)
{ return t;

} else {
while (t.Left !=null)
t=t.Left;
return t; } }
public T findMax() {
return findMax(root).NodeValue; }
private BSTNodefindMax(BSTNode t) {
if (t == null) { return null; }
else if (t.Right == null)
return t;
return findMax(t.Right); }
public T findMaxIterative() {
return findMaxIterative(root).NodeValue; }
private BSTNodefindMaxIterative(BSTNode t) {
if (t==null)
{ return t; }
else {
while (t.Right !=null)
t=t.Right;
return t; } }
public int numberOfLeaves() {
return numberOfLeaves(root); }
private int numberOfLeaves(BSTNode t){
if (t== null)
return 0;
else if (t.Left == null && t.Right == null)
return 1; else
return numberOfLeaves(t.Left) + numberOfLeaves(t.Right); }
private int height(BSTNode t) {
if (t == null)
return 0; else
return 1 + max(height(t.Left), height(t.Right)); }
private int max(int x, int y) {
if (x >= y)
return x;
else
return y; }
public int height() {
return height(root)-1; }

,>,>,>,>,>,>,>,>,>