Пример #1
0
        public void TestLevelofThree()
        {
            /// Test height of three
            BT testTree = new BT();

            testTree.Root = new Nodeb(100);

            Nodeb leftChild = new Nodeb(5);

            testTree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            testTree.Root.Right = rightChild;

            Nodeb leftLeftChild = new Nodeb(15);

            testTree.Root.Left.Left = leftLeftChild;

            Nodeb leftRightChild = new Nodeb(20);

            testTree.Root.Left.Right = leftRightChild;


            Assert.Equal(3, Program.FindLevel(testTree));
        }
Пример #2
0
        public void TestNonPerfectTree()
        {
            /// Test non perfect tree
            BT testTree = new BT();

            testTree.Root = new Nodeb(100);

            Nodeb leftChild = new Nodeb(5);

            testTree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            testTree.Root.Right = rightChild;


            Nodeb rightLeftRightChild = new Nodeb(12);

            testTree.Root.Right.Left = rightLeftRightChild;

            Nodeb rightRightChild = new Nodeb(15);

            testTree.Root.Right.Right = rightRightChild;

            Assert.Equal(2, Program.CalculateBinaryTreeHeight(testTree.Root));
        }
Пример #3
0
        public void BreadthFirstOnNonPerfect()
        {
            /// Breadth first of non perfect tree
            BT testTree = new BT();

            testTree.Root = new Nodeb(100);

            Nodeb leftChild = new Nodeb(5);

            testTree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            testTree.Root.Right = rightChild;

            Nodeb rightLeftLeaf = new Nodeb(30);

            testTree.Root.Right.Left = rightLeftLeaf;

            Nodeb rightRightLeaf = new Nodeb(35);

            testTree.Root.Right.Right = rightRightLeaf;
            List <int> expected = new List <int> {
                100, 5, 10, 30, 35
            };

            Assert.Equal(expected, Program.BreadthFirst(testTree));
        }
Пример #4
0
        /// <summary>
        /// Take in a binary tree and traverses it using breadth first principles, using a queue
        /// </summary>
        /// <param name="tree">binary tree</param>
        /// <returns> list of tree nodes in breadth first order</returns>
        public static List <int> BreadthFirst(BT tree)
        {
            List <int> list    = new List <int>();
            Queue      bfQueue = new Queue();

            bfQueue.Enqueue(tree.Root);
            if (tree.Root == null)
            {
                return(null);
            }
            while (bfQueue.Front != null)
            {
                Nodeb temp = bfQueue.Dequeue();
                list.Add(temp.Value);
                Console.Write($" {temp.Value} =>");
                if (temp.Left != null)
                {
                    bfQueue.Enqueue(temp.Left);
                }
                if (temp.Right != null)
                {
                    bfQueue.Enqueue(temp.Right);
                }
            }
            return(list);
        }
Пример #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            BT tree = new BT();

            tree.Root = new Nodeb(100);

            Nodeb leftChild = new Nodeb(5);

            tree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            tree.Root.Right = rightChild;

            Nodeb leftLeftLeaf = new Nodeb(15);

            tree.Root.Left.Left = leftLeftLeaf;

            Nodeb leftRightLeaf = new Nodeb(200);

            tree.Root.Left.Right = leftRightLeaf;

            Nodeb rightLeftLeaf = new Nodeb(30);

            tree.Root.Right.Left = rightLeftLeaf;

            Nodeb rightRightLeaf = new Nodeb(150);

            tree.Root.Right.Right = rightRightLeaf;

            Console.WriteLine(FindMaxValue(tree));
            Console.ReadLine();
        }
Пример #6
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            BT tree = new BT();

            tree.Root = new Nodeb(100);

            Nodeb leftChild = new Nodeb(5);

            tree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            tree.Root.Right = rightChild;

            Nodeb leftLeftLeaf = new Nodeb(15);

            tree.Root.Left.Left = leftLeftLeaf;

            Nodeb leftRightLeaf = new Nodeb(20);

            tree.Root.Left.Right = leftRightLeaf;

            Nodeb rightLeftLeaf = new Nodeb(30);

            tree.Root.Right.Left = rightLeftLeaf;

            Nodeb rightRightLeaf = new Nodeb(35);

            tree.Root.Right.Right = rightRightLeaf;

            BreadthFirst(tree);
            Console.ReadLine();
        }
