
중위 순회 in-order Recursive
public void InOrder(Node root) // 이진 트리 Recursive in-order
{
if (root.Left != null) InOrder(root.Left);
Console.WriteLine(root.Data);
if (root.Right != null) InOrder(root.Right);
}

중위 순회 in-order Stack

public void InOrderStack(Node root) // 이진 트리 Stack in-order
{
Stack<Node> nodes = new Stack<Node>();
while (true)
{
while (root != null)
{
nodes.Push(root);
root = root.Left;
}
if (nodes.Count == 0) return;
root = nodes.Pop();
Console.WriteLine(root.Data);
root = root.Right;
}
}

'C# > 수업 내용' 카테고리의 다른 글
| [C#] Binary Search Tree(이진 탐색 트리) Add, Search, Remove (0) | 2021.09.30 |
|---|---|
| [C#] 이진 트리 post-order(후위 순회) (0) | 2021.09.30 |
| [C#] 이진 트리 pre-order(전위 순회) (0) | 2021.09.30 |
| [C#] 배열 Binary Tree (0) | 2021.09.29 |
| [C#] 이진 트리 Left Skewed Tree, Recursive Print (0) | 2021.09.27 |