C# 二叉搜索树中的同级连接
我正在尝试连接二叉搜索树的兄弟节点。您可以在Connect()方法中找到逻辑。我的问题是,有没有更好的办法?我使用两个队列来实现逻辑是否做得过火了C# 二叉搜索树中的同级连接,c#,C#,我正在尝试连接二叉搜索树的兄弟节点。您可以在Connect()方法中找到逻辑。我的问题是,有没有更好的办法?我使用两个队列来实现逻辑是否做得过火了 using System; using System.Diagnostics; using System.Collections.Generic; using System.Text; namespace SampleCSParallel { class Tree { public Tree left = null;
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;
}
}
}
}
使用系统;
使用系统诊断;
使用System.Collections.Generic;
使用系统文本;
名称空间采样并行
{
类树
{
公共树left=null;
公共树权限=null;
公共树同级=null;
公共int_数据;
树(int数据)
{
_数据=数据;
左=空;
右=空;
}
左公树
{
得到
{
把这个还给我;
}
}
公树权
{
得到
{
归还这个。对;
}
}
公共树AddNode(树节点,int数据)
{
if(node==null)
{
节点=新树(数据);
返回节点;
}
else if(节点\数据)
{
node.right=AddNode(node.right,数据);
}
返回节点;
}
公共静态树CreateTree(树节点,int深度,int开始)
{
if(node==null)
节点=新树(开始);
如果(深度>1)
{
node.left=CreateTree(node.left,深度-1,开始+1);
node.right=CreateTree(node.right,深度-1,开始+1);
}
返回节点;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
//树节点=null;
treetr=Tree.CreateTree(null,4,1);
秒表sw=Stopwatch.StartNew();
int total=步行树(tr);
TimeSpan ts=经过的时间间隔;
WriteLine(“在{0}秒内”,ts.Seconds);
WriteLine(“总计:{0}”,总计);
连接(tr);
Console.ReadLine();
}
静态void连接(树根)
{
Queue nodeQueue=新队列();
nodeQueue.Enqueue(根);
控制台写入线(根数据);
连接兄弟姐妹(nodeQueue);
}
静态无效连接同级(队列节点)
{
Queue childrenQueue=新队列();
StringBuilder MsgStr=新的StringBuilder();
bool done=false;
而(!完成)
{
while(nodeQueue.Count!=0)
{
树父级=nodeQueue.Dequeue();
if(parent.left!=null)
{
childrenQueue.Enqueue(parent.left);
}
if(parent.right!=null)
{
childrenQueue.Enqueue(parent.right);
}
}
Tree-prevNode=null;
树节点=null;
while(childrenQueue.Count!=0)
{
currNode=childrenQueue.Dequeue();
nodeQueue.Enqueue(currende);
if(prevNode!=null)
{
MsgStr.Append(string.Format(“\t{0}”,currenode.\u data));
}
其他的
{
prevNode=currNode;
MsgStr.Append(string.Format(“\t{0}”,prevNode.\u data));
}
}
Console.WriteLine(MsgStr.ToString());
MsgStr.Remove(0,MsgStr.Length);
if(nodeQueue.Count==0&&childrenQueue.Count==0)
完成=正确;
}
}
}
}
可以使用prev节点数组以递归方式连接兄弟节点,每个深度一个:
void connect(Node node, int depth, List<Node> prev)
{
if (node == null)
return;
if(prev.Size <= depth)
prev.Add(depth);
else {
prev[depth].sibling = node;
prev[depth] = node;
}
connect(node.left, depth+1, prev);
connect(node.right, depth+1, prev);
}
void connect(节点,int-depth,List-prev)
{
if(node==null)
返回;
如果(prev.Size可以使用prev节点数组以递归方式连接兄弟节点,每个深度一个:
void connect(Node node, int depth, List<Node> prev)
{
if (node == null)
return;
if(prev.Size <= depth)
prev.Add(depth);
else {
prev[depth].sibling = node;
prev[depth] = node;
}
connect(node.left, depth+1, prev);
connect(node.right, depth+1, prev);
}
void connect(节点,int-depth,List-prev)
{
if(node==null)
返回;
if(prev.Size我在java中尝试了空间复杂度为O(1)的问题
public void setSibling(Node root){
Node start = null;
if (root == null) return;
do{
if(root.left!=null) {
root.left.sibling = getNextSibling(root,"left");
if(start == null) start = root.left;
}
if(root.right!=null){
root.right.sibling = getNextSibling(root,"right");
if(start == null) start = root.right;
}
root = root.sibling;
}while(root!=null);
root = start;
setSibling(root);
}
public Node getNextSibling(Node root,String marker){
if (marker.equals("left")){
if(root.right!=null)return root.right;
}
Node nextSibling = null;
root = root.sibling;
while(nextSibling == null && root != null){
if (root.left != null) nextSibling = root.left;
else if (root.right != null) nextSibling = root.right;
else root = root.sibling;
}
return nextSibling;
}
它可能并不优雅,但却很有效。我曾尝试用java中O(1)的空间复杂性来回答这个问题
public void setSibling(Node root){
Node start = null;
if (root == null) return;
do{
if(root.left!=null) {
root.left.sibling = getNextSibling(root,"left");
if(start == null) start = root.left;
}
if(root.right!=null){
root.right.sibling = getNextSibling(root,"right");
if(start == null) start = root.right;
}
root = root.sibling;
}while(root!=null);
root = start;
setSibling(root);
}
public Node getNextSibling(Node root,String marker){
if (marker.equals("left")){
if(root.right!=null)return root.right;
}
Node nextSibling = null;
root = root.sibling;
while(nextSibling == null && root != null){
if (root.left != null) nextSibling = root.left;
else if (root.right != null) nextSibling = root.right;
else root = root.sibling;
}
return nextSibling;
}
它可能不优雅,但很有效。谢谢,先生。它有效,而这一个比我实施的更优雅。谢谢,先生。它有效,而这一个比我实施的更优雅。