private OperatorNode GetTopNode(OperatorNode operatorNode) { while (operatorNode.Parent != null) { Trace.WriteLine(string.Format("Parent of {0}({1}{2}) is {3}({4}{5})", operatorNode, operatorNode.LeftChild, operatorNode.RightChild, operatorNode.Parent, operatorNode.Parent.LeftChild, operatorNode.Parent.RightChild)); operatorNode = operatorNode.Parent; } return operatorNode; }
public void Insert(OperatorNode operatorNodeToInsert, INode rightNode) { var operatorNodeAlreadyInTree = rightNode.Parent; var comparer = new OperatorNodeComparer(); bool newNodeInsertedInTree = false; while (!newNodeInsertedInTree) { if (comparer.Compare(operatorNodeToInsert, operatorNodeAlreadyInTree) <= 0) { var existingRightChild = operatorNodeAlreadyInTree.RightChild; Trace.WriteLine(string.Format("Existing right child: {0}", existingRightChild)); Trace.WriteLine(string.Format("Operator Node Already in tree: {0}", operatorNodeAlreadyInTree)); Trace.WriteLine(string.Format("Operator Node to insert: {0}", operatorNodeToInsert)); Trace.WriteLine(string.Format("Setting {0} as right child of {1}", operatorNodeToInsert, operatorNodeAlreadyInTree)); operatorNodeAlreadyInTree.RightChild = operatorNodeToInsert; operatorNodeToInsert.Parent = operatorNodeAlreadyInTree; Trace.WriteLine(string.Format("Setting {0} as left child of {1}", existingRightChild, operatorNodeToInsert)); operatorNodeToInsert.LeftChild = existingRightChild; existingRightChild.Parent = operatorNodeToInsert; newNodeInsertedInTree = true; } else { if (operatorNodeAlreadyInTree.Parent != null) { Trace.WriteLine(string.Format("{0} should be higher up tree than {1}. Moving up tree to compare {0} with next node.", operatorNodeToInsert, operatorNodeAlreadyInTree)); operatorNodeAlreadyInTree = operatorNodeAlreadyInTree.Parent; } else { Trace.WriteLine(string.Format("At top of the tree: making {0} the parent of {1}.", operatorNodeToInsert, operatorNodeAlreadyInTree)); operatorNodeToInsert.LeftChild = operatorNodeAlreadyInTree; operatorNodeAlreadyInTree.Parent = operatorNodeToInsert; newNodeInsertedInTree = true; } } } }