public void GivenATreeWithDepth2WhenGetIsSetLeftThenRightShouldReceiveBall() { BinaryTreeModel model = new BinaryTreeModel(); var depth = 2; var node = model.CreateFullTree(depth, 0); var nodeTraversal = new PreOrderTraversal(); var defaultGateVisitor = new SetDefaultGateVisitor(); defaultGateVisitor.GateToLeft = true; nodeTraversal.Traverse(node, defaultGateVisitor); BallStrategy strategy = new BallStrategy(node); var ballCount = 2; for (int i = 0; i < ballCount; i++) { strategy.SetBall(); } var childNodeVisitor = new ChildNodeVisitor(); nodeTraversal.Traverse(node, childNodeVisitor); childNodeVisitor.ChildNodes.Count.Should().Be(4); childNodeVisitor.ChildNodes[0].HasBall.Should().BeTrue(); childNodeVisitor.ChildNodes[1].HasBall.Should().BeFalse(); childNodeVisitor.ChildNodes[2].HasBall.Should().BeTrue(); childNodeVisitor.ChildNodes[3].HasBall.Should().BeFalse(); node.GateToLeft.Should().BeTrue(); }
public void GivenATreeWithDepthCheckTheDepthOfTree() { BinaryTreeModel model = new BinaryTreeModel(); var depth = 4; var node = model.CreateFullTree(depth, 0); var maxNodeVisitor = new MaxNodeVisitor(); var traversal = new PreOrderTraversal(); traversal.Traverse(node, maxNodeVisitor); maxNodeVisitor.MaxValue.Should().Be(depth); }
static void Main(string[] args) { int depth; Console.WriteLine("Please enter the depth of Tree"); depth = Convert.ToInt32(Console.ReadLine()); BinaryTreeModel model = new BinaryTreeModel(); var tree = model.CreateFullTree(depth, 1); BinaryTreeTranversal traversal = new PreOrderTraversal(); traversal.Traverse(tree); var randomGateVisitor = new RandomGateSetterVisitor(); traversal.Traverse(tree, randomGateVisitor); Console.WriteLine("Please enter number of balls"); var ballCount = Convert.ToInt32(Console.ReadLine()); BallStrategy strategy = new BallStrategy(tree); for (int i = 0; i < ballCount; i++) { strategy.SetBall(); } var childNodeVisitor = new ChildNodeVisitor(); traversal.Traverse(tree, childNodeVisitor); Console.WriteLine("Position of the hole from left to right which is empty"); for (int i = 0; i < childNodeVisitor.ChildNodes.Count; i++) { if (childNodeVisitor.ChildNodes[i].HasBall == false) { Console.WriteLine(string.Format("Position {0} is empty", i + 1)); } } Console.ReadLine(); }