Need help converting a complete binary tree from array to a dynamic
I have an array ["a","b","c","null","d","e",null] and I want to convert it to the Node style where each node is linked by node.left and node.right.
I don't have a problem implementing this structure by sorting it, but I do have a problem implementing it by inserting it level order.
Can anyone offer any suggestions.
Not very OOP, and i'd definitely use one of the java collections for this, but nevertheless:
public class ArrayToLinkedList {
static String[] myarray = { "a", "b", "c", null, "d", "e", null };
static Node first;
static class Node {
Node prev;
Node next;
String data;
Node(Node prev, String s) {
if (prev == null)
first = this;
else prev.next = this;
next = null;
data = s;
}
};
public static void main(String[] args) {
Node prev = null;
for (String s : myarray)
prev = new Node(prev, s);
for (Node n = first; n != null; n = n.next)
System.out.println(n.data == null ? "null" : n.data);
}
}