patternjavaMinor
Converting Binary Tree to LinkedLists
Viewed 0 times
convertingbinarylinkedliststree
Problem
The problem statement is :
Given a binary tree, design an algorithm which creates a linked list of all the
nodes at each depth (e.g., if you have a tree with depth D, you'll have D linked
lists).
My Algorithm is to use 2 Queues and BFS to accomplish this. The code is a bit involved and I've tried to make it as simple and interesting as possible. You can easily follow it reading the comments.
```
//LinkListNode which will be used to create linkedlists
private static class LinkListNode{
T data;
LinkListNode next;
public LinkListNode(T data){
this.data = data;
}
}
//Linked list made up of LinkListNodes
static class LinkList{
private LinkListNode head;
public void add(T toAdd){
if(head==null){
head = new LinkListNode(toAdd);
}else{
LinkListNode temp = head;
while(temp.next!=null){
temp = temp.next;
}
temp.next = new LinkListNode(toAdd);
}
}
//return true if the list is empty
public boolean isEmpty(){
return head==null;
}
//Pulls out the head Node
//i.e. first Node in the list(Like functionality
//of Queue)
public T poll(){
T data = head.data;
head = head.next;
return data;
}
//Prints out the LinkList
public void printList(){
LinkListNode temp = head;
if(head==null){
System.out.println("Null List");
return;
}
while(temp.next!=null){
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println(temp.data);
}
}
//used to create LinkedListCollector
static class LinkListCollectorNode{
LinkList headList;
LinkListCollectorNode nextCollector;
public LinkListCollectorNode(){
headList = new LinkList();
}
}
//LinkList collector, which will store multiple linked lists
//It can be called linkedlist of linkedlists
static class
Given a binary tree, design an algorithm which creates a linked list of all the
nodes at each depth (e.g., if you have a tree with depth D, you'll have D linked
lists).
My Algorithm is to use 2 Queues and BFS to accomplish this. The code is a bit involved and I've tried to make it as simple and interesting as possible. You can easily follow it reading the comments.
```
//LinkListNode which will be used to create linkedlists
private static class LinkListNode{
T data;
LinkListNode next;
public LinkListNode(T data){
this.data = data;
}
}
//Linked list made up of LinkListNodes
static class LinkList{
private LinkListNode head;
public void add(T toAdd){
if(head==null){
head = new LinkListNode(toAdd);
}else{
LinkListNode temp = head;
while(temp.next!=null){
temp = temp.next;
}
temp.next = new LinkListNode(toAdd);
}
}
//return true if the list is empty
public boolean isEmpty(){
return head==null;
}
//Pulls out the head Node
//i.e. first Node in the list(Like functionality
//of Queue)
public T poll(){
T data = head.data;
head = head.next;
return data;
}
//Prints out the LinkList
public void printList(){
LinkListNode temp = head;
if(head==null){
System.out.println("Null List");
return;
}
while(temp.next!=null){
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println(temp.data);
}
}
//used to create LinkedListCollector
static class LinkListCollectorNode{
LinkList headList;
LinkListCollectorNode nextCollector;
public LinkListCollectorNode(){
headList = new LinkList();
}
}
//LinkList collector, which will store multiple linked lists
//It can be called linkedlist of linkedlists
static class
Solution
-
The second queue is not necessary. Since the queue contains only legitimate nodes, you may use
-
By
The second queue is not necessary. Since the queue contains only legitimate nodes, you may use
null to signal end-of-level condition:queue.add(root);
queue.add(null);
while (!queue.empty()) {
TreeNode curr = queue.poll();
if (curr != null) {
myCollector.add(curr.data);
if (curr.left != null)
queue.add(curr.left);
if (curr.right != null)
queue.add(curr.right);
} else {
queue.add(null);
myCollector.nextCollector();
}
}-
By
poll you mean pull, right?Code Snippets
queue.add(root);
queue.add(null);
while (!queue.empty()) {
TreeNode curr = queue.poll();
if (curr != null) {
myCollector.add(curr.data);
if (curr.left != null)
queue.add(curr.left);
if (curr.right != null)
queue.add(curr.right);
} else {
queue.add(null);
myCollector.nextCollector();
}
}Context
StackExchange Code Review Q#117097, answer score: 2
Revisions (0)
No revisions yet.