public void EnumerateInOrderHandlesTinyTree()
        {
            PointerBackedBinaryTree <int> testTree = new PointerBackedBinaryTree <int>();

            testTree.Root       = new PointerBackedBinaryTreeNode <int>(1, null);
            testTree.Root.Left  = new PointerBackedBinaryTreeNode <int>(2, testTree.Root);
            testTree.Root.Right = new PointerBackedBinaryTreeNode <int>(3, testTree.Root);

            CollectionAssert.AreEqual(new int[] { 2, 1, 3 }, testTree.EnumerateInOrder());
        }
        public void EnumerateInOrderHandlesLargeTree()
        {
            PointerBackedBinaryTree <int> testTree = new PointerBackedBinaryTree <int>();

            testTree.Root                   = new PointerBackedBinaryTreeNode <int>(1, null);
            testTree.Root.Right             = new PointerBackedBinaryTreeNode <int>(3, testTree.Root);
            testTree.Root.Right.Left        = new PointerBackedBinaryTreeNode <int>(2, testTree.Root.Right);
            testTree.Root.Right.Right       = new PointerBackedBinaryTreeNode <int>(5, testTree.Root.Right);
            testTree.Root.Right.Right.Left  = new PointerBackedBinaryTreeNode <int>(4, testTree.Root.Right.Right);
            testTree.Root.Right.Right.Right = new PointerBackedBinaryTreeNode <int>(6, testTree.Root.Right.Right);

            CollectionAssert.AreEqual(new int[] { 1, 2, 3, 4, 5, 6 }, testTree.EnumerateInOrder());
        }
        public void SetParentPointerReplacesRootNode()
        {
            PointerBackedBinaryTree <int> testTree = new PointerBackedBinaryTree <int>();

            testTree.Root      = new PointerBackedBinaryTreeNode <int>(1, null);
            testTree.Root.Left = new PointerBackedBinaryTreeNode <int>(2, testTree.Root);

            testTree.SetParentPointer(testTree.Root, testTree.Root.Left);
            Assert.AreEqual(2, testTree.Root.Data);
            Assert.IsNull(testTree.Root.Parent);
            Assert.IsNull(testTree.Root.Left);
            Assert.IsNull(testTree.Root.Right);
        }
        public void RotateRightRotatesRootNode()
        {
            PointerBackedBinaryTree <int> testTree = new PointerBackedBinaryTree <int>();

            testTree.Root      = new PointerBackedBinaryTreeNode <int>(1, null);
            testTree.Root.Left = new PointerBackedBinaryTreeNode <int>(2, testTree.Root);

            testTree.RotateRight(testTree.Root);

            // Validate the entire tree structure after rotation, including internal pointers.
            Assert.AreEqual(2, testTree.Root.Data);
            Assert.IsNull(testTree.Root.Parent);
            Assert.IsNull(testTree.Root.Left);
            Assert.AreEqual(1, testTree.Root.Right.Data);
            Assert.AreEqual(testTree.Root, testTree.Root.Right.Parent);
            Assert.IsNull(testTree.Root.Right.Right);
            Assert.IsNull(testTree.Root.Right.Left);
        }
        public void RotateRightRotatesNode()
        {
            PointerBackedBinaryTree <int> testTree = new PointerBackedBinaryTree <int>();

            testTree.Root                   = new PointerBackedBinaryTreeNode <int>(1, null);
            testTree.Root.Right             = new PointerBackedBinaryTreeNode <int>(2, testTree.Root);
            testTree.Root.Right.Left        = new PointerBackedBinaryTreeNode <int>(3, testTree.Root.Right);
            testTree.Root.Right.Right       = new PointerBackedBinaryTreeNode <int>(4, testTree.Root.Right);
            testTree.Root.Right.Right.Left  = new PointerBackedBinaryTreeNode <int>(5, testTree.Root.Right.Right);
            testTree.Root.Right.Right.Right = new PointerBackedBinaryTreeNode <int>(6, testTree.Root.Right.Right);

            testTree.RotateRight(testTree.Root.Right);

            // Validate the tree structure
            Assert.AreEqual(1, testTree.Root.Data);
            Assert.AreEqual(3, testTree.Root.Right.Data);
            Assert.AreEqual(2, testTree.Root.Right.Right.Data);
            Assert.AreEqual(4, testTree.Root.Right.Right.Right.Data);
            Assert.AreEqual(5, testTree.Root.Right.Right.Right.Left.Data);
            Assert.AreEqual(6, testTree.Root.Right.Right.Right.Right.Data);
        }