/// <summary> /// Removes a node from this node's children. /// </summary> /// <param name="node">The node to remove.</param> /// <returns>Whether the node was removed successfully.</returns> /// /// <remarks> /// Removing fails if the node belongs to another node or to no node at all. /// </remarks> public bool RemoveChild(ExpressionTreeNode node) { if (node.Parent == this) { m_children.Remove(node); node.Parent = null; return(true); } return(false); }
/// <summary> /// Adds a new child to this node's children at the specified index. /// </summary> /// <param name="node">The node to add.</param> /// <param name="index">Specifies where to add the new node.</param> /// <returns>Whether the addition was successful.</returns> /// /// <remarks> /// The addition fails if the new node already has a parent node. /// </remarks> public bool AddChild(ExpressionTreeNode node, int index) { if (m_children.Contains(node) || node.Parent != null) { return(false); } m_children.Insert(index, node); node.Parent = this; return(true); }
// recursive helper for creating an expression tree private ExpressionTreeNode CreateSubTree(IEnumerator <ExpressionToken> tokens) { var rootNode = new ExpressionTreeNode(); if (!tokens.MoveNext()) { throw new Exception("No token on the stack!"); } var token = tokens.Current; rootNode.Token = token; if (token.type == TokenType.Operator) { // parse subtrees int arity = GetOperatorArity(token.token); for (int i = 0; i < arity; i++) { rootNode.AddChild(CreateSubTree(tokens), 0); } } return(rootNode); }
/// <summary> /// Creates a new instance of <see cref="ExpressionTree"/> using the specified node as root node. /// </summary> /// <param name="rootNode">The node to use as root node.</param> public ExpressionTree(ExpressionTreeNode rootNode) { RootNode = rootNode; }
/// <summary> /// Adds a new child to this node's children. /// </summary> /// <param name="node">The node to add</param> /// <returns>Returns whether the addition was successful.</returns> /// /// <remarks> /// The addition fails if the new node already has a parent node. /// The new node is added at the end of the children. /// </remarks> public bool AddChild(ExpressionTreeNode node) { return(AddChild(node, m_children.Count)); }