public static List <LinkedList <TreeBinaryNode <T> > > getListOfDepths <T>(TreeBinaryNode <T> node)
        {
            List <LinkedList <TreeBinaryNode <T> > > list     = new List <LinkedList <TreeBinaryNode <T> > >();
            LinkedList <TreeBinaryNode <T> >         parent   = new LinkedList <TreeBinaryNode <T> >();
            LinkedList <TreeBinaryNode <T> >         children = new LinkedList <TreeBinaryNode <T> >();

            if (node != null)
            {
                children.AddLast(node);
                do
                {
                    list.Add(children);
                    parent   = children;
                    children = new LinkedList <TreeBinaryNode <T> >();

                    foreach (var el in parent)
                    {
                        if (el.left != null)
                        {
                            children.AddLast(el.left);
                        }
                        if (el.right != null)
                        {
                            children.AddLast(el.right);
                        }
                    }
                } while (children.Count > 0);
            }
            return(list);
        }
        private static int checkDepth(TreeBinaryNode <T> node)
        {
            if (node == null)
            {
                return(-1);
            }

            int leftDepth = checkDepth(node.left);

            if (leftDepth == Int32.MinValue)
            {
                return(Int32.MinValue);
            }

            int rightDepth = checkDepth(node.right);

            if (rightDepth == Int32.MinValue)
            {
                return(Int32.MinValue);
            }

            if (Math.Abs(leftDepth - rightDepth) > 1)
            {
                return(Int32.MinValue);
            }
            else
            {
                return(Math.Max(leftDepth, rightDepth) + 1);
            }
        }
Example #3
0
        public static List <LinkedList <TreeBinaryNode <T> > > getListOfDepths <T>(TreeBinaryNode <T> tree)
        {
            List <LinkedList <TreeBinaryNode <T> > > lists = new List <LinkedList <TreeBinaryNode <T> > >();

            getDepths(tree, lists, 0);
            return(lists);
        }
Example #4
0
        private static List <LinkedList <TreeBinaryNode <T> > > getDepths <T>(TreeBinaryNode <T> node, List <LinkedList <TreeBinaryNode <T> > > lists, int level)
        {
            if (node == null)
            {
                return(null);
            }

            LinkedList <TreeBinaryNode <T> > linkedList;

            if (lists.Count.Equals(level))
            {
                linkedList = new LinkedList <TreeBinaryNode <T> >();
                lists.Add(linkedList);
            }
            else
            {
                linkedList = lists[level];
            }

            linkedList.AddLast(node);
            getDepths(node.left, lists, level + 1);
            getDepths(node.right, lists, level + 1);

            return(lists);
        }
Example #5
0
                public NodePrint(TreeBinaryNode <T> left, TreeBinaryNode <T> right)
                {
                    bool leftisNull  = left == null ? true : false;
                    bool rightIsNull = right == null ? true : false;

                    this.left  = left;
                    this.right = right;

                    if (!leftisNull && !rightIsNull)
                    {
                        this.pairDesc = $"[{left.value},{right.value}] ";
                    }
                    else if (!leftisNull)
                    {
                        this.pairDesc = $"[{left.value},-] ";
                    }
                    else if (!rightIsNull)
                    {
                        this.pairDesc = $"[-,{right.value}] ";
                    }
                    else
                    {
                        this.pairDesc = nullNode;
                    }
                }
Example #6
0
        public static List <List <T> > allSequences(TreeBinaryNode <T> node)
        {
            List <List <T> > result = new List <List <T> >();

            if (node == null)
            {
                result.Add(new List <T>());
                return(result);
            }

            List <T> prefix = new List <T>();

            prefix.Add(node.value);

            List <List <T> > leftSeq  = allSequences(node.left);
            List <List <T> > rightSeq = allSequences(node.right);

            foreach (var left in leftSeq)
            {
                foreach (var right in rightSeq)
                {
                    List <List <T> > mergedList = new List <List <T> >();
                    mergeLists(left, right, prefix, mergedList);
                    result.AddRange(mergedList);
                }
            }

            return(result);
        }
Example #7
0
        public static bool IsBST(TreeBinaryNode <int> tree)
        {
            Traversal <int> t           = new Traversal <int>(TypeTraversal.inOrder, tree);
            List <int>      inOrderList = t.getTraversalList();

            return(inOrderList.Zip(inOrderList.Skip(1)).All(l => l.First < l.Second));
        }
Example #8
0
 private void postOrderTraversal(TreeBinaryNode <T> node)
 {
     if (node != null)
     {
         postOrderTraversal(node.left);
         postOrderTraversal(node.right);
         this.listOrder.Add(node.value);
     }
 }
Example #9
0
        public static TreeBinaryNode <T> commonAncestorWithoutLinkParent(TreeBinaryNode <T> root, TreeBinaryNode <T> firstNode, TreeBinaryNode <T> secondNode)
        {
            if (!cover(root, firstNode) || !cover(root, secondNode))
            {
                return(null);
            }

            return(commonAncestor(root, firstNode, secondNode));
        }
