Last active
October 15, 2016 04:42
-
-
Save vnprc/20185aad1428f601ab01107ec81b6fb2 to your computer and use it in GitHub Desktop.
Given a binary tree implemented using an array, find the nearest neighbor to the right of any given node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FindRightNeighborArrayTree { | |
final Integer[] tree = new Integer[64]; | |
public void insert(int value) { | |
if (tree[1] == null) { | |
tree[1] = value; | |
return; | |
} | |
//set current to root node | |
Integer currentLocation = 1; | |
Integer currentValue = tree[currentLocation]; | |
while (true) { | |
Integer childLocation; | |
if (value < currentValue) { | |
childLocation = currentLocation * 2; | |
} else if (value > currentValue) { | |
childLocation = currentLocation * 2 + 1; | |
} else { | |
//ignore duplicate values | |
return; | |
} | |
if (tree[childLocation] == null) { | |
tree[childLocation] = value; | |
return; | |
} else { | |
currentValue = tree[childLocation]; | |
currentLocation = childLocation; | |
} | |
} | |
} | |
public Integer findValue(int value) { | |
return findValue(value, 1); | |
} | |
private Integer findValue(int value, int position) { | |
if (tree[position] == null) { | |
return null; | |
} | |
if (value < tree[position]) { | |
return findValue(value, position * 2); | |
} else if (value > tree[position]) { | |
return findValue(value, position * 2 + 1); | |
} else { | |
return position; | |
} | |
} | |
public Integer findNeighborToTheRight(int value) { | |
int startPosition = findValue(value); | |
//if start is a power of 2, this is the right most node on this level | |
if (isPowerOfTwo(startPosition + 1)) { | |
return null; | |
} | |
int current = startPosition + 1; | |
while (!isPowerOfTwo(current)) { | |
if (tree[current] != null) { | |
return tree[current]; | |
} | |
current++; | |
} | |
return null; | |
} | |
private boolean isPowerOfTwo(int num) { | |
return (num & (num - 1)) == 0; | |
} | |
public static void main(String args[]) { | |
FindRightNeighborArrayTree tree = new FindRightNeighborArrayTree(); | |
tree.insert(10); | |
tree.insert(5); | |
tree.insert(3); | |
tree.insert(4); | |
tree.insert(1); | |
tree.insert(2); | |
tree.insert(0); | |
tree.insert(7); | |
tree.insert(8); | |
tree.insert(9); | |
tree.insert(6); | |
tree.insert(15); | |
tree.insert(12); | |
tree.insert(11); | |
tree.insert(13); | |
tree.insert(14); | |
tree.insert(18); | |
tree.insert(16); | |
tree.insert(17); | |
tree.insert(20); | |
tree.insert(19); | |
// 10 | |
// / \ | |
// / \ | |
// / \ | |
// / \ | |
// 5 15 | |
// / \ / \ | |
// / \ / \ | |
// 3 7 12 18 | |
// / \ / \ / \ / \ | |
// 1 4 6 8 11 13 16 20 | |
// / \ \ \ \ / | |
// 0 2 9 14 17 19 | |
for (int i = 0; i < 21; i++) { | |
System.out.println("Node to the right of " + i + " is: " + | |
tree.findNeighborToTheRight(i)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment