Advertisement
ArXen42

ArithmeticExpressionTree.cs

May 21st, 2016
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ArithmeticExpressionsTree
  5. {
  6.     public struct TreeNode
  7.     {
  8.         public TreeNode(Int32 parentIndex, String value)
  9.         {
  10.             ParentIndex = parentIndex;
  11.             Value = value;
  12.         }
  13.  
  14.         public readonly Int32 ParentIndex;
  15.         public readonly String Value;
  16.     }
  17.  
  18.     public class ArithmeticExpressionTree
  19.     {
  20.         public ArithmeticExpressionTree(String rootValue)
  21.         {
  22.             nodes = new List<TreeNode> {new TreeNode(-1, rootValue)};
  23.         }
  24.  
  25.         public Int32 AddNode(Int32 parentNodeIndex, String value)
  26.         {
  27.             nodes.Add(new TreeNode(parentNodeIndex, value));
  28.             return nodes.Count - 1;
  29.         }
  30.  
  31.         public TreeNode GetNode(Int32 index)
  32.         {
  33.             return nodes[index];
  34.         }
  35.  
  36.         public List<Int32> GetAllChildrenIndexes(Int32 nodeIndex)
  37.         {
  38.             var result = new List<Int32>();
  39.             for (Int32 i = 0; i < nodes.Count; i++)
  40.             {
  41.                 if (nodes[i].ParentIndex == nodeIndex)
  42.                     result.Add(i);
  43.             }
  44.  
  45.             return result;
  46.         }
  47.  
  48.         public List<TreeNode> GetAllChildren(Int32 nodeIndex)
  49.         {
  50.             return nodes.FindAll(node => node.ParentIndex == nodeIndex);
  51.         }
  52.  
  53.         public Int32 RootIndex => 0;
  54.         public TreeNode Root => nodes[0];
  55.  
  56.         private List<TreeNode> nodes;
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement