Ejemplo n.º 1
0
 private static BinaryTreeNodeWithParent <T> MinNode <T>(BinaryTreeNodeWithParent <T> node) where T : IComparable <T>
 {
     while (node.Left != null)
     {
         node = node.Left;
     }
     return(node);
 }
        private static BinaryTreeNodeWithParent<int> AddChildren(BinaryTreeNodeWithParent<int> parent, int[] sortedArray)
        {
            if (sortedArray.Length == 0) return null;

            var newNode = new BinaryTreeNodeWithParent<int>(sortedArray[sortedArray.Length / 2]);
            newNode.Parent = parent;

            newNode.Left = AddChildren(newNode, sortedArray.Take(sortedArray.Length / 2).ToArray());
            newNode.Right = AddChildren(newNode, sortedArray.Skip(sortedArray.Length / 2 + 1).ToArray());

            return newNode;
        }
 //Adds all leaves of a binary tree to a given list
 private static void GetLeaves(BinaryTreeNodeWithParent <int> origin, ref NodePath leaves)
 {
     if (origin == null)
     {
         return;
     }
     if (origin.Left == null && origin.Right == null)
     {
         leaves.Add(origin);
         return;
     }
     GetLeaves(origin.Left, ref leaves);
     GetLeaves(origin.Right, ref leaves);
 }
        public static List <NodePath> FindPathsWithSum(this BinaryTreeNodeWithParent <int> root, int sum)
        {
            var paths  = new List <NodePath>();
            var leaves = new NodePath();

            GetLeaves(root, ref leaves);

            foreach (var leaf in leaves)
            {
                paths.AddRange(GetPathsWithSum(leaf, sum));
            }

            paths.ForEach(x => x.Reverse());

            return(paths.Count != 0 ? paths : null);
        }
Ejemplo n.º 5
0
        private static bool CompareNodes <T>(BinaryTreeNodeWithParent <T> subtree, BinaryTreeNodeWithParent <T> tree) where T : IComparable <T>
        {
            //Check if we are finished...
            if (subtree == null && tree == null)
            {
                return(true);
            }

            //otherwise compare children
            bool left;

            if (subtree.Left == null && tree.Left == null)
            {
                left = true;
            }
            else if (subtree.Left == null || tree.Left == null)
            {
                left = false;
            }
            else
            {
                left = subtree.Left.Data.CompareTo(tree.Left.Data) == 0;
            }

            bool right;

            if (subtree.Right == null && tree.Right == null)
            {
                right = true;
            }
            else if (subtree.Right == null || tree.Right == null)
            {
                right = false;
            }
            else
            {
                right = subtree.Right.Data.CompareTo(tree.Right.Data) == 0;
            }

            //If both children are null or equal, check their children.
            if (left && right)
            {
                return(CompareNodes(subtree.Left, tree.Left) && CompareNodes(subtree.Right, tree.Right));
            }

            return(false);
        }
        //Travel upwards from leaf node and return any path that adds to the sum
        //We don't stop when we reach the sum since there may be negative values
        private static List <NodePath> GetPathsWithSum(BinaryTreeNodeWithParent <int> node, int sum)
        {
            NodePath        currPath     = new NodePath();
            int             currSum      = 0;
            List <NodePath> pathsWithSum = new List <NodePath>();

            while (node != null)
            {
                currSum += node.Data;
                currPath.Add(node);
                if (currSum == sum)
                {
                    pathsWithSum.Add(currPath.ToList());                 //ToList to make a copy
                }
                node = node.Parent;
            }

            return(pathsWithSum);
        }
Ejemplo n.º 7
0
        public static BinaryTreeNodeWithParent <T> GetSuccessor <T>(this BinaryTreeNodeWithParent <T> node) where T : IComparable <T>
        {
            if (node.Right != null)
            {
                return(MinNode(node.Right));
            }

            while (node.Parent != null)
            {
                BinaryTreeNodeWithParent <T> prev = node;
                node = node.Parent;
                if (node?.Left == prev)
                {
                    return(node);
                }
            }

            return(null);
        }
Ejemplo n.º 8
0
        public static bool IsSubtreeOf <T>(this BinaryTreeNodeWithParent <T> subtreeRoot, BinaryTreeNodeWithParent <T> treeRoot) where T : IComparable <T>
        {
            if (subtreeRoot == null || treeRoot == null)
            {
                return(false);
            }

            BinaryTreeNodeWithParent <T> currNode;
            var queue = new Queue <BinaryTreeNodeWithParent <T> >();

            queue.Enqueue(treeRoot);

            //Iterate through each node and compare it to the first node of the subtree. If they match,
            //compare for each child in the subtree.
            while (!queue.IsEmpty)
            {
                currNode = queue.Dequeue();

                if (currNode.Data.CompareTo(subtreeRoot.Data) == 0)
                {
                    if (CompareNodes(subtreeRoot, currNode))
                    {
                        return(true);
                    }
                }

                if (currNode.Left != null)
                {
                    queue.Enqueue(currNode.Left);
                }
                if (currNode.Right != null)
                {
                    queue.Enqueue(currNode.Right);
                }
            }

            return(false);
        }