-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryEulerTree.java
More file actions
158 lines (138 loc) · 5.12 KB
/
Copy pathBinaryEulerTree.java
File metadata and controls
158 lines (138 loc) · 5.12 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.util.ArrayList;
import java.util.Stack;
public class EulerTree {
public Node root;
public ArrayList<Node> euler = new ArrayList();
public ArrayList<Integer> levels = new ArrayList();
public EulerTree(Node node){
this.root = node;
}
public void EulerTour(){
Stack<Node> stack = new Stack<>();
Node node = root;
stack.push(node);
node.hit = true;
euler.add(node);
levels.add(node.level);
node.occurrence = euler.size()-1;
while (stack.size() != 0){
if (node.left != null && !node.left.hit){
node = node.left;
stack.push(node);
node.hit = true;
euler.add(node);
node.occurrence = euler.size()-1;
levels.add(node.level);
}else if (node.right != null && !node.right.hit){
node = node.right;
stack.push(node);
node.hit = true;
euler.add(node);
node.occurrence = euler.size()-1;
levels.add(node.level);
}else {
stack.pop();
if (stack.size() != 0) {
node = stack.peek();
euler.add(stack.peek());
node.occurrence = euler.size()-1;
levels.add(node.level);
}
}
}
}
public void print(int func){
// func == 1 - print nodeValues
//func == 2 - print nodeOccurrences
// func == 3 - print nodeLevels
if (func == 1) {
for (int i = 0; i < euler.size(); i++) {
System.out.print(euler.get(i).value + " ");
}
}else if (func == 2){
for (int i = 0; i < euler.size(); i++) {
System.out.print(euler.get(i).occurrence + " ");
}
}else if (func == 3){
for (int i = 0; i < euler.size(); i++) {
System.out.print(euler.get(i).level + " ");
}
}
System.out.println();
}
public Node LCA(Node node1, Node node2){
int min;
if (node1.occurrence < node2.occurrence){
min = node1.occurrence;
for (int i = node1.occurrence+1; i < node2.occurrence; i++){
if (min > euler.get(i).level){
min = euler.get(i).level;
}
}
}else {
min = node2.occurrence;
for (int i = node2.occurrence+1; i < node1.occurrence; i++){
if (min > euler.get(i).level){
min = euler.get(i).level;
}
}
}
return euler.get(min);
}
public static void main(String[] args) {
Node root = new Node(null, 1);
root.left = new Node(root, 2);
root.right = new Node(root, 3);
root.left.left = new Node(root.left, 4);
root.left.right = new Node(root.left, 5);
root.right.left = new Node(root.right, 6);
root.right.right = new Node(root.right, 7);
// Illustration 1
// / \
// / \
// 2 3
// / \ / \
// 4 5 6 7
EulerTree tree = new EulerTree(root);
tree.EulerTour();
System.out.println("values");
tree.print(1);
System.out.println("occurrences");
tree.print(2);
System.out.println("levels");
tree.print(3);
System.out.println(tree.root.left.left.occurrence + " is index occurrence of 4");
System.out.println(tree.root.left.right.occurrence + " is index occurrence of 5");
System.out.println(tree.LCA(root.left.left, root.left.right).value == 2); // 4, 5 -> 2
System.out.println(tree.root.left.left.occurrence + " is index occurrence of 4");
System.out.println(tree.root.right.right.occurrence + " is index occurrence of 7");
System.out.println(tree.LCA(root.left.left, root.right.right).value == 1); // 4, 7 -> 1
System.out.println(tree.root.left.occurrence + " is index occurrence of 2");
System.out.println(tree.root.right.left.occurrence + " is index occurrence of 6");
System.out.println(tree.LCA(root.left, root.right.left).value == 1); // 2, 6 -> 1
}
}
class Node{
public int value;
public Node left; // left child of a node
public Node right;// right child of a node
public Node parent;// parent of a node
public int level; // level or height of a node
public boolean hit; // predicate whether a node is visited or not
public int occurrence; // efficient when it is need to get the index of a node from an array after euler tour
public Node(Node parent, int data){
this.value = data;
left = right = null;
this.parent = parent;
hit = false;
level = getLevel(parent);
}
public int getLevel(Node node){
int height = 0;
while (node != null){
height ++;
node = node.parent;
}
return height;
}
}