-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLowestCommonAncestor.java
171 lines (149 loc) · 3.58 KB
/
LowestCommonAncestor.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
162
163
164
165
166
167
168
169
170
171
//Added a branch
public class LowestCommonAncestor {
public Node head;
//Node class to represent Nodes of the binary tree with left, right and parent nodes as well as a key represented by a character
public class Node{
private char key;
public Node left, right, parent;
private int value;
}
//Creates a new binary tree with a head
public void createHead(char key,int value){
Node head = new Node();
head.key = key;
head.left = null;
head.right = null;
head.value = value;
this.head = head;
}
public char returnKey(Node node){
return node.key;
}
public int returnValue(Node node){
return node.value;
}
/*An add node function thats adds a node to the tree based on its
key by recursively calling a private function */
public void addNode(char key, int value){
Node newNode = new Node();
newNode.key = key;
newNode.left = null;
newNode.right = null;
newNode.value = value;
addNode(newNode, head);
}
private void addNode(Node newNode, Node head){
if(newNode.key < head.key){
if(head.left == null){
head.left = newNode;
newNode.parent = head;
}
else{
addNode(newNode, head.left);
}
}
else{
if(head.right == null){
head.right = newNode;
newNode.parent = head;
}
else{
addNode(newNode, head.right);
}
}
}
//A function to return a node given a key
public Node findNode(char key){
if(key == head.key){
return head;
}
else {
return findNode(key, head);
}
}
private Node findNode(char key, Node head){
Node ret = null;
if(head.left != null){
if (head.left.key == key){
return head.left;
}
else{
ret = findNode(key, head.left);
if(ret != null){
return ret;
}
}
}
if(head.right != null){
if(head.right.key == key){
return head.right;
}
else{
ret = findNode(key, head.right);
if(ret != null){
return ret;
}
}
}
return null;
}
public int depth(char key){
int depth = 0;
Node node = findNode(key);
while(node.parent != null){
depth++;
node = node.parent;
}
return depth;
}
//An implementation of the Lowest Common Ancestor Algorithm
public Node lowestCommonAncestor(Node root, Node p, Node q) {
if(root==null)
return null;
if(root==p || root==q)
return root;
Node l = lowestCommonAncestor(root.left, p, q);
Node r = lowestCommonAncestor(root.right, p, q);
if(l!=null&&r!=null){
return root;
}else if(l==null&&r==null){
return null;
}else{
return l==null?r:l;
}
}
public String prettyPrintKeys() {
if (head == null){
return "-null\n";
}
else if (head.left == null && head.right == null){
return "-" + head.key + "\n" + " |-null\n" + " -null\n";
}
return prettyPrintKeys(head,"");
}
private String prettyPrintKeys(Node x,String prefix){
if (x == null){
return prefix + "-null\n";
}
if (x.left != null){
return prefix + "-" + x.key + "\n" + prettyPrintKeys(x.left, prefix + " |") + prettyPrintKeys(x.right, prefix + " ");
}
else if (x.left == null){
return prefix + "-" + x.key + "\n" + prefix + " |" + "-null\n" + prettyPrintKeys(x.right,prefix + " ");
}
return null;
}
public String helloWorld(){
return "HelloWorld";
}
public static void main(String[] args) {
LowestCommonAncestor lca = new LowestCommonAncestor();
lca.createHead('e',0);
lca.addNode('b', 1);
lca.addNode('f', 1);
lca.addNode('a', 1);
lca.addNode('g', 1);
String tree = lca.prettyPrintKeys();
System.out.print(tree);
}
}