/*
         *  Given values of two nodes n1 and n2 in a Binary Tree,
         *  find the Lowest Common Ancestor (LCA)
         *
         *  Time complexity: O(n)
         */
        public static CtciTreeNode <int> FindFromBinaryTree(CtciTreeNode <int> parent, int value1, int value2)
        {
            if (parent.Value == value1 || parent.Value == value2)
            {
                return(parent);
            }

            CtciTreeNode <int> leftNode  = null;
            CtciTreeNode <int> rightNode = null;

            if (parent.Left != null)
            {
                leftNode = FindFromBinaryTree(parent.Left, value1, value2);
            }

            if (parent.Right != null)
            {
                rightNode = FindFromBinaryTree(parent.Right, value1, value2);
            }

            if (leftNode != null && rightNode != null)
            {
                return(parent);
            }
            else if (leftNode != null || rightNode != null)
            {
                return((leftNode != null) ? leftNode : rightNode);
            }
            else
            {
                return(null);
            }
        }
        protected List <LinkedList <T> > AllSequences(CtciTreeNode <T> node)
        {
            List <LinkedList <T> > result = new List <LinkedList <T> >();

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

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

            prefix.AddLast(node.Value);

            List <LinkedList <T> > left  = AllSequences(node.Left);
            List <LinkedList <T> > right = AllSequences(node.Right);

            foreach (LinkedList <T> leftList in left)
            {
                foreach (LinkedList <T> rightList in right)
                {
                    List <LinkedList <T> > weaved = new List <LinkedList <T> >();
                    WeaveLists(leftList, rightList, weaved, prefix);
                    CopyListNodes(weaved, result);
                }
            }

            return(result);
        }
        public void TestBinarySearchTree()
        {
            CtciTreeNode <int> node20 = CtciTreeNode <int> .CtciBinaryTreeNode(20);

            CtciTreeNode <int> node8 = CtciTreeNode <int> .CtciBinaryTreeNode(8);

            CtciTreeNode <int> node22 = CtciTreeNode <int> .CtciBinaryTreeNode(22);

            CtciTreeNode <int> node4 = CtciTreeNode <int> .CtciBinaryTreeNode(4);

            CtciTreeNode <int> node12 = CtciTreeNode <int> .CtciBinaryTreeNode(12);

            CtciTreeNode <int> node10 = CtciTreeNode <int> .CtciBinaryTreeNode(10);

            CtciTreeNode <int> node14 = CtciTreeNode <int> .CtciBinaryTreeNode(14);

            node20.Left  = node8;
            node20.Right = node22;

            node8.Left  = node4;
            node8.Right = node12;

            node12.Left  = node10;
            node12.Right = node14;

            Assert.Equal(node12, Ancestor.FindFromBST(node20, 10, 14));
            Assert.Equal(node8, Ancestor.FindFromBST(node20, 14, 8));
            Assert.Equal(node20, Ancestor.FindFromBST(node20, 10, 22));
        }
        public void BuildSequence()
        {
            CtciTreeNode <int> node1 = CtciTreeNode <int> .CtciBinaryTreeNode(1);

            CtciTreeNode <int> node2 = CtciTreeNode <int> .CtciBinaryTreeNode(2);

            CtciTreeNode <int> node3 = CtciTreeNode <int> .CtciBinaryTreeNode(3);

            CtciTreeNode <int> node4 = CtciTreeNode <int> .CtciBinaryTreeNode(4);

            CtciTreeNode <int> node5 = CtciTreeNode <int> .CtciBinaryTreeNode(5);

            CtciTreeNode <int> node6 = CtciTreeNode <int> .CtciBinaryTreeNode(6);

            node4.Left  = node2;
            node4.Right = node6;

            List <LinkedList <int> > results1 = sequence.BuildSequence(node4);

            Assert.Equal(2, results1.Count);

            node2.Left = node1;

            List <LinkedList <int> > results2 = sequence.BuildSequence(node4);

            Assert.Equal(3, results2.Count);

            node2.Right = node3;

            List <LinkedList <int> > results3 = sequence.BuildSequence(node4);

            Assert.Equal(8, results3.Count);
        }
        public void TestBinaryTree()
        {
            CtciTreeNode <int> node1 = CtciTreeNode <int> .CtciBinaryTreeNode(1);

            CtciTreeNode <int> node2 = CtciTreeNode <int> .CtciBinaryTreeNode(2);

            CtciTreeNode <int> node3 = CtciTreeNode <int> .CtciBinaryTreeNode(3);

            CtciTreeNode <int> node4 = CtciTreeNode <int> .CtciBinaryTreeNode(4);

            CtciTreeNode <int> node5 = CtciTreeNode <int> .CtciBinaryTreeNode(5);

            CtciTreeNode <int> node6 = CtciTreeNode <int> .CtciBinaryTreeNode(6);

            CtciTreeNode <int> node7 = CtciTreeNode <int> .CtciBinaryTreeNode(7);

            node1.Left  = node2;
            node1.Right = node3;

            node2.Left  = node4;
            node2.Right = node5;

            node3.Left  = node6;
            node3.Right = node7;

            Assert.Equal(node2, Ancestor.FindFromBinaryTree(node1, 4, 5));
            Assert.Equal(node1, Ancestor.FindFromBinaryTree(node1, 4, 6));
            Assert.Equal(node1, Ancestor.FindFromBinaryTree(node1, 3, 4));
            Assert.Equal(node2, Ancestor.FindFromBinaryTree(node1, 2, 4));
        }
        /*
         *  Given values of two nodes n1 and n2 in a Binary Search Tree,
         *  find the Lowest Common Ancestor (LCA)
         *
         *  Time complexity: O(log(n))
         */
        public static CtciTreeNode <int> FindFromBST(CtciTreeNode <int> parent, int value1, int value2)
        {
            if (value1 > value2)
            {
                int tmp = value2;
                value2 = value1;
                value1 = tmp;
            }

            if (parent.Value > value1 && parent.Value < value2)
            {
                return(parent);
            }

            if (parent.Value == value1 || parent.Value == value2)
            {
                return(parent);
            }

            if (parent.Value > value1 && parent.Value > value2)
            {
                return(FindFromBST(parent.Left, value1, value2));
            }
            else
            {
                return(FindFromBST(parent.Right, value1, value2));
            }
        }
