public void ConvertTree_ValidTree_ConvertsToCircularLinkedList() { // arrange BinaryTreeNode root = new BinaryTreeNode("0-root"); root.Left = new BinaryTreeNode("1-left"); root.Right = new BinaryTreeNode("1-right"); root.Left.Left = new BinaryTreeNode("2-left-left"); root.Left.Right = new BinaryTreeNode("2-left-right"); root.Right.Left = new BinaryTreeNode("2-right-left"); root.Right.Right = new BinaryTreeNode("2-right-right"); root.Right.Right.Left = new BinaryTreeNode("3-right-right-left"); root.Right.Right.Left.Right = new BinaryTreeNode("4-right-right-left-right"); ListOperations ops = new ListOperations(); // act ListOperations.LinkedListNode actual = ops.ConvertTree(root); // assert Assert.AreEqual(root.Value, actual.Value); Assert.AreEqual(root.Left.Value, actual.Next.Value); Assert.AreEqual(root.Right.Value, actual.Next.Next.Value); Assert.AreEqual(root.Left.Left.Value, actual.Next.Next.Next.Value); Assert.AreEqual(root.Left.Right.Value, actual.Next.Next.Next.Next.Value); Assert.AreEqual(root.Right.Left.Value, actual.Next.Next.Next.Next.Next.Value); Assert.AreEqual(root.Right.Right.Value, actual.Next.Next.Next.Next.Next.Next.Value); Assert.AreEqual(root.Right.Right.Left.Value, actual.Next.Next.Next.Next.Next.Next.Next.Value); Assert.AreEqual(root.Right.Right.Left.Right.Value, actual.Next.Next.Next.Next.Next.Next.Next.Next.Value); Assert.AreEqual(actual, actual.Next.Next.Next.Next.Next.Next.Next.Next.Next); }
public void InOrderTraversal_ValidTree_CorrectSequence() { // arrange BinaryTreeNode root = new BinaryTreeNode("0-root"); root.Left = new BinaryTreeNode("1-left"); root.Right = new BinaryTreeNode("1-right"); root.Left.Left = new BinaryTreeNode("2-left-left"); root.Left.Right = new BinaryTreeNode("2-left-right"); root.Right.Left = new BinaryTreeNode("2-right-left"); root.Right.Right = new BinaryTreeNode("2-right-right"); root.Right.Right.Left = new BinaryTreeNode("3-right-right-left"); root.Right.Right.Left.Right = new BinaryTreeNode("4-right-right-left-right"); TreeTraversal traversal = new TreeTraversal(); // act BinaryTreeNode[] levelFirst = traversal.InOrder(root).ToArray(); // assert Assert.AreEqual(levelFirst[0].Value, "2-left-left"); Assert.AreEqual(levelFirst[1].Value, "1-left"); Assert.AreEqual(levelFirst[2].Value, "2-left-right"); Assert.AreEqual(levelFirst[3].Value, "0-root"); Assert.AreEqual(levelFirst[4].Value, "2-right-left"); Assert.AreEqual(levelFirst[5].Value, "1-right"); Assert.AreEqual(levelFirst[6].Value, "3-right-right-left"); Assert.AreEqual(levelFirst[7].Value, "4-right-right-left-right"); Assert.AreEqual(levelFirst[8].Value, "2-right-right"); }
public void Next_ValidValues_ReturnsCorrectSequence() { // arrange BinaryTreeNode root = new BinaryTreeNode("0-root"); root.Left = new BinaryTreeNode("1-left"); root.Right = new BinaryTreeNode("1-right"); root.Left.Left = new BinaryTreeNode("2-left-left"); root.Left.Right = new BinaryTreeNode("2-left-right"); root.Right.Left = new BinaryTreeNode("2-right-left"); root.Right.Right = new BinaryTreeNode("2-right-right"); root.Right.Right.Left = new BinaryTreeNode("3-right-right-left"); root.Right.Right.Left.Right = new BinaryTreeNode("4-right-right-left-right"); InOrderEnumerator enumerator = new InOrderEnumerator(root); // act // assert Assert.AreEqual(enumerator.Next().Value, "2-left-left"); Assert.AreEqual(enumerator.Next().Value, "1-left"); Assert.AreEqual(enumerator.Next().Value, "2-left-right"); Assert.AreEqual(enumerator.Next().Value, "0-root"); Assert.AreEqual(enumerator.Next().Value, "2-right-left"); Assert.AreEqual(enumerator.Next().Value, "1-right"); Assert.AreEqual(enumerator.Next().Value, "3-right-right-left"); Assert.AreEqual(enumerator.Next().Value, "4-right-right-left-right"); Assert.AreEqual(enumerator.Next().Value, "2-right-right"); }
public IEnumerable<BinaryTreeNode> PostOrder(BinaryTreeNode node) { //if (node == null) { return Enumerable.Empty<BinaryTreeNode>(); } //return PostOrder(node.Left).Union(PostOrder(node.Right)).Union(new[] { node }); // non-recursive Stack<BinaryTreeNode> nodes = new Stack<BinaryTreeNode>(); Stack<BinaryTreeNode> reverse = new Stack<BinaryTreeNode>(); nodes.Push(node); while (nodes.Count > 0) { BinaryTreeNode current = nodes.Pop(); if (current.Left != null) { nodes.Push(current.Left); } if (current.Right != null) { nodes.Push(current.Right); } reverse.Push(current); } return reverse; }
public IEnumerable<BinaryTreeNode> InOrder(BinaryTreeNode node) { //if (node == null) { return Enumerable.Empty<BinaryTreeNode>(); } //return InOrder(node.Left) // .Union(new[] { node }) // .Union(InOrder(node.Right)); // non-recursive Stack<BinaryTreeNode> nodes = new Stack<BinaryTreeNode>(); BinaryTreeNode current = node; while (current != null || nodes.Count > 0) { while (current != null) { nodes.Push(current); current = current.Left; } current = nodes.Pop(); yield return current; current = current.Right; } }
public List<List<BinaryTreeNode>> AllRootToLeafPaths(BinaryTreeNode root) { List<BinaryTreeNode> list = new List<BinaryTreeNode>(); List<List<BinaryTreeNode>> results = new List<List<BinaryTreeNode>>(); AllRootToLeafPaths(root, list, results); return results; }
public BinaryTreeNode Next() { if (m_current == null && m_nodes.Count == 0) throw new InvalidOperationException(); while (m_current != null) { m_nodes.Push(m_current); m_current = m_current.Left; } BinaryTreeNode returnValue = m_nodes.Pop(); m_current = returnValue.Right; return returnValue; }
public IEnumerable<BinaryTreeNode> LevelOrder(BinaryTreeNode root) { Queue<BinaryTreeNode> nodes = new Queue<BinaryTreeNode>(); nodes.Enqueue(root); while (nodes.Count > 0) { BinaryTreeNode node = nodes.Dequeue(); if (node.Left != null) { nodes.Enqueue(node.Left); } if (node.Right != null) { nodes.Enqueue(node.Right); } yield return node; } }
public void AllRootToLeafPaths_ValidTree_CorrectSequence() { // arrange BinaryTreeNode root = new BinaryTreeNode("0-root"); root.Left = new BinaryTreeNode("1-left"); root.Right = new BinaryTreeNode("1-right"); root.Left.Left = new BinaryTreeNode("2-left-left"); root.Left.Right = new BinaryTreeNode("2-left-right"); root.Right.Left = new BinaryTreeNode("2-right-left"); root.Right.Right = new BinaryTreeNode("2-right-right"); root.Right.Right.Left = new BinaryTreeNode("3-right-right-left"); root.Right.Right.Left.Right = new BinaryTreeNode("4-right-right-left-right"); TreeTraversal traversal = new TreeTraversal(); // act List<List<BinaryTreeNode>> paths = traversal.AllRootToLeafPaths(root); // assert Assert.AreEqual(4, paths.Count); Assert.AreEqual('0', paths[0][0].Value[0]); Assert.AreEqual('1', paths[0][1].Value[0]); Assert.AreEqual('2', paths[0][2].Value[0]); Assert.AreEqual('0', paths[1][0].Value[0]); Assert.AreEqual('1', paths[1][1].Value[0]); Assert.AreEqual('2', paths[1][2].Value[0]); Assert.AreEqual('0', paths[2][0].Value[0]); Assert.AreEqual('1', paths[2][1].Value[0]); Assert.AreEqual('2', paths[2][2].Value[0]); Assert.AreEqual('0', paths[3][0].Value[0]); Assert.AreEqual('1', paths[3][1].Value[0]); Assert.AreEqual('2', paths[3][2].Value[0]); Assert.AreEqual('3', paths[3][3].Value[0]); Assert.AreEqual('4', paths[3][4].Value[0]); }
public InOrderEnumerator(BinaryTreeNode root) { m_current = root; m_nodes = new Stack<BinaryTreeNode>(); }
public void SameFringe_IdenticalTrees_ReturnsTrue() { // arrange BinaryTreeNode a = new BinaryTreeNode("0-root"); a.Left = new BinaryTreeNode("1-left"); a.Right = new BinaryTreeNode("1-right"); BinaryTreeNode b = new BinaryTreeNode("0-root2"); b.Left = new BinaryTreeNode("1-left"); b.Right = new BinaryTreeNode("1-right"); TreeTraversal traversal = new TreeTraversal(); // act // assert Assert.IsTrue(traversal.SameFringe(a, b)); }
public void SameFringe_DifferentLeaves_ReturnsFalse() { // arrange BinaryTreeNode a = new BinaryTreeNode("0-root"); a.Left = new BinaryTreeNode("1-left"); a.Right = new BinaryTreeNode("1-right"); BinaryTreeNode b = new BinaryTreeNode("0-root2"); b.Left = new BinaryTreeNode("1-leftdd"); b.Right = new BinaryTreeNode("1-right"); TreeTraversal traversal = new TreeTraversal(); // act // assert Assert.IsFalse(traversal.SameFringe(a, b)); }
public IEnumerable<BinaryTreeNode> PreOrder(BinaryTreeNode node) { if (node == null) { return Enumerable.Empty<BinaryTreeNode>(); } return new[] { node } .Union(PreOrder(node.Left)) .Union(PreOrder(node.Right)); // non-recursive //Stack<BinaryTreeNode> nodes = new Stack<BinaryTreeNode>(); //nodes.Push(node); //while (nodes.Count > 0) //{ // BinaryTreeNode current = nodes.Pop(); // yield return current; // if (current.Right != null) // { // nodes.Push(current.Right); // } // if (current.Left != null) // { // nodes.Push(current.Left); // } //} }
private IEnumerable<string> Fringe(BinaryTreeNode n) { if (n.Left == null && n.Right == null) { return new[] { n.Value }; } else { IEnumerable<string> result = Enumerable.Empty<string>(); if (n.Left != null) { result = result.Union(Fringe(n.Left)); } if (n.Right != null) { result = result.Union(Fringe(n.Right)); } return result; } }
private void AllRootToLeafPaths(BinaryTreeNode node, List<BinaryTreeNode> current, List<List<BinaryTreeNode>> paths) { // add node current.Add(node); // leaf if (node.Left == null && node.Right == null) { paths.Add(current.ToList()); } // recurse left if (node.Left != null) { AllRootToLeafPaths(node.Left, current, paths); } // recurse right if (node.Right != null) { AllRootToLeafPaths(node.Right, current, paths); } // remove node current.Remove(node); }
public bool SameFringe(BinaryTreeNode a, BinaryTreeNode b) { return Fringe(a).SequenceEqual(Fringe(b)); }