protected virtual void Append(T destinationPayload, string path) { // Trim any whitespaces or slashes to get rid of empty entries // path = path.Trim(' ', '/'); bool isEnd; string name; string nextPath = StepPath(path, out isEnd, out name); // Get the position of the first slash, signaling the next entry in the path // int nextChildIndex = path.IndexOf('/'); // if there is no slash it means we have reached the end of the path so we just add the payload if (isEnd) { children.Add(new BetterSearchTree <T>(InheritLabel(name), destinationPayload)); return; } // Get the name of first entry in the current path // string childName = path.Substring(0, nextChildIndex); // try to get the child entry or create a new one with that name var child = GetChild(name); if (child == null) { child = new BetterSearchTree <T>(InheritLabel(name)); children.Add(child); } // Recursively append the remaining entries child.Append(destinationPayload, nextPath); }
// Insert (or merge) another tree as a child into this one public void Insert(BetterSearchTree <T> child) { // Case 1: the provided entry is a leaf and is just added to the children if (child.IsLeaf) { children.Add(child); return; } // Case 2: there is no existing child with the same name as the provided tree // so we just add it to the list of children var subChild = GetChild(child.label.text); if (subChild == null) { children.Add(child); return; } // Case 3: We already have a child with the name of the provided tree so we need // to insert each sub child individually foreach (var c in child.children) { subChild.Insert(c); } }
protected virtual void AddTreeEntry(List <SearchTreeEntry> entries, BetterSearchTree <T> tree, int level) { // Add Leaf element if (tree.IsLeaf) { var entry = new SearchTreeEntry(tree.label) { level = level, userData = tree.payload }; entries.Add(entry); } // Add Group else { var group = new SearchTreeGroupEntry(new GUIContent(tree.label.text), level); entries.Add(group); // Add children foreach (var child in tree.children) { AddTreeEntry(entries, child, level + 1); } } }