Exemplo n.º 7
0
        public bool FindRoute2(CtciTreeNode <int> node1, CtciTreeNode <int> node2)
        {
            if (node1 == node2)
            {
                return(true);
            }

            Queue <CtciTreeNode <int> > queue = new Queue <CtciTreeNode <int> >();

            queue.Enqueue(node1);
            while (queue.Count > 0)
            {
                CtciTreeNode <int> node = queue.Dequeue();
                foreach (CtciTreeNode <int> child in node.Children)
                {
                    if (child == node2)
                    {
                        return(true);
                    }

                    queue.Enqueue(child);
                }
            }

            return(false);
        }
        protected void AddToTree(CtciTreeNode <int> head, CtciTreeNode <int> newNode)
        {
            CtciTreeNode <int> parent = head;
            CtciTreeNode <int> child  = head;

            while (child != null)
            {
                parent = child;

                if (newNode.Value <= child.Value)
                {
                    child = child.Left;
                }
                else
                {
                    child = child.Right;
                }
            }

            if (newNode.Value <= parent.Value)
            {
                parent.Left = newNode;
            }
            else
            {
                parent.Right = newNode;
            }
        }
        public CtciTreeNode <int> CreateMinimalHeight2(int[] sortedArr)
        {
            int start  = sortedArr.GetLowerBound(0);
            int finish = sortedArr.GetUpperBound(0);
            CtciTreeNode <int> head = CreateTreeRecursively(sortedArr, start, finish);

            return(head);
        }
        protected CtciTreeNode <T> FindMostLeft(CtciTreeNode <T> node)
        {
            while (node.Left != null)
            {
                node = node.Left;
            }

            return(node);
        }
        public List <LinkedList <T> > BuildSequence(CtciTreeNode <T> head)
        {
            if (head == null)
            {
                throw new Exception("Incorrect Data");
            }

            return(AllSequences(head));
        }
        protected CtciTreeNode <T> FindInOrderParent(CtciTreeNode <T> node)
        {
            while (node == node.Parent.Right)
            {
                node = node.Parent;
            }

            return(node.Parent);
        }
        public CtciTreeNode <T> FindAncestor(CtciTreeNode <T> head, CtciTreeNode <T> nodeP, CtciTreeNode <T> nodeQ)
        {
            if (head == null || nodeP == null || nodeQ == null)
            {
                throw new Exception("Incorrect Data");
            }

            Q08_Result <T> result = FindRecursively(head, nodeP, nodeQ);

            return(result.Node);
        }
 public CtciTreeNode <T> FindSuccessor(CtciTreeNode <T> node)
 {
     if (node.Right != null)
     {
         return(FindMostLeft(node.Right));
     }
     else
     {
         return(FindInOrderParent(node));
     }
 }
