forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeastCommonAncestorInBST.java
68 lines (60 loc) · 2.04 KB
/
LeastCommonAncestorInBST.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
package com.rampatra.trees;
import com.rampatra.base.BinaryNode;
import com.rampatra.base.BinarySearchTree;
import java.util.NoSuchElementException;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 6/26/15
* @time: 7:38 PM
*/
public class LeastCommonAncestorInBST {
public void leastCommonAncestor() {
/*int value1, value2;
Scanner in = new Scanner(System.in);
out.println("Enter value 1: ");
value1 = (E) Integer.valueOf(in.nextLine());
out.println("Enter value 1: ");
value2 = (E) in.nextLine();
out.println("LCA of " + value1 + " and " + value2 + " is: " + leastCommonAncestor(root, value1, value2).value);*/
}
/**
* Determines the LCA for a BST
* <p/>
* DEFINITION OF LCA:
* Let T be a rooted tree. The lowest
* common ancestor between two nodes n1 and
* n2 is defined as the lowest node in T that has
* both n1 and n2 as descendants (where we allow
* a node to be a descendant of itself).
*
* @param node
* @param value1
* @param value2
* @param <E>
* @return
*/
public static <E extends Comparable<E>> BinaryNode<E> leastCommonAncestor(BinaryNode<E> node, E value1, E value2) {
if (node == null || value1 == null || value2 == null || value1.compareTo(value2) > 0) {
throw new NoSuchElementException();
}
if (value1.compareTo(node.value) <= 0 && value2.compareTo(node.value) >= 0) {
return node;
} else if (value1.compareTo(node.value) > 0 && value2.compareTo(node.value) > 0) {
return leastCommonAncestor(node.right, value1, value2);
} else {
return leastCommonAncestor(node.left, value1, value2);
}
}
public static void main(String[] args) {
BinarySearchTree<Integer> bst = new BinarySearchTree<>();
bst.put(6);
bst.put(3);
bst.put(5);
bst.put(7);
bst.put(8);
bst.put(9);
System.out.println(leastCommonAncestor(bst.root, 5, 6).value);
}
}