public static BinaryTree DnfBinaryTree(List <BinaryTree> nodes) { if (nodes.Count == 1) { return(nodes[0]); } var binaryTree = new BinaryTree(); binaryTree.InsertNode(null, new Disjunction()); var dnfRoot = binaryTree.Root; foreach (var node in nodes) { binaryTree.PropositionalVariables.Variables.AddRange(node.PropositionalVariables.Variables); var root = node.Root; if (dnfRoot.LeftNode != null && dnfRoot.RightNode != null) { var newRoot = new Disjunction(); dnfRoot.Parent = newRoot; newRoot.LeftNode = dnfRoot; dnfRoot = newRoot; dnfRoot.RightNode = root; binaryTree.Root = newRoot; } else { if (dnfRoot.LeftNode == null) { dnfRoot.LeftNode = root; } else { dnfRoot.RightNode = root; } } } return(binaryTree); }
/// <summary> /// Generate a binary tree based for a proposition formula /// </summary> /// <returns>The root of binary tree</returns> private static void GenerateBinaryTreeProposition() { Component root = _binaryTree.Root; for (var i = 0; i <= Elements.Count - 1; i++) { var currentCharacter = Elements[i]; var currentCharacterType = TypeOfCharacter(currentCharacter, false); if (currentCharacterType == CharacterType.PropositionalVariable) { if (currentCharacter == '0') { _binaryTree.InsertNode(root, new TrueFalse(false)); } else if (currentCharacter == '1') { _binaryTree.InsertNode(root, new TrueFalse(true)); } else { var propositionVariable = new Variable(currentCharacter); _binaryTree.InsertNode(root, propositionVariable); } } else if (currentCharacterType == CharacterType.Connectives) { root = GenerateOperatorObject(currentCharacter, root); } } NodeCounter = 0; }