forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInOrderWithoutStackAndRecursion.java
80 lines (72 loc) · 2.61 KB
/
InOrderWithoutStackAndRecursion.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
package com.rampatra.trees;
import com.rampatra.base.BinaryNode;
import com.rampatra.base.BinarySearchTree;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 6/26/15
* @time: 7:23 PM
*/
public class InOrderWithoutStackAndRecursion<E extends Comparable<E>> extends BinarySearchTree<E> {
public void inOrder() {
inOrderWithoutStackAndRecursion(root);
}
/**
* Using Morris Traversal, we can traverse the tree without using stack and
* recursion. The idea of Morris Traversal is based on Threaded Binary Tree.
* In this traversal, we first create links to Inorder successor and print the
* data using these links, and finally revert the changes to restore original tree.
* <p/>
* A binary tree is THREADED by making all right child pointers that would normally
* be null point to the inorder successor of the node (if it exists), and all left
* child pointers that would normally be null point to the inorder predecessor of
* the node.
* <p/>
* PSEUDOCODE:
* 1. Initialize current as root
* 2. While current is not NULL
* If current does not have left child
* a) Print current’s data
* b) Go to right child, i.e., current = current->right
* Else
* a) Make current as right child of the rightmost node in current's left subtree
* b) Go to left child, i.e., current = current->left
*
* @param node
*/
public void inOrderWithoutStackAndRecursion(BinaryNode<E> node) {
if (node == null) return;
BinaryNode<E> curr = node;
while (curr != null) {
// print the leftmost node
if (curr.left == null) {
printValue(curr);
curr = curr.right;
} else { // make current as right child of the rightmost node in current's left subtree
BinaryNode<E> pre = curr.left;
while (pre.right != curr && pre.right != null) {
pre = pre.right;
}
if (pre.right != curr) {
pre.right = curr;
curr = curr.left;
} else {
printValue(curr);
curr = curr.right;
pre.right = null; // revert to the original tree structure
}
}
}
}
public static void main(String[] args) {
InOrderWithoutStackAndRecursion<Integer> bst = new InOrderWithoutStackAndRecursion<>();
bst.put(6);
bst.put(3);
bst.put(5);
bst.put(7);
bst.put(8);
bst.put(9);
bst.inOrder();
}
}