Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace ArithmeticExpressionsTree
- {
- public struct TreeNode
- {
- public TreeNode(Int32 parentIndex, String value)
- {
- ParentIndex = parentIndex;
- Value = value;
- }
- public readonly Int32 ParentIndex;
- public readonly String Value;
- }
- public class ArithmeticExpressionTree
- {
- public ArithmeticExpressionTree(String rootValue)
- {
- nodes = new List<TreeNode> {new TreeNode(-1, rootValue)};
- }
- public Int32 AddNode(Int32 parentNodeIndex, String value)
- {
- nodes.Add(new TreeNode(parentNodeIndex, value));
- return nodes.Count - 1;
- }
- public TreeNode GetNode(Int32 index)
- {
- return nodes[index];
- }
- public List<Int32> GetAllChildrenIndexes(Int32 nodeIndex)
- {
- var result = new List<Int32>();
- for (Int32 i = 0; i < nodes.Count; i++)
- {
- if (nodes[i].ParentIndex == nodeIndex)
- result.Add(i);
- }
- return result;
- }
- public List<TreeNode> GetAllChildren(Int32 nodeIndex)
- {
- return nodes.FindAll(node => node.ParentIndex == nodeIndex);
- }
- public Int32 RootIndex => 0;
- public TreeNode Root => nodes[0];
- private List<TreeNode> nodes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement