Repository navigation
Expand file tree
/
Copy pathTreeTraversals.java
More file actions
140 lines (127 loc) · 3.98 KB
/
Copy pathTreeTraversals.java
File metadata and controls
140 lines (127 loc) · 3.98 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.interview.tree;
import java.util.Deque;
import java.util.LinkedList;
/**
* Youtube link - https://youtu.be/nzmtCFNae9k
* Youtube link - https://youtu.be/elQcrJrfObg
* Youtube link - https://youtu.be/qT65HltK2uE
* Youtube link - https://youtu.be/ZM-sV9zQPEs
*
* http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/
* http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/
* http://www.geeksforgeeks.org/iterative-preorder-traversal/
*/
public class TreeTraversals {
public void inOrder(Node root){
if(root == null){
return;
}
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
public void preOrder(Node root){
if(root == null){
return;
}
System.out.print(root.data + " ");
preOrder(root.left);
preOrder(root.right);
}
public void postOrder(Node root){
if(root == null){
return;
}
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data + " ");
}
public void inorderItr(Node root){
Deque<Node> stack = new LinkedList<Node>();
Node node = root;
while(true){
if(node != null){
stack.addFirst(node);
node = node.left;
}
else{
if(stack.isEmpty()){
break;
}
node = stack.pollFirst();
System.out.println(node.data);
node = node.right;
}
}
}
public void preOrderItr(Node root){
Deque<Node> stack = new LinkedList<Node>();
stack.addFirst(root);
while(!stack.isEmpty()){
root = stack.pollFirst();
System.out.print(root.data + " ");
if(root.right != null){
stack.addFirst(root.right);
}
if(root.left!= null){
stack.addFirst(root.left);
}
}
}
public void postOrderItr(Node root){
Deque<Node> stack1 = new LinkedList<Node>();
Deque<Node> stack2 = new LinkedList<Node>();
stack1.addFirst(root);
while(!stack1.isEmpty()){
root = stack1.pollFirst();
if(root.left != null){
stack1.addFirst(root.left);
}
if(root.right != null){
stack1.addFirst(root.right);
}
stack2.addFirst(root);
}
while(!stack2.isEmpty()){
System.out.print(stack2.pollFirst().data + " ");
}
}
public void postOrderItrOneStack(Node root){
Node current = root;
Deque<Node> stack = new LinkedList<>();
while(current != null || !stack.isEmpty()){
if(current != null){
stack.addFirst(current);
current = current.left;
}else{
Node temp = stack.peek().right;
if (temp == null) {
temp = stack.poll();
System.out.print(temp.data + " ");
while (!stack.isEmpty() && temp == stack.peek().right) {
temp = stack.poll();
System.out.print(temp.data + " ");
}
} else {
current = temp;
}
}
}
}
public static void main(String args[]){
BinaryTree bt = new BinaryTree();
Node head = null;
head = bt.addNode(10, head);
head = bt.addNode(15, head);
head = bt.addNode(19, head);
head = bt.addNode(17, head);
head = bt.addNode(11, head);
head = bt.addNode(-11, head);
TreeTraversals tt = new TreeTraversals();
tt.postOrder(head);
System.out.println();
tt.postOrderItr(head);
System.out.println();
tt.postOrderItrOneStack(head);
}
}