Esempio n. 1
0
        public void RemoveMin()
        {
            if (Root == null)
            {
                return;
            }

            // Initialize loop variables
            BinaryNode <T> currNode = Root;
            BinaryNode <T> prev     = Root;

            // Traverse BST and look for the penultimate smallest node (single last left node).
            while (currNode.GetLeft() != null)
            {
                prev     = currNode;
                currNode = currNode.GetLeft();
            }

            // Check if Root is the only node or not, and remove the smallest node from the tree.
            if (prev.Equals(Root))
            {
                Root = null;
            }
            else
            {
                prev.SetLeft(null);
            }
        }
        public void TestEquals()
        {
            BinaryNode <int> testFirstLeftBinaryNode  = new BinaryNode <int>(element: 10);
            BinaryNode <int> testFirstRightBinaryNode = new BinaryNode <int>(element: 30);
            BinaryNode <int> testFirstBinaryNode      = new BinaryNode <int>(
                element: 20,
                leftNode: testFirstLeftBinaryNode,
                rightNode: testFirstRightBinaryNode);
            BinaryNode <int> testSecondLeftBinaryNode  = new BinaryNode <int>(element: 10);
            BinaryNode <int> testSecondRightBinaryNode = new BinaryNode <int>(element: 30);
            BinaryNode <int> testSecondBinaryNode      = new BinaryNode <int>(
                element: 20,
                leftNode: testSecondLeftBinaryNode,
                rightNode: testSecondRightBinaryNode);
            BinaryNode <int> testThirdBinaryNode = new BinaryNode <int>(element: 30);

            Assert.IsTrue(testFirstBinaryNode.Equals(otherNode: testSecondBinaryNode));
            Assert.IsFalse(testFirstBinaryNode.Equals(otherNode: testThirdBinaryNode));
        }
Esempio n. 3
0
        protected override void Run()
        {
            char[] preorderedNodes = new char[] { 'a', 'b', 'd', 'e', 'c', 'f', 'g' };
            char[] inorderedNodes  = new char[] { 'd', 'b', 'e', 'a', 'f', 'c', 'g' };

            BinaryNode <char> preorderedTree = BuildPreorderedTree(preorderedNodes);
            BinaryNode <char> inorderedTree  = BuildInorderedTree(inorderedNodes);

            Console.WriteLine("Preordered Tree:");
            preorderedTree.PrintTree();

            Console.WriteLine("Inordered Tree:");
            inorderedTree.PrintTree();

            Console.WriteLine("Preordered Tree == Inordered Tree : {0}.", preorderedTree.Equals(inorderedTree));
        }