Пример #7
0
        public void TestHeightofThree()
        {
            /// Test height of three
            BT testTree = new BT();

            testTree.Root = new Nodeb(100);

            Nodeb leftChild = new Nodeb(5);

            testTree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            testTree.Root.Right = rightChild;

            Nodeb leftLeftChild = new Nodeb(15);

            testTree.Root.Left.Left = leftLeftChild;

            Nodeb leftRightChild = new Nodeb(20);

            testTree.Root.Left.Right = leftRightChild;

            Nodeb rightRightChild = new Nodeb(20);

            testTree.Root.Right.Right = rightRightChild;

            Nodeb leftLeftLeftChild = new Nodeb(15);

            testTree.Root.Left.Left.Left = leftLeftLeftChild;


            Assert.Equal(3, Program.CalculateBinaryTreeHeight(testTree.Root));
        }
Пример #8
0
        /// <summary>
        /// Recursive method to do the traversal and logic to determine max value
        /// </summary>
        /// <param name="root">root of binary tree</param>
        /// <returns>Return maximum value to initial method</returns>
        public static int FindMaxValue(Nodeb root)
        {
            int temp     = root.Value;
            int leftMax  = 0;
            int rightMax = 0;

            if (root == null)
            {
                throw null;
            }

            if (root.Left != null)
            {
                leftMax = FindMaxValue(root.Left);
            }

            if (root.Right != null)
            {
                rightMax = FindMaxValue(root.Right);
            }

            if (temp < leftMax)
            {
                temp = leftMax;
            }

            if (temp < rightMax)
            {
                temp = rightMax;
            }
            return(temp);
        }
Пример #9
0
        public void AddOnBinarySearchTree()
        {
            /// Add for right branch
            BTS   testTree = new BTS(77);
            Nodeb testNode = new Nodeb(77);

            testTree.Add(88);
            Assert.Equal(88, testTree.Root.Right.Value);
        }
Пример #10
0
        public void InstantiateLeftBT()
        {
            /// testing that it is instantiating Left or right accordingly
            BT testTree = new BT();

            testTree.Root = new Nodeb(100);
            Nodeb leftChild = new Nodeb(5);

            testTree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            testTree.Root.Right = rightChild;
            Assert.Equal(10, testTree.Root.Right.Value);
        }
Пример #11
0
 /// <summary>
 /// Removes a node value from queue
 /// </summary>
 /// <returns>temp node value</returns>
 public Nodeb Dequeue()
 {
     try
     {
         Nodeb temp = Front;
         Front     = Front.Next;
         temp.Next = null;
         Size--;
         return(temp);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
     return(null);
 }
Пример #12
0
 /// <summary>
 /// Adds a node/value to queue
 /// </summary>
 /// <param name="value">integer value</param>
 public void Enqueue(Nodeb node)
 {
     if (Front == null)
     {
         Nodeb newNode = node;
         Front = newNode;
         Rear  = newNode;
         Size++;
     }
     else
     {
         //Nodeb node = new Nodeb(value);
         Rear.Next = node;
         Rear      = node;
         Size++;
     }
 }
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            BT tree = new BT();

            tree.Root = new Nodeb(100);

            Nodeb leftChild = new Nodeb(5);

            tree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            tree.Root.Right = rightChild;

            Nodeb leftLeftLeaf = new Nodeb(15);

            tree.Root.Left.Left = leftLeftLeaf;

            Nodeb leftLeftLeftLeaf = new Nodeb(17);

            tree.Root.Left.Left.Left = leftLeftLeftLeaf;

            Nodeb leftRightLeaf = new Nodeb(200);

            tree.Root.Left.Right = leftRightLeaf;

            Nodeb rightLeftLeaf = new Nodeb(30);

            tree.Root.Right.Left = rightLeftLeaf;

            Nodeb rightRightLeaf = new Nodeb(150);

            tree.Root.Right.Right = rightRightLeaf;
            Nodeb rightRightRightLeaf = new Nodeb(160);

            tree.Root.Right.Right.Right = rightRightRightLeaf;

            Nodeb rightRightRightRightLeaf = new Nodeb(170);

            tree.Root.Right.Right.Right.Right = rightRightRightRightLeaf;

            Console.WriteLine(CalculateBinaryTreeHeight(tree.Root));
            Console.WriteLine(FindLevel(tree));
            Console.ReadLine();
        }
        public void TestBalancedTree()
        {
            /// Test balanced tree
            BT testTree = new BT();

            testTree.Root = new Nodeb(100);

            Nodeb leftChild = new Nodeb(5);

            testTree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            testTree.Root.Right = rightChild;


            Assert.True(Program.IsBinaryTreeBalanced(testTree.Root));
        }
