public static IEnumerable <string> DFSBinarySearchTreePostOrderIterative(BinarySearchTreeKey.Node node) { var visitedNodes = new List <string>(); var toBeVisitedNodes = new Stack <BinarySearchTreeKey.Node>(); var currentNode = node; BinarySearchTreeKey.Node prevNode = null; while (currentNode != null || toBeVisitedNodes.Any()) { if (currentNode != null) { toBeVisitedNodes.Push(currentNode); currentNode = currentNode.Left; } else { var stackTopNode = toBeVisitedNodes.Pop(); if (stackTopNode.Right != null && stackTopNode.Right != prevNode) { currentNode = stackTopNode.Right; toBeVisitedNodes.Push(stackTopNode); } else { currentNode = stackTopNode; visitedNodes.Add(currentNode.Key); prevNode = currentNode; currentNode = null; } } } return(visitedNodes); }
public static IEnumerable <IEnumerable <string> > TraverseBinarySearchTreeByLevelRecursive( BinarySearchTreeKey.Node node) { var toBeTraversedNodes = new Queue <BinarySearchTreeKey.Node>(); toBeTraversedNodes.Enqueue(node); return(TraverseBinarySearchTreeByLevelRecursive(toBeTraversedNodes)); }
public static IEnumerable <string> DFSBinarySearchTreePostOrderRecursive(BinarySearchTreeKey.Node node) { var returnValue = new List <string>(); if (node == null) { return(Enumerable.Empty <string>()); } returnValue.AddRange(DFSBinarySearchTreePostOrderRecursive(node.Left)); returnValue.AddRange(DFSBinarySearchTreePostOrderRecursive(node.Right)); returnValue.Add(node.Key); return(returnValue); }
public static IEnumerable <string> TraverseBinarySearchTreeIterative(BinarySearchTreeKey.Node node) { var traversedNodes = new List <string>(); var toBeTraversedNodes = new Queue <BinarySearchTreeKey.Node>(); toBeTraversedNodes.Enqueue(node); while (!toBeTraversedNodes.Empty()) { var currentNode = toBeTraversedNodes.Dequeue(); if (currentNode == null) { continue; } traversedNodes.Add(currentNode.Key); toBeTraversedNodes.Enqueue(currentNode.Left); toBeTraversedNodes.Enqueue(currentNode.Right); } return(traversedNodes); }
public static IEnumerable <string> DFSBinarySearchTreePreOrderIterative(BinarySearchTreeKey.Node node) { var visitedNodes = new List <string>(); var toBeVisitedNodes = new Stack <BinarySearchTreeKey.Node>(); var currentNode = node; while (currentNode != null || toBeVisitedNodes.Any()) { if (currentNode != null) { visitedNodes.Add(currentNode.Key); toBeVisitedNodes.Push(currentNode); currentNode = currentNode.Left; } else { currentNode = toBeVisitedNodes.Pop(); currentNode = currentNode.Right; } } return(visitedNodes); }
public static IEnumerable <IEnumerable <string> > TraverseBinarySearchTreeByLevelIterative(BinarySearchTreeKey.Node node) { var traversedNodesByLevel = new List <List <string> >(); var toBeTraversedNodes = new Queue <BinarySearchTreeKey.Node>(); toBeTraversedNodes.Enqueue(node); var currentLevelNodes = new Queue <BinarySearchTreeKey.Node>(); while (!toBeTraversedNodes.Empty()) { while (!toBeTraversedNodes.Empty()) { currentLevelNodes.Enqueue(toBeTraversedNodes.Dequeue()); } traversedNodesByLevel.Add(new List <string>()); while (!currentLevelNodes.Empty()) { var currentNode = currentLevelNodes.Dequeue(); if (currentNode == null) { continue; } traversedNodesByLevel.Last().Add(currentNode.Key); if (currentNode.Left != null) { toBeTraversedNodes.Enqueue(currentNode.Left); } if (currentNode.Right != null) { toBeTraversedNodes.Enqueue(currentNode.Right); } } } return(traversedNodesByLevel); }