Example #10
0
        /// <summary>
        /// Calculate the depth of a Binary Tree.
        ///</summary>
        /// <param name="node"> TreeBinaryNode. The root of the Binary Tree.</param>
        /// <returns>The length of the Binary Tree.</returns>
        public static int getDepth <T>(TreeBinaryNode <T> node)
        {
            if (node == null)
            {
                return(-1);
            }

            return(Max(getDepth(node.left), getDepth(node.right)) + 1);
        }
Example #11
0
        private static bool cover(TreeBinaryNode <T> root, TreeBinaryNode <T> node)
        {
            if (root == null)
            {
                return(false);
            }
            if (root == node)
            {
                return(true);
            }

            return(cover(root.left, node) || cover(root.right, node));
        }
Example #12
0
        public static bool isBST(TreeBinaryNode <int> tree)
        {
            Traversal <int> t           = new Traversal <int>(TypeTraversal.inOrder, tree);
            List <int>      inOrderList = t.getTraversalList();

            for (int i = 0; i < inOrderList.Count - 1; i++)
            {
                if (inOrderList[i + 1] <= inOrderList[i])
                {
                    return(false);
                }
            }

            return(true);
        }
Example #13
0
            public Traversal(TypeTraversal type, TreeBinaryNode <T> t)
            {
                switch ((int)type)
                {
                case 0:
                    preOrderTraversal(t);
                    break;

                case 1:
                    inOrderTraversal(t);
                    break;

                case 2:
                    postOrderTraversal(t);
                    break;
                }
            }
Example #14
0
        public static bool isBalanced(TreeBinaryNode <T> tree)
        {
            if (tree == null)
            {
                return(true);
            }

            int diff = getDepth(tree.left) - getDepth(tree.right);

            if (Math.Abs(diff) > 1)
            {
                return(false);
            }
            else
            {
                return(isBalanced(tree.left) && isBalanced(tree.right));
            }
        }
Example #15
0
        private static TreeBinaryNode <T> commonAncestor(TreeBinaryNode <T> root, TreeBinaryNode <T> firstNode, TreeBinaryNode <T> secondNode)
        {
            if (root == firstNode || root == secondNode)
            {
                return(root);
            }

            bool firstNodeIsOnLeft  = cover(root.left, firstNode);
            bool secondNodeIsOnLeft = cover(root.left, secondNode);

            if (firstNodeIsOnLeft != secondNodeIsOnLeft)
            {
                return(root);
            }

            TreeBinaryNode <T> succNode = firstNodeIsOnLeft ? root.left: root.right;

            return(commonAncestorWithoutLinkParent(succNode, firstNode, secondNode));
        }
Example #16
0
 private static void addElements <T>(TreeBinaryNode <T> node, ref Queue <NodePrint <T> > queue)
 {
     if (node != null)
     {
         if (node.left != null && node.right != null)
         {
             queue.Enqueue(new NodePrint <T>(node.left, node.right));
         }
         else if (node.right != null)
         {
             queue.Enqueue(new NodePrint <T>(null, node.right));
         }
         else if (node.left != null)
         {
             queue.Enqueue(new NodePrint <T>(node.left, null));
         }
         else
         {
             queue.Enqueue(new NodePrint <T>(null, null));
         }
     }
 }
Example #17
0
 public static void printDescending <T>(this TreeBinaryNode <T> tree)
 {
     Utilities.PrintBinaryTree.pretty(tree);
 }
Example #18
0
            public static void pretty <T>(TreeBinaryNode <T> node)
            {
                if (node == null)
                {
                    return;
                }

                Queue <NodePrint <T> > oldQ = new Queue <NodePrint <T> >();
                Queue <NodePrint <T> > newQ = new Queue <NodePrint <T> >();

                oldQ.Enqueue(new NodePrint <T>(node));

                NodePrint <T> root = oldQ.Dequeue();

                WriteLine(root.pairDesc);
                WriteLine();

                if (root.left != null || root.right != null)
                {
                    oldQ.Enqueue(new NodePrint <T>(root.left, root.right));
                }

                while (oldQ.Count > 0)
                {
                    int ind = oldQ.Count;

                    for (int i = 0; i < ind; i++)
                    {
                        NodePrint <T> n = oldQ.Dequeue();

                        Write(n.pairDesc);

                        TreeBinaryNode <T> a = n.left;
                        TreeBinaryNode <T> b = n.right;

                        addElements(a, ref newQ);
                        addElements(b, ref newQ);
                    }

                    WriteLine();
                    WriteLine();
                    ind = newQ.Count;

                    bool atLeastOne = false;
                    for (int i = 0; i < ind; i++)
                    {
                        NodePrint <T> n = newQ.Dequeue();
                        if (n.pairDesc != nullNode)
                        {
                            atLeastOne = true;
                        }

                        oldQ.Enqueue(n);
                    }

                    if (!atLeastOne)
                    {
                        return;
                    }
                }
            }
Example #19
0
 public NodePrint(TreeBinaryNode <T> root)
 {
     this.left     = root.left;
     this.right    = root.right;
     this.pairDesc = $" --- {root.value} ---";
 }
Example #20
0
 public static bool isBalanced(TreeBinaryNode <T> tree)
 {
     return(checkDepth(tree) != Int32.MinValue);
 }