Пример #15
0
        public void InOrderEvenBT()
        {
            /// Testing In order traversal on even amoutn of branches
            BT testTree = new BT();

            testTree.Root = new Nodeb(100);
            Nodeb leftChild = new Nodeb(5);

            testTree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            testTree.Root.Right = rightChild;
            List <int> expected = new List <int> {
                5, 100, 10
            };

            Assert.Equal(expected, BT.IOrder(testTree.Root));
        }
Пример #16
0
        public void PostOrderEvenBT()
        {
            /// Testing post order on even amoutn of branches in tree
            BT testTree = new BT();

            testTree.Root = new Nodeb(100);
            Nodeb leftChild = new Nodeb(5);

            testTree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            testTree.Root.Right = rightChild;
            List <int> expected = new List <int> {
                5, 10, 100
            };

            Assert.Equal(expected, BT.PstOrder(testTree.Root));
        }
        public void TestMaxOnMiddleBranch()
        {
            /// Test when maximum value is left most branch
            BT testTree = new BT();

            testTree.Root = new Nodeb(100);
            Nodeb leftChild = new Nodeb(5);

            testTree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            testTree.Root.Right = rightChild;

            Nodeb leftRightLeaf = new Nodeb(130);

            testTree.Root.Left.Left = leftRightLeaf;

            Assert.Equal(130, Program.FindMaxValue(testTree));
        }
        public void TestMaxOnRoot()
        {
            /// Test when maximum value is root
            BT testTree = new BT();

            testTree.Root = new Nodeb(200);
            Nodeb leftChild = new Nodeb(5);

            testTree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            testTree.Root.Right = rightChild;

            Nodeb leftRightLeaf = new Nodeb(130);

            testTree.Root.Left.Left = leftRightLeaf;

            Assert.Equal(200, Program.FindMaxValue(testTree));
        }
Пример #19
0
        public void PreOrderEvenBT()
        {
            /// testing pre order on even number of branches
            BT testTree = new BT();

            testTree.Root = new Nodeb(100);
            Nodeb leftChild = new Nodeb(5);

            testTree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            testTree.Root.Right = rightChild;
            BT.PeOrder(testTree.Root);
            List <int> expected = new List <int> {
                100, 5, 10
            };

            Assert.Equal(expected, BT.PeOrder(testTree.Root));
        }
Пример #20
0
        /// <summary>
        /// Identify pairs between two binary trees
        /// </summary>
        /// <param name="treeOne">binary tree one</param>
        /// <param name="treeTwo">binary tree two</param>
        /// <returns>list of pair values between both trees</returns>
        public static List <int> TreeIntersection(BT treeOne, BT treeTwo)
        {
            List <int> values = new List <int>();
            Hashtable  table  = new Hashtable(100);

            Nodeb root1 = treeOne.Root;
            Nodeb root2 = treeTwo.Root;

            /// first helper method to traverse first tree adding to hash table
            void TraverseOne(Nodeb helperRoot)
            {
                if (helperRoot == null)
                {
                    return;
                }

                TraverseOne(helperRoot.Left);
                TraverseOne(helperRoot.Right);
                table.Add(helperRoot.Value.ToString(), helperRoot.Value);
            }

            TraverseOne(root1);
            ///second helper method to traverse second tree to cross reference table
            void TraverseTwo(Nodeb helperRoot)
            {
                if (helperRoot == null)
                {
                    return;
                }

                TraverseTwo(helperRoot.Left);
                TraverseTwo(helperRoot.Right);
                if (table.Contains(helperRoot.Value.ToString()))
                {
                    values.Add(helperRoot.Value);
                }
            }

            TraverseTwo(root2);
            return(values);
        }