Exemplo n.º 15
0
        public void FindAncestor()
        {
            CtciTreeNode <int> node1 = CtciTreeNode <int> .CtciBinaryTreeNode(1);

            CtciTreeNode <int> node2 = CtciTreeNode <int> .CtciBinaryTreeNode(2);

            CtciTreeNode <int> node3 = CtciTreeNode <int> .CtciBinaryTreeNode(3);

            CtciTreeNode <int> node4 = CtciTreeNode <int> .CtciBinaryTreeNode(4);

            CtciTreeNode <int> node5 = CtciTreeNode <int> .CtciBinaryTreeNode(5);

            CtciTreeNode <int> node6 = CtciTreeNode <int> .CtciBinaryTreeNode(6);

            CtciTreeNode <int> node7 = CtciTreeNode <int> .CtciBinaryTreeNode(7);

            CtciTreeNode <int> node8 = CtciTreeNode <int> .CtciBinaryTreeNode(8);

            CtciTreeNode <int> node9 = CtciTreeNode <int> .CtciBinaryTreeNode(9);

            CtciTreeNode <int> node10 = CtciTreeNode <int> .CtciBinaryTreeNode(10);

            CtciTreeNode <int> node11 = CtciTreeNode <int> .CtciBinaryTreeNode(11);

            CtciTreeNode <int> node12 = CtciTreeNode <int> .CtciBinaryTreeNode(12);

            node1.Left  = node2;
            node1.Right = node3;

            node2.Left  = node4;
            node2.Right = node5;

            node4.Left  = node8;
            node4.Right = node9;

            node5.Left  = node10;
            node5.Right = node11;

            node3.Left  = node6;
            node3.Right = node7;

            node7.Left = node12;

            Assert.Equal(node2, commonAncestor.FindAncestor(node1, node9, node10));
            Assert.Equal(node3, commonAncestor.FindAncestor(node1, node6, node12));
            Assert.Equal(node3, commonAncestor.FindAncestor(node1, node3, node12));
            Assert.Equal(node1, commonAncestor.FindAncestor(node1, node8, node7));
        }
        public CtciTreeNode <int> CreateMinimalHeight(int[] sortedArr)
        {
            if (sortedArr.Length <= 0)
            {
                throw new Exception("Incorrect Data");
            }

            int value = sortedArr[sortedArr.GetUpperBound(0) / 2];
            CtciTreeNode <int> head = CtciTreeNode <int> .CtciBinaryTreeNode(value);

            int start  = sortedArr.GetLowerBound(0);
            int finish = (sortedArr.GetUpperBound(0) / 2) - 1;

            Stack <Tuple <int, int> > stack = new Stack <Tuple <int, int> >();

            stack.Push(new Tuple <int, int>(start, finish));

            start  = (sortedArr.GetUpperBound(0) / 2) + 1;
            finish = sortedArr.GetUpperBound(0);
            stack.Push(new Tuple <int, int>(start, finish));

            while (stack.Count > 0)
            {
                Tuple <int, int> partition = stack.Pop();

                if (partition.Item1 > partition.Item2)
                {
                    continue;
                }

                value = sortedArr[(partition.Item1 + partition.Item2) / 2];
                CtciTreeNode <int> node = CtciTreeNode <int> .CtciBinaryTreeNode(value);

                AddToTree(head, node);

                start  = partition.Item1;
                finish = ((partition.Item1 + partition.Item2) / 2) - 1;

                stack.Push(new Tuple <int, int>(start, finish));

                start  = ((partition.Item1 + partition.Item2) / 2) + 1;
                finish = partition.Item2;

                stack.Push(new Tuple <int, int>(start, finish));
            }

            return(head);
        }
        public CtciTreeNode <int> CreateTreeRecursively(int[] sortedArr, int start, int finish)
        {
            if (start > finish)
            {
                return(null);
            }

            int mid   = (start + finish) / 2;
            int value = sortedArr[mid];
            CtciTreeNode <int> node = CtciTreeNode <int> .CtciBinaryTreeNode(value);

            node.Left  = CreateTreeRecursively(sortedArr, start, mid - 1);
            node.Right = CreateTreeRecursively(sortedArr, mid + 1, finish);

            return(node);
        }
