Beispiel #1
0
        public bool Add(int parentValue, int childValue)
        {
            if (this.Root != null)
            {
                TreeNode foundParentNode = this.FindNode(parentValue);
                TreeNode foundChildNode = this.FindNode(childValue);

                if (foundParentNode == null && foundChildNode != null)
                {
                    // parent not found, child found
                    TreeNode parentNode = new TreeNode(parentValue);
                    parentNode.Children.Add(foundChildNode);
                    this.Root = parentNode;
                    return true;
                }
                else if (foundParentNode != null && foundChildNode == null)
                {
                    // parent found, child not found
                    TreeNode childNode = new TreeNode(childValue);
                    foundParentNode.Children.Add(childNode);
                    return true;
                }

                return false;
            }
            else
            {
                TreeNode parentNode = new TreeNode(parentValue);
                TreeNode childNode = new TreeNode(childValue);
                parentNode.Children.Add(childNode);
                this.Root = parentNode;
                return true;
            }
        }
Beispiel #2
0
 private void PushChildren(Stack<TreeNode> stack, TreeNode node)
 {
     List<TreeNode> children = node.Children;
     for (int i = 0; i < children.Count; i++)
     {
         stack.Push(children[i]);
     }
 }
Beispiel #3
0
        private bool CheckIsSumEqual(TreeNode node, long sum)
        {
            long tempSum = 0;

            Stack<TreeNode> nodes = new Stack<TreeNode>();
            nodes.Push(node);

            while (nodes.Count != 0)
            {
                var currentNode = nodes.Pop();
                tempSum += currentNode.Value;

                for (int i = 0; i < currentNode.Children.Count; i++)
                {
                    var child = currentNode.Children[i];
                    nodes.Push(child);
                }
            }

            if (tempSum == sum)
            {
                return true;
            }

            return false;
        }
Beispiel #4
0
        public void ReadTree(TreeNode root = null)
        {
            if (root == null)
            {
                root = this.Root;
            }

            Console.WriteLine("\nRoot: " + root.Value);
            Stack<TreeNode> stack = new Stack<TreeNode>();
            this.PushChildren(stack, root);

            while (stack.Count != 0)
            {
                TreeNode node = stack.Pop();
                if (node.Children.Count != 0)
                {
                    this.PushChildren(stack, node);
                    Console.WriteLine("Middle node: " + node.Value);
                }
                else
                {
                    Console.WriteLine("Leaf node: " + node.Value);
                }
            }
        }