Пример #21
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            BT tree = new BT();

            tree.Root = new Nodeb(100);

            Nodeb leftChild = new Nodeb(5);

            tree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            tree.Root.Right = rightChild;

            Nodeb leftLeftLeaf = new Nodeb(15);

            tree.Root.Left.Left = leftLeftLeaf;

            Nodeb leftRightLeaf = new Nodeb(200);

            tree.Root.Left.Right = leftRightLeaf;

            Nodeb rightLeftLeaf = new Nodeb(30);

            tree.Root.Right.Left = rightLeftLeaf;

            Nodeb rightRightLeaf = new Nodeb(150);

            tree.Root.Right.Right = rightRightLeaf;
            Nodeb rightRightRightLeaf = new Nodeb(160);

            tree.Root.Right.Right.Right = rightRightRightLeaf;
            Nodeb rightRightRightRightLeaf = new Nodeb(160);

            tree.Root.Right.Right.Right.Right = rightRightRightRightLeaf;



            Console.WriteLine(IsBinaryTreeBalanced(tree.Root));
        }
Пример #22
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            BT tree = new BT();

            tree.Root = new Nodeb(100);
            Nodeb leftChild = new Nodeb(5);

            tree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            tree.Root.Right = rightChild;

            Nodeb leftRightLeaf = new Nodeb(20);

            tree.Root.Left.Right = leftRightLeaf;

            BT tree2 = new BT();

            tree2.Root = new Nodeb(100);
            Nodeb leftChild2 = new Nodeb(5);

            tree2.Root.Left = leftChild2;

            Nodeb rightChild2 = new Nodeb(10);

            tree2.Root.Right = rightChild2;

            Nodeb leftRightLeaf2 = new Nodeb(20);

            tree2.Root.Left.Right = leftRightLeaf2;

            List <int> intersectedValues = TreeIntersection(tree, tree2);

            foreach (var item in intersectedValues)
            {
                Console.Write($" Intersected values {item}");
            }
        }
Пример #23
0
        public void TestNonPerfectLevelTree()
        {
            /// Test non perfect tree
            BT testTree = new BT();

            testTree.Root = new Nodeb(100);

            Nodeb leftChild = new Nodeb(5);

            testTree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            testTree.Root.Right = rightChild;

            Nodeb rightRightChild = new Nodeb(15);

            testTree.Root.Right.Right = rightRightChild;

            Assert.Equal(3, Program.FindLevel(testTree));
        }
        public void FindRootIntersection()
        {
            /// Find root pairs
            BT testTree = new BT();

            testTree.Root = new Nodeb(100);
            Nodeb leftChild = new Nodeb(2);

            testTree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            testTree.Root.Right = rightChild;

            Nodeb leftRightLeaf = new Nodeb(21);

            testTree.Root.Left.Right = leftRightLeaf;

            BT testTree2 = new BT();

            testTree2.Root = new Nodeb(100);
            Nodeb leftChild2 = new Nodeb(5);

            testTree2.Root.Left = leftChild2;

            Nodeb rightChild2 = new Nodeb(15);

            testTree2.Root.Right = rightChild2;

            Nodeb leftRightLeaf2 = new Nodeb(20);

            testTree2.Root.Left.Right = leftRightLeaf2;
            List <int> expected = new List <int> {
                100
            };

            Assert.Equal(expected, Program.TreeIntersection(testTree, testTree2));
        }
        public void FindTwoIntersections()
        {
            /// Find two pairs between both trees
            BT testTree = new BT();

            testTree.Root = new Nodeb(100);
            Nodeb leftChild = new Nodeb(5);

            testTree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            testTree.Root.Right = rightChild;

            Nodeb leftRightLeaf = new Nodeb(20);

            testTree.Root.Left.Right = leftRightLeaf;

            BT testTree2 = new BT();

            testTree2.Root = new Nodeb(99);
            Nodeb leftChild2 = new Nodeb(5);

            testTree2.Root.Left = leftChild2;

            Nodeb rightChild2 = new Nodeb(15);

            testTree2.Root.Right = rightChild2;

            Nodeb leftRightLeaf2 = new Nodeb(20);

            testTree2.Root.Left.Right = leftRightLeaf2;
            List <int> expected = new List <int> {
                20, 5
            };

            Assert.Equal(expected, Program.TreeIntersection(testTree, testTree2));
        }
