-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeRightView.cs
More file actions
79 lines (65 loc) · 2.32 KB
/
BinaryTreeRightView.cs
File metadata and controls
79 lines (65 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using DataStructures.Libraries.Trees;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithms.Problem.BinaryTree.BinaryTreeView
{
public class BinaryTreeNodeEx
{
public BinaryTreeNode treeNode;
public int position;
}
[TestClass]
public class BinaryTreeRightView
{
public void PrintRightViewOfTree(BinaryTreeNode treeNode)
{
var queueBinaryTree = new Queue<BinaryTreeNode>();
queueBinaryTree.Enqueue(treeNode);
queueBinaryTree.Enqueue(null);
while (queueBinaryTree.Any())
{
BinaryTreeNode node = queueBinaryTree.Dequeue();
if (node == null && queueBinaryTree.Peek() != null)
{
Console.WriteLine(queueBinaryTree.Peek());
queueBinaryTree.Enqueue(null);
}
else
{
if (node.Right != null)
queueBinaryTree.Enqueue(node.Right);
if (node.Left != null)
queueBinaryTree.Enqueue(node.Left);
}
}
}
public void PrintTopViewOfTree(BinaryTreeNodeEx treeNode)
{
Dictionary<int, BinaryTreeNodeEx> dictionary = new Dictionary<int, BinaryTreeNodeEx>();
var queueBinaryTree = new Queue<BinaryTreeNodeEx>();
queueBinaryTree.Enqueue(treeNode);
dictionary.Add(0, treeNode);
while (queueBinaryTree.Any())
{
BinaryTreeNodeEx node = queueBinaryTree.Dequeue();
int leftPosition = node.position - 1;
int rightPosition = node.position + 1;
dictionary.Add(leftPosition, node);
}
}
[TestMethod]
public void ValidateTreeSpiralOrder()
{
var binarySearchTree = new BinarySearchTree();
binarySearchTree.PopulateDefaultBalanceTree();
int[] rightView = { 20, 30, 10, 5, 12, 25, 35 };
var spiralOrder = new BinaryTreeRightView();
spiralOrder.PrintRightViewOfTree(binarySearchTree.Root);
}
}
}