public TemplateNode(TemplateNode parent, object content) { Parent = parent; Content = content; Children = new List <TemplateNode>(); Searched = false; }
/* Method Removes Node corresponding to child from the element tree */ public bool RemoveChild(object child) { TemplateNode tnp = root; //If the parent dne, root nodes parent will be null and loop will exit //If the child has already been added, exit the loop while (tnp != null) { if (tnp.Content == child) //If current node is the child { string msg = "Are you sure you want to delete this item?\n All internal items will also be deleted."; var confirmResult = MessageBox.Show(msg, "Confirm Delete!!", MessageBoxButtons.YesNo); if (confirmResult == DialogResult.Yes) { tnp.Parent.Children.Remove(tnp); return(true); } else { return(false); } } else { int i = 0; //counter //While not the end of child list and child has been searched, check next child while (i != tnp.Children.Count && tnp.Children[i].Searched == true) { i++; } if (i != tnp.Children.Count) //Search next child if available { tnp = tnp.Children[i]; } else //else move back to parent node as all children have been checked { tnp.Searched = true; tnp = tnp.Parent; } } } MessageBox.Show("Error: Element not found in tree"); return(false); }
/* * Method creates a node for child, and adds it to the child list of the node corresponding to parent in the tree * Method performs depth-first traversal of tree looking for parent */ public bool AddChild(Object child, Object parent) { // If the first element of the tree has not been added, create root node if (root == null) { root = new TemplateNode(null, child); return(true); } else // Do a depth-first traversal of the template tree to find the parent node { TemplateNode tnp = root; //If the parent dne, root nodes parent will be null and loop will exit //If the child has already been added, exit the loop while (tnp != null) { if (tnp.Content == parent) //If current node is the parent, add the child node and set successful { tnp.AddChild(new TemplateNode(tnp, child)); return(true); } else { int i = 0; //counter //While not the end of child list and child has been searched, check next child while (i != tnp.Children.Count && tnp.Children[i].Searched == true) { i++; } if (i != tnp.Children.Count) //Search next child if available { tnp = tnp.Children[i]; } else //else move back to parent node as all children have been checked { tnp.Searched = true; tnp = tnp.Parent; } } } MessageBox.Show("Error: Parent element not found in tree."); return(false); } }
public void RemoveChild(TemplateNode child) { Children.Remove(child); }
public void AddChild(TemplateNode child) { Children.Add(child); }