/// <summary> /// Flattens the tree structure into list. /// </summary> /// <param name="root"> /// The root. /// </param> /// <returns> /// The <see> /// <cref>IEnumerable</cref> /// </see> /// . /// </returns> public static IEnumerable <BDDNode> Traverse(BDDNode root) { var stack = new Stack <BDDNode>(); var visited = new HashSet <BDDNode>(); stack.Push(root); while (stack.Count > 0) { BDDNode current = stack.Pop(); if (visited.Contains(current)) { continue; } visited.Add(current); yield return(current); if (current.GetType() != typeof(BDDVariableNode)) { continue; } stack.Push(current.HighNode); stack.Push(current.LowNode); } }
/// <summary> /// Creates a new node or returns reference to existing node if it shares the same properties. /// </summary> /// <param name="value"> /// The value. /// </param> /// <param name="highNode"> /// The high node. /// </param> /// <param name="lowNode"> /// The low node. /// </param> /// <returns> /// The <see cref="BDDNode"/>. /// </returns> public BDDNode CreateNode(int value, BDDNode highNode, BDDNode lowNode) { if (highNode == lowNode) { return(lowNode); } if (this.h.ContainsKey(new Tuple <int, BDDNode, BDDNode>(value, highNode, lowNode))) { return(this.h[new Tuple <int, BDDNode, BDDNode>(value, highNode, lowNode)]); } BDDNode n = new BDDVariableNode(value, highNode, lowNode); this.h.Add(new Tuple <int, BDDNode, BDDNode>(value, highNode, lowNode), n); return(n); }
/// <summary> /// Initializes a new instance of the <see cref="BDDNode"/> class. /// </summary> /// <param name="highNode"> /// The high node. /// </param> /// <param name="lowNode"> /// The low node. /// </param> protected BDDNode(BDDNode highNode, BDDNode lowNode) { this.HighNode = highNode; this.LowNode = lowNode; }
/// <summary> /// Initializes a new instance of the <see cref="BDDVariableNode"/> class. /// </summary> /// <param name="variable"> /// The variable. /// </param> /// <param name="highNode"> /// The high node. /// </param> /// <param name="lowNode"> /// The low node. /// </param> public BDDVariableNode(int variable, BDDNode highNode, BDDNode lowNode) : base(highNode, lowNode) { this.Variable = variable; }