Example #1
0
        public static TreeBinaryParentNode <T> successorIt(TreeBinaryParentNode <T> node)
        {
            TreeBinaryParentNode <T> succ = null;

            if (node == null)
            {
                return(null);
            }

            if (node.right != null)
            {
                node = (TreeBinaryParentNode <T>)node.right;
                while (node.left != null)
                {
                    node = (TreeBinaryParentNode <T>)node.left;
                }

                succ = node;
            }
            else
            {
                while (node.parent?.right == node)
                {
                    node = node.parent;
                }
                succ = node.parent;
            }

            return(succ);
        }
        private void FCADDescendantOfItselfTest()
        {
            TreeBinaryParentNode <int> firstNode   = (TreeBinaryParentNode <int>)tree.right;
            TreeBinaryParentNode <int> secondtNode = (TreeBinaryParentNode <int>)tree.right;
            TreeBinaryParentNode <int> ancestor    = commonAncestor(firstNode, secondtNode);

            Assert.Equal(tree.right, ancestor);
        }
        private void FCADifferentLevelTest()
        {
            TreeBinaryParentNode <int> firstNode  = (TreeBinaryParentNode <int>)tree.right;
            TreeBinaryParentNode <int> secondNode = (TreeBinaryParentNode <int>)tree.right.right.right;

            TreeBinaryParentNode <int> ancestor = commonAncestor(firstNode, secondNode);

            Assert.Equal(tree.right, ancestor);
        }
        private void FCANodeOfAnotherTreeTest()
        {
            TreeBinaryParentNode <int> tree2 = BuildParentTree.MinimalParentTree(new int[5] {
                1, 1, 2, 3, 0
            });

            TreeBinaryParentNode <int> firstNode   = (TreeBinaryParentNode <int>)tree.right;
            TreeBinaryParentNode <int> secondtNode = (TreeBinaryParentNode <int>)tree2.left;
            TreeBinaryParentNode <int> ancestor    = commonAncestor(firstNode, secondtNode);

            Assert.Null(ancestor);
        }
Example #5
0
        private static void TreeBinaryParentNodeBuildingTest()
        {
            int[] array = new int[5] {
                1, 5, 12, 4, 14
            };
            TreeBinaryParentNode <int> tree  = CreateBinaryTree <TreeBinaryParentNode <int>, int>(array);
            TreeBinaryParentNode <int> child = (TreeBinaryParentNode <int>)tree.right.right;

            Assert.Equal(4, child.parent.value);
            Assert.Equal(1, tree.left.value);
            Assert.Null(tree.left.left);
            Assert.Null(child.parent.left);
            Assert.Equal(14, child.parent.right.value);
        }
Example #6
0
        private static TreeBinaryParentNode <T> leftMostChild(TreeBinaryNode <T> node, TreeBinaryNode <T> lastNode)
        {
            TreeBinaryParentNode <T> lmc = null;

            if (node != null)
            {
                lmc = leftMostChild(node.left, node);
            }
            else
            {
                lmc = (TreeBinaryParentNode <T>)lastNode;
            }

            return(lmc);
        }
Example #7
0
        public static TreeBinaryParentNode <T> successor(TreeBinaryParentNode <T> node)
        {
            TreeBinaryParentNode <T> succ = null;

            if (node == null)
            {
                return(null);
            }

            if (node.right != null)
            {
                succ = leftMostChild(node.right, null);
            }
            else
            {
                succ = goingUp(node);
            }

            return(succ);
        }
Example #8
0
        private static TreeBinaryParentNode <T> goingUp(TreeBinaryParentNode <T> node)
        {
            TreeBinaryParentNode <T> parent = node.parent;

            if (parent == null)
            {
                return(null);
            }

            //while n right child of a n.parent
            TreeBinaryParentNode <T> succ = null;

            if (parent.right == node)
            {
                succ = goingUp(parent);
            }
            else
            {
                succ = parent;
            }

            return(succ);
        }
Example #9
0
        private static void TreeBinaryParentNodeBuildingTest()
        {
            int[] array = new int[7] {
                1, 5, 12, 4, 14, 28, 33
            };

            TreeBinaryParentNode <int> tree = BuildParentTree.MinimalParentTree(array);
            TreeBinaryParentNode <int> n    = (TreeBinaryParentNode <int>)tree.left.right;

            TreeBinaryParentNode <int> s = successor(n.parent);

            Assert.Equal(12, s.value);
            Assert.Equal(n, s);

            TreeBinaryParentNode <int> sIt = successorIt(n.parent);

            Assert.Equal(12, sIt.value);
            Assert.Equal(n, sIt);

            TreeBinaryParentNode <int> sn = successor(n);

            Assert.Equal(4, sn.value);
            Assert.Equal(sn, tree);

            TreeBinaryParentNode <int> snIt = successorIt(n);

            Assert.Equal(4, snIt.value);
            Assert.Equal(snIt, tree);

            TreeBinaryParentNode <int> lastNode = (TreeBinaryParentNode <int>)tree.right.right;

            Assert.Null(successor(lastNode));
            Assert.Null(successor(null));

            Assert.Null(successorIt(lastNode));
            Assert.Null(successorIt(null));
        }
        private void FCARootTest()
        {
            TreeBinaryParentNode <int> ancestor = commonAncestor(tree, tree);

            Assert.Equal(tree, ancestor);
        }
Example #11
0
 public TreeBinaryParentNode(T v, TreeBinaryParentNode <T> left, TreeBinaryParentNode <T> right, TreeBinaryParentNode <T> parent) : base(v, left, right)
 {
     this.parent = parent;
 }