From 9974584f09737c47afc2a8e581bf5a6d3e4da9bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=A7=88=ED=81=AC=28=EC=B1=84=EC=A2=85=EC=9A=B4=29?= Date: Sat, 21 May 2016 00:45:31 +0900 Subject: [PATCH] question 5 --- .../algorithm/question5/FindSameAncestor.java | 69 ++++++++++++++ .../java/com/algorithm/question5/Node.java | 11 +++ .../question5/mark/Test_findSameAncestor.java | 90 +++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 src/main/java/com/algorithm/question5/FindSameAncestor.java create mode 100644 src/main/java/com/algorithm/question5/Node.java create mode 100644 src/test/java/com/algorithm/question5/mark/Test_findSameAncestor.java diff --git a/src/main/java/com/algorithm/question5/FindSameAncestor.java b/src/main/java/com/algorithm/question5/FindSameAncestor.java new file mode 100644 index 0000000..c920186 --- /dev/null +++ b/src/main/java/com/algorithm/question5/FindSameAncestor.java @@ -0,0 +1,69 @@ +package com.algorithm.question5; + +public class FindSameAncestor { + public Node buildBinaryTree(int... datas) { + if(datas.length < 7) { + return null; + } + + Node head = new Node(datas[0]); + head.left = new Node(datas[1]); + head.right = new Node(datas[2]); + head.left.left = new Node(datas[3]); + head.left.right = new Node(datas[4]); + head.right.left = new Node(datas[5]); + head.right.right = new Node(datas[6]); + + return head; + } + + public Node findSameAncestorInBinaryTree(Node head, Node node1, Node node2) { + if(head == null) { + return null; + } + + boolean left = isNodeInTree(head.left, node1, node2); + boolean right = isNodeInTree(head.right, node1, node2); + + if(left && right) { + return head; + } else if(left && !right) { + return findSameAncestorInBinaryTree(head.left, node1, node2); + } else if(!left && right) { + return findSameAncestorInBinaryTree(head.right, node1, node2); + } + + return null; + } + + private boolean isNodeInTree(Node head, Node node1, Node node2) { + if(head == null) { + return false; + } + + if(head.data == node1.data || head.data == node2.data) { + return true; + } + + return isNodeInTree(head.left, node1, node2) || isNodeInTree(head.right, node1, node2); + } + + public Node findSameAncestorInSearchTree(Node head, Node node1, Node node2) { + if(head == null) { + return null; + } + + int high = node1.data > node2.data ? node1.data : node2.data; + int low = node1.data == high ? node2.data : node1.data; + + if(head.data > low && head.data < high) { + return head; + } else if(head.data >= high){ + return findSameAncestorInSearchTree(head.left, node1, node2); + } else if(head.data <= low) { + return findSameAncestorInSearchTree(head.right, node1, node2); + } + + return null; + } +} diff --git a/src/main/java/com/algorithm/question5/Node.java b/src/main/java/com/algorithm/question5/Node.java new file mode 100644 index 0000000..5f3ea73 --- /dev/null +++ b/src/main/java/com/algorithm/question5/Node.java @@ -0,0 +1,11 @@ +package com.algorithm.question5; + +public class Node { + public int data; + public Node left; + public Node right; + + public Node(int data) { + this.data = data; + } +} diff --git a/src/test/java/com/algorithm/question5/mark/Test_findSameAncestor.java b/src/test/java/com/algorithm/question5/mark/Test_findSameAncestor.java new file mode 100644 index 0000000..c35e83d --- /dev/null +++ b/src/test/java/com/algorithm/question5/mark/Test_findSameAncestor.java @@ -0,0 +1,90 @@ +package com.algorithm.question5.mark; + +import org.junit.Assert; +import org.junit.Test; + +import com.algorithm.question5.FindSameAncestor; +import com.algorithm.question5.Node; + +import static org.hamcrest.core.Is.is; + +public class Test_findSameAncestor { + private FindSameAncestor findSameAncestor = new FindSameAncestor(); + + @Test + public void findSameAncestorInBinaryTree_head_is_null() { + // setup + Node head = null; + + // execute + Node result = findSameAncestor.findSameAncestorInBinaryTree(head, new Node(4), new Node(5)); + + // verify + Assert.assertEquals(null, result); + } + + @Test + public void findSameAncestorInBinaryTree_only_head_is() { + // setup + Node head = new Node(1); + + // execute + Node result = findSameAncestor.findSameAncestorInBinaryTree(head, new Node(4), new Node(5)); + + // verify + Assert.assertEquals(null, result); + } + + @Test + public void findSameAncestorInBinaryTree_example() { + // setup + Node head = findSameAncestor.buildBinaryTree(1,2,3,4,5,6,7); + + // execute + Node result1 = findSameAncestor.findSameAncestorInBinaryTree(head, new Node(4), new Node(5)); + Node result2 = findSameAncestor.findSameAncestorInBinaryTree(head, new Node(4), new Node(6)); + + // verify + Assert.assertThat(result1.data, is(2)); + Assert.assertThat(result2.data, is(1)); + } + + @Test + public void findSameAncestorInSearchTree_head_is_null() { + // setup + Node head = null; + + // execute + Node result1 = findSameAncestor.findSameAncestorInSearchTree(head, new Node(4), new Node(5)); + + // verify + Assert.assertEquals(null, result1); + } + + @Test + public void findSameAncestorInSearchTree_only_head_is() { + // setup + Node head = new Node(1); + + // execute + Node result1 = findSameAncestor.findSameAncestorInSearchTree(head, new Node(4), new Node(5)); + + // verify + Assert.assertEquals(null, result1); + } + + @Test + public void findSameAncestorInSearchTree_example() { + // setup + Node head = findSameAncestor.buildBinaryTree(4,2,6,1,3,5,7); + + // execute + Node result1 = findSameAncestor.findSameAncestorInSearchTree(head, new Node(1), new Node(3)); + Node result2 = findSameAncestor.findSameAncestorInSearchTree(head, new Node(1), new Node(7)); + + // verify + Assert.assertThat(result1.data, is(2)); + Assert.assertThat(result2.data, is(4)); + } + +}