0
バイナリ検索ツリーの兄弟を接続しようとしています。ロジックはConnect()メソッドで見つけることができます。私の質問は、それを行うためのより良い方法はありますか?私は2つのキューを使用してロジックを実装することで過度な作業をしていますか?バイナリ検索ツリーで兄弟を接続する
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;
namespace SampleCSParallel
{
class Tree
{
public Tree left = null;
public Tree right = null;
public Tree sibling = null;
public int _data;
Tree(int data)
{
_data = data;
left = null;
right = null;
}
public Tree Left
{
get
{
return this.left;
}
}
public Tree Right
{
get
{
return this.right;
}
}
public Tree AddNode(Tree node, int data)
{
if (node == null)
{
node = new Tree(data);
return node;
}
else if (node._data <= data)
{
node.left = AddNode(node.left, data);
}
else if (node._data > data)
{
node.right = AddNode(node.right, data);
}
return node;
}
public static Tree CreateTree(Tree node, int depth, int start)
{
if (node == null)
node = new Tree(start);
if (depth > 1)
{
node.left = CreateTree(node.left, depth - 1, start + 1);
node.right = CreateTree(node.right, depth - 1, start + 1);
}
return node;
}
}
class Program
{
static void Main(string[] args)
{
//Tree node = null;
Tree tr = Tree.CreateTree(null, 4, 1);
Stopwatch sw = Stopwatch.StartNew();
int total = WalkTree(tr);
TimeSpan ts = sw.Elapsed;
Console.WriteLine("in {0} sec", ts.Seconds);
Console.WriteLine("total:{0}", total);
connect(tr);
Console.ReadLine();
}
static void connect(Tree root)
{
Queue<Tree> nodeQueue = new Queue<Tree>();
nodeQueue.Enqueue(root);
Console.WriteLine(root._data);
connectSiblings(nodeQueue);
}
static void connectSiblings(Queue<Tree> nodeQueue)
{
Queue<Tree> childrenQueue = new Queue<Tree>();
StringBuilder MsgStr = new StringBuilder();
bool done = false;
while (!done)
{
while (nodeQueue.Count != 0)
{
Tree parent = nodeQueue.Dequeue();
if (parent.left != null)
{
childrenQueue.Enqueue(parent.left);
}
if (parent.right != null)
{
childrenQueue.Enqueue(parent.right);
}
}
Tree prevNode = null;
Tree currNode = null;
while (childrenQueue.Count != 0)
{
currNode = childrenQueue.Dequeue();
nodeQueue.Enqueue(currNode);
if (prevNode != null)
{
MsgStr.Append(string.Format("\t{0}",currNode._data));
}
else
{
prevNode = currNode;
MsgStr.Append(string.Format("\t{0}",prevNode._data));
}
}
Console.WriteLine(MsgStr.ToString());
MsgStr.Remove(0, MsgStr.Length);
if (nodeQueue.Count == 0 && childrenQueue.Count == 0)
done = true;
}
}
}
}
ありがとう先生。それはうまくいって、これは私が実装したよりエレガントです。 – Jagannath