public void ListValuesInDepthFirstOrder() { int expectedValue = 6; DemoTree <int> myTree = CreateTreeWithValues(expectedValue); DisplayTree(myTree); var valuesString = String.Join(" ", myTree.ToList().ToArray()); Assert.AreEqual("1 2 4 5 3 6", valuesString); }
/// <summary> /// Iterates over the tree in depth-first fashion and returns a list of the tree's values /// </summary> /// <returns></returns> public List <T> ToList() { var myList = new List <T>(); myList.Add(Value); if (LeftChild != null) { myList.AddRange(LeftChild.ToList()); } if (RightChild != null) { myList.AddRange(RightChild.ToList()); } return(myList); }