【问题标题】:Finding a specific element in an arbitrary tree在任意树中查找特定元素
【发布时间】:2016-08-16 05:11:14
【问题描述】:

在遍历树 DFS 时,我一直在努力寻找元素。下面是我的树实现。它由另一个类的对象填充。我想要的是通过给定的值在树中找到一个元素,我在尝试这样做时遇到了一些实际问题。有没有办法可以向节点添加一些 Key 引用,然后在所有节点中搜索该引用?我非常感谢您的帮助! :) 谢谢。

public class TreeNode<T>
    {

    private T value;
    private bool hasParent;
    public TreeNode<T> parent;
    private List<TreeNode<T>> children;
    public TreeNode(T value, TreeNode<T> parent)
    {
        this.parent = parent;
        if (value == null)
        {
            throw new ArgumentNullException(
                  "Cannot insert null value!");
        }
        this.value = value;
        this.children = new List<TreeNode<T>>();
    }

    public T Value
    {
        get
        {
            return this.value;
        }
        set
        {
            this.value = value;
        }
    }
    public int ChildrenCount
    {
        get
        {
            return this.children.Count;
        }
    }
       public class Tree<T>
    {
    // The root of the tree
    private TreeNode<T> root;


    public Tree(T value)
    {
        if (value == null)
        {
            throw new ArgumentNullException(
                  "Cannot insert null value!");
        }

        this.root = new TreeNode<T>(value,null);
    }


    public Tree(T value, params Tree<T>[] children)
        : this(value)
    {
        foreach (Tree<T> child in children)
        {
            this.root.AddChild(child.root);
        }
    }

【问题讨论】:

    标签: c# oop tree traversal


    【解决方案1】:

    从代码来看,它是如何填充的并不是很明显,但是我对代码进行了一些调整并尝试清理它,以便我们可以实现填充和搜索树。我添加了一些公共属性和方法。

    public class Tree<T>
    {
        // The root of the tree
        private TreeNode<T> root;
    
    
        public Tree(T value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value), "Cannot insert null value!");
            }
    
            root = new TreeNode<T>(value, null);
        }
    
    
        public Tree(T value, params Tree<T>[] children)
            : this(value)
        {
            foreach (Tree<T> child in children)
            {
                root.AddChild(child.root);
            }
        }
    
        public TreeNode<T> Root => root;
    
        public TreeNode<T> FindByValue(T value) => Root.FindByValue(value);
    
    }
    
    
    public class TreeNode<T>
    {
        public TreeNode(T value, TreeNode<T> parent)
        {
            this.parent = parent;
            if (value == null)
            {
                throw new ArgumentNullException(nameof(parent), "Cannot insert null value!");
            }
            this.value = value;
            children = new List<TreeNode<T>>();
        }
    
    
        private T value;
        public TreeNode<T> parent;
        private List<TreeNode<T>> children;
    
    
        public T Value
        {
            get { return value; }
            set { this.value = value; }
        }
        public int ChildrenCount => children.Count;
    
        public TreeNode<T> AddChild(TreeNode<T> child)
        {
            children.Add(child);
            return child;
        }
    
        public TreeNode<T> AddChild(T value) => AddChild(new TreeNode<T>(value, this));
    
        public TreeNode<T> FindByValue(T value)
        {
            if (value.Equals(Value))
                return this;
    
            foreach (var child in children)
            {
                var match = child.FindByValue(value);
                if (match != null)
                    return match;
            }
            return null;
        }
    }
    

    示例用法:

    //create a new tree..
    var tree = new Tree<string>("root-item");
    
    //populate the tree with 10 items with 10 sub items
    for (int i = 0; i < 10; i++)
    {
        var node = tree.Root.AddChild($"item-{i}");
        for (int w = 0; w < 10; w++)
        {
            node.AddChild($"sub-item-{i}-{w}");
        }
    }
    
    //find the root node
    var findNode = tree.Root.FindByValue("root-item");
    
    //find a sub node item
    findNode = tree.FindByValue("sub-item-1-1");
    

    【讨论】:

    • 非常感谢,现在这更有意义了。这是非常有帮助的。我正在使用从 Employee 类实例化的对象填充树。我怎么说例如 var node = tree.Root.AddChild($new Employee); ?我的意思是如何让它填充这个 for 循环中的对象?这种数据可能吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    相关资源
    最近更新 更多