Пример #1
0
        public static void Show()
        {
            Expression <Func <int, int, int, int> > expression = (i, m, n) => i * 3 + m + 2 + n / 4;

            CustomBinarySearchTree tree1 = new CustomBinarySearchTree();

            tree1.Insert(10);
            tree1.SequentialTranversal();
            Console.WriteLine();
            tree1.Insert(5);
            tree1.SequentialTranversal();
            Console.WriteLine();
            tree1.Insert(1);
            tree1.SequentialTranversal();
            Console.WriteLine();

            tree1.Insert(8);
            tree1.SequentialTranversal();
            Console.WriteLine();
            tree1.Insert(20);
            tree1.SequentialTranversal();
            Console.WriteLine();
            tree1.Insert(28);
            tree1.SequentialTranversal();
            Console.WriteLine();
            tree1.Insert(12);
            tree1.SequentialTranversal();
            Console.WriteLine();
            tree1.Insert(6);
            tree1.SequentialTranversal();
            Console.WriteLine();
            tree1.Insert(7);
            tree1.SequentialTranversal();
            Console.WriteLine();
            tree1.Insert(25);
            tree1.SequentialTranversal();
            Console.WriteLine();

            Console.WriteLine(tree1.Min());
            Console.WriteLine(tree1.Max());
            Console.WriteLine(tree1.Find(25).iData);
        }
Пример #2
0
        public static void Show()
        {
            Expression <Func <int, int, int, int> > expression = (i, m, n) => i * 3 + m + 2 + n / 4;
            CustomTreeNode tree = new CustomTreeNode()
            {
                iData = 123,
                Left  = new CustomTreeNode()
                {
                    iData = 12,
                    Left  = new CustomTreeNode()
                    {
                        iData = 11,
                        Left  = null,
                        Right = null
                    },
                    Right = new CustomTreeNode()
                    {
                        iData = 12,
                        Left  = null,
                        Right = null
                    }
                },
                Right = new CustomTreeNode()
                {
                    iData = 15,
                    Left  = new CustomTreeNode()
                    {
                        iData = 13,
                        Left  = null,
                        Right = null
                    },
                    Right = new CustomTreeNode()
                    {
                        iData = 17,
                        Left  = null,
                        Right = null
                    }
                }
            };

            CustomBinarySearchTree tree1 = new CustomBinarySearchTree();

            tree1.Insert(10);
            tree1.SequentialTraversal();
            Console.WriteLine();
            tree1.Insert(5);
            tree1.SequentialTraversal();
            Console.WriteLine();
            tree1.Insert(1);
            tree1.SequentialTraversal();
            Console.WriteLine();
            tree1.Insert(8);
            tree1.SequentialTraversal();
            Console.WriteLine();
            tree1.Insert(20);
            tree1.SequentialTraversal();
            Console.WriteLine();
            tree1.Insert(28);
            tree1.SequentialTraversal();
            Console.WriteLine();
            tree1.Insert(12);
            tree1.SequentialTraversal();
            Console.WriteLine();
            tree1.Insert(6);
            tree1.SequentialTraversal();
            Console.WriteLine();
            tree1.Insert(7);
            tree1.SequentialTraversal();
            Console.WriteLine();
            tree1.Insert(25);
            tree1.SequentialTraversal();
            Console.WriteLine();


            Console.WriteLine(tree1.Min());
            Console.WriteLine(tree1.Max());
            Console.WriteLine(tree1.Find(25).iData);
        }
