snippetcsharpMinor
Convert binary tree to doubly linked list
Viewed 0 times
convertdoublybinarylistlinkedtree
Problem
The code works and I get the results I want. Please comment about complexity, a shorter way of implementing the solution, and advice for better unit tests.
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
public class BinaryTreeToList
{
public BinaryTreeToList()
{
}
public DoubleLinkedListNode ConvertTreeToDoubleLinkedListNode(TreeNode root)
{
if (root == null)
{
return null;
}
DoubleLinkedListNode res = new DoubleLinkedListNode();
res.Index = root.Index;
if (root.Left != null)
{
var newNode = ConvertTreeToDoubleLinkedListNode(root.Left);
newNode.Previous = res;
res.Next = newNode;
}
if (root.Right != null)
{
if (res.Next == null)
{
var newNode = ConvertTreeToDoubleLinkedListNode(root.Right);
newNode.Previous = res;
res.Next = newNode;
}
else if (res.Next.Next == null)
{
var newNode = ConvertTreeToDoubleLinkedListNode(root.Right);
newNode.Previous = res.Next;
res.Next.Next = newNode;
}
else
{
DoubleLinkedListNode temp = res;
while (temp.Next != null)
{
temp = temp.Next;
}
var newNode = ConvertTreeToDoubleLinkedListNode(root.Right);
newNode.Previous = temp;
temp.Next = newNode;
}
}
return res;
}
}
public class DoubleLinkedListNode
{
public int Index { get;
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
public class BinaryTreeToList
{
public BinaryTreeToList()
{
}
public DoubleLinkedListNode ConvertTreeToDoubleLinkedListNode(TreeNode root)
{
if (root == null)
{
return null;
}
DoubleLinkedListNode res = new DoubleLinkedListNode();
res.Index = root.Index;
if (root.Left != null)
{
var newNode = ConvertTreeToDoubleLinkedListNode(root.Left);
newNode.Previous = res;
res.Next = newNode;
}
if (root.Right != null)
{
if (res.Next == null)
{
var newNode = ConvertTreeToDoubleLinkedListNode(root.Right);
newNode.Previous = res;
res.Next = newNode;
}
else if (res.Next.Next == null)
{
var newNode = ConvertTreeToDoubleLinkedListNode(root.Right);
newNode.Previous = res.Next;
res.Next.Next = newNode;
}
else
{
DoubleLinkedListNode temp = res;
while (temp.Next != null)
{
temp = temp.Next;
}
var newNode = ConvertTreeToDoubleLinkedListNode(root.Right);
newNode.Previous = temp;
temp.Next = newNode;
}
}
return res;
}
}
public class DoubleLinkedListNode
{
public int Index { get;
Solution
Naming
General
-
by extracting the search for the last
-
if you use a guard claus for
-
the method should be static, because it doesn't depend on any internal state of the class itself. This makes the
Refactoring
After all for above is applied the refactored code looks like
- I personally prefer a class named
Convertfor such a task. This would also be more natural because the NET provided converting methods live in aConvertclass too.
- to be consistent with the NET naming I also would prefer the method be named
ToDoubleLinkedListNode()
- instead of shortening the name of the node to be returned to
res, name itconvertedNode
- you should name the node which is returned by the recursive calls
leftNodeorrightNode.
General
-
by extracting the search for the last
Next node which is not null to a separate method, the if..else if..else can be removed.-
if you use a guard claus for
root.Right == null you save horizontal spacing.-
the method should be static, because it doesn't depend on any internal state of the class itself. This makes the
constructor obsolete. Refactoring
After all for above is applied the refactored code looks like
public class Convert
{
public DoubleLinkedListNode ToDoubleLinkedListNode(TreeNode root)
{
if (root == null) { return null; }
DoubleLinkedListNode convertedNode = new DoubleLinkedListNode { Index = root.Index };
if (root.Left != null)
{
var leftNode = ToDoubleLinkedListNode(root.Left);
leftNode.Previous = convertedNode;
convertedNode.Next = leftNode;
}
if (root.Right == null) { return convertedNode; }
var rightNode = ToDoubleLinkedListNode(root.Right);
DoubleLinkedListNode lastNextNode = FindLastNextNode(convertedNode);
rightNode.Previous = lastNextNode;
lastNextNode.Next = rightNode;
return convertedNode;
}
private static DoubleLinkedListNode FindLastNextNode(DoubleLinkedListNode node)
{
while (node.Next != null)
{
node = node.Next;
}
return node;
}
}Code Snippets
public class Convert
{
public DoubleLinkedListNode ToDoubleLinkedListNode(TreeNode root)
{
if (root == null) { return null; }
DoubleLinkedListNode convertedNode = new DoubleLinkedListNode { Index = root.Index };
if (root.Left != null)
{
var leftNode = ToDoubleLinkedListNode(root.Left);
leftNode.Previous = convertedNode;
convertedNode.Next = leftNode;
}
if (root.Right == null) { return convertedNode; }
var rightNode = ToDoubleLinkedListNode(root.Right);
DoubleLinkedListNode lastNextNode = FindLastNextNode(convertedNode);
rightNode.Previous = lastNextNode;
lastNextNode.Next = rightNode;
return convertedNode;
}
private static DoubleLinkedListNode FindLastNextNode(DoubleLinkedListNode node)
{
while (node.Next != null)
{
node = node.Next;
}
return node;
}
}Context
StackExchange Code Review Q#74502, answer score: 2
Revisions (0)
No revisions yet.