Exemplo n.º 18
0
        public void FindRoute2()
        {
            CtciTreeNode <int> node1 = new CtciTreeNode <int>(1);
            CtciTreeNode <int> node2 = new CtciTreeNode <int>(1);
            CtciTreeNode <int> node3 = new CtciTreeNode <int>(1);
            CtciTreeNode <int> node4 = new CtciTreeNode <int>(1);
            CtciTreeNode <int> node5 = new CtciTreeNode <int>(1);

            node1.Children.Add(node2);
            node1.Children.Add(node4);

            node2.Children.Add(node5);

            node5.Children.Add(node3);

            Assert.True(route.FindRoute2(node1, node3));
            Assert.False(route.FindRoute2(node2, node1));
        }
Exemplo n.º 19
0
        protected bool Validate(CtciTreeNode <int> current, int?min, int?max)
        {
            if ((min != null && current.Value <= min) || (max != null && current.Value > max))
            {
                return(false);
            }

            if (current.Left != null && Validate(current.Left, min, current.Value) == false)
            {
                return(false);
            }

            if (current.Right != null && Validate(current.Right, current.Value, max) == false)
            {
                return(false);
            }

            return(true);
        }
        protected Q08_Result <T> FindRecursively(CtciTreeNode <T> head, CtciTreeNode <T> nodeP, CtciTreeNode <T> nodeQ)
        {
            if (head == null)
            {
                return(new Q08_Result <T>(null, false));
            }

            if (head == nodeP && head == nodeQ)
            {
                return(new Q08_Result <T>(head, true));
            }

            Q08_Result <T> resultLeft = FindRecursively(head.Left, nodeP, nodeQ);

            if (resultLeft.IsAncestor)
            {
                return(resultLeft);
            }

            Q08_Result <T> resultRight = FindRecursively(head.Right, nodeP, nodeQ);

            if (resultRight.IsAncestor)
            {
                return(resultRight);
            }

            if (resultLeft.Node != null && resultRight.Node != null)
            {
                return(new Q08_Result <T>(head, true));
            }
            else if (head == nodeP || head == nodeQ)
            {
                bool isAncestor = resultLeft.Node != null || resultRight.Node != null;
                return(new Q08_Result <T>(head, isAncestor));
            }
            else
            {
                return(new Q08_Result <T>(resultLeft.Node != null ? resultLeft.Node : resultRight.Node, false));
            }
        }
        public void Validate()
        {
            CtciTreeNode <int> node1 = CtciTreeNode <int> .CtciBinaryTreeNode(1);

            CtciTreeNode <int> node2 = CtciTreeNode <int> .CtciBinaryTreeNode(2);

            CtciTreeNode <int> node3 = CtciTreeNode <int> .CtciBinaryTreeNode(3);

            CtciTreeNode <int> node4 = CtciTreeNode <int> .CtciBinaryTreeNode(4);

            CtciTreeNode <int> node5 = CtciTreeNode <int> .CtciBinaryTreeNode(5);

            CtciTreeNode <int> node6 = CtciTreeNode <int> .CtciBinaryTreeNode(6);

            CtciTreeNode <int> node7 = CtciTreeNode <int> .CtciBinaryTreeNode(7);

            CtciTreeNode <int> node8 = CtciTreeNode <int> .CtciBinaryTreeNode(8);

            CtciTreeNode <int> node9 = CtciTreeNode <int> .CtciBinaryTreeNode(9);

            node5.Left  = node3;
            node5.Right = node8;

            node3.Left  = node2;
            node3.Right = node4;

            node2.Left = node1;

            node8.Left  = node6;
            node8.Right = node9;

            node6.Right = node7;

            Assert.True(validateBst.Validate(node5));

            node6.Left = node7;

            Assert.False(validateBst.Validate(node5));
        }
        public void FindSuccessor()
        {
            CtciTreeNode <int> node1 = new CtciTreeNode <int>(1);

            node1.Children.Add(null);
            node1.Children.Add(null);

            CtciTreeNode <int> node2 = new CtciTreeNode <int>(2);

            node2.Children.Add(null);
            node2.Children.Add(null);

            CtciTreeNode <int> node3 = new CtciTreeNode <int>(3);

            node3.Children.Add(null);
            node3.Children.Add(null);

            CtciTreeNode <int> node4 = new CtciTreeNode <int>(4);

            node4.Children.Add(null);
            node4.Children.Add(null);

            CtciTreeNode <int> node5 = new CtciTreeNode <int>(5);

            node5.Children.Add(null);
            node5.Children.Add(null);

            CtciTreeNode <int> node6 = new CtciTreeNode <int>(6);

            node6.Children.Add(null);
            node6.Children.Add(null);

            CtciTreeNode <int> node7 = new CtciTreeNode <int>(7);

            node7.Children.Add(null);
            node7.Children.Add(null);

            CtciTreeNode <int> node8 = new CtciTreeNode <int>(8);

            node8.Children.Add(null);
            node8.Children.Add(null);

            CtciTreeNode <int> node9 = new CtciTreeNode <int>(9);

            node9.Children.Add(null);
            node9.Children.Add(null);

            node1.Left   = node2;
            node2.Parent = node1;

            node1.Right  = node3;
            node3.Parent = node1;

            node3.Left   = node6;
            node6.Parent = node3;
            node3.Right  = node7;
            node7.Parent = node3;

            node2.Left   = node4;
            node4.Parent = node2;
            node2.Right  = node5;
            node5.Parent = node2;

            node5.Left   = node8;
            node8.Parent = node5;
            node5.Right  = node9;
            node9.Parent = node5;

            Assert.Equal(successor.FindSuccessor(node9), node1);
            Assert.Equal(successor.FindSuccessor(node8), node5);
            Assert.Equal(successor.FindSuccessor(node5), node9);
            Assert.Equal(successor.FindSuccessor(node2), node8);

            Assert.NotEqual(successor.FindSuccessor(node2), node4);
        }