Пример #3
0
        public static void Main(string[] args)
        {
            //var travelingSalesman = new TravelingSalesman();
            //travelingSalesman.minCost(new int[,] {
            //    {  0, 1, 15,  6 },
            //    {  2, 0,  7,  3 },
            //    {  9, 6,  0, 12 },
            //    { 10, 4,  8,  0 }
            //});

            //var fractionalKnapsack = new FractionalKnapsack();
            //fractionalKnapsack.Run(50, new int[,] { { 60, 10 }, { 100, 20 }, { 120, 30 } }, 3);



            //var mergeSort = new MergeSort();
            //var arr = new int[] { 38, 27, 43, 3, 9, 82, 10 };
            //mergeSort.Run(arr, 0, arr.Length - 1);
            //foreach (var item in arr)
            //{
            //    Console.WriteLine(item);
            //}

            //var minNumberOfCoins = new Greedy.MinNumberOfCoins();
            //minNumberOfCoins.Run(new int[] { 1, 2, 5, 10, 20, 50, 100 }, 121);

            //var minNumberOfPlatforms = new MinNumPlatforms();
            //var result = minNumberOfPlatforms.Run(new float[] { 9.00f, 9.40f, 9.50f, 11.00f, 15.00f, 18.00f }, new float[] { 9.10f, 12.00f, 11.20f, 11.30f, 19.00f, 20.00f });
            //Console.WriteLine($"Min number of platforms needed is {result}.");

            //var selectionSort = new SelectionSort();
            //selectionSort.Run(new int[] { 23, 42, 4, 16, 8, 15 });

            //var stableSelectionSort = new StableSelectionSort();
            //stableSelectionSort.Run(new int[] { 4, 5, 3, 2, 4, 1 });

            //var swap = new Swap();
            //swap.Run(5, 10);

            //var selectionSort = new SelectionSort();
            //selectionSort.Run(new int[] {64, 25, 12, 22, 11});

            //var insertionSort = new InsertionSort();
            //insertionSort.Run(new int[]{ 23, 42, 4, 16, 8, 15 });

            //var binarySearch = new BinarySearch();
            //binarySearch.RunRecursive(new int[] { 0, 2, 3, 4, 10, 40, 44 }, 2, 0, 6);
            //binarySearch.RunIterative(new int[] { 0, 2, 3, 4, 10, 40, 44 }, 2);

            //var quickSort = new Quicksort();
            //var inputArray = new int[] { 7, 2, 1, 6, 8, 5, 3, 4 };
            //quickSort.QuicksortWrapper(inputArray, 0, inputArray.Length - 1);
            //foreach (var item in inputArray)
            //{
            //    Console.WriteLine(item);
            //}

            //var interpolationSearch = new Search.InterpolationSearch();
            //var result = interpolationSearch.Run(new int[] { 1, 2, 4, 6, 7, 10, 11, 14, 15 }, 4);
            //Console.WriteLine(result);



            //var BFSGraph = new BFSGraph();
            //BFSGraph.BFS(2);

            //var activitySelection = new ActivitySelection();
            //activitySelection.Run(new int[] {10, 12, 20}, new int[] {20, 25, 30});

            //var ditka = new DitkasAlgo();
            //ditka.Run(new int[][]{new int[] {0,  4, 0,  0,  0,  0, 0,  8, 0},
            //                      new int[] {4,  0, 8,  0,  0,  0, 0, 11, 0},
            //                      new int[] {0,  8, 0,  7,  0,  4, 0,  0, 2},
            //                      new int[] {0,  0, 7,  0,  9, 14, 0,  0, 0},
            //                      new int[] {0,  0, 0,  9,  0, 10, 0,  0, 0},
            //                      new int[] {0,  0, 4, 14, 10,  0, 2,  0, 0},
            //                      new int[] {0,  0, 0,  0,  0,  2, 0,  1, 6},
            //                      new int[] {8, 11, 0,  0,  0,  0, 1,  0, 7},
            //                      new int[] {0,  0, 2,  0,  0,  0, 6,  7, 0}
            //}, 0);

            //var memoization = new Memoization();
            //Console.WriteLine(memoization.RunRecursive(6));
            //Console.WriteLine(memoization.RunMemoization(6));
            //Console.WriteLine(memoization.RunTabulation(6));


            //var lis = new LIS();
            //Console.WriteLine(lis.runLIS(new int[] { 10, 22, 9, 33, 21, 50, 41, 60 }, 8));

            //var lcs = new LCS();
            //var result = lcs.runLCS(new char[] {'A', 'G', 'G', 'T', 'A', 'B'}, new char[] {'G', 'T', 'X', 'T', 'X', 'A', 'Y', 'B'}, 6, 8);
            //Console.WriteLine(result);


            //var lps = new LPS();
            //var result = lps.RunLPS("BBABCBCAB");
            //Console.WriteLine($"The longest pallindrome subsequence is {result}.");

            //var wwp = new WWP();
            //wwp.RunWWPGreedy("aaa bb cc dddd");

            //var linkedList = new CustomLinkedList();
            //linkedList.AddNode(1);
            //linkedList.AddNode(2);
            //linkedList.AddNode(3);
            //linkedList.TraverseLinkedList();

            //var customStack = new CustomStack<int>();
            //customStack.Push(10);
            //customStack.Push(20);
            //customStack.Push(30);
            //Console.WriteLine(customStack.Pop());

            //var customStackList = new CustomStackList<int>();
            //customStackList.Push(10);
            //customStackList.Push(20);
            //customStackList.Push(30);
            //Console.WriteLine(customStackList.Pop());

            //var customQ = new CustomQueue(1000);
            //customQ.Enqueue(10);
            //customQ.Enqueue(20);
            //customQ.Enqueue(30);
            //customQ.Enqueue(40);
            //Console.WriteLine(customQ.Dequeue());
            //Console.WriteLine(customQ.Front());
            //Console.WriteLine(customQ.Rear());

            //var customHash = new CustomHash();
            //customHash.Insert("Hello", 3);
            //Console.WriteLine(customHash.Retrieve("Hello"));

            var bst = new CustomBinarySearchTree();

            bst.Insert(5);
            bst.Insert(3);
            bst.Insert(4);
            bst.Insert(10);
            bst.Insert(12);
            bst.Delete(12);
        }
