Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7705863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:58:06+00:00 2026-05-31T23:58:06+00:00

I need to create a product catalog, in tree type. every tree node presents

  • 0

I need to create a product catalog, in tree type.

every tree node presents by a ID(string), the functions on the tree data only 2:

  1. getChild(string ID), give a ID, get children (no need include childrens’
    children), if ID is null, get all root nodes
  2. getParent(string ID), return parent ID if have, or null if is root

Since once the tree decided, will not change, so I think put all code in static will be best.
So I start to try use Dictionary

"id": {parent:ID, child:[id2, id3, id4....]}

Since theres about 1000+ catalog, I found I quickly mess myself up, lots of mistake in the static data, and make final result on usable. Also, now I only wrote dozens and the code is looking like mess.

Please advice a way create this simple catalog tree with high performance. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:58:09+00:00Added an answer on May 31, 2026 at 11:58 pm

    Just make a class out of it.

    UPDATED:

    class TreeNode : IEnumerable<TreeNode>
    {
        private readonly Dictionary<string, TreeNode> _children =
                                            new Dictionary<string, TreeNode>();
    
        public readonly string ID;
        public TreeNode Parent { get; private set; }
    
        public TreeNode(string id)
        {
            this.ID = id;
        }
    
        public TreeNode GetChild(string id)
        {
            return this._children[id];
        }
    
        public void Add(TreeNode item)
        {
            if (item.Parent != null)
            {
                item.Parent._children.Remove(item.ID);
            }
    
            item.Parent = this;
            this._children.Add(item.ID, item);
        }
    
        public IEnumerator<TreeNode> GetEnumerator()
        {
            return this._children.Values.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    
        public int Count
        {
            get { return this._children.Count; }
        }
    }
    

    Usage will be fairly simple to statically define:

    var tree = new TreeNode("Root")
                   {
                       new TreeNode("Category 1")
                           {
                               new TreeNode("Item 1"),
                               new TreeNode("Item 2"),
                               new TreeNode("Item 3"),
                           },
                       new TreeNode("Category 2")
                           {
                               new TreeNode("Item 1"),
                               new TreeNode("Item 2"),
                               new TreeNode("Item 3"),
                               new TreeNode("Item 4"),
                           }
                   };
    

    Edit

    Some more functionality for even easier creation…

    public static TreeNode BuildTree(string tree)
    {
        var lines = tree.Split(new[] { Environment.NewLine },
                               StringSplitOptions.RemoveEmptyEntries);
    
        var result = new TreeNode("TreeRoot");
        var list = new List<TreeNode> { result };
    
        foreach (var line in lines)
        {
            var trimmedLine = line.Trim();
            var indent = line.Length - trimmedLine.Length;
    
            var child = new TreeNode(trimmedLine);
            list[indent].Add(child);
    
            if (indent + 1 < list.Count)
            {
                list[indent + 1] = child;
            }
            else
            {
                list.Add(child);
            }
        }
    
        return result;
    }
    
    public static string BuildString(TreeNode tree)
    {
        var sb = new StringBuilder();
    
        BuildString(sb, tree, 0);
    
        return sb.ToString();
    }
    
    private static void BuildString(StringBuilder sb, TreeNode node, int depth)
    {
        sb.AppendLine(node.ID.PadLeft(node.ID.Length + depth));
    
        foreach (var child in node)
        {
            BuildString(sb, child, depth + 1);
        }
    }
    

    Usage:

    var tree = TreeNode.BuildTree(@"
    Cat1
     Sub1
      Item1
      Item2
      Item3
     Sub2
      Item1
      Item2
    Cat2
     Sub1
     Sub2
      Item1
      Item2
     Sub3
      Item1
    Cat3
    Cat4");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a new product type and need to create and associate some custom
How can I create a product key for my C# Application? I need to
I need to create a backup of a SQL Server 2005 Database that's only
i have created a list_home.phtml that i call from {{block type=catalog/product_list category_id=6 template=catalog/product/list_home.phtml}} The
We have a product that we need to create an installer for. It has
I need to create a database solution to provide product discounting. Current tables: Products
I am working on a product page and need to create two separate galleries
I want to create a product catalog that allows for intricate details on each
I need to create a query in NHibernate that would search for a product
While developing products, we often need to create proprietary tools to test some of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.