Пример #26
0
        public void PostOrderOddBT()
        {
            /// Testing Post order traversal on odd number of branches
            BT testTree = new BT();

            testTree.Root = new Nodeb(100);
            Nodeb leftChild = new Nodeb(5);

            testTree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            testTree.Root.Right = rightChild;

            Nodeb leftRightLeaf = new Nodeb(20);

            testTree.Root.Left.Right = leftRightLeaf;
            List <int> expected = new List <int> {
                20, 5, 10, 100
            };

            Assert.Equal(expected, BT.PstOrder(testTree.Root));
        }
        public void FindNoIntersection()
        {
            /// Look for intersection where none exist
            BT testTree = new BT();

            testTree.Root = new Nodeb(99);
            Nodeb leftChild = new Nodeb(2);

            testTree.Root.Left = leftChild;

            Nodeb rightChild = new Nodeb(10);

            testTree.Root.Right = rightChild;

            Nodeb leftRightLeaf = new Nodeb(21);

            testTree.Root.Left.Right = leftRightLeaf;

            BT testTree2 = new BT();

            testTree2.Root = new Nodeb(100);
            Nodeb leftChild2 = new Nodeb(5);

            testTree2.Root.Left = leftChild2;

            Nodeb rightChild2 = new Nodeb(15);

            testTree2.Root.Right = rightChild2;

            Nodeb leftRightLeaf2 = new Nodeb(20);

            testTree2.Root.Left.Right = leftRightLeaf2;
            List <int> expected = new List <int> {
            };

            Assert.Equal(expected, Program.TreeIntersection(testTree, testTree2));
        }
Пример #28
0
        /// <summary>
        /// Determines if a binary tree is balanced - wraps two helper methods one to determine height of a tree, another to do a comparison between left and right
        /// </summary>
        /// <param name="root">root of a node</param>
        /// <returns></returns>
        public static bool IsBinaryTreeBalanced(Nodeb root)
        {
            if (root == null)
            {
                throw null;
            }
            Nodeb temp = root;

            int HeightHelper(Nodeb helperHeight)
            {
                if (helperHeight == null)
                {
                    return(0);
                }
                return(1 + Math.Max(HeightHelper(helperHeight.Left), HeightHelper(helperHeight.Right)));  ///Math.Max compares the given arguments and determines the greater, similiar to a conditional statement
            }

            bool ComparisonHelper(Nodeb helperComparison)
            {
                if (helperComparison == null)
                {
                    return(true);
                }
                int difference = HeightHelper(helperComparison.Left) - HeightHelper(helperComparison.Right);

                if (Math.Abs(difference) > 1)
                {
                    return(false);
                }
                else
                {
                    return(ComparisonHelper(helperComparison.Left) && ComparisonHelper(helperComparison.Right));
                }
            }

            return(ComparisonHelper(temp));
        }
        /// <summary>
        /// Find levels of tree
        /// </summary>
        /// <param name="tree">binary tree</param>
        /// <returns>level within a tree</returns>
        public static int FindLevel(BT tree)
        {
            if (tree.Root == null)
            {
                throw null;
            }

            Nodeb temp = tree.Root;

            int Helper(Nodeb tempH)
            {
                if (tempH == null)
                {
                    return(0);
                }
                int currentCountLeft  = Helper(tempH.Left);
                int currentCountRight = Helper(tempH.Right);


                return(1 + Math.Max(currentCountLeft, currentCountRight));  /// returns the greater of the two arguments
            }

            return(Helper(temp));
        }
        /// <summary>
        /// Find height recursively
        /// </summary>
        /// <param name="tree">binary tree</param>
        /// <returns>height of tree</returns>
        public static int CalculateBinaryTreeHeight(Nodeb root)
        {
            if (root == null)
            {
                throw null;
            }

            Nodeb temp = root;

            int Helper(Nodeb tempH)
            {
                if (tempH == null)
                {
                    return(-1);
                }
                int currentCountLeft  = Helper(tempH.Left);
                int currentCountRight = Helper(tempH.Right);


                return(1 + Math.Max(currentCountLeft, currentCountRight));  /// returns the greater of the two arguments
            }

            return(Helper(temp));
        }