コード例 #1
0
ファイル: Program.cs プロジェクト: virgilraj/code-learning
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            int[] arr = { 1, 2, 3, 3, 4, 5, 5, 8, 10, 10 };
            //var a = BubbleSort.GetSorted(arr);
            // QuickSort.GetSorted(arr);
            MyHeap.BuildHead();
            //foreach (var x in a)
            //{
            //    Console.WriteLine(x);
            //}

            var a = removeDuplicates(arr, arr.Length);

            // Print updated array
            for (int i = 0; i < a; i++)
            {
                Console.Write(arr[i] + " ");
            }

            var b = BianrySearch(arr, 225, 0, arr.Length - 1);

            Console.WriteLine(b);

            BinaryTree bt    = new BinaryTree();
            BinaryTree bt1   = new BinaryTree();
            var        root  = bt.Insert(null, 10);
            var        root1 = bt1.Insert(null, 10);

            /*root = bt.Insert(root, 20);
             * root = bt.Insert(root, 8);
             * root = bt.Insert(root, 9);
             * root = bt.Insert(root, 15);
             * root = bt.Insert(root, 25);
             */
            bt.Insert(root, 20);
            bt.Insert(root, 8);
            bt.Insert(root, 9);
            bt.Insert(root, 15);
            bt.Insert(root, 25);
            bt.Insert(root, 12);
            bt.Insert(root, 5);

            bt1.Insert(root1, 20);
            bt1.Insert(root1, 8);
            bt1.Insert(root1, 9);
            bt1.Insert(root1, 15);
            bt1.Insert(root1, 25);
            //bt1.Insert(root1, 12);
            bt1.Insert(root1, 7);

            TwoNodes tn = new TwoNodes {
                First = bt.RootNode, Second = bt1.RootNode
            };

            Console.WriteLine("Is_BST_Identical_Iterative  ::{0} ", bt.Is_BST_Identical_Iterative(tn));
            Console.WriteLine("Is_BST_Identical  ::{0} ", bt.isIdentical(bt.RootNode, bt1.RootNode));
            bt1.Height_Tree_Iterative(bt1.RootNode);

            Console.WriteLine("PreOrder");
            bt.PreOrder(bt.RootNode);
            Console.WriteLine("");
            Console.WriteLine("InOrder");
            bt.InOrder(bt.RootNode);
            Console.WriteLine("");
            Console.WriteLine("PostOrder");
            bt.PostOrder(bt.RootNode);

            bt.CreateBinaryTreeFromInAndPreOrder();

            Console.WriteLine("");
            Console.WriteLine("Find");
            var f = bt.Find(115, bt.RootNode);

            Console.WriteLine(f != null ? f.data : 0);

            Console.WriteLine("");
            Console.WriteLine("MinValue");

            var m = bt.GetMinValue(bt.RootNode);

            Console.WriteLine(m);
            int pos = 6;

            bt.GetKthMinValue(bt.RootNode, ref pos);
            //bt.LevelOrder(bt.RootNode);
            //bt.Inorder_Iterative(bt.RootNode);
            //bt.Preorder_Iterative(bt.RootNode);
            //bt.Postorder_Iterative(bt.RootNode);
            //bt.Get_Inorder_Predecessor(bt.RootNode);
            //bt.Get_Inorder_Successor(bt.RootNode);

            Console.WriteLine("\nInvert binary");
            bt.InvertBinary(bt.RootNode);
            bt.LevelOrder(bt.RootNode);

            Node ln = new Node {
                data = 20
            };
            Node rn = new Node {
                data = 30
            };
            Node no = new Node {
                data = 10, Left = ln, Right = rn
            };

            // Console.WriteLine("isBST :: {0}", bt.isBST(no, int.MinValue) );
            Console.WriteLine("isBST :: {0}", bt.isBinarySearch(no));

            AVLTree at = new AVLTree();

            at.Avl_execute();

            int[] arr1 = { 5, 6, 8, 9, 10, 12, 15, 20 };

            GetTriplets(arr1, 30);
            GetSumDobles(arr1, 15);
            GetSumDobles_Linear(arr1, 15);

            Console.WriteLine("KnapSack");
            int[] val = new int[] { 60, 120, 100 };
            int[] wt  = new int[] { 10, 30, 20 };
            int   Cap = 50;
            int   n   = val.Length;

            int[,] dp = new int[n + 1, Cap + 1];
            KnapSack k = new KnapSack();

            Console.WriteLine(k.KnapSack_Binary(Cap, wt, val, n, dp));
            k.KnapSack_Fraction(Cap, wt, val, n);

            Console.WriteLine("Pattern");
            Pattern.SearchPattern("AABAACAADAABAAABAACAAD", "CAAD");
            Pattern.Binary_MultipleOf3("110100000101");

            Console.WriteLine("Largest Sum Contiguous Subarray");
            int[] aa = { 1, 2, 3, -2, 5 };
            Pattern.maxSubArraySum(aa);
            Console.WriteLine("Missing elements");

            int[] aa1 = { 1, 2, 3, 5, 7, 8, 10 };
            Pattern.FindMissingElements(aa1);

            int[] aa2 = { 1, 2, 3, 1, 1 };
            Pattern.FindMajorityElement(aa2);

            //Search in a Rotated Array
            int[] aa3 = { 5, 6, 7, 8, 9, 10, 1, 2, 3, 4 };
            Pattern.SearchRotatedArray(aa3, 10);

            //Linked List
            LinkedList lst = new LinkedList();

            lst.InsertFront(10);
            lst.InsertLast(20);
            lst.InsertFront(5);
            lst.InsertLast(25);
            lst.InsertLast(35);
            lst.PrintList(lst);

            lst.FindMiddleElement(lst);

            Problem_30 p30 = new Problem_30();

            p30.FindSingeNumber();
            p30.HappyNumber();
            p30.Longest_Sum_Subarray();
            p30.FindSubarraySum();
            p30.MoveZeroRight();
            p30.MaxProfit();
            p30.IsAnagram();
            p30.StringSorting("kgadeA");
            p30.GroupingAnagrams();
            p30.CountingElement();
            p30.BackspaceStingCompare();
            //p30.LastStoneWeight();
            p30.SubArrayWithEqualZeroAndOne();
            p30.StringShif();
            p30.ProductOfArrayExceptSlef();
            Console.WriteLine("\nValid parenthesis:: {0}", p30.ValidParenthesis());
            p30.NumberOfIsland();
            p30.LeftmostColumnWithAtleastOne();
            p30.BitwiseAnd();
            p30.JumpingProblem();
            p30.LongestcommonSubsequence();
            p30.CountPairsGivenSum();
            p30.MaximumPathSum(bt.RootNode);
            p30.ValidSequence(bt.RootNode);
            p30.MinFromSatck();
            p30.MinimumPathSum();
            p30.HistogramMaxRectangle();
            //p30.Print_Combination_R_Elements_Array();

            ///All Array Problem
            ///
            ArrayProblem ar = new ArrayProblem();

            ar.Find_And_Print_SubArraySum();
            ar.SortBinaryArray();
            ar.FindDuplicate();
            ar.LongestConsecutiveSubArray();
            ar.MaximumSumSubArray();
            ar.MinimuSumSubArray();
            ar.SubArrayWithEqualZeroAndOne();
            ar.SortArrayOnlyZeroOneTwos();
            ar.Merge2SortedArray();
            ar.MergeArray_ReplaceZeroValue();
            ar.Find_Index_MaxLength_ContinuousOnes();
            ar.ProductOfTwoIntArray();
            ar.Suffle();
            ar.RearrangeArrayHighAndLow();
            ar.EquilibriumIndex();
            ar.FindMajorityElement();
            ar.MoveAllZeroToEnd();
            ar.ProductOfArrayWithotSlef();
            ar.BitonicSubArray();
            ar.FindMaximumDifference();
            ar.MaximumSumCircularSubArray();
            ar.FindSumMimumSubArray_Given_Size();
            ar.FindSumSubArray_Given_Sum();
            ar.LengthOfSmallestSubarray_SumOfElements_GreaterThanNumber();
            ar.Find_Smallest_Window_Will_Make_Entire_Array_Sorted();
            ar.TrappingRainWaterwithin_given_set_bars();
            ar.Find_Maximum_Sum_Subsequence_With_NO_Adjacent();
            ar.MinimumNumber_Platform();
            ar.LengthOf_Continuous_Same_Sum_Two_Binary_Array();
            ar.Find_Number_Rotations();
            ar.RodCutting();
            ar.Find_kth_Smallest_Element();
            ar.FindAllCombinations();

            //Trie data structure
            Trie tr = new Trie();

            string[] words = { "abc", "abgl", "cdf", "abcd", "lmn" };
            //string[] words = { "abc"};
            foreach (string st in words)
            {
                tr.Insert(st);
            }

            Console.WriteLine("\nIs string present {0} ", tr.Search("cdf"));
            tr.AutoComplete("ab");

            // List of graph edges as per above diagram
            List <Edge> edges = new List <Edge> {
                // Notice that node 0 is unconnected node
                new Edge(1, 2), new Edge(1, 7), new Edge(1, 8),
                new Edge(2, 3), new Edge(2, 6), new Edge(3, 4),
                new Edge(3, 5), new Edge(8, 9), new Edge(8, 12),
                new Edge(9, 10), new Edge(9, 11)
            };

            // Set number of vertices in the graph (0-12)
            int N = 13;

            // create a graph from edges
            Graph graph = new Graph(edges, N);

            // stores vertex is discovered or not
            bool[] discovered = new bool[N];

            // Do DFS traversal from all undiscovered nodes to
            // cover all unconnected components of graph
            for (int i = 0; i < N; i++)
            {
                if (!discovered[i])
                {
                    graph.DFS(graph, i, discovered);
                }
            }

            //Matrix
            Matrix mt = new Matrix();

            mt.Print_SpiralOrder();
            mt.Rotation_90();
            mt.FloodFill();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            #region 数组
            //ArrayList num = new ArrayList();
            //num.Add("a");
            //num.Add("b");
            //num.Add("c");
            //foreach(var item in num)
            //{
            //    Console.WriteLine(num.IndexOf(item) + ":" + item);
            //}
            //Console.WriteLine(num.Capacity);
            //Console.WriteLine(num.Count);
            //num.Add("d");
            //num.Add("e");
            //num.Add("f");
            //foreach (var item in num)
            //{
            //    Console.WriteLine(num.IndexOf(item) + ":" + item);
            //}
            //Console.WriteLine(num.Capacity);
            //Console.WriteLine(num.Count);
            //num.Remove("f");
            //num.Remove("e");
            //num.Remove("d");
            //Console.WriteLine(num.Capacity);
            //Console.WriteLine(num.Count);
            #endregion

            #region 双向链表
            //LinkedList<string> linked = new LinkedList<string>();
            //linked.AddFirst("a");
            //linked.AddFirst("b");
            //linked.AddFirst("c");
            //linked.AddLast("d");

            //foreach(var item in linked)
            //{
            //    Console.WriteLine(item);
            //}

            //DoubleLink<int> dlink = new DoubleLink<int>();// 创建双向链表
            //Console.WriteLine("将 20 插入到表头之后");
            //dlink.Append(0, 10);
            //dlink.ShowAll();
            //Console.WriteLine("将 40 插入到表头之后");
            //dlink.Append(1, 30);
            //dlink.ShowAll();
            //Console.WriteLine("将 10 插入到表头之前");
            //dlink.Insert(0, 40);
            //dlink.ShowAll();
            //Console.WriteLine("将 30 插入到第一个位置之前");
            //dlink.Insert(1, 20);
            //dlink.ShowAll();

            //dlink.Insert(2, 25);
            //dlink.ShowAll();
            //Console.WriteLine("展示第一个:" + dlink.GetFirst());
            //Console.WriteLine("删除第一个");
            //dlink.DelFirst();
            //Console.WriteLine("展示第一个:" + dlink.GetFirst());
            //Console.WriteLine("展示最后一个:" + dlink.GetLast());
            //Console.WriteLine("删除最后一个");
            //dlink.DelLast();
            //Console.WriteLine("展示最后一个:" + dlink.GetLast());
            //dlink.ShowAll();
            //Console.ReadKey();
            #endregion

            #region 二叉查询树
            BinaryTree binaryTree = new BinaryTree();
            //for (var i = 0; i < 5; i++)
            //{
            //    Random random = new Random();
            //    int num = random.Next(0, 100);
            //    binaryTree.Add(num);
            //}
            binaryTree.Add(15);
            binaryTree.Add(20);
            binaryTree.Add(10);
            binaryTree.Add(5);
            binaryTree.Add(18);
            binaryTree.Add(6);
            binaryTree.Add(19);
            binaryTree.Add(17);
            binaryTree.Add(4);
            binaryTree.Add(12);
            //binaryTree.Add(11);
            binaryTree.Add(13);
            binaryTree.Preorder();


            Tree tree = binaryTree.Search(4);
            //if (tree != null)
            //{
            //    Console.WriteLine(tree.Data);
            //}
            //binaryTree.Delete(4);
            //binaryTree.Delete(12);
            binaryTree.Delete(20);
            #endregion

            #region Hash一致性
            //int Replicas = 100;
            //KetamaNodeLocator.AddNode("127.0.0.1:6379", Replicas);
            //KetamaNodeLocator.AddNode("127.0.0.1:6380", Replicas);
            //KetamaNodeLocator.AddNode("127.0.0.1:6381", Replicas);
            //List<string> nodes = new List<string>();
            //for (int i = 0; i < 100; i++)
            //{
            //    nodes.Add(KetamaNodeLocator.GetTargetNode(i + "test" + (char)i));
            //}
            //var counts = nodes.GroupBy(n => n, n => n.Count()).ToList();
            //counts.ForEach(index => Console.WriteLine(index.Key + "-" + index.Count()));

            //Console.ReadLine();
            #endregion

            //Cat.sid = 100;
            //Cat mimi = new Cat("mimi");
            //Cat pipi = new Cat("pipi");
            //bool bol = true;
            //Int32 i = new Int32();
            //int j = 1;
            //int k = bol ? i : j;
            //Console.WriteLine(k);

            Console.ReadLine();
        }