Exemplo n.º 23
0
 public bool Validate(CtciTreeNode <int> head)
 {
     return(Validate(head, null, null));
 }
Exemplo n.º 24
0
        public void CreateMinimalHeight2()
        {
            int[] sortedArr1 = new int[] { 1, 2, 3, 4, 5, 6, 7 };
            int[] sortedArr2 = new int[] { 1, 2, 3, 4, 5 };

            CtciTreeNode <int> node1 = CtciTreeNode <int> .CtciBinaryTreeNode(1);

            CtciTreeNode <int> node2 = CtciTreeNode <int> .CtciBinaryTreeNode(2);

            CtciTreeNode <int> node3 = CtciTreeNode <int> .CtciBinaryTreeNode(3);

            CtciTreeNode <int> node4 = CtciTreeNode <int> .CtciBinaryTreeNode(4);

            CtciTreeNode <int> node5 = CtciTreeNode <int> .CtciBinaryTreeNode(5);

            CtciTreeNode <int> node6 = CtciTreeNode <int> .CtciBinaryTreeNode(6);

            CtciTreeNode <int> node7 = CtciTreeNode <int> .CtciBinaryTreeNode(7);

            node4.Left  = node2;
            node4.Right = node6;

            node2.Left  = node1;
            node2.Right = node3;

            node6.Left  = node5;
            node6.Right = node7;

            CtciTreeNode <int> result = minimal.CreateMinimalHeight2(sortedArr1);

            Assert.True(node4.Value == result.Value);
            Assert.True(node4.Left.Value == result.Left.Value);
            Assert.True(node4.Left.Left.Value == result.Left.Left.Value);
            Assert.True(node4.Left.Right.Value == result.Left.Right.Value);
            Assert.True(node4.Right.Value == result.Right.Value);
            Assert.True(node4.Right.Left.Value == result.Right.Left.Value);
            Assert.True(node4.Right.Right.Value == result.Right.Right.Value);

            node1 = CtciTreeNode <int> .CtciBinaryTreeNode(1);

            node2 = CtciTreeNode <int> .CtciBinaryTreeNode(2);

            node3 = CtciTreeNode <int> .CtciBinaryTreeNode(3);

            node4 = CtciTreeNode <int> .CtciBinaryTreeNode(4);

            node5 = CtciTreeNode <int> .CtciBinaryTreeNode(5);

            result = minimal.CreateMinimalHeight2(sortedArr2);

            node3.Left  = node1;
            node3.Right = node4;

            node1.Right = node2;

            node4.Right = node5;

            Assert.True(node3.Value == result.Value);
            Assert.True(node3.Left.Value == result.Left.Value);
            Assert.True(node3.Right.Value == result.Right.Value);
            Assert.True(node3.Left.Right.Value == result.Left.Right.Value);
            Assert.True(node3.Right.Right.Value == result.Right.Right.Value);
        }
 public Q08_Result(CtciTreeNode <T> node, bool isAncestor)
 {
     this.node       = node;
     this.isAncestor = isAncestor;
 }