petko_stankoski
petko_stankoski

Reputation: 10713

Deleting a node from a treelike structure

I have a class which represents a tree:

public class Tree
{
    public String id { get; set; }
    public String text { get; set; }
    public List<Tree> item { get; set; }
    public string im0 { get; set; }
    public string im1 { get; set; }
    public string im2 { get; set; }
    public String parentId { get; set; }

    public Tree()
    {
        id = "0";
        text = "";
        item = new List<Tree>();
    }
}

And the tree looks like this:

tree    {Tree}  Tree
  id    "0" string
  im0   null    string
  im1   null    string
  im2   null    string
  item  Count = 1   System.Collections.Generic.List<Tree>
    [0] {Tree}  Tree
            id  "F_1"   string
            im0 "fC.gif"    string
            im1 "fO.gif"    string
            im2 "fC.gif"    string
            item    Count = 12  System.Collections.Generic.List<Tree>
            parentId    "0" string
            text    "ok"    string
    parentId    null    string
    text    ""  string

How would I delete the node with id = someId?

For example, how would I delete the node with id = "F_123"? All of its children should be also deleted.

I have a method which searches in the tree for a given id. I tried using that method and then setting the node to null, but it doesn't work.

Here's what I got till now:

//This is the whole tree:
Tree tree = serializer.Deserialize<Tree>(someString);

//this is the tree whose root is the parent of the node I want to delete:
List<Tree> parentTree = Tree.Search("F_123", tree).First().item;

//This is the node I want to delete:
var child = parentTree.First(p => p.id == id);

How do I delete child from tree?

Upvotes: 1

Views: 99

Answers (2)

Servy
Servy

Reputation: 203819

So here's a fair straightforward traversal algorithm that can get the parent of a given node; it does so using an explicit stack rather than using recursion.

public static Tree GetParent(Tree root, string nodeId)
{
    var stack = new Stack<Tree>();
    stack.Push(root);

    while (stack.Any())
    {
        var parent = stack.Pop();

        foreach (var child in parent.item)
        {
            if (child.id == nodeId)
                return parent;

            stack.Push(child);
        }
    }

    return null;//not found
}

Using that it's simple enough to remove a node by finding it's parent and then removing it from the direct descendants:

public static void RemoveNode(Tree root, string nodeId)
{
    var parent = GetParent(root, nodeId).item
        .RemoveAll(child => child.id == nodeId);
}

Upvotes: 1

Barracoder
Barracoder

Reputation: 3764

Find the parent node of the node to delete (id=F_1). Recursively remove it from the tree

// something like
Tree parent = FindParentNodeOf("F_1");
var child = parent.Items.First(p=> p.id="F_1");
RecurseDelete(parent, child);

private void RecurseDelete(Tree theTree, Tree toDelete)
{
    foreach(var child in toDelete.item)
       RecurseDelete(toDelete, child);

    theTree.item.Remove(toDelete);
}

Upvotes: 0

Related Questions