-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeTraversal.java
94 lines (80 loc) · 2.58 KB
/
TreeTraversal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package TreeTraversalandRecursion;
import java.util.LinkedList;
import java.util.Queue;
public class TreeTraversal {
private static Node root;
public static void main(String [] args)
{
Tree tree = new Tree();
root = tree.insert(root, 10);
root = tree.insert(root, 9);
root = tree.insert(root, 11);
root = tree.insert(root, 10);
root = tree.insert(root, 8);
root = tree.insert(root, 9);
root = tree.insert(root, 12);
//BFS should be 10-9-11-8-9-10-12
//DFS should be 10-9-8-9-11-10-12
System.out.print("Pre-order : ");
tree.preOrder(root);
System.out.println();
System.out.print("Post-order : ");
tree.postOrder(root);
System.out.println();
System.out.print("In-order : ");
tree.inOrder(root);
System.out.println();
System.out.print("BFS: ");
BreadthFirstSearch(root);
System.out.println();
System.out.print("DFS: ");
DepthFirstSearch(root);
}
public static void BreadthFirstSearch(Node root)
{
//use queue to ensure each node's children are searched for in order
//add root to queue
//while the queue isn't empty, continue
//remove from front of the queue
//for each child of this node
//if unvisited, add to queue, mark as visited
if(root==null)
return;
Queue <Node> queue = new LinkedList <Node> ();
queue.add(root);
while(queue.peek()!=null)
{
//if(queue.peek()==null)
//return;
Node tempNode = (Node)queue.remove();
//to show traversal
System.out.print(tempNode.value+" ");
//a tree, not a graph, so nodes cannot be visited from non-parents
//if it was a graph,
//a node array/list would need to be
//made and checked for visitation
if(tempNode.getLeft()!=null)
queue.add(tempNode.getLeft());
if(tempNode.getRight()!=null)
queue.add(tempNode.getRight());
}
}
public static void DepthFirstSearch(Node root)
{
//no need for queue.
//search using recursion to ensure process continues down path
//if root is null, return (at end of tree)
//otherwise, mark root as visited, then
//for each child, if the child is unvisited,
//then call this method for that child
if(root==null)
return;
//to show traversal
System.out.print(root.value+" ");
//a tree, not a graph, so nodes cannot be visited from non-parents
//if it was a graph,
//a node field would need to be checked and toggled for visitation
DepthFirstSearch(root.getLeft());
DepthFirstSearch(root.getRight());
}
}