Пример #4
0
        static void BinarSearchTreeMain(string[] args)
        {
            try
            {
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine("Implementing the Binary Search Tree with basic Operations");
                Console.ForegroundColor = ConsoleColor.White;

                CustomBinarySearchTree customBinarySearchTree = new CustomBinarySearchTree();

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Pushing the elements 20, 10, 30, 6, 14, 24, 3, 8, 26 to Binary Search Tree");
                Console.ForegroundColor = ConsoleColor.White;

                customBinarySearchTree.Insert(20);
                customBinarySearchTree.Insert(10);
                customBinarySearchTree.Insert(30);
                customBinarySearchTree.Insert(6);
                customBinarySearchTree.Insert(14);
                customBinarySearchTree.Insert(24);
                customBinarySearchTree.Insert(3);
                customBinarySearchTree.Insert(8);
                customBinarySearchTree.Insert(26);

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Finding the element 26 in Binary Search Tree");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(customBinarySearchTree.Find(26));

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Finding the element 24 in Binary Search Tree");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(customBinarySearchTree.Find(24));

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Finding the element 20 in Binary Search Tree");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(customBinarySearchTree.Find(20));

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Finding the element 4 in Binary Search Tree");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(customBinarySearchTree.Find(4));

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Pre order Traversal results");
                Console.ForegroundColor = ConsoleColor.White;
                customBinarySearchTree.TraversePreOrder();

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("In order Traversal results");
                Console.ForegroundColor = ConsoleColor.White;
                customBinarySearchTree.TraverseInOrder();

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Post order Traversal results");
                Console.ForegroundColor = ConsoleColor.White;
                customBinarySearchTree.TraversePostOrder();

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Height of the tree is");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(customBinarySearchTree.CalculateHeightOfTree());

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Minimum value in tree is");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(customBinarySearchTree.FindMinimumValueInTree());

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Maximum value in tree is");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(customBinarySearchTree.FindMaximumValueInTree());


                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Minimum value in Binary Search tree is");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(customBinarySearchTree.FindMinimumValueInBinarySearchTree());

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Maximum value in Binary Search tree is");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(customBinarySearchTree.FindMaximumValueInTree());

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Comparing two tress are equal or not");
                Console.ForegroundColor = ConsoleColor.White;

                CustomBinarySearchTree otherBinarySearchTree = new CustomBinarySearchTree();
                otherBinarySearchTree.Insert(20);
                otherBinarySearchTree.Insert(10);
                otherBinarySearchTree.Insert(30);
                otherBinarySearchTree.Insert(6);
                otherBinarySearchTree.Insert(14);
                otherBinarySearchTree.Insert(24);
                otherBinarySearchTree.Insert(3);
                otherBinarySearchTree.Insert(8);
                otherBinarySearchTree.Insert(26);

                bool result = customBinarySearchTree.Equals(otherBinarySearchTree);
                Console.WriteLine("Are both trees are equal: {0}", result);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Validating the given tree is Binary Search tree or not");
                Console.ForegroundColor = ConsoleColor.White;
                result = customBinarySearchTree.IsBinarySearchTree();
                Console.WriteLine("Is given tree is Binary Search tree: {0}", result);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Print the nodes from the root with distance 2");
                Console.ForegroundColor = ConsoleColor.White;
                customBinarySearchTree.PrintNodesAtDistance(2);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Print the nodes from the root with distance 3");
                Console.ForegroundColor = ConsoleColor.White;
                customBinarySearchTree.PrintNodesAtDistance(3);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Level Traversal results");
                Console.ForegroundColor = ConsoleColor.White;
                customBinarySearchTree.LevelOrderTraversal();

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Calculaintg the size of tree");
                Console.ForegroundColor = ConsoleColor.White;
                int size = customBinarySearchTree.CalculateSizeOfTree();
                Console.WriteLine(size);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Calculaintg the total number of leaves in a tree");
                Console.ForegroundColor = ConsoleColor.White;
                size = customBinarySearchTree.CalculateTotalLeaves();
                Console.WriteLine(size);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Is Tree contains value 24");
                Console.ForegroundColor = ConsoleColor.White;
                result = customBinarySearchTree.IsTreeContainsValue(24);
                Console.WriteLine(result);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Is Tree contains value 30");
                Console.ForegroundColor = ConsoleColor.White;
                result = customBinarySearchTree.IsTreeContainsValue(30);
                Console.WriteLine(result);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Is Tree contains value 25");
                Console.ForegroundColor = ConsoleColor.White;
                result = customBinarySearchTree.IsTreeContainsValue(25);
                Console.WriteLine(result);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Is tree have siblings as 10, 30");
                Console.ForegroundColor = ConsoleColor.White;
                result = customBinarySearchTree.AreSiblings(10, 30);
                Console.WriteLine(result);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Is tree have siblings as 15, 20");
                Console.ForegroundColor = ConsoleColor.White;
                result = customBinarySearchTree.AreSiblings(15, 20);
                Console.WriteLine(result);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Is tree have siblings as 3, 8");
                Console.ForegroundColor = ConsoleColor.White;
                result = customBinarySearchTree.AreSiblings(3, 8);
                Console.WriteLine(result);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Get the ancestors for node 26");
                Console.ForegroundColor = ConsoleColor.White;
                customBinarySearchTree.GetAncestors(26);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Is tree is balanced");
                Console.ForegroundColor = ConsoleColor.White;
                result = customBinarySearchTree.IsBalanced();
                Console.WriteLine(result);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Is tree is Perfect");
                Console.ForegroundColor = ConsoleColor.White;
                result = customBinarySearchTree.IsPerfect();
                Console.WriteLine(result);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Finding the depth of node 14");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(customBinarySearchTree.DepthOfNode(14));

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Finding the depth of node 8");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(customBinarySearchTree.DepthOfNode(8));

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Finding the depth of node 20");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(customBinarySearchTree.DepthOfNode(20));

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Finding the height of node 14");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(customBinarySearchTree.HeightOfANode(14));

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Finding the height of node 10");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(customBinarySearchTree.HeightOfANode(10));

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Finding the height of node 24");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(customBinarySearchTree.HeightOfANode(24));

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Finding the height of node 20");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(customBinarySearchTree.HeightOfANode(20));

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Finding the longest path from root");
                Console.ForegroundColor = ConsoleColor.White;
                List <int> path = customBinarySearchTree.FindLongestPathFromRootToLeaf();
                int        n    = path.Count;
                if (n > 0)
                {
                    Console.Write(path[n - 1]);
                    for (int i = n - 2; i >= 0; i--)
                    {
                        Console.Write(" -> " + path[i]);
                    }
                }

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Finding the root to leaf sum binary tree");
                Console.ForegroundColor = ConsoleColor.White;
                path = customBinarySearchTree.GetRootToLeafSumBinaryTreePath(44);
                n    = path.Count;
                if (n > 0)
                {
                    Console.Write(path[n - 1]);
                    for (int i = n - 2; i >= 0; i--)
                    {
                        Console.Write(" -> " + path[i]);
                    }
                }
                else
                {
                    Console.WriteLine("No path to match the given sum");
                }

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Finding the Lowest common ancestor in binary search tree");
                Console.ForegroundColor = ConsoleColor.White;

                int ancestor = customBinarySearchTree.LowestCommonAncestorInBinarySearchTree(10, 30);
                Console.WriteLine(ancestor);

                ancestor = customBinarySearchTree.LowestCommonAncestorInBinarySearchTree(5, 7);
                Console.WriteLine(ancestor);

                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine(ex.Message);
                Console.ForegroundColor = ConsoleColor.White;
            }
        }