我正在尝试实现BT的Level Order Traversal
class Node
{
public int data;
public Node left;
public Node right;
public Node(int data)
{
this.data = data;
right = null;
left = null;
}
}
unsafe class BinaryTree
{
Node root;
public BinaryTree()
{
root = null;
}
public void display(Node level)
{
Node current = level;
if (current.left != null)
{
display(current.left);
}
Console.WriteLine(current.data);
if(current.right!=null)
{
display(current.right);
}
}
Queue<int> qu = new Queue<int>();
public void LevelOrderTraversal()
{
}
public void insert(int num)
{
Node newnode = new Node(num);
Node currentNode;
if(root==null)
{
root = newnode;
}
else
{
currentNode=root;
while(true)
{
Node tmpParent = currentNode;
if(newnode.data<=currentNode.data)
{
currentNode = currentNode.left;
if (currentNode == null)
{ tmpParent.left = newnode; return; }
}
else if(newnode.data>currentNode.data)
{
currentNode = currentNode.right;
if (currentNode == null)
{ tmpParent.right = newnode; return; }
}
}
}
}
static void Main(string[] args)
{
BinaryTree bTree = new BinaryTree();
bTree.root = new Node(50);
bTree.root.left = new Node(25);
bTree.root.right = new Node(75);
bTree.insert(22);
bTree.insert(78);
bTree.LevelOrderTraversal(&bTree.root); //Problem occurs here
Console.WriteLine(bTree.root);
Console.ReadLine();
}
}
我正在尝试传递根节点的地址,以便我可以将其添加到队列中,但我不确定如何传递根的地址并在函数中捕获它。它说地址不能采用托管类型的地址。任何帮助将非常感激。谢谢
答案 0 :(得分:1)
感谢您的帮助。我想通了。这是答案
class Node
{
public int data;
public Node left;
public Node right;
public Node(int data)
{
this.data = data;
right = null;
left = null;
}
}
class BinaryTree
{
Node root;
public BinaryTree()
{
root = null;
}
public void display(Node level)
{
Node current = level;
if (current.left != null)
{
display(current.left);
}
Console.WriteLine(current.data);
if(current.right!=null)
{
display(current.right);
}
}
Queue<Node> qu = new Queue<Node>();
public void LevelOrderTraversal()
{
while (true)
{
if (qu.Count == 0)
{ return; }
else
{
Node current = qu.Dequeue();
Console.WriteLine(current.data);
if (current.left != null)
qu.Enqueue(current.left);
if (current.right != null)
qu.Enqueue(current.right);
}
}
}
public void insert(int num)
{
Node newnode = new Node(num);
Node currentNode;
if(root==null)
{
root = newnode;
}
else
{
currentNode=root;
while(true)
{
Node tmpParent = currentNode;
if(newnode.data<=currentNode.data)
{
currentNode = currentNode.left;
if (currentNode == null)
{ tmpParent.left = newnode; return; }
}
else if(newnode.data>currentNode.data)
{
currentNode = currentNode.right;
if (currentNode == null)
{ tmpParent.right = newnode; return; }
}
}
}
}
static void Main(string[] args)
{
BinaryTree bTree = new BinaryTree();
bTree.root = new Node(50);
bTree.root.left = new Node(25);
bTree.root.right = new Node(75);
bTree.insert(22);
bTree.insert(78);
bTree.qu.Enqueue(bTree.root);
bTree.LevelOrderTraversal();
Console.ReadLine();
}
}