-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdistancebw2nodes.java
161 lines (130 loc) · 3.39 KB
/
distancebw2nodes.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
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
159
160
161
package binnarytree;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
public class distancebw2nodes {
public class BinaryTree {
private class Node {
int data;
Node left;
Node right;
Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
private Node root = null;
private int size = 0;
BinaryTree(String s) {
Scanner scn = new Scanner(System.in);
this.root = inputfromstring(s, null);
}
private Node inputfromstring(String s, Node parent) {
if (s.length() == 0) {
return null;
}
Node node = new Node(0, null, null);
int i = 0;
while (i < s.length() && s.charAt(i) != '(') {
i++;
}
int start = i + 1;
String no = s.substring(0, i);
node.data = Integer.parseInt(no);
if (i < s.length()) {
Stack<Character> stack = new Stack<>();
stack.push(s.charAt(i));
i++;
while (!stack.isEmpty() && i < s.length()) {
if (s.charAt(i) == '(') {
stack.push(s.charAt(i));
} else if (s.charAt(i) == ')') {
stack.pop();
}
i++;
}
int end = i;
String leftsub = s.substring(start, end - 1);
node.left = inputfromstring(leftsub, node);
if (end + 1 < s.length() - 1) {
String rightsub = s.substring(end + 1, s.length() - 1);
node.right = inputfromstring(rightsub, node);
}
}
return node;
}
public void display() {
display(this.root);
}
private void display(Node parent) {
String str = "";
if (parent.left != null) {
str += parent.left.data + " => ";
} else {
str += "End => ";
}
str += parent.data;
if (parent.right != null) {
str += " <= " + parent.right.data;
} else {
str += " <= End";
}
System.out.println(str);
if (parent.left != null) {
display(parent.left);
}
if (parent.right != null) {
display(parent.right);
}
}
public ArrayList<Integer> roottonode(Node parent, int n) {
if (parent == null) {
return new ArrayList<Integer>();
}
if (parent.data == n) {
ArrayList<Integer> list = new ArrayList<>();
list.add(parent.data);
return list;
}
ArrayList<Integer> listleft = roottonode(parent.left, n);
ArrayList<Integer> listright = roottonode(parent.right, n);
if (listleft.size() > 0) {
listleft.add(parent.data);
return listleft;
}
if (listright.size() > 0) {
listright.add(parent.data);
return listright;
}
return new ArrayList<Integer>();
}
public int distance(int n1, int n2) {
return distance(this.root, n1, n2);
}
private int distance(Node parent, int n1, int n2) {
ArrayList<Integer> listn1 = roottonode(parent, n1);
ArrayList<Integer> listn2 = roottonode(parent, n2);
int i = listn1.size() - 1, j = listn2.size() - 1;
while (i >= 0 && j >= 0 && listn1.get(i) == listn2.get(j)) {
i--;
j--;
}
i++;
j++;
return i + j;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// 10 true 20 true 40 false false true 50 false false true 30 true 60 false
// false true 73 false false
distancebw2nodes m = new distancebw2nodes();
// BinaryTree tree = m.new BinaryTree("1(3(2)(1(1)))(-1(4(1)(2))(5(6)))");
// BinaryTree tree = m.new BinaryTree("1(3(2)(4))(6)");
BinaryTree tree = m.new BinaryTree("1(2)");
tree.display();
System.out.println();
System.out.println(tree.distance(1, 2));
}
}