using System.Collections.Generic;
public static void Main(string[] args)
root.LeftChild = new Node(3);
root.LeftChild.LeftChild = new Node(2);
root.LeftChild.LeftChild.LeftChild = new Node(1);
root.LeftChild.RightChild = new Node(4);
root.LeftChild.RightChild.LeftChild = new Node(5);
root.LeftChild.RightChild.RightChild = new Node(6);
root.RightChild = new Node(8);
root.RightChild.RightChild = new Node(10);
root.RightChild.LeftChild = new Node(9);
root.RightChild.RightChild.LeftChild = new Node(11);
Console.WriteLine("Алгоритмы поиска в глубину" + Environment.NewLine);
Console.WriteLine("Способ прямого обхода рекурсивно:");
DepthСLRRecursively(root);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Способ прямого обхода итерационно:");
DepthСLRIteratively(root);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Способ центрированного обхода рекурсивно:");
DepthLCRRecursively(root);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Способ центрированного обхода итерационно:");
DepthLCRIteratively(root);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Способ обратного обхода рекурсивно:");
DepthLRCRecursively(root);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Способ обратного обхода итерационно:");
DepthLRCIteratively(root);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Алгоритмы поиска в ширину" + Environment.NewLine);
private static void DepthСLRRecursively(Node node)
DepthСLRRecursively(node.LeftChild);
DepthСLRRecursively(node.RightChild);
private static void DepthСLRIteratively(Node node)
Stack<Node> stack = new Stack<Node>();
if (node.RightChild != null)
stack.Push(node.RightChild);
if (node.LeftChild != null)
stack.Push(node.LeftChild);
private static void DepthLCRRecursively(Node node)
DepthLCRRecursively(node.LeftChild);
DepthLCRRecursively(node.RightChild);
private static void DepthLCRIteratively(Node node)
Stack<Node> stack = new Stack<Node>();
while (stack.Count > 0 | node != null)
private static void DepthLRCRecursively(Node node)
DepthLRCRecursively(node.LeftChild);
DepthLRCRecursively(node.RightChild);
private static void DepthLRCIteratively(Node node)
Stack<Node> stack = new Stack<Node>();
Node lastShowedNode = null;
while (stack.Count > 0 | node != null)
Node peekedNode = stack.Peek();
if (peekedNode.RightChild != null & lastShowedNode != peekedNode.RightChild)
node = peekedNode.RightChild;
lastShowedNode = stack.Pop();
private static void Breadth(Node node)
Queue<Node> queue = new Queue<Node>();
if (node.LeftChild != null)
queue.Enqueue(node.LeftChild);
if (node.RightChild != null)
queue.Enqueue(node.RightChild);
public int ID { get; set; }
public Node LeftChild { get; set; }
public Node RightChild { get; set; }
Console.WriteLine("ID текущего пройденного узла " + ID);